Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:24:14

0001 ///////////////////////////////////////////////////////////////////////////////////
0002 ////////// Example usages:
0003 //////////  Data: 
0004 //////////    dumpPlotFromEffFile.C("efficiency-data-GsfElectronToId.root", "GsfElectronToId", false, false)
0005 //////////    dumpPlotFromEffFile.C("efficiency-data-SCToGsfElectron.root", "SuperClusterToGsfElectron", false, false)
0006 //////////    dumpPlotFromEffFile.C("efficiency-data-WP90ToHLT.root", "WP90ToHLT")
0007 //////////    dumpPlotFromEffFile.C("efficiency-data-WP85ToHLT.root", "WP85ToHLT")
0008 //////////    dumpPlotFromEffFile.C("efficiency-data-WP80ToHLT.root", "WP80ToHLT")
0009 //////////    dumpPlotFromEffFile.C("efficiency-data-CicLooseToHLT.root", "CicLooseToHLT")
0010 //////////    dumpPlotFromEffFile.C("efficiency-data-CicTightToHLT.root", "CicTightToHLT")
0011 //////////    dumpPlotFromEffFile.C("efficiency-data-CicSuperTightToHLT.root", "CicSuperTightToHLT")
0012 //////////  MC: 
0013 //////////    dumpPlotFromEffFile.C("efficiency-mc-GsfElectronToId.root", "GsfElectronToId")
0014 //////////    dumpPlotFromEffFile.C("efficiency-mc-SCToGsfElectron.root", "SuperClusterToGsfElectron")
0015 //////////    dumpPlotFromEffFile.C("efficiency-mc-WP90ToHLT.root", "WP90ToHLT")
0016 //////////    dumpPlotFromEffFile.C("efficiency-mc-WP85ToHLT.root", "WP85ToHLT")
0017 //////////    dumpPlotFromEffFile.C("efficiency-mc-WP80ToHLT.root", "WP80ToHLT")
0018 //////////    dumpPlotFromEffFile.C("efficiency-mc-CicLooseToHLT.root", "CicLooseToHLT")
0019 //////////    dumpPlotFromEffFile.C("efficiency-mc-CicTightToHLT.root", "CicTightToHLT")
0020 //////////    dumpPlotFromEffFile.C("efficiency-mc-CicSuperTightToHLT.root", "CicSuperTightToHLT")
0021 
0022 
0023 
0024 void  dumpPlotFromEffFile( char* inputFileName = "testEfficiency_data.root",
0025                char* dir="GsfElectronToId", 
0026                bool isCutAndCount=true, 
0027                bool isMCTruth = false ) {
0028 
0029   std::cout << "   ##################################################   " << std::endl;
0030   if(isCutAndCount) {
0031     std::cout << "Plotting efficiency from cut & count. No background subtraction performed !" << std::endl;
0032     std::cout << "If you want to plot MC truth efficiency, please set: isMCTruth = true." << std::endl;
0033   }
0034   else std::cout << "Plotting efficiency from simultaneous fit." << std::endl;
0035   std::cout << "   ##################################################   " << std::endl;
0036 
0037 
0038 
0039   TFile* f = new TFile(inputFileName);
0040   f->cd(dir);
0041 
0042   TKey *key;
0043   TCanvas* c;
0044   TIter next( gDirectory->GetListOfKeys() ); 
0045   while ((key = (TKey*) next())) {
0046 
0047     TObject *obj = key->ReadObj();
0048     char* name = obj->GetName();
0049 
0050     if ( !(obj->IsA()->InheritsFrom( "TDirectory" )) ) continue;
0051     if( !isMCTruth && TString(name).Contains("MCtruth_") ) continue;
0052     if( isMCTruth && !(TString(name).Contains("MCtruth_")) ) continue;
0053 
0054     std::cout << "   ==================================================   " << std::endl;
0055     TString dirName = Form("%s/cnt_eff_plots/", name);
0056     if( !isCutAndCount ) dirName = Form("%s/fit_eff_plots/", name);
0057     cout << "****************************dirName = " << dirName << endl;
0058     gDirectory->cd(dirName);
0059     char* canvasname = "probe_sc_et_probe_sc_eta_PLOT";
0060     if(isMCTruth) canvasname = "probe_sc_et_probe_sc_eta_PLOT_mcTrue_true";
0061     if(dir=="SuperClusterToGsfElectron") canvasname = "probe_et_probe_eta_PLOT";
0062     if(dir=="SuperClusterToGsfElectron" && isMCTruth) 
0063       canvasname = "probe_et_probe_eta_PLOT_mcTrue_true";
0064 
0065 
0066     c = (TCanvas*) gDirectory->Get( canvasname );
0067     if(c==0) continue; // do nothing if the canvas doesn't exist
0068     gStyle->SetPalette(1);
0069     gStyle->SetPaintTextFormat(".2f"); 
0070     TH2F* h = (TH2F*) c->FindObject(canvasname );
0071     c->Draw();
0072     c->SaveAs( TString(name)+TString(".png")); 
0073     makeTable( h, (const char*) (TString(name)+TString(".txt")) );
0074 
0075 
0076     if( !isCutAndCount ) {
0077       
0078       //now make plot of the fit distributions
0079       gDirectory->cd("../");
0080       gDirectory->ls();
0081 
0082       TKey *innerkey;
0083       TIter innernext( gDirectory->GetListOfKeys() );
0084       while ((innerkey = (TKey*) innernext())) {
0085       obj = innerkey->ReadObj();
0086       char* innername = obj->GetName();
0087       if(!(obj->IsA()->InheritsFrom( "TDirectory" )) || 
0088       !(TString(innername).Contains("_bin")) ) continue;
0089       gDirectory->cd(innername);
0090       c = (TCanvas*) gDirectory->Get("fit_canvas");
0091       c->Draw();
0092       TString plotname = TString("fit")+TString(name)+TString("_")+
0093       TString(innername)+TString(".gif");
0094       plotname.ReplaceAll("probe_sc_", "");
0095       plotname.ReplaceAll("__pdfSignalPlusBackground", "");
0096       c->SaveAs(plotname); 
0097       gDirectory->cd("../");
0098       } // end while innerkey
0099     } // end isCutAndCount
0100 
0101 
0102     //get back to the initial directory
0103     if( !isCutAndCount ) gDirectory->cd("../");
0104     else gDirectory->cd("../../");
0105 
0106       std::cout << "   ==================================================   " << std::endl;
0107   }
0108   
0109 } 
0110 
0111 
0112 
0113 
0114 
0115 // generic method to make a efficiency table
0116 void makeTable(TH2F* h, char* tablefilename)
0117 {
0118 
0119   int nX = h->GetNbinsX();
0120   int nY = h->GetNbinsY();
0121 
0122 
0123   FILE *file = fopen(tablefilename,"w+");
0124 
0125 
0126   for(int i=1; i<=nX; ++i) {
0127   
0128     Double_t pT0 = h->GetXaxis()->GetBinLowEdge(i);
0129     Double_t pT1 = h->GetXaxis()->GetBinLowEdge(i+1);
0130 
0131     for(int j=1; j<=nY; ++j) {
0132       Double_t x = h->GetBinContent(i,j);
0133       Double_t dx = h->GetBinError(i,j);
0134       Double_t eta0 = h->GetYaxis()->GetBinLowEdge(j);
0135       Double_t eta1 = h->GetYaxis()->GetBinLowEdge(j+1);
0136 
0137       fprintf( file ,"%4.1f  %4.1f   %+6.4f   %+6.4f  %6.4f   %6.4f \n", 
0138            pT0, pT1, eta0, eta1, x, dx);
0139     }
0140   }
0141 
0142   fclose(file);
0143 }