CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/src/JetMETCorrections/MCJet/bin/ResponseFitter.cc

Go to the documentation of this file.
00001 #include <iostream>
00002 #include <string.h>
00003 #include <fstream>
00004 #include <cmath>
00005 #include <TFile.h>
00006 #include <TH1F.h>
00007 #include <TMath.h>
00008 #include <TKey.h>
00009 #include <TList.h>
00010 #include "Utilities.h"
00011 
00012 using namespace std;
00013 
00014 int main(int argc, char**argv)
00015 {
00016   CommandLine c1;
00017   c1.parse(argc,argv);
00018 
00019   std::string HistoFilename           = c1.getValue<string>("HistoFilename");
00020   std::string FitterFilename          = c1.getValue<string>("FitterFilename");
00021   std::string L3ResponseTxtFilename   = c1.getValue<string>("L3ResponseTxtFilename");
00022   std::string L3CorrectionTxtFilename = c1.getValue<string>("L3CorrectionTxtFilename");
00023   std::string L3OutputROOTFilename    = c1.getValue<string>("L3OutputROOTFilename");
00024   std::string L2CorrectionTxtFilename = c1.getValue<string>("L2CorrectionTxtFilename");
00025   std::string L2OutputROOTFilename    = c1.getValue<string>("L2OutputROOTFilename");
00026   bool UseRatioForResponse       = c1.getValue<bool>("UseRatioForResponse");
00027   std::vector<double> pt_vec          = c1.getVector<double>("RefPtBoundaries");
00028   std::vector<double> eta_vec         = c1.getVector<double>("EtaBoundaries");
00029   if (!c1.check()) return 0; 
00030   c1.print();
00032   const int MAX_NETA = 83;
00033   const int MAX_NREFPTBINS = 30; 
00034   int NRefPtBins = pt_vec.size()-1;
00035   int NETA = eta_vec.size()-1; 
00036   char name[100];
00037   int j,etabin;
00038   double e,mR,eR,sR,seR,mRBarrel,eRBarrel,sRBarrel,seRBarrel,r,c;
00039   double mCaloPt,eCaloPt,sCaloPt,mRefPt,eRefPt,sRefPt;
00040   double mRefPtEtaBin,eRefPtEtaBin,sRefPtEtaBin,mCaloPtEtaBin,eCaloPtEtaBin,sCaloPtEtaBin;
00041   double EtaBoundaries[MAX_NETA],RefPtBoundaries[MAX_NREFPTBINS];
00042   std::vector<std::string> HistoNamesList; 
00043   for(j=0;j<=NRefPtBins;j++)
00044     RefPtBoundaries[j] = pt_vec[j];
00045   for(j=0;j<=NETA;j++)
00046     EtaBoundaries[j] = eta_vec[j]; 
00047   TFile *inf;//Input file containing the response histograms.
00048   TFile *outf;//Output file containing the fitter results. 
00049   TH1F *BarrelResponse;//Histogram with the barrel response in RefPt bins.
00050   TH1F *BarrelCorrection;//Histogram with the barrel correction in RefPt bins.
00051   TH1F *MeanRefPt_Barrel;//Histogram with the barrel average RefPt in RefPt bins.
00052   TH1F *MeanCaloPt_Barrel;//Histogram with the barrel average CaloPt in RefPt bins.
00053   TH1F *MeanRefPt_EtaBin[MAX_NETA];//Histograms with the average RefPt in Eta & RefPt bins. 
00054   TH1F *MeanCaloPt_EtaBin[MAX_NETA];//Histograms with the average CaloPt in Eta & RefPt bins. 
00055   TH1F *ResponseVsEta_RefPt[MAX_NREFPTBINS];//Histograms with the average response vs Eta in RefPt bins.
00056   TH1F *CorrectionVsEta_RefPt[MAX_NREFPTBINS];//Histograms with the average correction vs Eta in RefPt bins.   
00057   TH1F *h;//Auxilary histogram; 
00058   TKey *key;
00060   inf = new TFile(HistoFilename.c_str(),"r");
00061   if (inf->IsZombie()) return(0);
00062   TIter next(inf->GetListOfKeys());
00063   while ((key = (TKey*)next()))
00064     HistoNamesList.push_back(key->GetName());
00065   outf = new TFile(FitterFilename.c_str(),"RECREATE");
00066   TDirectory *dir_Response = (TDirectory*)outf->mkdir("FittedHistograms");//Directory in output file to store the fitted histograms.
00067   BarrelResponse = new TH1F("Response","Response",NRefPtBins,RefPtBoundaries);
00068   BarrelCorrection = new TH1F("Correction","Correction",NRefPtBins,RefPtBoundaries);
00069   MeanRefPt_Barrel = new TH1F("MeanRefPt","MeanRefPt",NRefPtBins,RefPtBoundaries); 
00070   MeanCaloPt_Barrel = new TH1F("MeanCaloPt","MeanCaloPt",NRefPtBins,RefPtBoundaries);
00071   if (NETA>1)//multiple eta bins: used for L2+L3 correction calculation
00072     {
00073       std::cout<<"************* Fitting Response Histograms in multiple Eta bins. ************"<<std::endl;     
00074       for(etabin=0;etabin<NETA;etabin++)
00075        { 
00076          sprintf(name,"MeanRefPt_Eta%d",etabin);
00077          MeanRefPt_EtaBin[etabin] = new TH1F(name,name,NRefPtBins,RefPtBoundaries);
00078          sprintf(name,"MeanCaloPt_Eta%d",etabin);
00079          MeanCaloPt_EtaBin[etabin] = new TH1F(name,name,NRefPtBins,RefPtBoundaries);
00080        }
00081       for (j=0; j<NRefPtBins; j++)//loop over RefPt bins
00082         {  
00083           std::cout<<"RefJetPt Bin: ["<<RefPtBoundaries[j]<<","<<RefPtBoundaries[j+1]<<"] GeV"<<std::endl;
00084           sprintf(name,"ptRef_RefPt%d_Barrel",j);
00085           if (!HistoExists(HistoNamesList,name)) return(0);
00086           h = (TH1F*)inf->Get(name);
00087           GetMEAN(h,mRefPt,eRefPt,sRefPt);
00088           sprintf(name,"ptCalo_RefPt%d_Barrel",j);
00089           if (!HistoExists(HistoNamesList,name)) return(0); 
00090           h = (TH1F*)inf->Get(name);
00091           GetMEAN(h,mCaloPt,eCaloPt,sCaloPt);
00092           sprintf(name,"Response_RefPt%d_Barrel",j);
00093           if (!HistoExists(HistoNamesList,name)) return(0);
00094           h = (TH1F*)inf->Get(name);
00095           GetMPV(name,h,dir_Response,mRBarrel,eRBarrel,sRBarrel,seRBarrel);  
00097           MeanRefPt_Barrel->SetBinContent(j+1,mRefPt);
00098           MeanRefPt_Barrel->SetBinError(j+1,eRefPt);
00100           MeanCaloPt_Barrel->SetBinContent(j+1,mCaloPt);
00101           MeanCaloPt_Barrel->SetBinError(j+1,eCaloPt);
00103           CalculateResponse(UseRatioForResponse,mRefPt,eRefPt,mRBarrel,eRBarrel,r,e);
00104           BarrelResponse->SetBinContent(j+1,r);
00105           BarrelResponse->SetBinError(j+1,e);
00107           CalculateCorrection(UseRatioForResponse,mRefPt,eRefPt,mRBarrel,eRBarrel,c,e);
00108           BarrelCorrection->SetBinContent(j+1,c);
00109           BarrelCorrection->SetBinError(j+1,e);
00111           sprintf(name,"Response_vs_Eta_RefPt%d",j);
00112           ResponseVsEta_RefPt[j] = new TH1F(name,name,NETA,EtaBoundaries);
00113           sprintf(name,"Correction_vs_Eta_RefPt%d",j);
00114           CorrectionVsEta_RefPt[j] = new TH1F(name,name,NETA,EtaBoundaries);
00115           for(etabin=0;etabin<NETA;etabin++)//loop over eta bins
00116             {
00118               sprintf(name,"Response_RefPt%d_Eta%d",j,etabin);
00119               if (!HistoExists(HistoNamesList,name)) return(0);
00120               h = (TH1F*)inf->Get(name);
00121               GetMPV(name,h,dir_Response,mR,eR,sR,seR);
00122               sprintf(name,"ptRef_RefPt%d_Eta%d",j,etabin);
00123               if (!HistoExists(HistoNamesList,name)) return(0); 
00124               h = (TH1F*)inf->Get(name);
00125               GetMEAN(h,mRefPtEtaBin,eRefPtEtaBin,sRefPtEtaBin);
00126               sprintf(name,"ptCalo_RefPt%d_Eta%d",j,etabin);
00127               if (!HistoExists(HistoNamesList,name)) return(0);
00128               h = (TH1F*)inf->Get(name);
00129               GetMEAN(h,mCaloPtEtaBin,eCaloPtEtaBin,sCaloPtEtaBin);
00131               MeanRefPt_EtaBin[etabin]->SetBinContent(j+1,mRefPtEtaBin);
00132               MeanRefPt_EtaBin[etabin]->SetBinError(j+1,eRefPtEtaBin);
00134               MeanCaloPt_EtaBin[etabin]->SetBinContent(j+1,mCaloPtEtaBin);
00135               MeanCaloPt_EtaBin[etabin]->SetBinError(j+1,eCaloPtEtaBin);
00137               CalculateResponse(UseRatioForResponse,mRefPtEtaBin,eRefPtEtaBin,mR,eR,r,e);
00138               ResponseVsEta_RefPt[j]->SetBinContent(etabin+1,r);
00139               ResponseVsEta_RefPt[j]->SetBinError(etabin+1,e);
00141               CalculateCorrection(UseRatioForResponse,mRefPtEtaBin,eRefPtEtaBin,mR,eR,c,e);
00142               CorrectionVsEta_RefPt[j]->SetBinContent(etabin+1,c);
00143               CorrectionVsEta_RefPt[j]->SetBinError(etabin+1,e);
00144             }//end of EtaBin loop 
00145         }// end of Pt loop
00146     }
00147   else//single eta bin: used for L3 correction calculation
00148     {  
00149       std::cout<<"************* Fitting Response Histograms in single eta bin. ************"<<std::endl;
00150       for (j=0; j<NRefPtBins; j++)//loop over Pt bins
00151         {  
00152           std::cout<<"RefJetPt Bin: ["<<RefPtBoundaries[j]<<","<<RefPtBoundaries[j+1]<<"] GeV"<<std::endl; 
00153           sprintf(name,"ptRef_RefPt%d",j);
00154           if (!HistoExists(HistoNamesList,name)) return(0);
00155           h = (TH1F*)inf->Get(name);
00156           GetMEAN(h,mRefPt,eRefPt,sRefPt);
00157           sprintf(name,"ptCalo_RefPt%d",j);
00158           if (!HistoExists(HistoNamesList,name)) return(0);
00159           h = (TH1F*)inf->Get(name);
00160           GetMEAN(h,mCaloPt,eCaloPt,sCaloPt);
00161           sprintf(name,"Response_RefPt%d",j);
00162           if (!HistoExists(HistoNamesList,name)) return(0);
00163           h = (TH1F*)inf->Get(name);
00164           GetMPV(name,h,dir_Response,mRBarrel,eRBarrel,sRBarrel,seRBarrel);  
00166           MeanRefPt_Barrel->SetBinContent(j+1,mRefPt);
00167           MeanRefPt_Barrel->SetBinError(j+1,eRefPt);
00169           MeanCaloPt_Barrel->SetBinContent(j+1,mCaloPt);
00170           MeanCaloPt_Barrel->SetBinError(j+1,eCaloPt);
00172           CalculateResponse(UseRatioForResponse,mRefPt,eRefPt,mRBarrel,eRBarrel,r,e);
00173           BarrelResponse->SetBinContent(j+1,r);
00174           BarrelResponse->SetBinError(j+1,e);
00176           CalculateCorrection(UseRatioForResponse,mRefPt,eRefPt,mRBarrel,eRBarrel,c,e);
00177           BarrelCorrection->SetBinContent(j+1,c);
00178           BarrelCorrection->SetBinError(j+1,e);
00179         }// end of Pt loop
00180     }
00182   outf->cd();
00183   MeanRefPt_Barrel->Write();
00184   MeanCaloPt_Barrel->Write();
00185   BarrelResponse->Write();
00186   BarrelCorrection->Write();
00187   if (NETA>1)
00188     {
00189       for(etabin=0;etabin<NETA;etabin++)
00190         {
00191           MeanRefPt_EtaBin[etabin]->Write();
00192           MeanCaloPt_EtaBin[etabin]->Write();
00193         }
00194       for(j=0;j<NRefPtBins;j++)
00195         {
00196           ResponseVsEta_RefPt[j]->Write();
00197           CorrectionVsEta_RefPt[j]->Write();
00198         }
00199     }
00200   outf->Close();  
00201 }