Example #1
0
void createInputs(int n = 2) 
{
   for(UInt_t i = 0; i < (UInt_t)n; ++i ) {
      TFile *file = TFile::Open(TString::Format("input%d.root",i),"RECREATE");
      TH1F * h = new TH1F("h1","",10,0,100);
      h->Fill(10.5); h->Fill(20.5);
 
      Int_t nbins[5];
      Double_t xmin[5];
      Double_t xmax[5];
      for(UInt_t j = 0; j < 5; ++j) {
         nbins[j] = 10; xmin[j] = 0; xmax[j] = 10;
      }
      THnSparseF *sparse = new THnSparseF("sparse", "sparse", 5, nbins, xmin, xmax);
      Double_t coord[5] = {0.5, 1.5, 2.5, 3.5, 4.5};
      sparse->Fill(coord);
      sparse->Write();
      
      THStack *stack = new THStack("stack","");
      h = new TH1F("hs_1","",10,0,100);
      h->Fill(10.5); h->Fill(20.5);
      h->SetDirectory(0);
      stack->Add(h);
      h = new TH1F("hs_2","",10,0,100);
      h->Fill(30.5); h->Fill(40.5);
      h->SetDirectory(0);
      stack->Add(h);
      stack->Write();

      TGraph *gr = new TGraph(3);
      gr->SetName("exgraph");
      gr->SetPoint(0,1,1);
      gr->SetPoint(1,2,2);
      gr->SetPoint(2,3,3);
      
      gr->Write();
      
      TTree *tree = new TTree("tree","simplistic tree");
      Int_t data = 0;
      tree->Branch("data",&data);
      for(Int_t l = 0; l < 2; ++l) {
         data = l;
         tree->Fill();
      }
      
      file->Write();
      delete file;
   }
}
void compareDataStackMC(std::string histoName,
                        std::string inputFile="inputFile.txt", bool update=false,
                        double xmin=-9999.0, double xmax=-9999.0,int rebin=1)
{

    //---------------------------------------------------------------------------
    // read in the MC root files
    //---------------------------------------------------------------------------
    std::string mcName[]= {
        "Dimuon Data",
        "DY+jets",
        "t#bar{t} +jets",
        "WW",
        "WZ"
    };

    std::string leptonName="test";
    double lumi =5000;

    if(inputFile.find("electron")!= std::string::npos) {
        leptonName="electron";
        lumi = 5200.7;
        mcName[0]="DiElectron Data";
    }
    else if(inputFile.find("muon")!= std::string::npos) {
        leptonName="muon";
        lumi = 5204.7;
    }

    cout << "Data luminosity = " << lumi << endl;

    std::vector<MCFile> myMCFiles;
    std::string dataFile;

    FILE *fTable = fopen(inputFile.data(),"r");

    int flag=1;
    while (flag!=-1) {
        // first reading input file
        char filename[500];
        flag=fscanf(fTable,"%s",filename);

        char tmp[1000];
        // read in x-section
        flag=fscanf(fTable,"%s",tmp);
        double cross=atof(tmp);

        // read in number of events
        flag=fscanf(fTable,"%s",tmp);
        double nevt=atof(tmp);

        double scale =lumi*cross/nevt;

        MCFile tempMCfile;
        tempMCfile.filename = filename;
        tempMCfile.scaleFactor = scale;

        if(flag!=-1 && cross>1e-6)
        {
            myMCFiles.push_back(tempMCfile);
            cout << tempMCfile.filename << "\t" << tempMCfile.scaleFactor << endl;
        }
        else if(flag!=-1) // is data
        {
            dataFile = filename;
        }
    }

    const unsigned int nfiles = myMCFiles.size();
    cout << "Data file = " << dataFile << endl;
    cout << "Reading " << nfiles << " files" << endl;

    if(nfiles==0)
    {
        cout << "There is no file for me to process!" << endl;
        return;
    }

    //-------------------------------------------------------------------------------
    // Declaring histograms to be used
    //-------------------------------------------------------------------------------

    // local histograms
    THStack *hs = new THStack(Form("%s_stack",histoName.data()),"");

    // for local debugging
    TH1D* h_deno[nfiles];
    int COLORCODE[]= {
        kRed-7,
        kRed-10,
        kRed-6,
        kMagenta-2,
        kMagenta-6,
        kBlue-7,
        kBlue-9
    };

    cout << "opening " << myMCFiles[0].filename << endl;
    TFile *f1 = TFile::Open(myMCFiles[0].filename.data());

    TH1D* h_template = (TH1D*)(f1->Get(Form("%s",histoName.data())));
    h_template->Reset();

    TH1D* h_all = (TH1D*)h_template->Clone(Form("%s_all",histoName.data()));
    h_all -> Reset();

    TFile *fdata = TFile::Open(dataFile.data());

    TH1D* h_data= (TH1D*)(fdata->Get(Form("%s",histoName.data())));
    h_data-> SetName(Form("%s_data",histoName.data()));

    h_data->Draw();

    //-------------------------------------------------------------------------------
    // combine
    //-------------------------------------------------------------------------------


    for(int ifile=nfiles-1; ifile>=0; ifile--) {

        cout << "File " << ifile << endl;
        TFile *f_temp = TFile::Open(myMCFiles[ifile].filename.data());
        cout << "opening " << myMCFiles[ifile].filename << endl;


        h_deno[ifile] = (TH1D*)(f_temp->Get(Form("%s",histoName.data())));
        h_deno[ifile] -> SetName(Form("h_deno_%d",ifile));
        h_deno[ifile] -> SetTitle(f_temp->GetName());
        h_deno[ifile] -> Rebin(rebin);
        h_deno[ifile] -> SetLineColor(COLORCODE[ifile]);
        h_deno[ifile] -> SetFillColor(COLORCODE[ifile]);
        h_deno[ifile] -> SetFillStyle(1001);
        h_deno[ifile] -> SetMarkerColor(COLORCODE[ifile]);
        h_deno[ifile] -> Sumw2();
        double weight = myMCFiles[ifile].scaleFactor;
        h_deno[ifile] -> Scale(weight);
        if(ifile==nfiles-1)
        {
            h_all    -> Rebin(rebin);
            h_all    -> Sumw2();
        }

        h_all    -> Add(h_deno[ifile]);
        hs       -> Add(h_deno[ifile]);

        // to be used with TEfficiency methods
        cout << h_deno[ifile]->GetEntries() << endl;
    } // end of loop over files

    float x1NDC = 0.620968;
    float y1NDC = 0.684322;
    float x2NDC = 0.762097;
    float y2NDC = 0.898305;

    TLegend* leg = new TLegend(x1NDC,y1NDC,x2NDC,y2NDC);
    leg->SetFillColor(0);
    leg->SetFillStyle(0);
    leg->SetTextSize(0.04);
    leg->SetBorderSize(0);
    leg->AddEntry(h_data,mcName[0].data());
    for(int i=0; i < nfiles; i++)
        leg->AddEntry(h_deno[i], mcName[i+1].data());

    int maxBin = h_data->GetMaximumBin();
    double max = h_data->GetMaximum()+h_data->GetBinError(maxBin);
    double maxmc = hs->GetMaximum();

    if(maxmc>max)max=maxmc;

    h_data->SetMaximum(1.1*max);

    TCanvas* c1 = new TCanvas("c1",inputFile.data(),0,0,500,500);
    h_data->SetMarkerSize(1);
    h_data->SetMarkerStyle(24);
    h_data->SetTitle("");
    h_data->Draw("e");
    if(xmin>-9999 && xmax>-9999) {
        h_data->GetXaxis()->SetRangeUser(xmin,xmax);
    }
    cout << "Data integral = " << h_data->Integral() << endl;
    hs->Draw("histsame");
    h_data->Draw("esame");
    leg->Draw("same");


    std::string dirName = "compareDataMC_" + leptonName;
    gSystem->mkdir(dirName.data());

    std::string filename;
    std::string psname ;
    psname = dirName+ "/overlay_" + histoName;
    filename = psname + ".eps";
    c1->Print(filename.data());
    filename = psname + ".gif";
    c1->Print(filename.data());
    filename = psname + ".pdf";
    c1->Print(filename.data());


    // study the ratios

    TCanvas* c2 = new TCanvas("c2","",700,0,700,1000);
    c2->Divide(1,2,0.01,0);
    c2->cd(1);
    gPad->SetTopMargin(0.01);
    gPad->SetBottomMargin(0);
    gPad->SetRightMargin(0.04);

    h_data->Draw("e");
    if(xmin>-9999 && xmax>-9999) {
        h_data->GetXaxis()->SetRangeUser(xmin,xmax);
    }
    cout << h_data->GetName() << " integral = " << h_data->Integral() << endl;
    hs->Draw("histsame");
    h_data->Draw("esame");
    leg->Draw("same");


    c2->cd(2);
    gStyle->SetStatW       (0.3);
    gStyle->SetStatH       (0.3);
    gStyle->SetStatX       (0.879447);
    gStyle->SetStatY       (0.939033);
    gStyle->SetStatFontSize(0.05);
    gStyle->SetStatBorderSize(0);
    gPad->SetRightMargin(0.04);
    gPad->SetTopMargin(0);
    gPad->SetBottomMargin(0.2);
    gPad->SetTickx();
    gStyle->SetOptFit(1);

    TH1D* hratio = (TH1D*)h_data->Clone("hratio");
    hratio->Reset();
    hratio->Divide(h_data,h_all,1.0,1.0);
    hratio->SetTitle("");
    hratio->SetMaximum(1.5);
    hratio->SetMinimum(0.5);
    hratio->SetTitleOffset(1.2,"Y");
    hratio->GetXaxis()->SetTitle(h_data->GetXaxis()->GetTitle());
    hratio->Draw("e1");

    cout << "( " << h_data->GetBinContent(maxBin) << "+-" << h_data->GetBinError(maxBin) << " )/("
         << h_all->GetBinContent(maxBin) << "+-" << h_all->GetBinError(maxBin) <<  ")= "
         << hratio->GetBinContent(maxBin) << "+-" << hratio->GetBinError(maxBin) << endl;

    psname = dirName+ "/ratio_" + histoName;
    filename = psname + ".eps";
    c2->Print(filename.data());
    filename = psname + ".gif";
    c2->Print(filename.data());
    filename = psname + ".pdf";
    c2->Print(filename.data());


    std::string remword  =".txt";
    size_t pos  = inputFile.find(remword);

    if(pos!= std::string::npos)
        inputFile.replace(pos,remword.length(),"");

    std::string command = "recreate";

    if(update)command ="update";

    TFile* outFile = new TFile(Form("combined_%s.root",inputFile.data()),command.data());
    h_all->Write();
    hs->Write();
    h_data->Write();
    outFile->Close();


}
//------------------------------------------------------------------------------
// PlotHiggsRes_LP
//------------------------------------------------------------------------------
void RunMakeRazorPlots ( string signalfile, string signalLabel,  vector<string> bkgfiles,vector<string> bkgLabels, int boxOption = 0, int option = -1, string label = "", string latexlabel = "") {

  //--------------------------------------------------------------------------------------------------------------
  // Settings 
  //============================================================================================================== 
  double intLumi = 2000; //in units of pb^-1
  string Label = "";
  if (label != "") Label = "_" + label;

  vector<string> inputfiles;
  vector<string> processLabels;

  bool hasSignal = false;
  if (signalfile != "") {
    hasSignal = true;
    inputfiles.push_back(signalfile);
    processLabels.push_back(signalLabel);
  }
  assert(bkgfiles.size() == bkgLabels.size());
  for (int i=0; i < bkgfiles.size(); ++i) {
     inputfiles.push_back(bkgfiles[i]);
     processLabels.push_back(bkgLabels[i]);
  }

  //*******************************************************************************************
  //Define Histograms
  //*******************************************************************************************
  TH1F* histMRAllBkg =  new TH1F( "MRAllBkg",";M_{R} [GeV/c^{2}];Number of Events", 100, 400, 2400);
  TH1F* histRsqAllBkg =  new TH1F( "RsqAllBkg", ";R^{2};Number of Events", 24, 0.25, 1.45);
  histMRAllBkg->SetStats(false);
  histRsqAllBkg->SetStats(false);
  
  vector<TH1F*> histMR;
  vector<TH1F*> histRsq; 

  vector<TH2F*> histMRRsq;
  vector<TH1F*> histUnrolled; 
  vector<TH1F*> histUnrolled2bins; 
  vector<TH1F*> histUnrolledPercentage; 
  vector<TH1F*> histUnrolledPercentage2bins; 

  //  float MRBinLowEdges[] = {500, 600, 700, 900, 1200, 1600, 2500, 4000};
  //  float RsqBinLowEdges[] = {0.25, 0.30, 0.41, 0.52, 0.64, 1.5};

  // float MRBinLowEdges[] = {500, 600, 700, 900, 1200, 1600, 2500, 4000}; // Multijet Bins
  // float RsqBinLowEdges[] = {0.25, 0.30, 0.41, 0.52, 0.64, 1.5}; // Multijet Bins

  float MRBinLowEdges[] = {400, 500, 600, 700, 900, 1200, 1600, 2500, 4000}; // Lepton boxes
  float RsqBinLowEdges[] = {0.15, 0.20, 0.25, 0.30, 0.41, 0.52, 0.64, 1.5};  // Lepton boxes


  const int nMRBins = sizeof(MRBinLowEdges)/sizeof(float)-1;
  const int nRsqBins = sizeof(RsqBinLowEdges)/sizeof(float)-1;

  std::cout<<"AAAAAAA "<<nMRBins<<" "<<nRsqBins<<std::endl;

  assert (inputfiles.size() == processLabels.size());

  for (int i=0; i < inputfiles.size(); ++i) {    
    // histMR.push_back( new TH1F( Form("MR_%s",processLabels[i].c_str()), ";M_{R} [GeV/c^{2}];Number of Events", 100, 400, 2400));
    histMR.push_back( new TH1F( Form("MR_%s",processLabels[i].c_str()), ";M_{R} [GeV/c^{2}];Number of Events", nMRBins, MRBinLowEdges));
    if (!hasSignal || i != 0) histMR[i]->SetFillColor(color[i]);
    if (hasSignal && i==0) histMR[i]->SetLineWidth(3);
    histMR[i]->SetLineColor(color[i]);    
    histMR[i]->SetStats(false);    
    histMR[i]->Sumw2();

    // histRsq.push_back( new TH1F( Form("Rsq_%s",processLabels[i].c_str()), ";R^{2} ;Number of Events", 24, 0.25, 1.45));
    histRsq.push_back( new TH1F( Form("Rsq_%s",processLabels[i].c_str()), ";R^{2} ;Number of Events", nRsqBins, RsqBinLowEdges));
    if (!hasSignal || i != 0) histRsq[i]->SetFillColor(color[i]);
    if (hasSignal && i==0) histRsq[i]->SetLineWidth(3);
    histRsq[i]->SetLineColor(color[i]);
    histRsq[i]->SetStats(false);

    histMRRsq.push_back( new TH2F( Form("MRRsq_%s",processLabels[i].c_str()), ";M_{R} [GeV/c^{2}]; R^{2}", nMRBins, MRBinLowEdges, nRsqBins, RsqBinLowEdges));
    if (!hasSignal || i != 0) histMRRsq[i]->SetFillColor(color[i]);
    if (hasSignal && i==0) histMRRsq[i]->SetLineWidth(3);
    histMRRsq[i]->SetLineColor(color[i]);
    histMRRsq[i]->SetStats(false);
    histMRRsq[i]->Sumw2();

    histUnrolled.push_back( new TH1F( Form("Unrolled_%s",processLabels[i].c_str()), ";Bin Number ;Number of Events", nMRBins*nRsqBins, 0, nMRBins*nRsqBins));
    if (!hasSignal || i != 0) histUnrolled[i]->SetFillColor(color[i]);
    if (hasSignal && i==0) histUnrolled[i]->SetLineWidth(3);
    histUnrolled[i]->SetLineColor(color[i]);
    histUnrolled[i]->SetStats(false);     

    histUnrolled2bins.push_back( new TH1F( Form("Unrolled2bins_%s",processLabels[i].c_str()), ";Bin Number ;Event Density", 3, 0, 3));
    if (!hasSignal || i != 0) histUnrolled2bins[i]->SetFillColor(color[i]);
    if (hasSignal && i==0) histUnrolled2bins[i]->SetLineWidth(3);
    histUnrolled2bins[i]->SetLineColor(color[i]);
    histUnrolled2bins[i]->SetStats(false);     

    histUnrolledPercentage2bins.push_back( new TH1F( Form("UnrolledPercentage2bins_%s",processLabels[i].c_str()), ";;Event Density", 3, 0, 3));
    if (!hasSignal || i != 0) histUnrolledPercentage2bins[i]->SetFillColor(color[i]);
    if (hasSignal && i==0) histUnrolledPercentage2bins[i]->SetLineWidth(3);
    histUnrolledPercentage2bins[i]->SetLineColor(color[i]);
    histUnrolled2bins[i]->SetStats(false);     

    histUnrolledPercentage.push_back( new TH1F( Form("UnrolledPercentage_%s",processLabels[i].c_str()), ";Bin Number ; Fraction of total", nMRBins*nRsqBins, 0, nMRBins*nRsqBins));
    if (!hasSignal || i != 0) histUnrolledPercentage[i]->SetFillColor(color[i]);
    if (hasSignal && i==0) histUnrolledPercentage[i]->SetLineWidth(3);
    histUnrolledPercentage[i]->SetLineColor(color[i]);
    histUnrolledPercentage[i]->SetStats(false);     
}

  //*******************************************************************************************
  //Define Counts
  //*******************************************************************************************


  //*******************************************************************************************
  //Read files
  //*******************************************************************************************
  for (uint i=0; i < inputfiles.size(); ++i) {

    TFile* inputFile = new TFile(inputfiles[i].c_str(),"READ");
    assert(inputFile);
    TTree* tree = 0;
    tree = (TTree*)inputFile->Get("RazorInclusive");
 
    float weight = 0;
    int box = -1;
    int nBTaggedJets = 0;
    float dPhiRazor = 0;
    float MR = 0;
    float Rsq = 0;
    float mT = 0;
    int nGenMuons = 0;
    int nGenElectrons = 0;
    int nGenTaus = 0;

    // bool Flag_HBHENoiseFilter = false;
    // bool Flag_goodVertices    = false;
    // bool Flag_eeBadScFilter   = false;
    // bool Flag_EcalDeadCellTriggerPrimitiveFilter = false;


    tree->SetBranchAddress("weight",&weight);
    tree->SetBranchAddress("box",&box);
    tree->SetBranchAddress("nBTaggedJets",&nBTaggedJets);
    tree->SetBranchAddress("dPhiRazor",&dPhiRazor);
    tree->SetBranchAddress("MR",&MR);
    tree->SetBranchAddress("Rsq",&Rsq);
    tree->SetBranchAddress("mT",&mT);
    tree->SetBranchAddress("nGenMuons",&nGenMuons);
    tree->SetBranchAddress("nGenElectrons",&nGenElectrons);
    tree->SetBranchAddress("nGenTaus",&nGenTaus);
    // tree->SetBranchAddress("Flag_HBHENoiseFilter",&Flag_HBHENoiseFilter);
    // tree->SetBranchAddress("Flag_goodVertices",&Flag_goodVertices);
    // tree->SetBranchAddress("Flag_eeBadScFilter",&Flag_eeBadScFilter);
    // tree->SetBranchAddress("Flag_EcalDeadCellTriggerPrimitiveFilter",&Flag_EcalDeadCellTriggerPrimitiveFilter);
 

    cout << "Process : " << processLabels[i] << " : Total Events: " << tree->GetEntries() << "\n";
    for (int n=0;n<tree->GetEntries();n++) { 
    // for (int n=0;n<10000;n++) { 
    
      tree->GetEntry(n);
      if (n % 1000000 == 0) cout << "Processing Event " << n << "\n";       

      // if (intLumi*weight > 100) continue;

      //Box Options
      if (option == 0 ) {
	if (nBTaggedJets != 0) continue; 
      }
      if (option == 1 ) {
	if (nBTaggedJets != 1) continue;
      }
      if (option == 2 ) {
	if (nBTaggedJets != 2) continue;
      }
      if (option == 3 ) {
	if (nBTaggedJets < 3) continue;
      }
      if (option == 4 ) {
	if (nBTaggedJets < 0) continue; // all b-tag categories combined
      }

     //Box Options
      if (boxOption == 0) { // Multijet Box for Jamboree
	if( !(box == 11 || box == 12) ) continue;
      }
      if (boxOption == 1) { // MuonMultijet Box for Jamboree
	if( !(box == 3 || box == 4) ) continue;
      } 
      if (boxOption == 2) { // EleMultijet Box for Jamboree
	if( !(box == 6 || box == 7) ) continue;
      }

      // LeptonMultijet Box for Jamboree
      if(boxOption == 1 || boxOption == 2)
	if(mT<120) continue;

      // Multijet Box for Jamboree
      if (boxOption == 0)
	if(fabs(dPhiRazor) > 2.8) continue;

      //apply baseline cuts
      if(boxOption == 1 || boxOption == 2) 
	if (!(MR > 400 && Rsq > 0.15)) continue;
      
      if(boxOption == 0) 
	if (!(MR > 500 && Rsq > 0.25)) continue;

      // if(!Flag_HBHENoiseFilter) continue;
      // if(!Flag_goodVertices) continue;
      // if(!Flag_EcalDeadCellTriggerPrimitiveFilter) continue;
      // if(!Flag_eeBadScFilter) continue;
      
      // fill the histos
      if (!hasSignal || i>0) {
	histMRAllBkg->Fill(MR, intLumi*weight);
	histRsqAllBkg->Fill(Rsq, intLumi*weight);
      }
            
      if(strstr(processLabels[i].c_str(), "TTJets")==NULL && strstr(processLabels[i].c_str(), "T1bbbb")==NULL)
      	histMRRsq[i]->Fill(MR, Rsq, intLumi*weight);
      
      // if(strstr(processLabels[i].c_str(), "T1bbbb")!=NULL)
      // 	histMRRsq[i]->Fill(MR, Rsq, intLumi*weight*2.69506e-07);
      
      histMR[i]->Fill(MR, intLumi*weight);
      histRsq[i]->Fill(Rsq, intLumi*weight);
      
      // separate by number of gen leptons for lepton boxes
      if(boxOption==1 || boxOption==2)
	{
	  if(strstr(processLabels[i].c_str(), "TTJets")!=NULL && strstr(bkgLabels[i].c_str(), "2L")!=NULL) {
	    if(nGenMuons+nGenElectrons>=2)
	      histMRRsq[i]->Fill(MR, Rsq, intLumi*weight);
	  }
	  else if(strstr(processLabels[i].c_str(), "TTJets")!=NULL && strstr(bkgLabels[i].c_str(), "Tau")!=NULL) {
	    if((nGenMuons+nGenElectrons+nGenTaus>=2) && !(nGenMuons+nGenElectrons>=2))
	      histMRRsq[i]->Fill(MR, Rsq, intLumi*weight);
	  }
	  else if(strstr(processLabels[i].c_str(), "TTJets")!=NULL && strstr(bkgLabels[i].c_str(), "1L")!=NULL) {
	    if(!(nGenMuons+nGenElectrons>=2) && !(nGenMuons+nGenElectrons+nGenTaus>=2))
	      histMRRsq[i]->Fill(MR, Rsq, intLumi*weight);
	  }
	}
      
	// Multijet box top
      	if(boxOption==0)
	  if(strstr(processLabels[i].c_str(), "TTJets")!=NULL) {
	    histMRRsq[i]->Fill(MR, Rsq, intLumi*weight);
	  }
      }

    inputFile->Close();
    delete inputFile;
  }
  
  //*******************************************************************************************
  //Draw Plots
  //*******************************************************************************************
  cv = new TCanvas("cv","cv", 800,600);
  legend = new TLegend(0.7,0.53,0.90,0.88);
  legend->SetTextSize(0.03);
  legend->SetBorderSize(0);
  legend->SetFillStyle(0);

  for (Int_t i = histMRRsq.size()-1 ; i >= 0; --i) {
    if (hasSignal && i==0) {
      legend->AddEntry(histMRRsq[i],processLabels[i].c_str(), "L");
    } else {
      legend->AddEntry(histMRRsq[i],processLabels[i].c_str(), "F");
    }
  }

  THStack *stackUnrolled = new THStack();
  THStack *stackUnrolled2bins = new THStack();
  THStack *stackUnrolledPercentage = new THStack();
  THStack *stackUnrolledPercentage2bins = new THStack();

  float bintotal[nMRBins*nRsqBins] = {0.};

  // fill out the unrolled histograms 
  for (uint i=0; i < histMRRsq.size(); ++i) {
    
    int binN = 0;
    float total_SB = 0.;
    float total_SR = 0.;

    for(int ii = 0; ii<nMRBins; ii++)
      for (int jj = 0; jj<nRsqBins; jj++)      
  	{      
  	  float value = (histMRRsq[i]->GetBinContent(ii+1, jj+1) > 0) ? histMRRsq[i]->GetBinContent(ii+1, jj+1) : 0. ;
	  
	  float Xrange = histMRRsq[i]->GetXaxis()->GetBinLowEdge(ii+2) - histMRRsq[i]->GetXaxis()->GetBinLowEdge(ii+1);
	  float Yrange = histMRRsq[i]->GetYaxis()->GetBinLowEdge(jj+2) - histMRRsq[i]->GetYaxis()->GetBinLowEdge(jj+1);

	  float area =1.;
	  
	  if(density) area = Xrange*Yrange; //normalize each bin by its area

	  histUnrolled[i]->SetBinContent(binN+1, value/area);

	  if(!hasSignal || i>0)
	    bintotal[binN+1] += value/area;	 
	  
	  if(ii<1 || jj<1)
	    total_SB += value/area;
	  else
	    total_SR += value/area;
	  	  
  	  binN++;
  	}

    histUnrolled2bins[i]->SetBinContent(1, total_SB);
    histUnrolled2bins[i]->SetBinContent(2, total_SR);

    histUnrolled[i]->SetMinimum(0.00001);

    if ( histUnrolled[i]->Integral() > 0) {
      if( !hasSignal || i > 0 )
	stackUnrolled->Add(histUnrolled[i]);
    }

    if ( histUnrolled[i]->Integral() > 0) {
      if( !hasSignal || i > 0 )
	stackUnrolled2bins->Add(histUnrolled2bins[i]);
    }

    cout << "Process : " << processLabels[i] << "\n";	  
  }


  // Unroll into two bins for fractions
  float AllBkg_SB = 0;
  float AllBkg_SR = 0;

  for (uint i=0; i < histMRRsq.size(); ++i) {
    if( !hasSignal || i > 0 ){
      AllBkg_SB += histUnrolled2bins[i]->GetBinContent(1);
      AllBkg_SR += histUnrolled2bins[i]->GetBinContent(2);
    }
  }

  for (uint i=0; i < histMRRsq.size(); ++i) {
    if( !hasSignal || i > 0 ){
      histUnrolledPercentage2bins[i]->SetBinContent(1, histUnrolled2bins[i]->GetBinContent(1)/AllBkg_SB);
      histUnrolledPercentage2bins[i]->SetBinContent(2, histUnrolled2bins[i]->GetBinContent(2)/AllBkg_SR);
    }
    
    if ( histUnrolled2bins[i]->Integral() > 0) {
      if( !hasSignal || i > 0 )
	stackUnrolledPercentage2bins->Add(histUnrolledPercentage2bins[i]);
    }
  }
  
  ///
  // fill out the unrolled percentage histograms 
  for (uint i=0; i < histMRRsq.size(); ++i) {
    if( hasSignal && i == 0 ) continue;
      
    int binN = 0;

    for(int ii = 0; ii<nMRBins; ii++)
      for (int jj = 0; jj<nRsqBins; jj++)      
  	{      
  	  float value = (histMRRsq[i]->GetBinContent(ii+1, jj+1) > 0) ? histMRRsq[i]->GetBinContent(ii+1, jj+1) : 0. ;
	  
	  float Xrange = histMRRsq[i]->GetXaxis()->GetBinLowEdge(ii+2) - histMRRsq[i]->GetXaxis()->GetBinLowEdge(ii+1);
	  float Yrange = histMRRsq[i]->GetYaxis()->GetBinLowEdge(jj+2) - histMRRsq[i]->GetYaxis()->GetBinLowEdge(jj+1);

	  float area =1.;
	  
	  if(density) area = Xrange*Yrange; //normalize each bin by its area

	  if(bintotal[binN+1]>0)
	    histUnrolledPercentage[i]->SetBinContent(binN+1, (value/area)/bintotal[binN+1]);

	  binN++;
	}
		
    if ( histUnrolled[i]->Integral() > 0) {
	stackUnrolledPercentage->Add(histUnrolledPercentage[i]);
    }

    cout << "Unrolling Percentage for Process : " << processLabels[i] << "\n";	  
  }

  /// Unrolled plots in bins of R&MR
  TLatex t1(0.1,0.92, "CMS Preliminary");
  TLatex t2(0.6,0.92, "#sqrt{s}=13 TeV, L = 2 fb^{-1}");
  TLatex t3(0.4,0.92, Form("%s",latexlabel.c_str()) );
  t1.SetNDC();
  t2.SetNDC();
  t3.SetNDC();
  t1.SetTextSize(0.05);
  t2.SetTextSize(0.05);
  t3.SetTextSize(0.02);
  t1.SetTextFont(42);
  t2.SetTextFont(42);
  t3.SetTextFont(42);

  stackUnrolled->Draw();
  stackUnrolled->SetMinimum(0.0001);
  // stackUnrolled->SetMaximum(1000);
  cv->SetLogy();
  stackUnrolled->GetHistogram()->GetXaxis()->SetTitle(((TH1F*)(stackUnrolled->GetHists()->At(0)))->GetXaxis()->GetTitle());
  stackUnrolled->GetHistogram()->GetYaxis()->SetTitle(((TH1F*)(stackUnrolled->GetHists()->At(0)))->GetYaxis()->GetTitle());
  stackUnrolled->Draw();
  if(hasSignal) histUnrolled[0]->Draw("same hist");
  legend->Draw();
  t1.Draw();
  t2.Draw();
  t3.Draw();
  cv->SaveAs(Form("Unrolled%s.pdf",Label.c_str()));

  // Unrolled plots in percentages
  cv = new TCanvas("cv","cv", 800,600);
  legend = new TLegend(0.85,0.20,0.95,0.80);
  legend->SetTextSize(0.03);
  legend->SetBorderSize(0);

  for (Int_t i = histMRRsq.size()-1 ; i >= 0; --i) {
    if (hasSignal && i==0) {
      legend->AddEntry(histMRRsq[i],processLabels[i].c_str(), "L");
    } else {
      legend->AddEntry(histMRRsq[i],processLabels[i].c_str(), "F");
    }
  }
  stackUnrolledPercentage->Draw();
  stackUnrolledPercentage->GetHistogram()->GetXaxis()->SetTitle(((TH1F*)(stackUnrolledPercentage->GetHists()->At(0)))->GetXaxis()->GetTitle());
  // stackUnrolledPercentage->GetHistogram()->GetXaxis()->SetRangeUser(0, 35);
  stackUnrolledPercentage->GetHistogram()->GetYaxis()->SetTitle(((TH1F*)(stackUnrolledPercentage->GetHists()->At(0)))->GetYaxis()->GetTitle());
  if(hasSignal) histUnrolledPercentage[0]->Draw("same hist"); 
  legend->Draw();
  t1.Draw();
  t2.Draw();
  t3.Draw();
  cv->SaveAs(Form("UnrolledPercentage%s.pdf",Label.c_str()));

  // Unrolled plots in sideband vs signal box
  cv = new TCanvas("cv","cv", 800,600);
  legend = new TLegend(0.85,0.20,0.95,0.80);
  legend->SetTextSize(0.03);
  legend->SetBorderSize(0);

  for (Int_t i = histMRRsq.size()-1 ; i >= 0; --i) {
    if (hasSignal && i==0) {
      legend->AddEntry(histMRRsq[i],processLabels[i].c_str(), "L");
    } else {
      legend->AddEntry(histMRRsq[i],processLabels[i].c_str(), "F");
    }
  }
  stackUnrolled2bins->Draw();
  stackUnrolled2bins->GetHistogram()->GetXaxis()->SetTitle(((TH1F*)(stackUnrolled2bins->GetHists()->At(0)))->GetXaxis()->GetTitle());
  stackUnrolled2bins->GetHistogram()->GetYaxis()->SetTitle(((TH1F*)(stackUnrolled2bins->GetHists()->At(0)))->GetYaxis()->GetTitle());
  if(hasSignal) histUnrolled2bins[0]->Draw("same hist");
  legend->Draw();
  t1.Draw();
  t2.Draw();
  t3.Draw();
  cv->SaveAs(Form("Unrolled2bins%s.pdf",Label.c_str()));

  // Unrolled plots in sideband vs signal box in fractions
  cv = new TCanvas("cv","cv", 800,600);
  legend = new TLegend(0.7,0.23,0.90,0.88);
  legend->SetTextSize(0.03);
  legend->SetBorderSize(0);

  for (Int_t i = histMRRsq.size()-1 ; i >= 0; --i) {
    if (hasSignal && i==0) {
      legend->AddEntry(histMRRsq[i],processLabels[i].c_str(), "L");
    } else {
      legend->AddEntry(histMRRsq[i],processLabels[i].c_str(), "F");
    }
  }

  stackUnrolledPercentage2bins->Draw();
  stackUnrolledPercentage2bins->GetHistogram()->GetXaxis()->SetTitle(((TH1F*)(stackUnrolledPercentage2bins->GetHists()->At(0)))->GetXaxis()->GetTitle());
  stackUnrolledPercentage2bins->GetHistogram()->GetYaxis()->SetTitle(((TH1F*)(stackUnrolledPercentage2bins->GetHists()->At(0)))->GetYaxis()->GetTitle());
  stackUnrolledPercentage2bins->GetHistogram()->GetXaxis()->SetBinLabel(1, "Sideband");
  stackUnrolledPercentage2bins->GetHistogram()->GetXaxis()->SetBinLabel(2, "Signal Sensitive Region");
  if(hasSignal) histUnrolledPercentage2bins[0]->Draw("same hist");
  legend->Draw();
  t1.Draw();
  t2.Draw();
  t3.Draw();
  cv->SaveAs(Form("UnrolledPercentage2bins%s.pdf",Label.c_str()));


   //--------------------------------------------------------------------------------------------------------------
  // Output
  //==============================================================================================================
  TFile *file = TFile::Open(("RazorPlots"+Label+".root").c_str(), "RECREATE");
  file->cd();

  for(int i=0; i<int(inputfiles.size()); i++) {
    file->WriteTObject(histMR[i], Form("histMR_%s",processLabels[i].c_str()), "WriteDelete");
    file->WriteTObject(histRsq[i], Form("histRsq_%s",processLabels[i].c_str()), "WriteDelete");
    file->WriteTObject(histMRRsq[i], Form("histMRRsq_%s",processLabels[i].c_str()), "WriteDelete");
    histUnrolled[i]->Write();  
    histUnrolled2bins[i]->Write();  
    histUnrolledPercentage[i]->Write();  
    histUnrolledPercentage2bins[i]->Write();  
  }
  
  stackUnrolled->Write();
  stackUnrolled2bins->Write();
  stackUnrolledPercentage->Write();
  stackUnrolledPercentage2bins->Write();
 }
