CMS 3D CMS Logo

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 
00052 #include "DataFormats/TrackReco/interface/Track.h"
00053 
00054 //Using declarations
00055 using std::vector;
00056 using std::pair;
00057 using std::string;
00058 
00059 
00060 
00061 //
00062 //-----------------Constructors---------------------
00063 //
00064 MuIsoValidation::MuIsoValidation(const edm::ParameterSet& iConfig)
00065 {
00066 
00067         rootfilename = iConfig.getUntrackedParameter<string>("rootfilename");
00068         requireCombinedMuon = iConfig.getUntrackedParameter<bool>("requireCombinedMuon");
00069         dirName = iConfig.getParameter<std::string>("@module_label");
00070 
00071         //--------Initialize tags-------
00072         Muon_Tag = iConfig.getUntrackedParameter<edm::InputTag>("Global_Muon_Label");
00073         tkIsoDeposit_Tag = iConfig.getUntrackedParameter<edm::InputTag>("tkIsoDeposit_Label");
00074         hcalIsoDeposit_Tag = iConfig.getUntrackedParameter<edm::InputTag>("hcalIsoDeposit_Label");
00075         ecalIsoDeposit_Tag = iConfig.getUntrackedParameter<edm::InputTag>("ecalIsoDeposit_Label");
00076         hoIsoDeposit_Tag = iConfig.getUntrackedParameter<edm::InputTag>("hoIsoDeposit_Label");
00077 
00078 
00079         //-------Initialize counters----------------
00080         nEvents = 0;
00081         nIncMuons = 0;   
00082         nCombinedMuons = 0;
00083 
00084         InitStatics();
00085 
00086         //Set up DAQ
00087         dbe = 0;
00088         dbe = edm::Service<DQMStore>().operator->();
00089 
00090         //------"allocate" space for the data vectors-------
00091         
00092         /*
00093         h_1D        is a 2D vector with indices [var][muon#]
00094         cd_plots    is a 2D vector with indices [var][muon#]  
00095         h_2D        is a 3D vector with indices [var][var][muon#]
00096         p_2D        is a 3D vector with indices [var][var][muon#]
00097         */
00098         //NOTE:the total number of muons and events is initially unknown, 
00099         //         so that dimension is not initialized. Hence, theMuonData
00100         //     needs no resizing.
00101 
00102         h_1D.resize    (NUM_VARS);
00103         cd_plots.resize(NUM_VARS);
00104         h_2D.resize(NUM_VARS, vector<MonitorElement*>     (NUM_VARS));
00105         p_2D.resize(NUM_VARS, vector<MonitorElement*>(NUM_VARS));
00106 
00107         dbe->cd();
00108 }
00109 
00110 //
00111 //----------Destructor-----------------
00112 //
00113 MuIsoValidation::~MuIsoValidation(){
00114 
00115         //Deallocate memory
00116 
00117 }
00118 
00119 //
00120 //------------Methods-----------------------
00121 //
00122 void MuIsoValidation::InitStatics(){
00123 
00124         //-----------Initialize primatives-----------
00125         S_BIN_WIDTH = 1.0;//in GeV
00126         L_BIN_WIDTH = 2.0;//in GeV
00127         LOG_BINNING_ENABLED = 1;
00128         NUM_LOG_BINS = 15;
00129         LOG_BINNING_RATIO = 1.1;//ratio by which each bin is wider than the last for log binning
00130                                                         //i.e.  bin widths are (x), (r*x), (r^2*x), ..., (r^(nbins)*x)
00131                                                         
00132 
00133         //-------Initialize Titles---------
00134         title_sam = "";//"[Sample b-jet events] ";
00135         title_cone = "";//" [in R=0.3 IsoDeposit Cone]";
00136         //The above two pieces of info will be printed on the title of the whole page,
00137         //not for each individual histogram
00138         title_cd = "Cum Dist of ";
00139 
00140         //-------"Allocate" memory for vectors
00141         main_titles.resize(NUM_VARS);
00142         axis_titles.resize(NUM_VARS);
00143         names.resize(NUM_VARS);
00144         param.resize(NUM_VARS, vector<double>(3) );
00145         isContinuous.resize(NUM_VARS);
00146 
00147         //-----Titles of the plots-----------
00148         main_titles[0 ] = "Total Tracker Momentum";
00149         main_titles[1 ] = "Total EM Cal Energy";
00150         main_titles[2 ] = "Total Had Cal Energy";
00151         main_titles[3 ] = "Total HO Cal Energy";
00152         main_titles[4 ] = "Number of Tracker Tracks";
00153         main_titles[5 ] = "Number of EM Cal Towers";
00154         main_titles[6 ] = "Number of Had Cal Towers";
00155         main_titles[7 ] = "Number of HO Cal Towers";
00156         main_titles[8 ] = "Muon Momentum";
00157         main_titles[9 ] = "Average Momentum per Track ";
00158         main_titles[10] = "Weighted Energy";
00159 
00160         //------Titles on the X or Y axis------------
00161         axis_titles[0 ] = "#Sigma p_{T}   (GeV)";
00162         axis_titles[1 ] = "#Sigma E_{T}^{EM}   (GeV)";
00163         axis_titles[2 ] = "#Sigma E_{T}^{Had}   (GeV)";
00164         axis_titles[3 ] = "#Sigma E_{T}^{HO}   (GeV)";
00165         axis_titles[4 ] = "N_{Tracks}";
00166         axis_titles[5 ] = "N_{EM Towers}";
00167         axis_titles[6 ] = "N_{Had Towers}";
00168         axis_titles[7 ] = "N_{HO Towers}";
00169         axis_titles[8 ] = "p_{T}^{#mu}";
00170         axis_titles[9 ] = "#Sigma p_{T} / N_{Tracks} (GeV)";
00171         axis_titles[10] = "(1.5) X #Sigma E_{T}^{EM} + #Sigma E_{T}^{Had}";
00172 
00173         //-----------Names given for the root file----------
00174         names[0 ] = "sumPt";
00175         names[1 ] = "emEt";
00176         names[2 ] = "hadEt";
00177         names[3 ] = "hoEt";
00178         names[4 ] = "nTracks";
00179         names[5 ] = "nEMtowers";
00180         names[6 ] = "nHADtowers";
00181         names[7 ] = "nHOtowers";
00182         names[8 ] = "muonPt";
00183         names[9 ] = "avgPt";
00184         names[10] = "weightedEt";
00185         
00186         //----------Parameters for binning of histograms---------
00187         //param[var][0] is the number of bins
00188         //param[var][1] is the low edge of the low bin
00189         //param[var][2] is the high edge of the high bin
00190         //
00191         // maximum value------,
00192         //                    |
00193         //                    V                  
00194         param[0 ][0]= (int)( 70.0/L_BIN_WIDTH); param[0 ][1]=  0.0; param[0 ][2]= param[0 ][0]*L_BIN_WIDTH;
00195         param[1 ][0]= (int)( 50.0/L_BIN_WIDTH); param[1 ][1]=  0.0; param[1 ][2]= param[1 ][0]*L_BIN_WIDTH;
00196         param[2 ][0]= (int)( 40.0/L_BIN_WIDTH); param[2 ][1]=  0.0; param[2 ][2]= param[2 ][0]*L_BIN_WIDTH;
00197         param[3 ][0]= (int)( 10.0/S_BIN_WIDTH); param[3 ][1]=  0.0; param[3 ][2]= param[3 ][0]*S_BIN_WIDTH;
00198         param[4 ][0]=                                           16; param[4 ][1]= -0.5; param[4 ][2]= param[4 ][0]-0.5;
00199         param[5 ][0]=                                           17; param[5 ][1]= -0.5; param[5 ][2]= param[5 ][0]-0.5;
00200         param[6 ][0]=                                           10; param[6 ][1]= -0.5; param[6 ][2]= param[6 ][0]-0.5;
00201         param[7 ][0]=                                           16; param[7 ][1]= -0.5; param[7 ][2]= param[7 ][0]-0.5;
00202         param[8 ][0]= (int)( 40.0/S_BIN_WIDTH); param[8 ][1]=  0.0; param[8 ][2]= param[8 ][0]*S_BIN_WIDTH;
00203         param[9 ][0]= (int)( 15.0/S_BIN_WIDTH); param[9 ][1]=  0.0; param[9 ][2]= param[9 ][0]*S_BIN_WIDTH;
00204         param[10][0]= (int)(140.0/L_BIN_WIDTH); param[10][1]=  0.0; param[10][2]= param[10][0]*L_BIN_WIDTH;
00205         
00206         //--------------Is the variable continuous (i.e. non-integer)?-------------
00207         //---------(Log binning will only be used for continuous variables)--------
00208         isContinuous[0 ] = 1;
00209         isContinuous[1 ] = 1;
00210         isContinuous[2 ] = 1;
00211         isContinuous[3 ] = 1;
00212         isContinuous[4 ] = 0;
00213         isContinuous[5 ] = 0;
00214         isContinuous[6 ] = 0;
00215         isContinuous[7 ] = 0;
00216         isContinuous[8 ] = 1;
00217         isContinuous[9 ] = 1;
00218         isContinuous[10] = 1;
00219         
00220 }
00221    
00222 
00223 // ------------ method called for each event  ------------
00224 void MuIsoValidation::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup){
00225 
00226         ++nEvents;
00227         edm::LogInfo("Tutorial") << "\nInvestigating event #" << nEvents<<"\n";
00228 
00229         // Get Muon Collection 
00230         edm::Handle<reco::MuonCollection> muonsHandle; //this is an instance of std:vector<muon> . It has methods begin(), end(), size(), etc.
00231         iEvent.getByLabel(Muon_Tag, muonsHandle);
00232 
00233         // Get IsoDeposit Collection 
00234         MuIsoDepHandle tkIsoHandle;
00235         MuIsoDepHandle ecalIsoHandle;
00236         MuIsoDepHandle hcalIsoHandle;
00237         MuIsoDepHandle hoIsoHandle;
00238         iEvent.getByLabel(tkIsoDeposit_Tag, tkIsoHandle);
00239         iEvent.getByLabel(ecalIsoDeposit_Tag, ecalIsoHandle);
00240         iEvent.getByLabel(hcalIsoDeposit_Tag, hcalIsoHandle);
00241         iEvent.getByLabel(hoIsoDeposit_Tag, hoIsoHandle);
00242    
00243     //Fill event entry in histogram of number of muons
00244         edm::LogInfo("Tutorial") << "Number of Muons: " << muonsHandle->size();
00245         theMuonData = muonsHandle->size();
00246         h_nMuons->Fill(theMuonData);
00247 
00248         //Fill historgams concerning muon isolation 
00249         uint iMuon=0;
00250         dbe->setCurrentFolder(dirName.c_str());
00251         for (MuonIterator muon = muonsHandle->begin(); muon != muonsHandle->end(); ++muon, ++iMuon ) {
00252           ++nIncMuons;
00253           if (requireCombinedMuon) {
00254             if (muon->combinedMuon().isNull()) continue;
00255           }
00256           ++nCombinedMuons;
00257           reco::MuonRef muRef(muonsHandle,iMuon);
00258           MuIsoDepRef& tkDep  = ( *tkIsoHandle)[muRef];
00259           MuIsoDepRef& ecalDep = (*ecalIsoHandle)[muRef];
00260           MuIsoDepRef& hcalDep = (*hcalIsoHandle)[muRef];
00261           MuIsoDepRef& hoDep   = (  *hoIsoHandle)[muRef];
00262           
00263           RecordData(muon,tkDep,ecalDep,hcalDep,hoDep);
00264           FillHistos();
00265         }
00266         dbe->cd();
00267 
00268 }
00269 
00270 //---------------Record data for a signle muon's data---------------------
00271 void MuIsoValidation::RecordData(MuonIterator muon, 
00272                                  MuIsoDepRef& ctfDep, MuIsoDepRef& ecalDep, 
00273                                  MuIsoDepRef& hcalDep, MuIsoDepRef& hoDep){
00274   
00275   
00276   theData[0] = ctfDep.depositWithin(0.3);
00277   theData[1] = ecalDep.depositWithin(0.3);
00278   theData[2] = hcalDep.depositWithin(0.3);
00279   theData[3] = hoDep.depositWithin(0.3);
00280   
00281   theData[4] = ctfDep.depositAndCountWithin(0.3).second;
00282   theData[5] = ecalDep.depositAndCountWithin(0.3).second;
00283   theData[6] = hcalDep.depositAndCountWithin(0.3).second;
00284   theData[7] = hoDep.depositAndCountWithin(0.3).second;
00285   
00286   theData[8] = muon->pt();
00287   // make sure nTracks != 0 before filling this one
00288   if (theData[4] != 0) theData[9] = (double)theData[0] / (double)theData[4];
00289   else theData[9] = -99;
00290 
00291   theData[10] = 1.5 * theData[1] + theData[2];
00292 
00293 }
00294 
00295 // ------------ method called once each job just before starting event loop  ------------
00296 void 
00297 MuIsoValidation::beginJob(const edm::EventSetup&)
00298 {
00299         edm::LogInfo("Tutorial") << "\n#########################################\n\n"
00300                 << "Lets get started! " 
00301                 << "\n\n#########################################\n";
00302         dbe->setCurrentFolder(dirName.c_str());
00303         InitHistos();
00304         dbe->cd();
00305 
00306 }
00307 
00308 // ------------ method called once each job just after ending the event loop  ------------
00309 void 
00310 MuIsoValidation::endJob() {
00311 
00312         edm::LogInfo("Tutorial") << "\n#########################################\n\n"
00313                 << "Total Number of Events: " << nEvents
00314                 << "\nTotal Number of Muons: " << nIncMuons
00315                 << "\n\n#########################################\n"
00316                 << "\nInitializing Histograms...\n";
00317 
00318         edm::LogInfo("Tutorial") << "\nIntializing Finished.  Filling...\n";
00319         NormalizeHistos();
00320         edm::LogInfo("Tutorial") << "\nFilled.  Saving...\n";
00321         dbe->save(rootfilename);
00322         edm::LogInfo("Tutorial") << "\nSaved.  Peace, homie, I'm out.\n";
00323 
00324 }
00325 
00326 void MuIsoValidation::InitHistos(){
00327 
00328         //---initialize number of muons histogram---
00329         h_nMuons = dbe->book1D("nMuons", title_sam + "Number of Muons", 20, 0., 20.);
00330         h_nMuons->setAxisTitle("Number of Muons",XAXIS);
00331         h_nMuons->setAxisTitle("Fraction of Events",YAXIS);
00332 
00333 
00334         //---Initialize 1D Histograms---
00335         for(int var = 0; var < NUM_VARS; var++){
00336                 h_1D[var] = dbe->book1D(
00337                         names[var], 
00338                         title_sam + main_titles[var] + title_cone, 
00339                         (int)param[var][0], 
00340                         param[var][1], 
00341                         param[var][2]
00342                 );
00343                 cd_plots[var] = dbe->book1D(
00344                         names[var] + "_cd", 
00345                         title_sam + title_cd + main_titles[var] + title_cone, 
00346                         (int)param[var][0], 
00347                         param[var][1], 
00348                         param[var][2]
00349                 );
00350                 
00351                 h_1D[var]->setAxisTitle(axis_titles[var],XAXIS);
00352                 h_1D[var]->setAxisTitle("Fraction of Muons",YAXIS);
00353                 GetTH1FromMonitorElement(h_1D[var])->Sumw2();
00354 
00355                 cd_plots[var]->setAxisTitle(axis_titles[var],XAXIS);
00356                 cd_plots[var]->setAxisTitle("Fraction of Muons",YAXIS);
00357                 GetTH1FromMonitorElement(cd_plots[var])->Sumw2();
00358         
00359         }//Finish 1D
00360 
00361         //---Initialize 2D Histograms---
00362         for(int var1 = 0; var1 < NUM_VARS; var1++){
00363                 for(int var2 = 0; var2 < NUM_VARS; var2++){
00364                         if(var1 == var2) continue;
00365 
00366                         h_2D[var1][var2] = dbe->book2D(
00367                                 names[var1] + "_" + names[var2] + "_s",
00368                                 //title is in "y-var vs. x-var" format
00369                                 title_sam + main_titles[var2] + " <vs> " + main_titles[var1] + title_cone, 
00370                                 (int)param[var1][0],
00371                                 param[var1][1],
00372                                 param[var1][2],
00373                                 (int)param[var2][0],
00374                                 param[var2][1],
00375                                 param[var2][2]
00376                         );
00377                         
00378                         //Monitor elements is weird and takes y axis parameters as well
00379                         //as x axis parameters for a 1D profile plot
00380                         p_2D[var1][var2] = dbe->bookProfile(
00381                                 names[var1] + "_" + names[var2],
00382                                 title_sam + main_titles[var2] + " <vs> " + main_titles[var1] + title_cone,
00383                                 (int)param[var1][0],
00384                                 param[var1][1],
00385                                 param[var1][2],
00386                                 (int)param[var2][0], //documentation says this is disregarded
00387                                 param[var2][1],      //does this do anything?
00388                                 param[var2][2],      //does this do anything?
00389                                 " "                  //profile errors = spread/sqrt(num_datums)
00390                         );
00391                         
00392                         if(LOG_BINNING_ENABLED && isContinuous[var1]){
00393                                 Double_t * bin_edges = new Double_t[NUM_LOG_BINS+1];
00394                                 // nbins+1 because there is one more edge than there are bins
00395                                 MakeLogBinsForProfile(bin_edges, param[var1][1], param[var1][2]);
00396                                 GetTProfileFromMonitorElement(p_2D[var1][var2])->
00397                                         SetBins(NUM_LOG_BINS, bin_edges);
00398                                 delete[] bin_edges;
00399                         }
00400                         h_2D[var1][var2]->setAxisTitle(axis_titles[var1],XAXIS);
00401                         h_2D[var1][var2]->setAxisTitle(axis_titles[var2],YAXIS);
00402                         GetTH2FromMonitorElement(h_2D[var1][var2])->Sumw2();
00403 
00404                         p_2D[var1][var2]->setAxisTitle(axis_titles[var1],XAXIS);
00405                         p_2D[var1][var2]->setAxisTitle(axis_titles[var2],YAXIS);
00406                         //                      GetTProfileFromMonitorElement(p_2D[var1][var2])->Sumw2();
00407                 }
00408         }//Finish 2D
00409 
00410 
00411 
00412         //avg pT not defined for zero tracks.
00413         //MonitorElement is inflxible and won't let me change the
00414         //number of bins!  I guess all I'm doing here is changing 
00415         //range of the x axis when it is printed, not the actual
00416         //bins that are filled
00417         p_2D[4][9]->setAxisRange(0.5,15.5,XAXIS);
00418    
00419 }
00420 
00421 void MuIsoValidation::MakeLogBinsForProfile(Double_t* bin_edges,        const double min, 
00422         const double max){
00423         
00424         const double &r = LOG_BINNING_RATIO;
00425         const int &nbins = NUM_LOG_BINS;
00426         
00427         const double first_bin_width = (r > 1.0) ? //so we don't divide by zero
00428                 (max - min)*(1-r)/(1-pow(r,nbins)) :
00429                 (max - min)/nbins;
00430         
00431         bin_edges[0] = min;
00432         bin_edges[1] = min + first_bin_width;
00433         for(int n = 2; n<nbins; ++n){
00434                 bin_edges[n] = bin_edges[n-1] + (bin_edges[n-1] - bin_edges[n-2])*r;
00435         }
00436         bin_edges[nbins] = max;
00437 }
00438 
00439 void MuIsoValidation::NormalizeHistos() {
00440   for(int var=0; var<NUM_VARS; var++){   
00441     //turn cd_plots into CDF's
00442     //underflow -> bin #0.  overflow -> bin #(nbins+1)
00443     //0th bin doesn't need changed
00444     int n_max = int(param[var][0])+1;
00445     for(int n=1; n<=n_max; ++n){
00446       cd_plots[var]->setBinContent(n, cd_plots[var]->getBinContent(n) + cd_plots[var]->getBinContent(n-1)); //Integrate.
00447     }
00448     //----normalize------
00449     if (requireCombinedMuon) {
00450       GetTH1FromMonitorElement(h_1D[var])->Scale(1./nCombinedMuons);
00451       GetTH1FromMonitorElement(cd_plots[var])->Scale(1./nCombinedMuons);
00452     }
00453     else {
00454       GetTH1FromMonitorElement(h_1D[var])->Scale(1./nIncMuons);
00455       GetTH1FromMonitorElement(cd_plots[var])->Scale(1./nIncMuons);
00456     }
00457   }
00458 }
00459 
00460 void MuIsoValidation::FillHistos() {
00461   
00462   //----------Fill 1D histograms---------------
00463   for(int var=0; var<NUM_VARS; var++){  
00464     h_1D[var]->Fill(theData[var]);
00465     cd_plots[var]->Fill(theData[var]);//right now, this is a regular PDF (just like h_1D)
00466   }//Finish 1D
00467   
00468   
00469   //----------Fill 2D histograms---------------
00470   for(int var1=0; var1<NUM_VARS; ++var1){
00471     for(int var2=0; var2<NUM_VARS; ++var2){
00472       if(var1 == var2) continue;
00473       //change below to regular int interating!
00474       h_2D[var1][var2]->Fill(theData[var1], theData[var2]);
00475       p_2D[var1][var2]->Fill(theData[var1], theData[var2]);
00476     }
00477   }//Finish 2D
00478 }
00479 
00480 TH1* MuIsoValidation::GetTH1FromMonitorElement(MonitorElement* me) {
00481   return me->getTH1();
00482 }
00483 
00484 TH2* MuIsoValidation::GetTH2FromMonitorElement(MonitorElement* me) {
00485   return me->getTH2F();
00486 }
00487 
00488 TProfile* MuIsoValidation::GetTProfileFromMonitorElement(MonitorElement* me) {
00489   return me->getTProfile();
00490 }
00491 
00492 
00493 //define this as a plug-in
00494 DEFINE_FWK_MODULE(MuIsoValidation);

Generated on Tue Jun 9 17:49:25 2009 for CMSSW by  doxygen 1.5.4