CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/Validation/MuonIsolation/src/MuIsoValidation.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 // MuIsoValidation.cc
00003 // Package:    Muon Isolation Validation
00004 // Class:      MuIsoValidation
00005 // 
00006 /*
00007 
00008 
00009  Description: Muon Isolation Validation class
00010 
00011  Implementation: This code will accept a data set and generate plots of
00012         various quantities relevent to the Muon Isolation module. We will 
00013         be using the IsoDeposit class, *not* the MuonIsolation struct.
00014          
00015         The sequence of events is... 
00016                 * initalize statics (which variables to plot, axis limtis, etc.)
00017                 * run over events
00018                         * for each event, run over the muons in that event
00019                                 *record IsoDeposit data
00020                 * transfer data to histograms, profile plots
00021                 * save histograms to a root file
00022                 
00023         Easy-peasy.
00024         
00025 */
00026 //
00027 // Original Author:  "C. Jess Riedel", UC Santa Barbara
00028 //         Created:  Tue Jul 17 15:58:24 CDT 2007
00029 //
00030 
00031 //Class header file
00032 #include "Validation/MuonIsolation/interface/MuIsoValidation.h"
00033 
00034 //System included files
00035 #include <memory>
00036 #include <string>
00037 #include <typeinfo>
00038 #include <utility>
00039 
00040 //Root included files
00041 #include "TH1.h"
00042 #include "TH2.h"
00043 #include "TProfile.h"
00044 
00045 //Event framework included files
00046 #include "FWCore/Framework/interface/MakerMacros.h"
00047 #include "FWCore/Framework/interface/Event.h"
00048 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00049 
00050 //Other included files
00051 #include "DataFormats/TrackReco/interface/Track.h"
00052 
00053 //Using declarations
00054 using std::vector;
00055 using std::pair;
00056 using std::string;
00057 
00058 
00059 
00060 //
00061 //-----------------Constructors---------------------
00062 //
00063 MuIsoValidation::MuIsoValidation(const edm::ParameterSet& iConfig)
00064 {
00065   
00066   //  rootfilename = iConfig.getUntrackedParameter<string>("rootfilename"); // comment out for inclusion
00067   requireCombinedMuon = iConfig.getUntrackedParameter<bool>("requireCombinedMuon");
00068   dirName = iConfig.getParameter<std::string>("directory");
00069   //  subDirName = iConfig.getParameter<std::string>("@module_label");
00070   
00071   //  dirName += subDirName;
00072   
00073   //--------Initialize tags-------
00074   Muon_Tag = iConfig.getUntrackedParameter<edm::InputTag>("Global_Muon_Label");
00075   
00076   //-------Initialize counters----------------
00077   nEvents = 0;
00078   nIncMuons = 0;   
00079   nCombinedMuons = 0;
00080   
00081   InitStatics();
00082   
00083   //Set up DAQ
00084   dbe = 0;
00085   dbe = edm::Service<DQMStore>().operator->();
00086   
00087   //------"allocate" space for the data vectors-------
00088   
00089   /*
00090     h_1D        is a 2D vector with indices [var][muon#]
00091     cd_plots    is a 2D vector with indices [var][muon#]  
00092     h_2D        is a 3D vector with indices [var][var][muon#]
00093     p_2D        is a 3D vector with indices [var][var][muon#]
00094   */
00095   //NOTE:the total number of muons and events is initially unknown, 
00096   //       so that dimension is not initialized. Hence, theMuonData
00097   //     needs no resizing.
00098   
00099   h_1D.resize    (NUM_VARS);
00100   cd_plots.resize(NUM_VARS);
00101   //  h_2D.resize(NUM_VARS, vector<MonitorElement*>     (NUM_VARS));
00102   p_2D.resize(NUM_VARS, vector<MonitorElement*>(NUM_VARS));
00103   
00104   dbe->cd();
00105 }
00106 
00107 //
00108 //----------Destructor-----------------
00109 //
00110 MuIsoValidation::~MuIsoValidation(){
00111   
00112   //Deallocate memory
00113   
00114 }
00115 
00116 //
00117 //------------Methods-----------------------
00118 //
00119 void MuIsoValidation::InitStatics(){
00120   
00121   //-----------Initialize primatives-----------
00122   S_BIN_WIDTH = 1.0;//in GeV
00123   L_BIN_WIDTH = 2.0;//in GeV
00124   LOG_BINNING_ENABLED = 1;
00125   NUM_LOG_BINS = 15;
00126   LOG_BINNING_RATIO = 1.1;//ratio by which each bin is wider than the last for log binning
00127   //i.e.  bin widths are (x), (r*x), (r^2*x), ..., (r^(nbins)*x)
00128   
00129   
00130   //-------Initialize Titles---------
00131   title_sam = "";//"[Sample b-jet events] ";
00132   title_cone = "";//" [in R=0.3 IsoDeposit Cone]";
00133   //The above two pieces of info will be printed on the title of the whole page,
00134   //not for each individual histogram
00135   title_cd = "C.D. of ";
00136   
00137   //-------"Allocate" memory for vectors
00138   main_titles.resize(NUM_VARS);
00139   axis_titles.resize(NUM_VARS);
00140   names.resize(NUM_VARS);
00141   param.resize(NUM_VARS, vector<double>(3) );
00142   isContinuous.resize(NUM_VARS);
00143   
00144   //-----Titles of the plots-----------
00145   main_titles[0 ] = "Total Tracker Momentum";
00146   main_titles[1 ] = "Total EM Cal Energy";
00147   main_titles[2 ] = "Total Had Cal Energy";
00148   main_titles[3 ] = "Total HO Cal Energy";
00149   main_titles[4 ] = "Number of Tracker Tracks";
00150   main_titles[5 ] = "Number of Jets around Muon";
00151   main_titles[6 ] = "Tracker p_{T} within veto cone";
00152   main_titles[7 ] = "EM E_{T} within veto cone";
00153   main_titles[8 ] = "Had E_{T} within veto cone";
00154   main_titles[9 ] = "HO E_{T} within veto cone";
00155   main_titles[10] = "Muon p_{T}";
00156   main_titles[11] = "Muon #eta";
00157   main_titles[12] = "Muon #phi";
00158   main_titles[13] = "Average Momentum per Track ";
00159   main_titles[14] = "Weighted Energy";
00160 
00161   //------Titles on the X or Y axis------------
00162   axis_titles[0 ] = "#Sigma p_{T}   (GeV)";
00163   axis_titles[1 ] = "#Sigma E_{T}^{EM}   (GeV)";
00164   axis_titles[2 ] = "#Sigma E_{T}^{Had}   (GeV)";
00165   axis_titles[3 ] = "#Sigma E_{T}^{HO}   (GeV)";
00166   axis_titles[4 ] = "N_{Tracks}";
00167   axis_titles[5 ] = "N_{Jets}";
00168   axis_titles[6 ] = "#Sigma p_{T,veto} (GeV)";
00169   axis_titles[7 ] = "#Sigma E_{T,veto}^{EM}   (GeV)";
00170   axis_titles[8 ] = "#Sigma E_{T,veto}^{Had}   (GeV)";
00171   axis_titles[9 ] = "#Sigma E_{T,veto}^{HO}   (GeV)";
00172   axis_titles[10] = "p_{T,#mu} (GeV)";
00173   axis_titles[11] = "#eta_{#mu}";
00174   axis_titles[12] = "#phi_{#mu}";
00175   axis_titles[13] = "#Sigma p_{T} / N_{Tracks} (GeV)";
00176   axis_titles[14] = "(1.5) X #Sigma E_{T}^{EM} + #Sigma E_{T}^{Had}";
00177 
00178   //-----------Names given for the root file----------
00179   names[0 ] = "sumPt";
00180   names[1 ] = "emEt";
00181   names[2 ] = "hadEt";
00182   names[3 ] = "hoEt";
00183   names[4 ] = "nTracks";
00184   names[5 ] = "nJets";
00185   names[6 ] = "trackerVetoPt";
00186   names[7 ] = "emVetoEt";
00187   names[8 ] = "hadVetoEt";
00188   names[9 ] = "hoVetoEt";
00189   names[10] = "muonPt";
00190   names[11] = "muonEta";
00191   names[12] = "muonPhi";
00192   names[13] = "avgPt";
00193   names[14] = "weightedEt";
00194 
00195   //----------Parameters for binning of histograms---------
00196   //param[var][0] is the number of bins
00197   //param[var][1] is the low edge of the low bin
00198   //param[var][2] is the high edge of the high bin
00199   //
00200   // maximum value------,
00201   //                    |
00202   //                    V                  
00203   param[0 ][0]= (int)( 20.0/S_BIN_WIDTH); param[0 ][1]=  0.0; param[0 ][2]= param[0 ][0]*S_BIN_WIDTH;
00204   param[1 ][0]= (int)( 20.0/S_BIN_WIDTH); param[1 ][1]=  0.0; param[1 ][2]= param[1 ][0]*S_BIN_WIDTH;
00205   param[2 ][0]= (int)( 20.0/S_BIN_WIDTH); param[2 ][1]=  0.0; param[2 ][2]= param[2 ][0]*S_BIN_WIDTH;
00206   param[3 ][0]=                       20; param[3 ][1]=  0.0; param[3 ][2]=                      2.0;
00207   param[4 ][0]=                       16; param[4 ][1]= -0.5; param[4 ][2]=         param[4 ][0]-0.5;
00208   param[5 ][0]=                        4; param[5 ][1]= -0.5; param[5 ][2]=         param[5 ][0]-0.5;
00209   param[6 ][0]= (int)( 40.0/S_BIN_WIDTH); param[6 ][1]=  0.0; param[6 ][2]= param[6 ][0]*S_BIN_WIDTH;
00210   param[7 ][0]=                       20; param[7 ][1]=  0.0; param[7 ][2]=                     10.0;
00211   param[8 ][0]= (int)( 20.0/S_BIN_WIDTH); param[8 ][1]=  0.0; param[8 ][2]= param[8 ][0]*S_BIN_WIDTH;
00212   param[9 ][0]=                       20; param[9 ][1]=  0.0; param[9 ][2]=                      5.0;
00213   param[10][0]= (int)( 40.0/S_BIN_WIDTH); param[10][1]=  0.0; param[10][2]= param[10][0]*S_BIN_WIDTH;
00214   param[11][0]=                       24; param[11][1]= -2.4; param[11][2]=                      2.4;
00215   param[12][0]=                       32; param[12][1]= -3.2; param[12][2]=                      3.2;
00216   param[13][0]= (int)( 15.0/S_BIN_WIDTH); param[13][1]=  0.0; param[13][2]= param[13][0]*S_BIN_WIDTH;
00217   param[14][0]= (int)( 20.0/S_BIN_WIDTH); param[14][1]=  0.0; param[14][2]= param[14][0]*S_BIN_WIDTH;
00218 
00219   //--------------Is the variable continuous (i.e. non-integer)?-------------
00220   //---------(Log binning will only be used for continuous variables)--------
00221   isContinuous[0 ] = 1;
00222   isContinuous[1 ] = 1;
00223   isContinuous[2 ] = 1;
00224   isContinuous[3 ] = 1;
00225   isContinuous[4 ] = 0;
00226   isContinuous[5 ] = 0;
00227   isContinuous[6 ] = 1;
00228   isContinuous[7 ] = 1;
00229   isContinuous[8 ] = 1;
00230   isContinuous[9 ] = 1;
00231   isContinuous[10] = 1;
00232   isContinuous[11] = 1;
00233   isContinuous[12] = 1;
00234   isContinuous[13] = 1;
00235   isContinuous[14] = 1;
00236 
00237 }
00238 
00239 
00240 // ------------ method called for each event  ------------
00241 void MuIsoValidation::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup){
00242   
00243   ++nEvents;
00244   edm::LogInfo("Tutorial") << "\nInvestigating event #" << nEvents<<"\n";
00245   
00246   // Get Muon Collection 
00247   edm::Handle<edm::View<reco::Muon> > muonsHandle; // 
00248   iEvent.getByLabel(Muon_Tag, muonsHandle);
00249   
00250   //Fill event entry in histogram of number of muons
00251   edm::LogInfo("Tutorial") << "Number of Muons: " << muonsHandle->size();
00252   theMuonData = muonsHandle->size();
00253   h_nMuons->Fill(theMuonData);
00254   
00255   //Fill historgams concerning muon isolation 
00256   uint iMuon=0;
00257   dbe->setCurrentFolder(dirName.c_str());
00258   for (MuonIterator muon = muonsHandle->begin(); muon != muonsHandle->end(); ++muon, ++iMuon ) {
00259     ++nIncMuons;
00260     if (requireCombinedMuon) {
00261       if (muon->combinedMuon().isNull()) continue;
00262     }
00263     ++nCombinedMuons;
00264     RecordData(muon);
00265     FillHistos();
00266   }
00267   dbe->cd();
00268   
00269 }
00270 
00271 //---------------Record data for a signle muon's data---------------------
00272 void MuIsoValidation::RecordData(MuonIterator muon){
00273   
00274   
00275   theData[0] = muon->isolationR03().sumPt;
00276   theData[1] = muon->isolationR03().emEt;
00277   theData[2] = muon->isolationR03().hadEt;
00278   theData[3] = muon->isolationR03().hoEt;
00279 
00280   theData[4] = muon->isolationR03().nTracks;
00281   theData[5] = muon->isolationR03().nJets;
00282   theData[6] = muon->isolationR03().trackerVetoPt;
00283   theData[7] = muon->isolationR03().emVetoEt;
00284   theData[8] = muon->isolationR03().hadVetoEt;
00285   theData[9] = muon->isolationR03().hoVetoEt;
00286   
00287   theData[10] = muon->pt();
00288   theData[11] = muon->eta();
00289   theData[12] = muon->phi();
00290 
00291   // make sure nTracks != 0 before filling this one
00292   if (theData[4] != 0) theData[13] = (double)theData[0] / (double)theData[4];
00293   else theData[13] = -99;
00294 
00295   theData[14] = 1.5 * theData[1] + theData[2];
00296 
00297 }
00298 
00299 // ------------ method called once each job just before starting event loop  ------------
00300 void 
00301 MuIsoValidation::beginJob()
00302 {
00303   
00304   edm::LogInfo("Tutorial") << "\n#########################################\n\n"
00305                            << "Lets get started! " 
00306                            << "\n\n#########################################\n";
00307   dbe->setCurrentFolder(dirName.c_str());
00308   InitHistos();
00309   dbe->cd();
00310   
00311 }
00312 
00313 // ------------ method called once each job just after ending the event loop  ------------
00314 void 
00315 MuIsoValidation::endJob() {
00316   
00317   // check if ME still there (and not killed by MEtoEDM for memory saving)
00318   if( dbe )
00319     {
00320       // check existence of first histo in the list
00321       if (! dbe->get(dirName+"/nMuons")) return;
00322     }
00323   else
00324     return;
00325 
00326   edm::LogInfo("Tutorial") << "\n#########################################\n\n"
00327                            << "Total Number of Events: " << nEvents
00328                            << "\nTotal Number of Muons: " << nIncMuons
00329                            << "\n\n#########################################\n"
00330                            << "\nInitializing Histograms...\n";
00331   
00332   edm::LogInfo("Tutorial") << "\nIntializing Finished.  Filling...\n";
00333   NormalizeHistos();
00334   edm::LogInfo("Tutorial") << "\nFilled.  Saving...\n";
00335   //  dbe->save(rootfilename); // comment out for incorporation
00336   edm::LogInfo("Tutorial") << "\nSaved.  Peace, homie, I'm out.\n";
00337   
00338 }
00339 
00340 void MuIsoValidation::InitHistos(){
00341   
00342   //---initialize number of muons histogram---
00343   h_nMuons = dbe->book1D("nMuons", title_sam + "Number of Muons", 20, 0., 20.);
00344   h_nMuons->setAxisTitle("Number of Muons",XAXIS);
00345   h_nMuons->setAxisTitle("Fraction of Events",YAXIS);
00346   
00347   
00348   //---Initialize 1D Histograms---
00349   for(int var = 0; var < NUM_VARS; var++){
00350     h_1D[var] = dbe->book1D(
00351                             names[var], 
00352                             title_sam + main_titles[var] + title_cone, 
00353                             (int)param[var][0], 
00354                             param[var][1], 
00355                             param[var][2]
00356                             );
00357     cd_plots[var] = dbe->book1D(
00358                                 names[var] + "_cd", 
00359                                 title_sam + title_cd + main_titles[var] + title_cone, 
00360                                 (int)param[var][0], 
00361                                 param[var][1], 
00362                                 param[var][2]
00363                                 );
00364     
00365     h_1D[var]->setAxisTitle(axis_titles[var],XAXIS);
00366     h_1D[var]->setAxisTitle("Fraction of Muons",YAXIS);
00367     GetTH1FromMonitorElement(h_1D[var])->Sumw2();
00368     
00369     cd_plots[var]->setAxisTitle(axis_titles[var],XAXIS);
00370     cd_plots[var]->setAxisTitle("Fraction of Muons",YAXIS);
00371     GetTH1FromMonitorElement(cd_plots[var])->Sumw2();
00372     
00373   }//Finish 1D
00374   
00375   //---Initialize 2D Histograms---
00376   for(int var1 = 0; var1 < NUM_VARS; var1++){
00377     for(int var2 = 0; var2 < NUM_VARS; var2++){
00378       if(var1 == var2) continue;
00379       
00380       /*      h_2D[var1][var2] = dbe->book2D(
00381               names[var1] + "_" + names[var2] + "_s",
00382               //title is in "y-var vs. x-var" format
00383               title_sam + main_titles[var2] + " <vs> " + main_titles[var1] + title_cone, 
00384               (int)param[var1][0],
00385               param[var1][1],
00386               param[var1][2],
00387               (int)param[var2][0],
00388               param[var2][1],
00389               param[var2][2]
00390               );
00391       */
00392       //Monitor elements is weird and takes y axis parameters as well
00393       //as x axis parameters for a 1D profile plot
00394       p_2D[var1][var2] = dbe->bookProfile(
00395                                           names[var1] + "_" + names[var2],
00396                                           title_sam + main_titles[var2] + " <vs> " + main_titles[var1] + title_cone,
00397                                           (int)param[var1][0],
00398                                           param[var1][1],
00399                                           param[var1][2],
00400                                           (int)param[var2][0], //documentation says this is disregarded
00401                                           param[var2][1],      //does this do anything?
00402                                           param[var2][2],      //does this do anything?
00403                                           " "                  //profile errors = spread/sqrt(num_datums)
00404                                           );
00405       
00406       if(LOG_BINNING_ENABLED && isContinuous[var1]){
00407         Double_t * bin_edges = new Double_t[NUM_LOG_BINS+1];
00408         // nbins+1 because there is one more edge than there are bins
00409         MakeLogBinsForProfile(bin_edges, param[var1][1], param[var1][2]);
00410         GetTProfileFromMonitorElement(p_2D[var1][var2])->
00411           SetBins(NUM_LOG_BINS, bin_edges);
00412         delete[] bin_edges;
00413       }
00414       /*      h_2D[var1][var2]->setAxisTitle(axis_titles[var1],XAXIS);
00415               h_2D[var1][var2]->setAxisTitle(axis_titles[var2],YAXIS);
00416               GetTH2FromMonitorElement(h_2D[var1][var2])->Sumw2();
00417       */
00418       
00419       p_2D[var1][var2]->setAxisTitle(axis_titles[var1],XAXIS);
00420       p_2D[var1][var2]->setAxisTitle(axis_titles[var2],YAXIS);
00421       //                        GetTProfileFromMonitorElement(p_2D[var1][var2])->Sumw2();
00422     }
00423   }//Finish 2D
00424   
00425   
00426   
00427   //avg pT not defined for zero tracks.
00428   //MonitorElement is inflxible and won't let me change the
00429   //number of bins!  I guess all I'm doing here is changing 
00430   //range of the x axis when it is printed, not the actual
00431   //bins that are filled
00432   p_2D[4][9]->setAxisRange(0.5,15.5,XAXIS);
00433   
00434 }
00435 
00436 void MuIsoValidation::MakeLogBinsForProfile(Double_t* bin_edges,        const double min, 
00437                                             const double max){
00438   
00439   const double &r = LOG_BINNING_RATIO;
00440   const int &nbins = NUM_LOG_BINS;
00441   
00442   const double first_bin_width = (r > 1.0) ? //so we don't divide by zero
00443     (max - min)*(1-r)/(1-pow(r,nbins)) :
00444     (max - min)/nbins;
00445   
00446   bin_edges[0] = min;
00447   bin_edges[1] = min + first_bin_width;
00448   for(int n = 2; n<nbins; ++n){
00449     bin_edges[n] = bin_edges[n-1] + (bin_edges[n-1] - bin_edges[n-2])*r;
00450   }
00451   bin_edges[nbins] = max;
00452 }
00453 
00454 void MuIsoValidation::NormalizeHistos() {
00455   for(int var=0; var<NUM_VARS; var++){   
00456     //turn cd_plots into CDF's
00457     //underflow -> bin #0.  overflow -> bin #(nbins+1)
00458     //0th bin doesn't need changed
00459     
00460     double entries = GetTH1FromMonitorElement(h_1D[var])->GetEntries();
00461     if (entries==0)continue;
00462     int n_max = int(param[var][0])+1;
00463     for(int n=1; n<=n_max; ++n){
00464       cd_plots[var]->setBinContent(n, cd_plots[var]->getBinContent(n) + cd_plots[var]->getBinContent(n-1)); //Integrate.
00465     }
00466     //----normalize------
00467     if (requireCombinedMuon) {
00468       GetTH1FromMonitorElement(h_1D[var])->Scale(1./entries);
00469       GetTH1FromMonitorElement(cd_plots[var])->Scale(1./entries);
00470     }
00471     else {
00472       GetTH1FromMonitorElement(h_1D[var])->Scale(1./entries);
00473       GetTH1FromMonitorElement(cd_plots[var])->Scale(1./entries);
00474     }
00475   }
00476 }
00477 
00478 void MuIsoValidation::FillHistos() {
00479   
00480   int overFlowBin;
00481   double overFlow = 0;
00482   
00483   //----------Fill 1D histograms---------------
00484   for(int var=0; var<NUM_VARS; var++){  
00485     h_1D[var]->Fill(theData[var]);
00486     cd_plots[var]->Fill(theData[var]);//right now, this is a regular PDF (just like h_1D)
00487     if (theData[var] > param[var][2]) {
00488       // fill the overflow bin
00489       overFlowBin = (int) param[var][0] + 1;
00490       overFlow = GetTH1FromMonitorElement(h_1D[var])->GetBinContent(overFlowBin);
00491       GetTH1FromMonitorElement(h_1D[var])->SetBinContent(overFlowBin, overFlow + 1);
00492     }
00493   }//Finish 1D
00494   
00495   //----------Fill 2D histograms---------------
00496   for(int var1=0; var1<NUM_VARS; ++var1){
00497     for(int var2=0; var2<NUM_VARS; ++var2){
00498       if(var1 == var2) continue;
00499       //change below to regular int interating!
00500       //      h_2D[var1][var2]->Fill(theData[var1], theData[var2]);
00501       p_2D[var1][var2]->Fill(theData[var1], theData[var2]);
00502     }
00503   }//Finish 2D
00504 }
00505 
00506 TH1* MuIsoValidation::GetTH1FromMonitorElement(MonitorElement* me) {
00507   return me->getTH1();
00508 }
00509 
00510 TH2* MuIsoValidation::GetTH2FromMonitorElement(MonitorElement* me) {
00511   return me->getTH2F();
00512 }
00513 
00514 TProfile* MuIsoValidation::GetTProfileFromMonitorElement(MonitorElement* me) {
00515   return me->getTProfile();
00516 }
00517 
00518 
00519 //define this as a plug-in
00520 DEFINE_FWK_MODULE(MuIsoValidation);