Example #4
0
void Stacker_onePoint(TString path,TString cut,double ME_Br,double EM_Br)
{
	TString MCSamples[11]={"wt", "ZZ","H2WWleptonic", "WlepZmue","WincZtautau",
			"H2tt", "ttbar", "WWleptonic",  "Z2tt","H2tm","H2te"};
	Int_t   MCcolors[11]={ kMagenta+3, kMagenta,kBlue+1,kOrange+1,kOrange,
			kGreen, kRed, kYellow, kCyan, kBlack, kBlack};


	TCanvas* c1 = new TCanvas("canvasLog"+cut,"canvasLog"+cut,600,600);
	c1->SetLogy();
//	TCanvas* c2 = new TCanvas("c_diff","c_diff",600,600);
	TCanvas* c3 = new TCanvas("c_ratio"+cut,"c_ratio"+cut,600,600);
	TCanvas* c5 = new TCanvas("canvasStack2"+cut,"canvasStack2"+cut,600,600); c5->SetLogy();
	TCanvas* c4 = new TCanvas("canvasStack"+cut,"canvasStack"+cut,600,600);	c4->SetLogy();
	TCanvas* c6 = new TCanvas("canvasEM_ME","canvasEM_ME",600,600); c6->SetLogy();

	TLegend* leg = new TLegend(0.5,0.7,0.7,0.9);
	leg->SetFillColor(kWhite);
	leg->SetBorderSize(1);

	THStack* hs = new THStack("ME_EM","ME_EM");
	THStack* hsStackedME = new THStack("stacked_ME",";"
			"M_{Collinear} (GeV);Events / 4 GeV");
	THStack* hsStackedEM = new THStack("stacked_EM","e#mu Channel;"
			"M_{Collinear} (GeV);Events / 4 GeV");
	THStack* hsEM_ME = new THStack("stacked_EM_ME",";"
				"M_{Collinear} (GeV);Events / 4 GeV");

	TH1D* EM_sum = new TH1D("EM_sum","bla",250,0,500);
	TH1D* ME_sum = new TH1D("ME_sum","bla",250,0,500);

	TFile* fs = new TFile(path+"H2tm"+cut+".root");
	TH1D* signal_ME = (TH1D*)fs->Get("ME_Mcoll"); signal_ME->SetLineColor(kBlack);
	signal_ME->SetLineStyle(2);
	signal_ME->SetName("signal_ME_Mcoll");
	TH1D* signal_EM = (TH1D*)fs->Get("EM_Mcoll"); signal_EM->SetLineColor(kBlack);
	signal_EM->SetLineStyle(2);
	signal_EM->SetName("signal_EM_Mcoll");

	double signalME_c = ME_Br*10;
	double signalEM_c = EM_Br*10;

	double c[11]={1, 1,1,1,1,1,1,1,1,signalME_c,signalEM_c};

	//group BG
	//diboson
	TFile* f_ZZ = new TFile(path+"ZZ"+cut+".root");
	TFile* f_WZ1 = new TFile(path+"WlepZmue"+cut+".root");
	TFile* f_WZ2 = new TFile(path+"WincZtautau"+cut+".root");
	TFile* f_WW = new TFile(path+"WWleptonic"+cut+".root");

	TH1D* h_ME_ZZ = (TH1D*)f_ZZ->Get("ME_Mcoll");h_ME_ZZ->Scale(c[1]);
	TH1D* h_EM_ZZ = (TH1D*)f_ZZ->Get("EM_Mcoll");h_EM_ZZ->Scale(c[1]);
	TH1D* h_ME_WZ1 = (TH1D*)f_WZ1->Get("ME_Mcoll");h_ME_WZ1->Scale(c[3]);
	TH1D* h_EM_WZ1 = (TH1D*)f_WZ1->Get("EM_Mcoll");h_EM_WZ1->Scale(c[3]);
	TH1D* h_ME_WZ2 = (TH1D*)f_WZ2->Get("ME_Mcoll");h_ME_WZ2->Scale(c[4]);
	TH1D* h_EM_WZ2 = (TH1D*)f_WZ2->Get("EM_Mcoll");h_EM_WZ2->Scale(c[4]);
	TH1D* h_ME_WW = (TH1D*)f_WW->Get("ME_Mcoll");h_ME_WW->Scale(c[7]);
	TH1D* h_EM_WW = (TH1D*)f_WW->Get("EM_Mcoll");h_EM_WW->Scale(c[7]);

	TH1D* Diboson_ME = new TH1D("Diboson_ME","Diboson_ME",250,0,500);
	Diboson_ME = (TH1D*)h_ME_ZZ->Clone("ME_Mcoll");
	Diboson_ME->Add(h_ME_WZ1);Diboson_ME->Add(h_ME_WZ2);Diboson_ME->Add(h_ME_WW);
	TH1D* Diboson_EM = new TH1D("Diboson_EM","Diboson_EM",250,0,500);
	Diboson_EM = (TH1D*)h_ME_ZZ->Clone("EM_Mcoll");
	Diboson_EM->Add(h_EM_WZ1);Diboson_EM->Add(h_EM_WZ2);Diboson_EM->Add(h_EM_WW);

	TFile *diboson_out = new TFile(path+"Diboson"+cut+".root","RECREATE");
	Diboson_ME->Write();
	Diboson_EM->Write();
	diboson_out->Close();

	//Higgs SM
	TFile* f_H2WW = new TFile(path+"H2WWleptonic"+cut+".root");
	TFile* f_H2tt = new TFile(path+"H2tt"+cut+".root");
	TH1D* h_ME_H2WW = (TH1D*)f_H2WW->Get("ME_Mcoll");h_ME_H2WW->Scale(c[2]);
	TH1D* h_EM_H2WW = (TH1D*)f_H2WW->Get("EM_Mcoll");h_EM_H2WW->Scale(c[2]);
	TH1D* h_ME_H2tt = (TH1D*)f_H2tt->Get("ME_Mcoll");h_ME_H2tt->Scale(c[5]);
	TH1D* h_EM_H2tt = (TH1D*)f_H2tt->Get("EM_Mcoll");h_EM_H2tt->Scale(c[5]);

	TH1D* SMHiggs_ME = new TH1D("SMHiggs_ME","SMHiggs_ME",250,0,500);
	SMHiggs_ME = (TH1D*)h_ME_H2WW->Clone("ME_Mcoll");
	SMHiggs_ME->Add(h_ME_H2tt);
	TH1D* SMHiggs_EM = new TH1D("SMHiggs_EM","SMHiggs_EM",250,0,500);
	SMHiggs_EM = (TH1D*)h_EM_H2WW->Clone("EM_Mcoll");
	SMHiggs_EM->Add(h_EM_H2tt);

	TFile *SMHiggs_out = new TFile(path+"SMHiggs"+cut+".root","RECREATE");
	SMHiggs_ME->Write();
	SMHiggs_EM->Write();
	SMHiggs_out->Close();

	TString MCSamplesGrouped[5]={"wt",	"SMHiggs", "ttbar", "Diboson",  "Z2tt"};
	Int_t   MCcolorsGrouped[5]={ kMagenta+3,kGreen, kRed, kYellow, kCyan};
	double cGrouped[5]={1, 1,1,1,1};


	//BG
	for(int i=0; i<5; i++)
		{
			TFile* f = new TFile(path+MCSamplesGrouped[i]+cut+".root");
			TH1D* h_ME = (TH1D*)f->Get("ME_Mcoll");
			TH1D* h_EM = (TH1D*)f->Get("EM_Mcoll");
			TH1D* h_ME_l = (TH1D*)h_ME->Clone();
			TH1D* h_EM_l = (TH1D*)h_EM->Clone();
			h_ME->Scale(cGrouped[i]); h_EM->Scale(cGrouped[i]);
			h_ME_l->Scale(cGrouped[i]); h_EM_l->Scale(cGrouped[i]);
			h_ME->Rebin(2); h_EM->Rebin(2); h_ME_l->Rebin(2); h_EM_l->Rebin(2);
			h_ME->SetLineColor(MCcolorsGrouped[i]); h_EM->SetLineColor(MCcolorsGrouped[i]);
			h_ME_l->SetLineColor(MCcolorsGrouped[i]); h_EM_l->SetLineColor(MCcolorsGrouped[i]);
			h_ME->SetFillColor(MCcolorsGrouped[i]); h_EM->SetFillColor(MCcolorsGrouped[i]);
//			h_EM->SetLineStyle(7); h_EM_l->SetLineStyle(7);

			if (i==0){
				EM_sum = (TH1D*)h_EM->Clone("EM_sum");
				ME_sum = (TH1D*)h_ME->Clone("ME_sum");
			}
			else{
				EM_sum->Add(h_EM);
				ME_sum->Add(h_ME);
			}
			TH1D* h_ratio = (TH1D*)h_ME->Clone("h_ratio");
			h_ratio->Divide(h_EM);
			h_ratio->GetXaxis()->SetRangeUser(0,300); h_ratio->GetYaxis()->SetRangeUser(0,20);
			hs->Add(h_ME_l); hs->Add(h_EM_l);
			hsStackedME->Add(h_ME);
			hsStackedEM->Add(h_EM);

			c3->cd();
			h_ratio->Draw("sames");

		}
	//add Signals
	for(int i=9;i<11;i++){
		TFile* f = new TFile(path+MCSamples[i]+cut+".root");
		TH1D* h_ME = (TH1D*)f->Get("ME_Mcoll");
		TH1D* h_EM = (TH1D*)f->Get("EM_Mcoll");
		h_ME->Scale(c[i]); h_EM->Scale(c[i]);
		h_ME->Rebin(2); h_EM->Rebin(2);
		h_ME->GetXaxis()->SetRangeUser(0,300); h_EM->GetXaxis()->SetRangeUser(0,300);
		h_ME->SetLineColor(MCcolors[i]); h_EM->SetLineColor(MCcolors[i]);
		h_ME->SetLineStyle(2);h_EM->SetLineStyle(2);
		EM_sum->Add(h_EM);
		ME_sum->Add(h_ME);
		TH1D* h_ratio = (TH1D*)h_ME->Clone("h_ratio");
		h_ratio->Divide(h_EM);
		h_ratio->GetXaxis()->SetRangeUser(0,300); h_ratio->GetYaxis()->SetRangeUser(0,20);
		hs->Add(h_ME); hs->Add(h_EM);
		c3->cd();
		h_ratio->Draw("sames");
	}

	TString MCSamplesGrouped2[5]={"Wt",	"SM Higgs", "t#bar{t}", "Diboson",  "Z#rightarrow#tau#tau"};
	//legend
	leg->AddEntry(signal_ME,"Signal","l");
	for(int j=4;j>=0;j--){
		TFile* f = new TFile(path+MCSamplesGrouped[j]+cut+".root");
		TH1D* h_ME = (TH1D*)f->Get("ME_Mcoll");
		h_ME->SetLineColor(MCcolorsGrouped[j]);
		h_ME->SetFillColor(MCcolorsGrouped[j]);
		leg->AddEntry(h_ME,MCSamplesGrouped2[j],"f");
	}

	ME_sum->GetXaxis()->SetRangeUser(0,300); EM_sum->GetXaxis()->SetRangeUser(0,300);
	ME_sum->SetLineColor(kBlue); ME_sum->SetFillColor(0);
	EM_sum->SetLineColor(kGreen+2); EM_sum->SetFillColor(0);
	ME_sum->SetOption("E1"); EM_sum->SetOption("E1");
	hsEM_ME->Add(ME_sum); hsEM_ME->Add(EM_sum);
	TLegend* leg2 = new TLegend(0.5,0.7,0.7,0.9);
	leg2->SetFillColor(kWhite); leg2->SetLineColor(0);
	leg2->AddEntry(ME_sum,"#mue sample","l");
	leg2->AddEntry(EM_sum,"e#mu sample","l");
	leg2->SetTextFont(42);
	TLatex* t = new TLatex(0.5,0.8,"#sqrt{s} = 8 TeV");
	t->SetTextSize(0.07);
	c6->cd();
//	ME_sum->Draw("E1");
//	EM_sum->Draw("E1 sames");
//	hsEM_ME->GetXaxis()->SetRangeUser(0,300);
	hsEM_ME->Draw("nostack, e1");
	leg2->Draw();
	t->Draw();


	c3->cd();
	c3->SetTitle("ratio"+cut);
	leg->Draw();

	c1->cd();
	hs->Draw("nostack");
	leg->Draw();
	c4->cd();
	signal_ME->Scale(c[9]);
	signal_ME->Rebin(2);
	hsStackedME->Add(signal_ME);
//	hsStackedME->GetXaxis()->SetRangeUser(0,300);
	hsStackedME->Draw("hist");
//	signal_ME->Draw("sames");
	leg->SetLineColor(0);
	leg->Draw();

//	c5->cd();
//	signal_EM->Scale(c[9]);
//	signal_EM->Rebin(2);
//	hsStackedEM->Add(signal_EM);
////	hsStackedEM->GetXaxis()->SetRangeUser(0,300);
//	hsStackedEM->Draw("hist");
//	//	signal_EM->Draw("sames");
//	leg->Draw();

	std::ostringstream strsME;
	strsME << ME_Br;
	std::string strBrME = strsME.str();

	std::ostringstream strsEM;
	strsEM << EM_Br;
	std::string strBrEM = strsEM.str();

	TFile *outputf = new TFile("MCStacked"+cut+"BR_ME"+strBrME+"BR_EM"+strBrEM+".root","RECREATE");

	hsStackedME->Write();
	hsStackedEM->Write();
	EM_sum->Write();
	ME_sum->Write();

	signal_ME->Write();

	c1->Write();
	c4->Write();
	c3->Write();
	c5->Write();
	c6->Write();

	outputf->Close();

}
Example #5
0
int main(){

  char tempname[200];
  vector<TFile *> T_inputfilevec,WJet_inputfilevec, TTbar_inputfilevec, ZJet_inputfilevec, QCD_inputfilevec;
  map<int, string> cutname, histname, Hname;
  TFile *file, *file2, *file30, *file3;
  TH1D *temphist, *temphist2, * temphist30, *temphistI, *temphistII, *temphistIII, *temphistI_lowDphi, *temphistII_lowDphi, *temphistIII_lowDphi;
  THStack * tempstack;
  TDirectory *cdtoitt, *cdtoit;
  Selection2 * sel = new Selection2();
  cutname = sel->cutName();
  map<int, string> Ttype, WJettype, TTbartype, ZJettype, QCDtype;
  WJettype[0]="allEvents";
  TTbartype[0]="allEvents";
  Ttype[0]="allEvents";
  ZJettype[0]="allEvents";
  QCDtype[0]="allEvents";

  int qcdHT=7, zjnHT=4, ttbarnHT=1, wjnHT=7, tnHT=4;



// ..............................................................................................................//
// QCD Section
// .............................................................................................................//

  for(int i=1; i<=qcdHT ; i++){
    if(i==1)sprintf(tempname,"../results_filelist_Spring15_QCD_HT_200_300_.root");
    else if(i==2)sprintf(tempname,"../results_filelist_Spring15_QCD_HT_300_500_.root");
    else if(i==3)sprintf(tempname,"../results_filelist_Spring15_QCD_HT_500_700_.root");
    else if(i==4)sprintf(tempname,"../results_filelist_Spring15_QCD_HT_700_1000_.root");
    else if(i==5)sprintf(tempname,"../results_filelist_Spring15_QCD_HT_1000_1500_.root");
    else if(i==6)sprintf(tempname,"../results_filelist_Spring15_QCD_HT_1500_2000_.root");
    else if(i==7)sprintf(tempname,"../results_filelist_Spring15_QCD_HT_2000_Inf_.root");
    else{cout << " Error!! There are only 4 QCD ht binned sample " << endl;}
    QCD_inputfilevec.push_back(TFile::Open(tempname,"R"));
  }//end of loop over HTbins


  // Stack
  tempstack = new THStack("stack","Binned Sample Stack");
  sprintf(tempname,"results_filelist_Spring15_QCD_stacked.root");
  file = new TFile(tempname,"RECREATE");
  histname.clear();
  histname[0]="weight";
  histname[1]="HT";
  histname[2]="MHT";
  histname[3]="NJet";
  histname[4]="NBtag";

  Hname.clear();
  Hname[0]="yield_tauId";
  Hname[1]="yield_tauId_trk";
  Hname[2]="cutflow_preselection";


  for(int j=0; j< Hname.size(); j++){

    for(int i=0; i<qcdHT ; i++){                                                  // loop over different HT bins

      sprintf(tempname,"%s",(Hname[j]).c_str());
      temphist = (TH1D *) QCD_inputfilevec.at(i)->Get(tempname)->Clone();
      //if (luminosity>0&&!doScale) temphist->Scale(scalefactor);
      temphist->SetFillColor(i+2);
      tempstack->Add(temphist);

    }//end of loop over HTbins 1..7
    sprintf(tempname,"%s",(Hname[j]).c_str());
    tempstack->Write(tempname);

    delete tempstack;
    tempstack = new THStack("stack","Binned Sample Stack");

  }

    for(map<int , string >::iterator itt=QCDtype.begin(); itt!=QCDtype.end();itt++){        // loop over different event types

    cdtoitt = file->mkdir((itt->second).c_str());
    cdtoitt->cd();

    for(map<int , string >::iterator it=cutname.begin(); it!=cutname.end();it++){   // loop over different cutnames

      cdtoit =  cdtoitt->mkdir((it->second).c_str());
      cdtoit->cd();

      for(int j=0; j<histname.size(); j++){                                        // loop over different histograms

        for(int i=0; i<qcdHT ; i++){                                                  // loop over different HT bins

          //cout << "================================" << endl;
          //cout << "HT#: " <<i << ", WJtype: " << itt->second << ", cutname: " << it->second << ", hist#: " << j << endl;
          sprintf(tempname,"%s/%s/%s_%s_%s",(itt->second).c_str(),(it->second).c_str(),(histname[j]).c_str(),(it->second).c_str(),(itt->second).c_str());
          temphist = (TH1D *) QCD_inputfilevec.at(i)->Get(tempname)->Clone();
          //if (luminosity>0&&!doScale) temphist->Scale(scalefactor);
          temphist->SetFillColor(i+2);
          tempstack->Add(temphist);

        }//end of loop over HTbins 1..7

        sprintf(tempname,"%s_%s_%s",histname[j].c_str(),(it->second).c_str(),(itt->second).c_str());
        tempstack->Write(tempname);

        delete tempstack;
        tempstack = new THStack("stack","Binned Sample Stack");

      }//end of loop over histograms

    }//end of loop over cutnames

  }//end of loop over event types

  file->Close();
  printf("QCD main histograms stacked \n ");





// ..............................................................................................................//
// ZJet Section
// .............................................................................................................//

  for(int i=1; i<=zjnHT ; i++){
    if(i==1)sprintf(tempname,"../results_filelist_Spring15_ZJet_HT_100_200_.root");
    else if(i==2)sprintf(tempname,"../results_filelist_Spring15_ZJet_HT_200_400_.root");
    else if(i==3)sprintf(tempname,"../results_filelist_Spring15_ZJet_HT_400_600_.root");
    else if(i==4)sprintf(tempname,"../results_filelist_Spring15_ZJet_HT_600_Inf_.root");
    else{cout << " Error!! There are only 4 ZJet ht binned sample " << endl;}
    ZJet_inputfilevec.push_back(TFile::Open(tempname,"R"));
  }//end of loop over HTbins


  // Stack
  tempstack = new THStack("stack","Binned Sample Stack");
  sprintf(tempname,"results_filelist_Spring15_ZJet_stacked.root");
  file = new TFile(tempname,"RECREATE");
  histname.clear();
  histname[0]="weight";
  histname[1]="HT";
  histname[2]="MHT";
  histname[3]="NJet";
  histname[4]="NBtag";

  Hname.clear();
  Hname[0]="yield_tauId";
  Hname[1]="yield_tauId_trk";
  Hname[2]="cutflow_preselection";


  for(int j=0; j< Hname.size(); j++){

    for(int i=0; i<zjnHT ; i++){                                                  // loop over different HT bins

      sprintf(tempname,"%s",(Hname[j]).c_str());
      temphist = (TH1D *) ZJet_inputfilevec.at(i)->Get(tempname)->Clone();
      //if (luminosity>0&&!doScale) temphist->Scale(scalefactor);
      temphist->SetFillColor(i+2);
      tempstack->Add(temphist);

    }//end of loop over HTbins 1..7
    sprintf(tempname,"%s",(Hname[j]).c_str());
    tempstack->Write(tempname);

    delete tempstack;
    tempstack = new THStack("stack","Binned Sample Stack");

  }

    for(map<int , string >::iterator itt=ZJettype.begin(); itt!=ZJettype.end();itt++){        // loop over different event types

    cdtoitt = file->mkdir((itt->second).c_str());
    cdtoitt->cd();

    for(map<int , string >::iterator it=cutname.begin(); it!=cutname.end();it++){   // loop over different cutnames

      cdtoit =  cdtoitt->mkdir((it->second).c_str());
      cdtoit->cd();

      for(int j=0; j<histname.size(); j++){                                        // loop over different histograms

        for(int i=0; i<zjnHT ; i++){                                                  // loop over different HT bins

          //cout << "================================" << endl;
          //cout << "HT#: " <<i << ", WJtype: " << itt->second << ", cutname: " << it->second << ", hist#: " << j << endl;
          sprintf(tempname,"%s/%s/%s_%s_%s",(itt->second).c_str(),(it->second).c_str(),(histname[j]).c_str(),(it->second).c_str(),(itt->second).c_str());
          temphist = (TH1D *) ZJet_inputfilevec.at(i)->Get(tempname)->Clone();
          //if (luminosity>0&&!doScale) temphist->Scale(scalefactor);
          temphist->SetFillColor(i+2);
          tempstack->Add(temphist);

        }//end of loop over HTbins 1..7

        sprintf(tempname,"%s_%s_%s",histname[j].c_str(),(it->second).c_str(),(itt->second).c_str());
        tempstack->Write(tempname);

        delete tempstack;
        tempstack = new THStack("stack","Binned Sample Stack");

      }//end of loop over histograms

    }//end of loop over cutnames

  }//end of loop over event types

  file->Close();
  printf("ZJet main histograms stacked \n ");



// .....................................................................................................................................................//
// TTbar Section
// .....................................................................................................................................................//

  // Load the files to a vector
  // These are the HT, MHT, .. variables
  for(int i=1; i<=ttbarnHT ; i++){
    if(i==1)sprintf(tempname,"../results_filelist_Spring15_TTbar_.root");
    else{cout << " Error!! There are only 1 TTbaret ht binned sample " << endl;}
    TTbar_inputfilevec.push_back(TFile::Open(tempname,"R"));
  }//end of loop over HTbins

  // Stack
  tempstack = new THStack("stack","Binned Sample Stack");
  sprintf(tempname,"results_filelist_Spring15_TTbar_stacked.root");
  file = new TFile(tempname,"RECREATE");
  histname.clear();
  histname[0]="weight";
  histname[1]="HT";
  histname[2]="MHT";
  histname[3]="NJet";
  histname[4]="NBtag";

  Hname.clear();
  Hname[0]="yield_tauId";
  Hname[1]="yield_tauId_trk";
  Hname[2]="cutflow_preselection";

  for(int j=0; j< Hname.size(); j++){

    for(int i=0; i<ttbarnHT ; i++){                                                  // loop over different HT bins

      sprintf(tempname,"%s",(Hname[j]).c_str());
      temphist = (TH1D *) TTbar_inputfilevec.at(i)->Get(tempname)->Clone();
      //if (luminosity>0&&!doScale) temphist->Scale(scalefactor);
      temphist->SetFillColor(i+2);
      tempstack->Add(temphist);

    }//end of loop over HTbins 1..7
    sprintf(tempname,"%s",(Hname[j]).c_str());
    tempstack->Write(tempname);

    delete tempstack;
    tempstack = new THStack("stack","Binned Sample Stack");

  }

  for(map<int , string >::iterator itt=TTbartype.begin(); itt!=TTbartype.end();itt++){        // loop over different event types

    cdtoitt = file->mkdir((itt->second).c_str());
    cdtoitt->cd();

    for(map<int , string >::iterator it=cutname.begin(); it!=cutname.end();it++){   // loop over different cutnames

      cdtoit =  cdtoitt->mkdir((it->second).c_str());
      cdtoit->cd();

      for(int j=0; j<histname.size(); j++){                                        // loop over different histograms

        for(int i=0; i<ttbarnHT ; i++){                                                  // loop over different HT bins

          //cout << "================================" << endl;
          //cout << "HT#: " <<i << ", TTbartype: " << itt->second << ", cutname: " << it->second << ", hist#: " << j << endl;
          sprintf(tempname,"%s/%s/%s_%s_%s",(itt->second).c_str(),(it->second).c_str(),(histname[j]).c_str(),(it->second).c_str(),(itt->second).c_str());
          temphist = (TH1D *) TTbar_inputfilevec.at(i)->Get(tempname)->Clone();
          //if (luminosity>0&&!doScale) temphist->Scale(scalefactor);
          temphist->SetFillColor(i+2);
          tempstack->Add(temphist);

        }//end of loop over HTbins 1..7

        sprintf(tempname,"%s_%s_%s",histname[j].c_str(),(it->second).c_str(),(itt->second).c_str());
        tempstack->Write(tempname);
        delete tempstack;
        tempstack = new THStack("stack","Binned Sample Stack");

      }//end of loop over histograms

    }//end of loop over cutnames

  }//end of loop over event types

  file->Close();
  printf("TTbar main histograms stacked \n ");




// ..............................................................................................................//
// WJet Section
// .............................................................................................................//

  for(int i=1; i<=wjnHT ; i++){
    if(i==1)sprintf(tempname,"../results_filelist_Spring15_WJet_HT_100_200_.root");
    else if(i==2)sprintf(tempname,"../results_filelist_Spring15_WJet_HT_200_400_.root");
    else if(i==3)sprintf(tempname,"../results_filelist_Spring15_WJet_HT_400_600_.root");
    else if(i==4)sprintf(tempname,"../results_filelist_Spring15_WJet_HT_600_800_.root");
    else if(i==5)sprintf(tempname,"../results_filelist_Spring15_WJet_HT_800_1200_.root");
    else if(i==6)sprintf(tempname,"../results_filelist_Spring15_WJet_HT_1200_2500_.root");
    else if(i==7)sprintf(tempname,"../results_filelist_Spring15_WJet_HT_2500_Inf_.root");
    else{cout << " Error!! There are only 4 WJet ht binned sample " << endl;}
    WJet_inputfilevec.push_back(TFile::Open(tempname,"R"));
  }//end of loop over HTbins


  // Stack
  tempstack = new THStack("stack","Binned Sample Stack");
  sprintf(tempname,"results_filelist_Spring15_WJet_stacked.root");
  file = new TFile(tempname,"RECREATE");
  histname.clear();
  histname[0]="weight";
  histname[1]="HT";
  histname[2]="MHT";
  histname[3]="NJet";
  histname[4]="NBtag";

  Hname.clear();
  Hname[0]="yield_tauId";
  Hname[1]="yield_tauId_trk";
  Hname[2]="cutflow_preselection";


  for(int j=0; j< Hname.size(); j++){

    for(int i=0; i<wjnHT ; i++){                                                  // loop over different HT bins

      sprintf(tempname,"%s",(Hname[j]).c_str());
      temphist = (TH1D *) WJet_inputfilevec.at(i)->Get(tempname)->Clone();
      //if (luminosity>0&&!doScale) temphist->Scale(scalefactor);
      temphist->SetFillColor(i+2);
      tempstack->Add(temphist);

    }//end of loop over HTbins 1..7
    sprintf(tempname,"%s",(Hname[j]).c_str());
    tempstack->Write(tempname);

    delete tempstack;
    tempstack = new THStack("stack","Binned Sample Stack");

  }

    for(map<int , string >::iterator itt=WJettype.begin(); itt!=WJettype.end();itt++){        // loop over different event types

    cdtoitt = file->mkdir((itt->second).c_str());
    cdtoitt->cd();

    for(map<int , string >::iterator it=cutname.begin(); it!=cutname.end();it++){   // loop over different cutnames

      cdtoit =  cdtoitt->mkdir((it->second).c_str());
      cdtoit->cd();

      for(int j=0; j<histname.size(); j++){                                        // loop over different histograms

        for(int i=0; i<wjnHT ; i++){                                                  // loop over different HT bins

          //cout << "================================" << endl;
          //cout << "HT#: " <<i << ", WJtype: " << itt->second << ", cutname: " << it->second << ", hist#: " << j << endl;
          sprintf(tempname,"%s/%s/%s_%s_%s",(itt->second).c_str(),(it->second).c_str(),(histname[j]).c_str(),(it->second).c_str(),(itt->second).c_str());
          temphist = (TH1D *) WJet_inputfilevec.at(i)->Get(tempname)->Clone();
          //if (luminosity>0&&!doScale) temphist->Scale(scalefactor);
          temphist->SetFillColor(i+2);
          tempstack->Add(temphist);

        }//end of loop over HTbins 1..7

        sprintf(tempname,"%s_%s_%s",histname[j].c_str(),(it->second).c_str(),(itt->second).c_str());
        tempstack->Write(tempname);

        delete tempstack;
        tempstack = new THStack("stack","Binned Sample Stack");

      }//end of loop over histograms

    }//end of loop over cutnames

  }//end of loop over event types

  file->Close();
  printf("WJet main histograms stacked \n ");

// ..............................................................................................................................//
// Single Top Section
// ..............................................................................................................................//

  for(int i=1; i<=tnHT ; i++){
    if     (i==1)sprintf(tempname,"../results_filelist_Spring15_ST_t_top_.root");
    else if(i==2)sprintf(tempname,"../results_filelist_Spring15_ST_t_antitop_.root");
    else if(i==3)sprintf(tempname,"../results_filelist_Spring15_ST_tW_top_.root");
    else if(i==4)sprintf(tempname,"../results_filelist_Spring15_ST_tW_antitop_.root");
    //else if(i==5)sprintf(tempname,"../");
    //else if(i==6)sprintf(tempname,"../");
    else{cout << " Error!! There are only 6 T ht binned sample " << endl;}
    T_inputfilevec.push_back(TFile::Open(tempname,"R"));
  }//end of loop over HTbins


  // Stack
  tempstack = new THStack("stack","Binned Sample Stack");
  sprintf(tempname,"results_filelist_Spring15_T_stacked.root");
  file = new TFile(tempname,"RECREATE");
  histname.clear();
  histname[0]="weight";
  histname[1]="HT";
  histname[2]="MHT";
  histname[3]="NJet";
  histname[4]="NBtag";

  Hname.clear();
  Hname[0]="yield_tauId";
  Hname[1]="yield_tauId_trk";
  Hname[2]="cutflow_preselection";

  // Loop over Hname
  for(int j=0; j< Hname.size(); j++){

    for(int i=0; i<tnHT ; i++){                                                  // loop over different HT bins

      sprintf(tempname,"%s",(Hname[j]).c_str());
      temphist = (TH1D *) T_inputfilevec.at(i)->Get(tempname)->Clone();
      //if (luminosity>0&&!doScale) temphist->Scale(scalefactor);
      temphist->SetFillColor(i+2);
      tempstack->Add(temphist);

    }//end of loop over HTbins 1..7
    sprintf(tempname,"%s",(Hname[j]).c_str());
    tempstack->Write(tempname);

    delete tempstack;
    tempstack = new THStack("stack","Binned Sample Stack");

  }

  // Loop over event types, cutnames, etc
  for(map<int , string >::iterator itt=Ttype.begin(); itt!=Ttype.end();itt++){        // loop over different event types

    cdtoitt = file->mkdir((itt->second).c_str());
    cdtoitt->cd();

    for(map<int , string >::iterator it=cutname.begin(); it!=cutname.end();it++){   // loop over different cutnames

      cdtoit =  cdtoitt->mkdir((it->second).c_str());
      cdtoit->cd();

      for(int j=0; j<histname.size(); j++){                                        // loop over different histograms

        for(int i=0; i<tnHT ; i++){                                                  // loop over different HT bins

          //cout << "================================" << endl;
          //cout << "HT#: " <<i << ", WJtype: " << itt->second << ", cutname: " << it->second << ", hist#: " << j << endl;
          sprintf(tempname,"%s/%s/%s_%s_%s",(itt->second).c_str(),(it->second).c_str(),(histname[j]).c_str(),(it->second).c_str(),(itt->second).c_str());
          temphist = (TH1D *) T_inputfilevec.at(i)->Get(tempname)->Clone();
          //if (luminosity>0&&!doScale) temphist->Scale(scalefactor);
          temphist->SetFillColor(i+2);
          tempstack->Add(temphist);

        }//end of loop over HTbins 1..7

        sprintf(tempname,"%s_%s_%s",histname[j].c_str(),(it->second).c_str(),(itt->second).c_str());
        tempstack->Write(tempname);

        delete tempstack;
        tempstack = new THStack("stack","Binned Sample Stack");

      }//end of loop over histograms

    }//end of loop over cutnames

  }//end of loop over event types

  file->Close();
  printf("T main histograms stacked \n ");


// ..................................................................................................................................................... //
// Stack main histograms from TTbar and WJet and Single Top and ZJet and QCD
// ..................................................................................................................................................... //

  // There are three contributors 1-TTbar and 2-WJet 3-T
  int NSamples=5;

  // A vector that contains all the samples
  vector<TFile*> sample_inputfilevec;

  THStack * tempstack2 = new THStack("stack","Binned Sample Stack");

  // Load the files to a vector
  // These are the HT, MHT, .. variables
  for(int i=1; i<=NSamples ; i++){
    if(i==1)sprintf(tempname,"results_filelist_Spring15_TTbar_stacked.root");
    else if(i==2)sprintf(tempname,"results_filelist_Spring15_WJet_stacked.root");
    else if(i==3)sprintf(tempname,"results_filelist_Spring15_T_stacked.root");
    else if(i==4)sprintf(tempname,"results_filelist_Spring15_ZJet_stacked.root");
    else if(i==5)sprintf(tempname,"results_filelist_Spring15_QCD_stacked.root");
    else{cout << " Error!! There are only 3 contributors! " << endl;}
    sample_inputfilevec.push_back(TFile::Open(tempname,"R"));
  }//end of loop over HTbins


  // Stack
  delete tempstack;
  tempstack = new THStack("stack","Binned Sample Stack");
  sprintf(tempname,"results_filelist_Spring15_stacked.root");
  file = new TFile(tempname,"RECREATE");
  histname.clear();
  histname[0]="weight";
  histname[1]="HT";
  histname[2]="MHT";
  histname[3]="NJet";
  histname[4]="NBtag";


  for(int j=0; j< Hname.size(); j++){

    for(int i=0; i<NSamples ; i++){                                                  // loop over different HT bins

      sprintf(tempname,"%s",(Hname[j]).c_str());
      tempstack2 = (THStack *) sample_inputfilevec.at(i)->Get(tempname)->Clone();
      temphist = (TH1D*)tempstack2->GetStack()->Last();

      temphist->SetFillColor(i+2);
      tempstack->Add(temphist);



    }//end of loop over HTbins 1..7
    sprintf(tempname,"%s",(Hname[j]).c_str());
    tempstack->Write(tempname);

    delete tempstack;
    tempstack = new THStack("stack","Binned Sample Stack");

  }

  for(map<int , string >::iterator itt=WJettype.begin(); itt!=WJettype.end();itt++){        // loop over different event types

    cdtoitt = file->mkdir((itt->second).c_str());
    cdtoitt->cd();

    for(map<int , string >::iterator it=cutname.begin(); it!=cutname.end();it++){   // loop over different cutnames

      cdtoit =  cdtoitt->mkdir((it->second).c_str());
      cdtoit->cd();


      for(int j=0; j<histname.size(); j++){                                        // loop over different histograms

        for(int i=0; i<NSamples ; i++){                                                  // loop over different HT bins

          //cout << "================================" << endl;
          //cout << "HT#: " <<i << ", WJettype: " << itt->second << ", cutname: " << it->second << ", hist#: " << j << endl;
          sprintf(tempname,"%s/%s/%s_%s_%s",(itt->second).c_str(),(it->second).c_str(),(histname[j]).c_str(),(it->second).c_str(),(itt->second).c_str());
          tempstack2 = (THStack *) sample_inputfilevec.at(i)->Get(tempname)->Clone();
          temphist = (TH1D*)tempstack2->GetStack()->Last();

          temphist->SetFillColor(i+2);
          tempstack->Add(temphist);

        }//end of loop over HTbins 1..7

        sprintf(tempname,"%s_%s_%s",histname[j].c_str(),(it->second).c_str(),(itt->second).c_str());
        tempstack->Write(tempname);
        delete tempstack;
        tempstack = new THStack("stack","Binned Sample Stack");

      }//end of loop over histograms

    }//end of loop over cutnames

  }//end of loop over event types

  file->Close();
  printf("All samples main histograms stacked \n ");





}
Example #6
0
mainClass(int luminosity){//constructor
//Importnat
//make sure this initialization of the 
//maps is the same as that in main.cpp
     cutname[0]="RA2nocut";
    cutname[1]="RA2Asys";
    cutname[2]="RA2Inc3Jetcut";
    cutname[3]="RA2HT500cut";
    cutname[4]="RA2MHT200cut";
    cutname[5]="RA2delphicut";
    cutname[6]="RA2noleptoncut";
    cutname[7]="noPhotoncut";
    cutname[8]="RA2Inc4Jetcut";
    cutname[9]="RA2Inc5Jetcut";
    cutname[10]="RA2Inc6Jetcut";
    cutname[11]="RA2allbutHT2500cut";
    cutname[12]="RA2allbutMHT1000cut";
    cutname[13]="RA2allcut";
    cutname[14]="RA2noleptoncutMHT1000";
    cutname[15]="RA2noleptoncutBtag2";
    cutname[16]="RA2noleptoncutBtag2MHT1000";
    cutname[17]="RA2Inc4JetcutMHT1000";
    cutname[18]="RA2Inc4JetcutBtag2";
    cutname[19]="RA2Inc4JetcutBtag2MHT1000";
    cutname[20]="RA2Inc5JetcutMHT1000";
    cutname[21]="RA2Inc5JetcutBtag2";
    cutname[22]="RA2Inc5JetcutBtag2MHT1000";
    cutname[23]="RA2Inc6JetcutMHT1000";
    cutname[24]="RA2Inc6JetcutBtag2";
    cutname[25]="RA2Inc6JetcutBtag2MHT1000";


    sigtype[0]="allEvents";
    sigtype[1]="glgl";

    BJtype[0]="allEvents";
    BJtype[1]="W";
    BJtype[2]="Wlv";
    BJtype[3]="Wjj";
    BJtype[4]="Z";
    BJtype[5]="Zll";
    BJtype[6]="Zvv";
    BJtype[7]="Zjj";
    BJtype[8]="photon";
    BJtype[9]="H";

   TTtype[0]="allEvents";
   TTtype[1]="TTbar";
   TTtype[2]="TTSingLep";
   TTtype[3]="TTdiLep";
   TTtype[4]="TThadronic";



  //KH
  histname[0]="weight";
  histname[1]="HT";
  histname[2]="MHT";
  histname[3]="NJet";
  histname[4]="NBtagLoose";
  histname[5]="NBtagTight";
  histname[6]="BtagLoose1Pt";
histname[7]="BtagLoose1Eta";
histname[8]="BtagLoose1Phi";
histname[9]="BtagLoose2Pt";
histname[10]="BtagLoose2Eta";
histname[11]="BtagLoose2Phi";
histname[12]="BtagTight1Pt";
histname[13]="BtagTight1Eta";
histname[14]="BtagTight1Phi";
histname[15]="BtagTight2Pt";
histname[16]="BtagTight2Eta";
histname[17]="BtagTight2Phi";

  ///end of initialization of the maps

yieldmap.clear();



//Signal Section//Signal Section//Signal Section//Signal Section//Signal Section//Signal Section//Signal Section//Signal Section

  //build a vector of scale factors
  //first load the cross sections into a vector
//Sig_xs_vec.push_back(0.757); /// v1
//Sig_xs_vec.push_back(1.12); // v2
//Sig_xs_vec.push_back(1.15); // v3
//Sig_xs_vec.push_back(1.14); // M(Stop,LSP)=(450,410) and also M(Stop,LSP)=(450,440)
//Sig_xs_vec.push_back(2.18); // M(Stop,LSP)=(400,390) and also M(Stop,LSP)=(400,360)
//Sig_xs_vec.push_back(4.41); // M(Stop,LSP)=(350,340) and also M(Stop,LSP)=(350,310)
//Sig_xs_vec.push_back(0.009635); //STOCv4
Sig_xs_vec.push_back(1.58); //StauC

  double Sig_numberofevents =0;//this will use GetSumOfWeights() 
  const int Sig_nHT = 1;   // Total number of HT bin samples
  const int nHist = 18; // Number of histograms in each TDirectory


  for(int i=1; i<=Sig_nHT ; i++){
//sprintf(tempname,"../Results/results_PhaseII4_Stop_CharmLSP_14TEV_140PileUp_00.root");
//sprintf(tempname,"../Results/results_PhaseII4_Stop_CharmLSPv2_14TEV_140PileUp_00.root");
//sprintf(tempname,"../Results/results_PhaseII4_Stop_CharmLSPv3_14TEV_140PileUp_00.root");
//sprintf(tempname,"../Results/results_PhaseII4_t2cc450410_14TEV_140PileUp_00.root");  
//sprintf(tempname,"../Results/results_PhaseII4_t2cc450440_14TEV_140PileUp_00.root");
//sprintf(tempname,"../Results/results_PhaseII4_t2cc400390_14TEV_140PileUp_00.root");
//sprintf(tempname,"../Results/results_PhaseII4_t2cc400360_14TEV_140PileUp_00.root");
//sprintf(tempname,"../Results/results_PhaseII4_t2cc350340_14TEV_140PileUp_00.root");
//sprintf(tempname,"../Results/results_PhaseII4_t2cc350310_14TEV_140PileUp_00.root");
//sprintf(tempname,"../Results/results_PhaseII4_Stop_CharmLSPv4_14TEV_140PileUp_00.root");
sprintf(tempname,"../Results/results_PhaseII4_StauC_14TEV_140PileUp.root");
  file = new TFile(tempname, "R");
    sprintf(tempname,"allEvents/RA2nocut/MHT_RA2nocut_allEvents");
    tempvalue = (luminosity*Sig_xs_vec[i-1])/((* (TH1D* ) file->Get(tempname)).GetEntries());
    Sig_scalevec.push_back(tempvalue);
  }//end of loop over HTbins 
  std::cout << "normalization scale factor determination done" << std::endl;
for(int i=1; i<=Sig_nHT; i++){
//sprintf(tempname,"../Results/results_PhaseII4_Stop_CharmLSP_14TEV_140PileUp_00.root");
//sprintf(tempname,"../Results/results_PhaseII4_Stop_CharmLSPv2_14TEV_140PileUp_00.root");
//sprintf(tempname,"../Results/results_PhaseII4_Stop_CharmLSPv3_14TEV_140PileUp_00.root");
//sprintf(tempname,"../Results/results_PhaseII4_t2cc450410_14TEV_140PileUp_00.root");  
//sprintf(tempname,"../Results/results_PhaseII4_t2cc450440_14TEV_140PileUp_00.root");
//sprintf(tempname,"../Results/results_PhaseII4_t2cc400390_14TEV_140PileUp_00.root");
//sprintf(tempname,"../Results/results_PhaseII4_t2cc400360_14TEV_140PileUp_00.root");
//sprintf(tempname,"../Results/results_PhaseII4_t2cc350340_14TEV_140PileUp_00.root");
//sprintf(tempname,"../Results/results_PhaseII4_t2cc350310_14TEV_140PileUp_00.root");
//sprintf(tempname,"../Results/results_PhaseII4_Stop_CharmLSPv4_14TEV_140PileUp_00.root");
sprintf(tempname,"../Results/results_PhaseII4_StauC_14TEV_140PileUp.root");
Sig_inputfilevec.push_back(TFile::Open(tempname,"R"));
}

tempstack = new THStack("stack","Binned Sample Stack");
//sprintf(tempname,"PhaseII4_Stop_CharmLSP_14TEV_140PileUp_00.root");
//sprintf(tempname,"PhaseII4_Stop_CharmLSPv2_14TEV_140PileUp_00.root");
//sprintf(tempname,"PhaseII4_Stop_CharmLSPv3_14TEV_140PileUp_00.root");
//sprintf(tempname,"PhaseII4_t2cc450410_14TEV_140PileUp_00.root");  
//sprintf(tempname,"PhaseII4_t2cc450440_14TEV_140PileUp_00.root");
//sprintf(tempname,"PhaseII4_t2cc400390_14TEV_140PileUp_00.root");
//sprintf(tempname,"PhaseII4_t2cc400360_14TEV_140PileUp_00.root");
//sprintf(tempname,"PhaseII4_t2cc350340_14TEV_140PileUp_00.root");
//sprintf(tempname,"PhaseII4_t2cc350310_14TEV_140PileUp_00.root");
//sprintf(tempname,"PhaseII4_Stop_CharmLSPv4_14TEV_140PileUp_00.root");
sprintf(tempname,"PhaseII4_StauC_14TEV_140PileUp.root");
file = new TFile(tempname,"RECREATE");
 for(map<int , string >::iterator itt=sigtype.begin(); itt!=sigtype.end();itt++){        // loop over different event types
    cdtoitt = file->mkdir((itt->second).c_str());
    cdtoitt->cd();
int c=0;
    for(map<int , string >::iterator it=cutname.begin(); it!=cutname.end();it++){   // loop over different cutnames
      cdtoit =  cdtoitt->mkdir((it->second).c_str());
      cdtoit->cd();
      for(int j=0; j<histname.size(); j++){                                        // loop over different histograms
        for(int i=0; i<Sig_nHT ; i++){                                                  // loop over different HT bins
sprintf(tempname,"%s/%s/%s_%s_%s",(itt->second).c_str(),(it->second).c_str(),(histname[j]).c_str(),(it->second).c_str(),(itt->second).c_str());
temphist = (TH1D *) Sig_inputfilevec.at(i)->Get(tempname)->Clone();
temphist->Scale(Sig_scalevec[i]);
if(histname[j]=="MHT"){
Sig_numberofevents+=(double)temphist->GetSumOfWeights();
} 
temphist->SetFillColor(i+2);
tempstack->Add(temphist);


               }//end of loop over HTbins 1..7
if(histname[j]=="MHT"){
if(itt->second=="allEvents"){
yieldmap[c].push_back(Sig_numberofevents);
}

}
Sig_numberofevents=0;
        sprintf(tempname,"%s_%s_%s",histname[j].c_str(),(it->second).c_str(),(itt->second).c_str());
        tempstack->Write(tempname);
        delete tempstack;
        tempstack = new THStack("stack","Binned Sample Stack");
      }//end of loop over histograms
 c+=1;   }//end of loop over cutnames
  }//end of loop over event types
file->Close();

//BJ Section//BJ Section//BJ Section//BJ Section//BJ Section//BJ Section//BJ Section//BJ Section//BJ Section//BJ Section//BJ Section

 //build a vector of scale factors
  //first load the cross sections into a vector
  BJ_xs_vec.push_back(34409.92339);
  BJ_xs_vec.push_back(2642.85309);
  BJ_xs_vec.push_back(294.12311);
  BJ_xs_vec.push_back(25.95000);
  BJ_xs_vec.push_back(2.42111);
  BJ_xs_vec.push_back(0.22690);
  BJ_xs_vec.push_back(0.02767);

  double BJ_numberofevents =0;
  const int bjnHT = 7;   // Total number of HT bin samples


  for(int i=1; i<=bjnHT ; i++){
    sprintf(tempname,"../Results/results_PhaseII4_BJ_14TEV_HT%d_140PileUp.root",i);
    file = new TFile(tempname, "R");
    sprintf(tempname,"allEvents/RA2nocut/MHT_RA2nocut_allEvents");
    tempvalue = (luminosity*BJ_xs_vec[i-1])/((* (TH1D* ) file->Get(tempname)).GetEntries());
    BJ_scalevec.push_back(tempvalue);
  }//end of loop over HTbins 
  std::cout << "normalization scale factor determination done" << std::endl;
for(int i=1; i<=bjnHT; i++){
sprintf(tempname,"../Results/results_PhaseII4_BJ_14TEV_HT%d_140PileUp.root",i);
BJ_inputfilevec.push_back(TFile::Open(tempname,"R"));
}

//tempstack = new THStack("stack","Binned Sample Stack");
sprintf(tempname,"PhaseII4_BJ_14TEV_140PileUp.root");
//file = new TFile(tempname,"RECREATE");
 for(map<int , string >::iterator itt=BJtype.begin(); itt!=BJtype.end();itt++){        // loop over different event types
//    cdtoitt = file->mkdir((itt->second).c_str());
//    cdtoitt->cd();
int c=0;
    for(map<int , string >::iterator it=cutname.begin(); it!=cutname.end();it++){   // loop over different cutnames
//      cdtoit =  cdtoitt->mkdir((it->second).c_str());
//      cdtoit->cd();
      for(int j=0; j<histname.size(); j++){                                        // loop over different histograms
        for(int i=0; i<bjnHT ; i++){                                                  // loop over different HT bins
//cout << "================================" << endl;
//cout << "HT#: " <<i << ", BJtype: " << itt->second << ", cutname: " << it->second << ", hist#: " << j << endl;  
sprintf(tempname,"%s/%s/%s_%s_%s",(itt->second).c_str(),(it->second).c_str(),(histname[j]).c_str(),(it->second).c_str(),(itt->second).c_str());
temphist = (TH1D *) BJ_inputfilevec.at(i)->Get(tempname)->Clone();
temphist->Scale(BJ_scalevec[i]);
if(histname[j]=="MHT"){
BJ_numberofevents+=(double)temphist->GetSumOfWeights();
}
temphist->SetFillColor(i+2);
//tempstack->Add(temphist);
               }//end of loop over HTbins 1..7
if(histname[j]=="MHT"){
if(itt->second=="Wlv" || itt->second=="Zvv"){
yieldmap[c].push_back(BJ_numberofevents);
}

}
BJ_numberofevents=0;
        sprintf(tempname,"%s_%s_%s",histname[j].c_str(),(it->second).c_str(),(itt->second).c_str());
  //      tempstack->Write(tempname);
  //      delete tempstack;
  //      tempstack = new THStack("stack","Binned Sample Stack");
      }//end of loop over histograms
c+=1;    }//end of loop over cutnames
  }//end of loop over event types
//file->Close();


//TTbar Section//TTbar Section//TTbar Section//TTbar Section//TTbar Section//TTbar Section//TTbar Section//TTbar Section

  //build a vector of scale factors
  //first load the cross sections into a vector
  TT_xs_vec.push_back(530.89358);
  TT_xs_vec.push_back(42.55351);
  TT_xs_vec.push_back(4.48209);
  TT_xs_vec.push_back(0.52795);
  TT_xs_vec.push_back(0.05449);

  double TT_numberofevents =0;
  const int ttnHT = 5;   // Total number of HT bin samples


  for(int i=1; i<=ttnHT ; i++){
    sprintf(tempname,"../Results/results_PhaseII4_TT_14TEV_HT%d_140PileUp.root",i);
    file = new TFile(tempname, "R");
    sprintf(tempname,"allEvents/RA2nocut/MHT_RA2nocut_allEvents");
    tempvalue = (luminosity*TT_xs_vec[i-1])/((* (TH1D* ) file->Get(tempname)).GetEntries());
    TT_scalevec.push_back(tempvalue);
  }//end of loop over HTbins 
  std::cout << "normalization scale factor determination done" << std::endl;
for(int i=1; i<=ttnHT; i++){
sprintf(tempname,"../Results/results_PhaseII4_TT_14TEV_HT%d_140PileUp.root",i);
TT_inputfilevec.push_back(TFile::Open(tempname,"R"));
}

//tempstack = new THStack("stack","Binned Sample Stack");
sprintf(tempname,"PhaseII4_TT_14TEV_140PileUp.root");
//file = new TFile(tempname,"RECREATE");
 for(map<int , string >::iterator itt=TTtype.begin(); itt!=TTtype.end();itt++){        // loop over different event types
//    cdtoitt = file->mkdir((itt->second).c_str());
//    cdtoitt->cd();
int c=0;
    for(map<int , string >::iterator it=cutname.begin(); it!=cutname.end();it++){   // loop over different cutnames
//      cdtoit =  cdtoitt->mkdir((it->second).c_str());
//      cdtoit->cd();
      for(int j=0; j<histname.size(); j++){                                        // loop over different histograms
        for(int i=0; i<ttnHT ; i++){                                                  // loop over different HT bins
sprintf(tempname,"%s/%s/%s_%s_%s",(itt->second).c_str(),(it->second).c_str(),(histname[j]).c_str(),(it->second).c_str(),(itt->second).c_str());
temphist = (TH1D *) TT_inputfilevec.at(i)->Get(tempname)->Clone();
temphist->Scale(TT_scalevec[i]);
if(histname[j]=="MHT"){
TT_numberofevents+=(double)temphist->GetSumOfWeights();
}
temphist->SetFillColor(i+2);
//tempstack->Add(temphist);
               }//end of loop over HTbins 1..5
if(histname[j]=="MHT"){
if(itt->second=="allEvents"){
yieldmap[c].push_back(TT_numberofevents);
}

}
TT_numberofevents=0;
        sprintf(tempname,"%s_%s_%s",histname[j].c_str(),(it->second).c_str(),(itt->second).c_str());
//        tempstack->Write(tempname);
//        delete tempstack;
//        tempstack = new THStack("stack","Binned Sample Stack");
      }//end of loop over histograms
c+=1;   }//end of loop over cutnames
  }//end of loop over event types
//file->Close();



///write the output in a file
fstream ff;
ff.open("CutFlow.txt", std::fstream::out);
ff << " Cut Name,    " << "  Signal,      " << "  Wlv,      " << "  Zvv,     " << "  TTbar,      "<< "    Total BG,   " << " % Signal/Background,   "  <<  "    Significance " << endl; 
double totalBG=0, delWlv=0, delZvv=0, delTT=0, delB=0, delBsquare=0;
for(int i=0; i<yieldmap.size(); i++){
totalBG=(double) (yieldmap[i].at(1)+yieldmap[i].at(2)+yieldmap[i].at(3));
delWlv= 0.08*yieldmap[i].at(1);///uncrtainty for Wlv is 8%
delZvv= 0.05*yieldmap[i].at(2);
delTT= 0.5*yieldmap[i].at(3);///uncrtainty for TTbar is 50%
delBsquare=pow(delWlv,2)+pow(delZvv,2)+pow(delTT,2);///delta_background = sqrt(delWlv^2+delZvv^2+delTT^2)
ff << "  " <<cutname[i]<<",     " << yieldmap[i].at(0) << ",     " << yieldmap[i].at(1) <<",     " << yieldmap[i].at(2) <<",     " <<yieldmap[i].at(3) << ",      "<< totalBG << ",      " << yieldmap[i].at(0)/totalBG*100  <<  ",       " << yieldmap[i].at(0)/sqrt(delBsquare+totalBG+yieldmap[i].at(0))  <<endl;  
}
ff.close();

/*
///write the output in a file
fstream ff;
ff.open("CutFlow.txt", std::fstream::out);
ff << " Cut Name,    " << "  Signal,      "  << endl; 
for(int i=0; i<yieldmap.size(); i++){
ff << "  " <<cutname[i]<<",     " << yieldmap[i].at(0) << endl;  
}
ff.close();
*/
}//end of the constructor
//------------------------------------------------------------------------------
// PlotHiggsRes_LP
//------------------------------------------------------------------------------
void RunMakeRazorPlots ( string signalfile, string signalLabel,  vector<string> bkgfiles,vector<string> bkgLabels, int boxOption = 0, int option = -1, string label = "", string latexlabel = "") {

  //--------------------------------------------------------------------------------------------------------------
  // Settings 
  //============================================================================================================== 
  double intLumi = 2100; //in units of pb^-1
  string Label = "";
  if (label != "") Label = "_" + label;

  vector<string> inputfiles;
  vector<string> processLabels;

  bool hasSignal = false;
  if (signalfile != "") {
    hasSignal = true;
    inputfiles.push_back(signalfile);
    processLabels.push_back(signalLabel);
  }
  assert(bkgfiles.size() == bkgLabels.size());
  for (int i=0; i < bkgfiles.size(); ++i) {
     inputfiles.push_back(bkgfiles[i]);
     processLabels.push_back(bkgLabels[i]);
  }

  //*******************************************************************************************
  //Define Histograms
  //*******************************************************************************************
  vector<TH1F*> histUnrolled; 
  float MRBinLowEdges[] = {500, 600, 700, 900, 1200, 1600, 2500, 4000}; // Multijet Bins
  float RsqBinLowEdges[] = {0.25, 0.30, 0.41, 0.52, 0.64, 1.5}; // Multijet Bins

  const int nMRBins = 7;
  const int nRsqBins = 5;

  TH1F* histMRAllBkg =  new TH1F( "MRAllBkg",";M_{R} [GeV/c^{2}];Number of Events", nMRBins, MRBinLowEdges);
  TH1F* histRsqAllBkg =  new TH1F( "RsqAllBkg", ";R^{2};Number of Events", nRsqBins, RsqBinLowEdges);
  histMRAllBkg->SetStats(false);
  histRsqAllBkg->SetStats(false);  
  histRsqAllBkg->Sumw2();
  histMRAllBkg->Sumw2();

  TH1F* histMRQCD =  new TH1F( "MRQCD",";M_{R} [GeV/c^{2}];Number of Events", nMRBins, MRBinLowEdges);
  TH1F* histRsqQCD =  new TH1F( "RsqQCD", ";R^{2};Number of Events", nRsqBins, RsqBinLowEdges);
  histMRQCD->SetStats(false);
  histRsqQCD->SetStats(false);  
  histRsqQCD->Sumw2();
  histMRQCD->Sumw2();

  TH1F* histMRData =  new TH1F( "MRData",";M_{R} [GeV/c^{2}];Number of Events", nMRBins, MRBinLowEdges);
  TH1F* histRsqData =  new TH1F( "RsqData", ";R^{2};Number of Events", nRsqBins, RsqBinLowEdges);

  vector<TH1F*> histMR;
  vector<TH1F*> histRsq; 
  vector<TH2F*> histMRRsq;

  histMRQCD->SetFillColor(kAzure+4);
  histMRAllBkg->SetFillColor(kMagenta);
  histMRQCD->SetFillStyle(1001);
  histMRAllBkg->SetFillStyle(1001);

  histMRQCD->SetLineColor(kAzure+4);
  histMRAllBkg->SetLineColor(kMagenta);

  assert (inputfiles.size() == processLabels.size());
  for (int i=0; i < inputfiles.size(); ++i) {    
    histMRRsq.push_back( new TH2F( Form("MRRsq_%s",processLabels[i].c_str()), ";M_{R} [GeV/c^{2}]; R^{2}", nMRBins, MRBinLowEdges, nRsqBins, RsqBinLowEdges));
    if (!hasSignal || i != 0) histMRRsq[i]->SetFillColor(color[i]);
    if (hasSignal && i==0) histMRRsq[i]->SetLineWidth(3);
    histMRRsq[i]->SetLineColor(color[i]);
    histMRRsq[i]->SetStats(false);
    histMRRsq[i]->Sumw2();

    histUnrolled.push_back( new TH1F( Form("Unrolled_%s",processLabels[i].c_str()), ";Bin Number ;Number of Events", nMRBins*nRsqBins, 0, nMRBins*nRsqBins));
    if (!hasSignal || i != 0) histUnrolled[i]->SetFillColor(color[i]);
    if (hasSignal && i==0) histUnrolled[i]->SetLineWidth(3);
    histUnrolled[i]->SetLineColor(color[i]);
    histUnrolled[i]->SetStats(false);     
  }
  THStack *stackUnrolled = new THStack();

  //*******************************************************************************************
  //Define Counts
  //*******************************************************************************************


  //*******************************************************************************************
  //Read files
  //*******************************************************************************************
  for (uint i=0; i < inputfiles.size(); ++i) {

    TFile* inputFile = new TFile(inputfiles[i].c_str(),"READ");
    assert(inputFile);
    TTree* tree = 0;
    tree = (TTree*)inputFile->Get("RazorInclusive");
  // if (box == 0) {
    //   tree = (TTree*)inputFile->Get("MultiJet");
    // } else if (box == 1) {
    //   tree = (TTree*)inputFile->Get("LooseLeptonMultiJet");
    // } else if (box == 2) {
    //   tree = (TTree*)inputFile->Get("MuMultiJet");
    // } else if (box == 3) {
    //   tree = (TTree*)inputFile->Get("EleMultiJet");
    // }
 
    float weight = 0;
    int box = -1;
    int nBTaggedJets = 0;
    float dPhiRazor = 0;
    float MR = 0;
    float Rsq = 0;
    float mT = 0;

    tree->SetBranchAddress("weight",&weight);
    tree->SetBranchAddress("box",&box);
    tree->SetBranchAddress("nBTaggedJets",&nBTaggedJets);
    tree->SetBranchAddress("dPhiRazor",&dPhiRazor);
    tree->SetBranchAddress("MR",&MR);
    tree->SetBranchAddress("Rsq",&Rsq);
    tree->SetBranchAddress("mT",&mT);

    cout << "Process : " << processLabels[i] << " : Total Events: " << tree->GetEntries() << "\n";
    for (int n=0;n<tree->GetEntries();n++) { 
     // for (int n=0;n<1000;n++) { 
   
      tree->GetEntry(n);
      if (n % 1000000 == 0) cout << "Processing Event " << n << "\n";       


      // if (intLumi*weight > 100) continue;

      //Box Options
      if (option == 0 ) {
	if (nBTaggedJets != 0) continue;
      }
      if (option == 1 ) {
	if (nBTaggedJets != 1) continue;
      }
      if (option == 2 ) {
	if (nBTaggedJets != 2) continue;
      }
      if (option == 3 ) {
	if (nBTaggedJets < 3) continue;
      }
      if (option == 4 ) {
	if (nBTaggedJets < 0) continue; // all b-tag categories combined
      }

      if (boxOption == 0) { // Multijet Box for Jamboree
	if( !(box == 11 || box == 12) ) continue;
      } 
      if (boxOption == 1) { // LeptonJet Box for Jamboree
	if( !(box == 3 || box == 4 || box == 6 || box == 7) ) continue;
      } 
      if (boxOption == 2) { // Multijet Box for Jamboree
	if( !(box == 14) ) continue;
      } 

      //apply baseline cuts
      if (!(MR > 400 && Rsq > 0.25)) continue;

      // if (!(MR < 500 )) continue;
      // if (!(Rsq < 0.3)) continue;

      if (!(fabs(dPhiRazor) > 2.8)) continue;

      if (!hasSignal || i>1) {
	histMRAllBkg->Fill(MR, intLumi*weight);
	histRsqAllBkg->Fill(Rsq, intLumi*weight);
	histMRRsq[i]->Fill(MR, Rsq, intLumi*weight);
      }

      if(i==1){
	if (intLumi*weight > 30) continue;
	float qcdweight = 1.56841;
	histMRQCD->Fill(MR, intLumi*weight*qcdweight);
	histRsqQCD->Fill(Rsq, intLumi*weight*qcdweight);	
  	histMRRsq[i]->Fill(MR, Rsq, intLumi*weight*qcdweight);
    }

      if (hasSignal && i==0) {
	histMRData->Fill(MR);
	histRsqData->Fill(Rsq);	
	histMRRsq[i]->Fill(MR, Rsq);
      }
    }

    inputFile->Close();
    delete inputFile;
  
  }
  
  std::cout<<"Data: "<<histMRData->Integral()<<", All Backgrounds: "<<histMRAllBkg->Integral()<<" , QCD: "<<histMRQCD->Integral()<<", Data2 "<< histMRRsq[0]->Integral() <<" QCD2 "<<histMRRsq[1]->Integral() <<std::endl;


  //*******************************************************************************************
  //Draw Plots
  //*******************************************************************************************
 // fill out the unrolled histograms 
  std::cout<<"Rsq bins: "<<nRsqBins<<" "<<nMRBins<<std::endl;
  for (uint i=0; i < histMRRsq.size(); ++i) {
    
    int binN = 0;

    for(int ii = 0; ii<nMRBins; ii++)
      for (int jj = 0; jj<nRsqBins; jj++)      
  	{      
  	  float value = (histMRRsq[i]->GetBinContent(ii+1, jj+1) > 0) ? histMRRsq[i]->GetBinContent(ii+1, jj+1) : 0. ;
	  
	  float Xrange = histMRRsq[i]->GetXaxis()->GetBinLowEdge(jj+2) - histMRRsq[i]->GetXaxis()->GetBinLowEdge(jj+1);
	  float Yrange = histMRRsq[i]->GetYaxis()->GetBinLowEdge(ii+2) - histMRRsq[i]->GetYaxis()->GetBinLowEdge(ii+1);

	  float area =1.;
	  
	  if(density) area = Xrange*Yrange; //normalize each bin by its area

	  histUnrolled[i]->SetBinContent(binN+1, value/area);
  	  binN++;
  	}

    histUnrolled[i]->SetMinimum(0.00001);

    if ( histUnrolled[i]->Integral() > 0) {
      if( !hasSignal || i > 0 )
	stackUnrolled->Add(histUnrolled[i]);
    }

    cout << "Process : " << processLabels[i] << "\n";	  
  }

  TCanvas *cv = 0;
  TLegend *legend = 0;
  TLatex *tex = 0;
  cv = new TCanvas("cv","cv", 800,600);
  legend = new TLegend(0.7,0.53,0.90,0.88);
  legend->SetTextSize(0.03);
  legend->SetBorderSize(0);
  legend->SetFillStyle(0);

  for (Int_t i = histMRRsq.size()-1 ; i >= 0; --i) {
    if (hasSignal && i==0) {
      legend->AddEntry(histMRRsq[i],processLabels[i].c_str(), "L");
    } else {
      legend->AddEntry(histMRRsq[i],processLabels[i].c_str(), "F");
    }
  }

  /// Unrolled plots in bins of R&MR
  TLatex t1(0.1,0.92, "CMS Preliminary");
  TLatex t2(0.6,0.92, "#sqrt{s}=13 TeV, L = 2.1 fb^{-1}");
  TLatex t3(0.4,0.92, Form("%s",latexlabel.c_str()) );
  t1.SetNDC();
  t2.SetNDC();
  t3.SetNDC();
  t1.SetTextSize(0.05);
  t2.SetTextSize(0.05);
  t3.SetTextSize(0.02);
  t1.SetTextFont(42);
  t2.SetTextFont(42);
  t3.SetTextFont(42);
  stackUnrolled->Draw();
  stackUnrolled->SetMinimum(0.01);
  stackUnrolled->SetMaximum(1000);
  cv->SetLogy();
  stackUnrolled->GetHistogram()->GetXaxis()->SetTitle(((TH1F*)(stackUnrolled->GetHists()->At(0)))->GetXaxis()->GetTitle());
  stackUnrolled->GetHistogram()->GetYaxis()->SetTitle(((TH1F*)(stackUnrolled->GetHists()->At(0)))->GetYaxis()->GetTitle());
  stackUnrolled->Draw();
  if(hasSignal) histUnrolled[0]->Draw("same PE");
  legend->Draw();
  t1.Draw();
  t2.Draw();
  t3.Draw();
  cv->SaveAs(Form("Unrolled_QCD%s.root",Label.c_str()));
  ////

  //*******************************************************************************************
  //MR
  //*******************************************************************************************
  cv = new TCanvas("cv","cv", 800,600);
  legend = new TLegend(0.50,0.54,0.90,0.84);
  legend->SetTextSize(0.03);
  legend->SetBorderSize(0);
  legend->SetFillStyle(0);

  tex = new TLatex();
  tex->SetNDC();
  tex->SetTextSize(0.030);
  tex->SetTextFont(42);
  tex->SetTextColor(kBlack);
  tex->DrawLatex(0.2, 0.92, Form("CMS Simulation #sqrt{s} = 13 TeV, #int L = %d fb^{-1}, %s",int(intLumi/1000), latexlabel.c_str()));

  THStack *stackMR = new THStack("stackMR", "");
  THStack *stackRsq = new THStack();

  //*******************************************************************************************
  //MR Before and After DPhi Cut
  //*******************************************************************************************
  //////////////////
  stackMR->Add(histMRAllBkg);
  stackMR->Add(histMRQCD);
  cv = new TCanvas("cv","cv", 800,600);
  legend = new TLegend(0.50,0.54,0.90,0.84);
  legend->SetTextSize(0.03);
  legend->SetBorderSize(0);
  legend->SetFillStyle(0);
  stackMR->Draw();
  stackMR->GetHistogram()->GetXaxis()->SetTitle(((TH1F*)(stackMR->GetHists()->At(0)))->GetXaxis()->GetTitle());
  stackMR->GetHistogram()->GetYaxis()->SetTitle(((TH1F*)(stackMR->GetHists()->At(0)))->GetYaxis()->GetTitle());
  stackMR->Draw("");
  histMRData->Draw("same PE");
  legend->Draw();
  cv->SetLogy();
  cv->SaveAs(Form("MRStack_QCD_%s.pdf",Label.c_str()));

  ///////////////////////
  cv = new TCanvas("cv","cv", 800,600);
  legend = new TLegend(0.50,0.54,0.90,0.84);
  legend->SetTextSize(0.03);
  legend->SetBorderSize(0);
  legend->SetFillStyle(0);

  histMRAllBkg->SetLineColor(kRed);
  histMRAllBkg->GetYaxis()->SetTitle("Number of Events");
  histMRAllBkg->GetYaxis()->SetTitleOffset(1.2);
  histMRData->SetMarkerStyle(8);

  legend->AddEntry(histMRAllBkg, "All Backgrounds", "L");
  legend->AddEntry(histMRData, "Data", "L");

  histMRAllBkg->Add(histMRQCD, 1.0);

  histMRAllBkg->Draw("hist");
  histMRData->Draw("PE same");

  legend->Draw();
  cv->SetLogy();
  cv->SaveAs(Form("MR_QCD_%s.pdf",Label.c_str()));


  //////

  cv = new TCanvas("cv","cv", 800,600);
  legend = new TLegend(0.50,0.54,0.90,0.84);
  legend->SetTextSize(0.03);
  legend->SetBorderSize(0);
  legend->SetFillStyle(0);

  histRsqAllBkg->SetLineColor(kRed);
  histRsqAllBkg->GetYaxis()->SetTitle("Number of Events");
  histRsqAllBkg->GetYaxis()->SetTitleOffset(1.2);
  histRsqData->SetMarkerStyle(8);

  legend->AddEntry(histRsqAllBkg, "All Backgrounds", "L");
  legend->AddEntry(histRsqData, "Data", "L");

  histRsqAllBkg->Add(histRsqQCD, 1.0);

  histRsqAllBkg->Draw("hist");
  histRsqData->Draw("PE same");

  legend->Draw();
  cv->SetLogy();
  cv->SaveAs(Form("Rsq_QCD_%s.pdf",Label.c_str()));

  //////////////////
  histRsqQCD->SetFillColor(kAzure+4);
  histRsqAllBkg->SetFillColor(kMagenta);

  stackRsq->Add(histRsqAllBkg);
  stackRsq->Add(histRsqQCD);
  cv = new TCanvas("cv","cv", 800,600);
  legend = new TLegend(0.50,0.54,0.90,0.84);
  legend->SetTextSize(0.03);
  legend->SetBorderSize(0);
  legend->SetFillStyle(0);
  stackRsq->Draw();
  stackRsq->GetHistogram()->GetXaxis()->SetTitle(((TH1F*)(stackRsq->GetHists()->At(0)))->GetXaxis()->GetTitle());
  stackRsq->GetHistogram()->GetYaxis()->SetTitle(((TH1F*)(stackRsq->GetHists()->At(0)))->GetYaxis()->GetTitle());
  stackRsq->Draw();
  histRsqData->Draw("same PE");
  legend->Draw();
  cv->SetLogy();
  cv->SaveAs(Form("RsqStack_QCD_%s.pdf",Label.c_str()));
 
   //--------------------------------------------------------------------------------------------------------------
  // Output
  //==============================================================================================================
  TFile *file = TFile::Open(("RazorPlots"+Label+".root").c_str(), "RECREATE");
  file->cd();

  for(int i=0; i<int(inputfiles.size()); i++) {
    file->WriteTObject(histMRRsq[i], Form("histMRRsq_%s",processLabels[i].c_str()), "WriteDelete");
    histUnrolled[i]->Write();  
  }
  
  stackUnrolled->Write();
 }
