• Main Page
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

/Users/jmonk/Physics/ForIA/src/AnalysisTools/JESUncertaintyProvider.cxx

Go to the documentation of this file.
00001 #include "ForIA/AnalysisTools/JESUncertaintyProvider.hh"
00002 
00003 // Constructor
00004 JESUncertaintyProvider::JESUncertaintyProvider(std::string CollectionName, std::string FileName):
00005   m_GeV(1000.0), m_collectionName(CollectionName), m_fileName(FileName), m_isInit(false)
00006 {
00007 
00008   m_inputFile = 0;
00009   m_uncGraph.clear();
00010   m_pileupUncGraph.clear();
00011 
00012 }
00013 
00014 
00015 // Destructor
00016 JESUncertaintyProvider::~JESUncertaintyProvider()
00017 {
00018 
00019   // delete the histograms (we own them now)
00020   map<int, TH2D* >::iterator coll = m_uncGraph.begin();
00021   for(;coll!=m_uncGraph.end();coll++) { 
00022     if(coll->first==6) continue; //last unc pointer is a to pileup histo
00023     delete coll->second;
00024   }
00025   coll = m_pileupUncGraph.begin(); 
00026   for(;coll!=m_pileupUncGraph.end();coll++) {
00027     delete coll->second;
00028   }
00029 
00030 }
00031 
00032 
00033 void JESUncertaintyProvider::init() 
00034 {
00035 
00036   //prevent multiple initializations
00037   if (m_isInit == false) {
00038 
00039     m_inputFile = openInputFile(m_fileName);
00040 
00041     if(!m_inputFile)
00042     {
00043       Error( "JESUncertaintyProvider::init()", ("ERROR: Input File "+m_fileName+" could not be found").c_str());
00044     } else {
00045       //the flag will be set as initialized if everything goes right
00046       m_isInit = setInputCollection(m_collectionName);
00047 
00048       m_inputFile->Close();
00049       //is someone trying to delete this afterwards?
00050       //delete m_inputFile;
00051     }
00052 
00053   }
00054 
00055 }
00056 
00057 // Open the file 
00058 TFile* JESUncertaintyProvider::openInputFile(std::string FileName) 
00059 {
00060   // Open Input File
00061   // The ROOT file containing the uncertainty plots must be placed in the current directory for the standalone version
00062   return new TFile(FileName.c_str()); //this is to avoid the file pointer to be invalid when it gets out of the function, TFile::Open wouldn't work - root magic... 
00063 }
00064 
00065 
00066 // Read the plots from the chosen Jet Collection
00067 bool JESUncertaintyProvider::setInputCollection(std::string CollectionName)
00068 {
00069   // Jet Collection used
00070   std::string suffixName;
00071 
00072   if(CollectionName == "AntiKt6H1TopoJets" || CollectionName == "AntiKt6EMJESTopoJets" || CollectionName == "AntiKt6TopoJetsEM" )
00073     suffixName = "_AntiKt6Topo_EMJES";
00074   else if(CollectionName == "AntiKt4H1TopoJets" || CollectionName == "AntiKt4EMJESTopoJets" || CollectionName == "AntiKt4TopoJetsEM") 
00075     suffixName = "_AntiKt4Topo_EMJES";
00076   else
00077   {
00078     Warning("JESUncertaintyProvider::setInputCollection()", "ERROR: Name not recognized, using default AntiKt6EMJESTopoJets");
00079     suffixName = "_AntiKt6Topo_EMJES";
00080   }
00081 
00082   //  std::string plotNames[m_nUncertainties] = {"DeadMaterial","PhysicsList","NoiseThresholds","Beamspot","PerugiaTune","ProfessorTune","AlpgenHerwigJimmy","TBEMScale","EtaIntercalibration", "MCIntercalibration", "PileUp", "NonClosure"};
00083   std::string plotNames[m_nUncertainties] = {"Calorimeter","NoiseThresholds","Perugia2010", "AlpgenJimmy","EtaIntercalibration", "NonClosure", "Pileup"};
00084   // Pull the correct uncertainty graphs
00085   for(unsigned int i=0; i<m_nUncertainties; i++)
00086   {
00087 
00088     std::string currentPlot = "";
00089 
00090     // Pileup plot (=6) is handled separately
00091     if (i!=6)  
00092     {
00093       // Combine names to get the right plots - CINT compatibility issue
00094       currentPlot = plotNames[i]+suffixName;
00095       m_inputFile->GetObject(currentPlot.c_str(),m_uncGraph[i]);
00096       m_uncGraph[i]->SetName(TString("the").Append(currentPlot.c_str()));
00097       m_uncGraph[i]->SetDirectory(0);
00098     }
00099 
00100     else 
00101     {
00102       // Default pile-up plot: no additional vertices (=empty)
00103       currentPlot = "Pileup"+suffixName+"_NPV_1";
00104       m_inputFile->GetObject(currentPlot.c_str(),m_uncGraph[i]);
00105       m_uncGraph[i]->SetName(TString("the").Append(currentPlot.c_str()));
00106       m_uncGraph[i]->SetDirectory(0);
00107     }
00108 
00109     if(!m_uncGraph[i]) 
00110     {
00111       Error("JESUncertaintyProvider::SetInputCollection()",("ERROR: Problem finding Required Input Graph: "+currentPlot).c_str());
00112       return false;
00113     }
00114 
00115   }
00116 
00117   // Pull the correct vertex-dependent pileup graph
00118   for (unsigned int i=0; i<m_nVertices; i++) 
00119   {
00120 
00121     // Turn the index into a string for the number of vertices 
00122     std::ostringstream osstream;
00123     osstream << i+1;
00124     std::string string_i = osstream.str();
00125 
00126     // Combine names to get the right plots - CINT compatibility issue
00127     std::string currentPlot = "Pileup"+suffixName+"_NPV_"+string_i;
00128 
00129     m_inputFile->GetObject(currentPlot.c_str(),m_pileupUncGraph[i]);
00130     m_pileupUncGraph[i]->SetName(TString("the").Append(currentPlot.c_str()));
00131     m_pileupUncGraph[i]->SetDirectory(0);
00132 
00133     if(!m_pileupUncGraph[i]) 
00134     {
00135       Error("JESUncertaintyProvider::SetInputCollection()",("ERROR: Problem finding Required Input Graph: "+currentPlot).c_str());
00136       return false;
00137     }
00138 
00139   }
00140 
00141   return true;
00142 
00143 }
00144 
00145 // Absolute Uncertainty
00146 double JESUncertaintyProvider::getAbsUncert(double pT, double Eta, Components UncertComps, unsigned int nVtx)
00147 {
00148   return getRelUncert(pT, Eta, UncertComps, nVtx)*pT;
00149 }
00150 
00151 // Relative Uncertainty
00152 double JESUncertaintyProvider::getRelUncert(double pT, double Eta, Components UncertComps, unsigned int nVtx)
00153 {
00154   // Convert units
00155   pT /= m_GeV;
00156   //std::cout<<"JES: I am her 1" <<std::endl; 
00157   // Protect against jets in the wrong range
00158   if(pT <= 15 || pT >= 7000)
00159   {
00160     Warning("JESUncertaintyProvider::getRelUncert()", "pT outside of covered range (15-7000): Returning -1");
00161     return -1;
00162   } 
00163   //std::cout<<"JES: I am her 2" <<std::endl;
00164   // Protect against jets in the wrong region
00165   if(fabs(Eta) >= 4.5)
00166   {
00167     Warning("JESUncertaintyProvider::getRelUncert()", "Eta outside of covered range (0.0<|eta|<4.5): Returning -1");
00168     return -1;
00169   }
00170   //std::cout<<"JES: I am her 3" <<std::endl;
00171   // Use the last filled value in the histogram
00172   if(pT > 2500)
00173     pT = 2400.;
00174   //std::cout<<"JES: I am her 4" <<std::endl;
00175   // Find the bin with the given pT, Eta value
00176   //std::cout<<"JES: I am her 5" <<std::endl;
00177   int currentBin = m_uncGraph[0]->FindBin(pT, fabs(Eta));
00178   // add uncertainties in the proper way
00179   return getComponents(currentBin, UncertComps, nVtx);
00180   //std::cout<<"JES: I am her 1" <<std::endl;
00181 }
00182 
00183 // Get a copy of the 2D Graph containing the Uncertainties
00184 TH2D* JESUncertaintyProvider::getUncGraphCopy(Components UncertComps, unsigned int nVtx)
00185 {
00186   TH2D* myPlot = (TH2D*)m_uncGraph[0]->Clone();
00187   myPlot->Reset("ICE");
00188   int nBinsX = myPlot->GetNbinsX();
00189   int nBinsY = myPlot->GetNbinsY();
00190 
00191   for(int g=0; g<nBinsX; g++)
00192   {
00193     for(int h=0; h<nBinsY; h++)
00194     {
00195       // Follow the math highlighted in the TH2D class
00196       int currentBin = (g+1)+(nBinsX+2)*(h+1);
00197       myPlot->SetBinContent(currentBin, getComponents(currentBin, UncertComps, nVtx));
00198     }
00199   }
00200   return myPlot;
00201 }
00202 
00203 // Construct the uncertainty from a set of components
00204 double JESUncertaintyProvider::getComponents(int currentBin, Components UncertComps, unsigned int nVtx)
00205 {
00206   // Terms which will be added in quadrature
00207   double quadrature(0);
00208 
00209   // Initialize the bitmask 
00210   // Use int multiplication to avoid potential machine 
00211   // precision mismatches when using the pow function
00212   int bitmask = 1;
00213 
00214   // Take care of picking up the right pileup contribution first:  
00215 
00216   // check we have enough vertices stored (return 7 if there are more vertices in the event)
00217   if (nVtx > m_nVertices) nVtx=7;
00218 
00219   // protection against zero-vertex events (thanks Dag!)
00220   if (nVtx == 0) nVtx=1;
00221 
00222   // substitute the pile-up plot on the fly
00223   m_uncGraph[6] = m_pileupUncGraph[nVtx-1];
00224 
00225   //Loop on the uncertainties
00226   for(unsigned int i=0; i<m_nUncertainties; i++)
00227 
00228   {
00229     double currentComponent = 0;
00230 
00231     // Check if current component is requested
00232     if(int(UncertComps) & bitmask)
00233       currentComponent = m_uncGraph[i]->GetBinContent(currentBin);
00234 
00235     // Now all uncertainties are added in quadrature
00236     quadrature += currentComponent*currentComponent;
00237 
00238     // Prepare the bitmask for the next iteration
00239     bitmask *=2;
00240   }
00241 
00242   return sqrt(quadrature); 
00243 
00244 }

Generated on Mon Jul 30 2012 16:56:35 for ForIA by  doxygen 1.7.2