Example #8
0
mainClass(int luminosity=5000){ // luminosity is in /pb unit



  cutname[0]="nocut";cutname[1]="Njet_4";cutname[2]="ht_500" ;cutname[3]="mht_200";
  cutname[4]="delphi";cutname[5]="iso";
  cutname[6]="CSVM_0";
  cutname[7]="CSVM_1";
  cutname[8]="CSVM_2";
  cutname[9]="CSVM_3";

  WJtype[0]="EventsWith_1RecoMuon_0RecoElectron";
  TTbartype[0]="EventsWith_1RecoMuon_0RecoElectron";



// .....................................................................................................................................................//
// WJ Section
// .....................................................................................................................................................//

  //build a vector of scale factors
  //first load the cross sections into a vector
  vector<double> WJ_xs_vec;

  WJ_xs_vec.push_back(1817.0); // HT 100-200
  WJ_xs_vec.push_back(471.6);  // HT 200-400
  WJ_xs_vec.push_back(55.61);  // HT 400-600
  WJ_xs_vec.push_back(18.81);  // HT 600-Inf

  const int wjnHT = (int) WJ_xs_vec.size();   // Total number of HT bin samples

  for(int i=1; i<=wjnHT ; i++){
    if(i==1)sprintf(tempname,"../Results/results_WJ_HT-100to200_.root");
    else if(i==2)sprintf(tempname,"../Results/results_WJ_HT-200to400_.root");
    else if(i==3)sprintf(tempname,"../Results/results_WJ_HT-400to600_.root");
    else if(i==4)sprintf(tempname,"../Results/results_WJ_HT-600toInf_.root");
    else{cout << " Error!! There are only 4 WJet ht binned sample " << endl;}
    file = new TFile(tempname, "R");
    sprintf(tempname,"allEvents/nocut/MHT_nocut_allEvents");
    tempvalue = (luminosity*WJ_xs_vec[i-1])/((* (TH1D* ) file->Get(tempname)).GetEntries());

printf("Scale: %g, N: %g, Lum: %d, XS: %g \n ",tempvalue,((* (TH1D* ) file->Get(tempname)).GetEntries()),luminosity,WJ_xs_vec[i-1]);

    WJ_scalevec.push_back(tempvalue);
  }//end of loop over HTbins 
  std::cout << "WJ normalization scale factor determination done \n " << std::endl;




//..........................................//
// main histograms like HT, MHT, ...
//..........................................//

  // Load the files to a vector 
  // These are the HT, MHT, .. variables
  for(int i=1; i<=wjnHT ; i++){
    if(i==1)sprintf(tempname,"HadTauEstimation_WJ_HT-100to200_.root");
    else if(i==2)sprintf(tempname,"HadTauEstimation_WJ_HT-200to400_.root");
    else if(i==3)sprintf(tempname,"HadTauEstimation_WJ_HT-400to600_.root");
    else if(i==4)sprintf(tempname,"HadTauEstimation_WJ_HT-600toInf_.root");
    else{cout << " Error!! There are only 4 WJet ht binned sample " << endl;}
    WJ_inputfilevec.push_back(TFile::Open(tempname,"R"));
  }//end of loop over HTbins 

  // Stack
  tempstack = new THStack("stack","Binned Sample Stack");
  sprintf(tempname,"HadTauEstimation_WJ_stacked.root");
  file = new TFile(tempname,"RECREATE");
  histname.clear();
  histname[0]="weight";
  histname[1]="HT";
  histname[2]="MHT";
  histname[3]="NJet";
  histname[4]="NBtag";
  histname[5]="MuonPt";
  histname[6]="MtW";

  for(map<int , string >::iterator itt=WJtype.begin(); itt!=WJtype.end();itt++){        // loop over different event types

    cdtoitt = file->mkdir((itt->second).c_str());
    cdtoitt->cd();

    for(map<int , string >::iterator it=cutname.begin(); it!=cutname.end();it++){   // loop over different cutnames

      cdtoit =  cdtoitt->mkdir((it->second).c_str());
      cdtoit->cd();

      for(int j=0; j<histname.size(); j++){                                        // loop over different histograms

        for(int i=0; i<wjnHT ; i++){                                                  // loop over different HT bins

          //cout << "================================" << endl;
          //cout << "HT#: " <<i << ", WJtype: " << itt->second << ", cutname: " << it->second << ", hist#: " << j << endl;  
          sprintf(tempname,"%s/%s/%s_%s_%s",(itt->second).c_str(),(it->second).c_str(),(histname[j]).c_str(),(it->second).c_str(),(itt->second).c_str());
          temphist = (TH1D *) WJ_inputfilevec.at(i)->Get(tempname)->Clone();
          temphist->Scale(WJ_scalevec[i]);
          temphist->SetFillColor(i+2);
          tempstack->Add(temphist);

        }//end of loop over HTbins 1..7

        sprintf(tempname,"%s_%s_%s",histname[j].c_str(),(it->second).c_str(),(itt->second).c_str());
        tempstack->Write(tempname);

        delete tempstack;
        tempstack = new THStack("stack","Binned Sample Stack");

      }//end of loop over histograms

    }//end of loop over cutnames 

  }//end of loop over event types

  file->Close();
  printf("WJ main histograms stacked \n ");






// .....................................................................................................................................................//
// TTbar Section
// .....................................................................................................................................................//

  //build a vector of scale factors
  //first load the cross sections into a vector
  vector<double> TTbar_xs_vec;

  TTbar_xs_vec.push_back(806.1); // 

  const int ttbarnHT = (int) TTbar_xs_vec.size();   // Total number of HT bin samples

  for(int i=1; i<=ttbarnHT ; i++){
    if(i==1)sprintf(tempname,"../Results/results_TTbar_.root");
    else{cout << " Error!! There are only 1 TTbaret ht binned sample " << endl;}
    file = new TFile(tempname, "R");
    sprintf(tempname,"allEvents/nocut/MHT_nocut_allEvents");
    tempvalue = (luminosity*TTbar_xs_vec[i-1])/((* (TH1D* ) file->Get(tempname)).GetEntries());

printf("Scale: %g, N: %g, Lum: %d, XS: %g \n ",tempvalue,((* (TH1D* ) file->Get(tempname)).GetEntries()),luminosity,TTbar_xs_vec[i-1]);

    TTbar_scalevec.push_back(tempvalue);
  }//end of loop over HTbins 
  std::cout << "TTbar normalization scale factor determination done \n " << std::endl;
  



//..........................................//
// main histograms like HT, MHT, ...
//..........................................//

  // Load the files to a vector 
  // These are the HT, MHT, .. variables
  for(int i=1; i<=ttbarnHT ; i++){
    if(i==1)sprintf(tempname,"HadTauEstimation_TTbar_.root");
    else{cout << " Error!! There are only 1 TTbaret ht binned sample " << endl;}
    TTbar_inputfilevec.push_back(TFile::Open(tempname,"R"));
  }//end of loop over HTbins 

  // Stack
  tempstack = new THStack("stack","Binned Sample Stack");
  sprintf(tempname,"HadTauEstimation_TTbar_stacked.root");
  file = new TFile(tempname,"RECREATE");
  histname.clear();
  histname[0]="weight";
  histname[1]="HT";
  histname[2]="MHT";
  histname[3]="NJet";
  histname[4]="NBtag";
  histname[5]="MuonPt";
  histname[6]="MtW";

  for(map<int , string >::iterator itt=TTbartype.begin(); itt!=TTbartype.end();itt++){        // loop over different event types

    cdtoitt = file->mkdir((itt->second).c_str());
    cdtoitt->cd();

    for(map<int , string >::iterator it=cutname.begin(); it!=cutname.end();it++){   // loop over different cutnames

      cdtoit =  cdtoitt->mkdir((it->second).c_str());
      cdtoit->cd();

      for(int j=0; j<histname.size(); j++){                                        // loop over different histograms

        for(int i=0; i<ttbarnHT ; i++){                                                  // loop over different HT bins

          //cout << "================================" << endl;
          //cout << "HT#: " <<i << ", TTbartype: " << itt->second << ", cutname: " << it->second << ", hist#: " << j << endl;  
          sprintf(tempname,"%s/%s/%s_%s_%s",(itt->second).c_str(),(it->second).c_str(),(histname[j]).c_str(),(it->second).c_str(),(itt->second).c_str());
          temphist = (TH1D *) TTbar_inputfilevec.at(i)->Get(tempname)->Clone();
          temphist->Scale(TTbar_scalevec[i]);
          temphist->SetFillColor(i+2);
          tempstack->Add(temphist);

        }//end of loop over HTbins 1..7

        sprintf(tempname,"%s_%s_%s",histname[j].c_str(),(it->second).c_str(),(itt->second).c_str());
        tempstack->Write(tempname);
        delete tempstack;
        tempstack = new THStack("stack","Binned Sample Stack");

      }//end of loop over histograms

    }//end of loop over cutnames 

  }//end of loop over event types

  file->Close();
  printf("TTbar main histograms stacked \n ");



// ..................................................................................................................................................... //
// Stack main histograms from TTbar and WJet
// ..................................................................................................................................................... //

  // There are two contributors 1-TTbar and 2-WJ
  int NSamples=2;

  // A vector that contains all the samples
  vector<TFile*> sample_inputfilevec;

  THStack * tempstack2 = new THStack("stack","Binned Sample Stack");

  // Load the files to a vector 
  // These are the HT, MHT, .. variables
  for(int i=1; i<=NSamples ; i++){
    if(i==1)sprintf(tempname,"HadTauEstimation_TTbar_stacked.root");
    else if(i==2)sprintf(tempname,"HadTauEstimation_WJ_stacked.root");
    else{cout << " Error!! There are only 2 contributors! " << endl;}
    sample_inputfilevec.push_back(TFile::Open(tempname,"R"));
  }//end of loop over HTbins 

  // Stack
  delete tempstack;
  tempstack = new THStack("stack","Binned Sample Stack");
  sprintf(tempname,"HadTauEstimation_stacked.root");
  file = new TFile(tempname,"RECREATE");
  histname.clear();
  histname[0]="weight";
  histname[1]="HT";
  histname[2]="MHT";
  histname[3]="NJet";
  histname[4]="NBtag";
  histname[5]="MuonPt";
  histname[6]="MtW";

  for(map<int , string >::iterator itt=WJtype.begin(); itt!=WJtype.end();itt++){        // loop over different event types

    cdtoitt = file->mkdir((itt->second).c_str());
    cdtoitt->cd();

    for(map<int , string >::iterator it=cutname.begin(); it!=cutname.end();it++){   // loop over different cutnames

      cdtoit =  cdtoitt->mkdir((it->second).c_str());
      cdtoit->cd();


      for(int j=0; j<histname.size(); j++){                                        // loop over different histograms

        for(int i=0; i<NSamples ; i++){                                                  // loop over different HT bins

          //cout << "================================" << endl;
          //cout << "HT#: " <<i << ", WJtype: " << itt->second << ", cutname: " << it->second << ", hist#: " << j << endl;  
          sprintf(tempname,"%s/%s/%s_%s_%s",(itt->second).c_str(),(it->second).c_str(),(histname[j]).c_str(),(it->second).c_str(),(itt->second).c_str());
          tempstack2 = (THStack *) sample_inputfilevec.at(i)->Get(tempname)->Clone();
          temphist = (TH1D*)tempstack2->GetStack()->Last(); 
          
          temphist->SetFillColor(i+2);
          tempstack->Add(temphist);

        }//end of loop over HTbins 1..7

        sprintf(tempname,"%s_%s_%s",histname[j].c_str(),(it->second).c_str(),(itt->second).c_str());
        tempstack->Write(tempname);
        delete tempstack;
        tempstack = new THStack("stack","Binned Sample Stack");

      }//end of loop over histograms

    }//end of loop over cutnames 

  }//end of loop over event types

  file->Close();
  printf("All samples main histograms stacked \n ");






} // End of the constructor 
mainClass(int luminosity){//constructor
//Importnat
//make sure this initialization of the 
//maps is the same as that in main.cpp
 
    cutname[0]="nocut";
    cutname[1]="Asys";
    cutname[2]="MET200";
    cutname[3]="jetone";
    cutname[4]="jettwo";
    cutname[5]="3jet";
    cutname[6]="dphi";
    cutname[7]="nolep";
    cutname[8]="MET";
    cutname[9]="pt250";
    cutname[10]="pt300";
    cutname[11]="pt350";
    cutname[12]="pt400";
    cutname[13]="pt450";
    cutname[14]="pt500";
    cutname[15]="pt600";
    cutname[16]="pt700";
    cutname[17]="pt800";
    cutname[18]="pt900";
    cutname[19]="pt1000";
    cutname[20]="pt1100";
    cutname[21]="pt1200";
    cutname[22]="pt1300";
    cutname[23]="pt1400";
    cutname[24]="pt1500";


    type[0]="allEvents";
//    type[1]="W";
  //  type[2]="Wlv";
  //  type[3]="Wjj";
  //  type[4]="Z";
  //  type[5]="Zll";
  //  type[6]="Zvv";
  //  type[7]="Zjj";
  //  type[8]="photon";
//    type[9]="H";
   type[10]="TTbar";
   type[11]="TTSingLep";
   type[12]="TTdiLep";
   type[13]="TThadronic";



  //KH
  histname[0]="weight";
  histname[1]="METAsys";
  histname[2]="MET";
  histname[3]="NJet";
  histname[4]="j1Pt";
  histname[5]="Jet1Eta";
  histname[6]="Jet1Phi";
histname[7]="j2Pt";
histname[8]="Jet2Eta";
histname[9]="Jet2Phi";
histname[10]="j3Pt";
histname[11]="Jet3Eta";
histname[12]="Jet3Phi";
histname[13]="DelPhij1j2";
histname[14]="NLep";
histname[15]="NElec";
histname[16]="NMuon";
histname[17]="NTau";
  ///end of initialization of the maps

  //build a vector of scale factors
  //first load the cross sections into a vector
  xs_vec.push_back(530.89358);
  xs_vec.push_back(42.55351);
  xs_vec.push_back(4.48209);
  xs_vec.push_back(0.52795);
  xs_vec.push_back(0.05449);

  double numberofevents =0;
  const int ttnHT = 5;   // Total number of HT bin samples
  const int nHist = 18; // Number of histograms in each TDirectory


  for(int i=1; i<=ttnHT ; i++){
    sprintf(tempname,"../Results/results_PhaseI_TT_14TEV_HT%d_NoPileUp_00.root",i);
    file = new TFile(tempname, "R");
    sprintf(tempname,"allEvents/nocut/MET_nocut_allEvents");
    tempvalue = (luminosity*xs_vec[i-1])/((* (TH1D* ) file->Get(tempname)).GetEntries());
    scalevec.push_back(tempvalue);
  }//end of loop over HTbins 
  std::cout << "normalization scale factor determination done" << std::endl;
for(int i=1; i<=ttnHT; i++){
sprintf(tempname,"../Results/results_PhaseI_TT_14TEV_HT%d_NoPileUp_00.root",i);
inputfilevec.push_back(TFile::Open(tempname,"R"));
}

tempstack = new THStack("stack","Binned Sample Stack");
file = new TFile("stack.root","RECREATE");
 for(map<int , string >::iterator itt=type.begin(); itt!=type.end();itt++){        // loop over different event types
    cdtoitt = file->mkdir((itt->second).c_str());
    cdtoitt->cd();
    for(map<int , string >::iterator it=cutname.begin(); it!=cutname.end();it++){   // loop over different cutnames
      cdtoit =  cdtoitt->mkdir((it->second).c_str());
      cdtoit->cd();
      for(int j=0; j<histname.size(); j++){                                        // loop over different histograms
        for(int i=0; i<ttnHT ; i++){                                                  // loop over different HT bins
sprintf(tempname,"%s/%s/%s_%s_%s",(itt->second).c_str(),(it->second).c_str(),(histname[j]).c_str(),(it->second).c_str(),(itt->second).c_str());
temphist = (TH1D *) inputfilevec.at(i)->Get(tempname)->Clone();
temphist->Scale(scalevec[i]);
if(histname[j]=="MET"){numberofevents+=(double)temphist->GetSumOfWeights();} //all the histograms in one directory have the same number of events
//if(histname[j]=="MET"){cout << " temphist->GetSumOfWeights() " << temphist->GetSumOfWeights() << endl;}
/*if(i==0){
cout << "" << endl;
cout << "type: " << (itt->second).c_str() << ",  cutname: " << (it->second).c_str()<< ", histname: " << histname[j].c_str() << ", bin#: " << i << endl;
cout << "temphist->GetEntries():  " << temphist->GetEntries() << endl;
cout << "temphist->GetSumOfWeights(): " << temphist->GetSumOfWeights() << endl;
cout << " ===============================================================" << endl;
}
*/
temphist->SetFillColor(i+2);
          tempstack->Add(temphist);
               }//end of loop over HTbins 1..7
if(histname[j]=="MET"){
cout << " +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" << endl;
cout << "type: " << (itt->second).c_str() << ",  cutname: " << (it->second).c_str() << endl;
cout << "Number of events:  " << numberofevents << endl;
cout << " +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" << endl;
}
numberofevents=0;
        sprintf(tempname,"%s_%s_%s",histname[j].c_str(),(it->second).c_str(),(itt->second).c_str());
        tempstack->Write(tempname);
        delete tempstack;
        tempstack = new THStack("stack","Binned Sample Stack");
      }//end of loop over histograms
    }//end of loop over cutnames
  }//end of loop over event types
file->Close();


}//end of the constructor
Example #10
0
void stack_spectrum(std::string histoName, std::string xtitle, 
		    std::string inputFile="inputFile.txt", bool update=false,
		    int rebin=1, double xmin=-9999.0, double xmax=-9999.0)
{
  
  //-------------------------------------------------------------------------------
  // read in the MC root files
  //-------------------------------------------------------------------------------

  std::vector<MCFile> myMCFiles;

  double lumi = 1000.0*4.890;
  FILE *fTable = fopen(inputFile.data(),"r");
   
  int flag=1;   
  while (flag!=-1){
    // first reading input file
    char filename[500];
    flag=fscanf(fTable,"%s",filename);

    char tmp[1000];
    // read in x-section
    flag=fscanf(fTable,"%s",tmp);
    double cross=atof(tmp);

    // read in number of events
    flag=fscanf(fTable,"%s",tmp);
    double nevt=atof(tmp);

    double scale =lumi*cross/nevt;

    MCFile tempMCfile;
    tempMCfile.filename = filename;
    tempMCfile.scaleFactor = scale;

    if(flag!=-1)
      {
	myMCFiles.push_back(tempMCfile);
        cout << tempMCfile.filename << "\t" << tempMCfile.scaleFactor << endl;
      }

      
  }	 
	
  const unsigned int nfiles = myMCFiles.size();
   
  cout << "Reading " << nfiles << " files" << endl;

  if(nfiles==0)
    {
      cout << "There is no file for me to process!" << endl;
      return;
    }

  //-------------------------------------------------------------------------------
  // Declaring histograms to be used
  //-------------------------------------------------------------------------------

  // local histograms
  TH1D* h_all;
  THStack *hs = new THStack("hs","Stacked 1D histograms");

  // for local debugging
  TH1D* h_deno[nfiles];
  int COLORCODE[nfiles];

  COLORCODE[0] = kRed-7;
  COLORCODE[1] = kRed-10;
  COLORCODE[2] = kRed-6;
  COLORCODE[3] = kMagenta-2;
  COLORCODE[4] = kMagenta-6;
  COLORCODE[5] = kBlue-7;
  COLORCODE[6] = kBlue-9;
  for(int i=7; i<nfiles;i++)
    COLORCODE[i] = kGreen-6;

  cout << "opening " << myMCFiles[0].filename << endl;
  TFile *f1 = TFile::Open(myMCFiles[0].filename.data());

  TH1D* h_template = (TH1D*)(f1->Get(Form("%s",histoName.data())));
  h_template->Reset();

  h_all = (TH1D*)h_template->Clone(Form("%s_all",histoName.data()));

  h_all -> Reset();

  //-------------------------------------------------------------------------------
  // combine
  //-------------------------------------------------------------------------------

  

  for(int ifile=nfiles-1; ifile>=0; ifile--){

    cout << "File " << ifile << endl;
    TFile *f_temp = TFile::Open(myMCFiles[ifile].filename.data());
    cout << "opening " << myMCFiles[ifile].filename << endl;


    h_deno[ifile] = (TH1D*)(f_temp->Get(Form("%s",histoName.data())));
    h_deno[ifile] -> SetName(Form("h_deno_%d",ifile));
    h_deno[ifile] -> Rebin(rebin);
    h_deno[ifile] -> SetLineColor(COLORCODE[ifile]);
    h_deno[ifile] -> SetFillColor(COLORCODE[ifile]);
    h_deno[ifile] -> SetFillStyle(1001);
    h_deno[ifile] -> SetMarkerColor(COLORCODE[ifile]);
    h_deno[ifile] -> Sumw2();
    double weight = myMCFiles[ifile].scaleFactor;
    h_deno[ifile] -> Scale(weight);
    if(ifile==nfiles-1)
      {
	h_all    -> Rebin(rebin);
	h_all    -> Sumw2();
      }

    h_all    -> Add(h_deno[ifile]);
    hs       -> Add(h_deno[ifile]);

    // to be used with TEfficiency methods
    cout << h_deno[ifile]->GetEntries() << endl;
  } // end of loop over files

  
  TCanvas* c1 = new TCanvas("c1",inputFile.data(),0,0,500,500);
  h_all->SetMarkerSize(1);
  h_all->SetMarkerStyle(24);
  h_all->Draw("e");
  h_all->SetXTitle(xtitle.data());
  h_all->GetXaxis()->SetRangeUser(xmin,xmax);
  for(unsigned int ifile=0; ifile < nfiles; ifile++){
    h_deno[ifile]->Draw("hist,same");
  }
  
  std::string remword  =".txt";
  size_t pos  = inputFile.find(remword);

  if(pos!= std::string::npos)
    inputFile.swap(inputFile.erase(pos,remword.length()));

  std::string command = "recreate";

  if(update)command ="update";

  TCanvas* c2 = new TCanvas("c2",inputFile.data(),500,0,500,500);
  hs->Draw("hist");
  hs->GetXaxis()->SetRangeUser(xmin,xmax);
  hs->GetXaxis()->SetTitle(xtitle.data());
  hs->Draw("hist");

  

  TFile* outFile = new TFile(Form("combined_%s.root",inputFile.data()),command.data());               
  h_all->Write();
  hs->Write();
  outFile->Close();     


}
Example #11
0
void Interpolate(const TString& trigger="INEL")
{
  if (gSystem->Getenv("FWD"))
    fwd = gSystem->Getenv("FWD");
  else 
    fwd = gSystem->ExpandPathName("$ALICE_PHYSICS/PWGLF/FORWARD/analysis2");
  gROOT->SetMacroPath(Form("%s/dndeta:%s", gROOT->GetMacroPath(),fwd));

  if (!gROOT->GetClass("Drawer"))  gROOT->LoadMacro("Drawer.C+");

  TH1* h0900 = GetOne( 900, trigger);
  TH1* h2760 = GetOne(2760, trigger);
  TH1* h7000 = GetOne(7000, trigger);
  TH1* h8000 = GetOne(8000, trigger);
  Info("","900: %p 2760: %p 7000: %p 8000: %p", 
       h0900, h2760, h7000, h8000);
  Double_t e8000 = (trigger.EqualTo("INEL") ? 0.852 : 0.93);
  h8000->Scale(e8000);

  TFile* out = TFile::Open("trends.root", "RECREATE");
  THStack* sOrig = new THStack("orig", Form("pp - %s", trigger.Data()));
  sOrig->Add(h8000);
  sOrig->Add(h7000);
  sOrig->Add(h2760);
  sOrig->Add(h0900);

  TCanvas* cOrig = new TCanvas("cOrig", "Original", 1200, 1200);
  cOrig->SetTopMargin(0.01);
  cOrig->SetRightMargin(0.01);
  sOrig->Draw("nostack");
  sOrig->GetHistogram()->SetYTitle("1/#it{N} d#it{N}_{ch}/d#it{#eta}");
  sOrig->GetHistogram()->SetXTitle("#it{#eta}");
  sOrig->DrawClone("nostack");
  sOrig->Write();
  
  TLegend* l = cOrig->BuildLegend(.35, .2, .55, .6, "#sqrt{s}");
  l->SetFillColor(0);
  l->SetFillStyle(0);
  l->SetBorderSize(0);

  cOrig->Modified();
  cOrig->Update();
  cOrig->cd();
  cOrig->Write();
  Info("", "Wrote original"); 

  TCanvas* cG = new TCanvas("cG", "one", 1200, 1200);
  cG->SetTopMargin(0.01);
  cG->SetRightMargin(0.01);
  
  Info("","Creating tuple");
  TNtuple* tuple = new TNtuple("tuple", "Tuple", 
			       "eta:deta:"
			       "v0900:e0900:v2760:e2760:"
			       "v7000:e7000:v8000:e8000");
  TMultiGraph* mg = new TMultiGraph;
  Int_t n = h0900->GetNbinsX();
  Info("","Loop over bins %d", n);

  for (Int_t i = 1; i <= n; i++) {
    Info("", "Getting one bin %d,%p,%p,%p,%p,%p,%p",
	 i, h0900,h2760,h7000,h8000,mg,tuple);
    OneBin(i, h0900, h2760, h7000, h8000, mg, tuple);
  }
  mg->Draw("alp");

  cG->Modified();
  cG->Update();
  cG->cd();

  TPrincipal* p =tuple->Principal("v0900:v2760:v7000:v8000","eta<0", "npdhc");
}