CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
KKCorrectionFactors.cc
Go to the documentation of this file.
2 
4 {
5 
6  interpolate3D_ = pset.exists("interpolate3D") && pset.getUntrackedParameter<bool>("interpolate3D");
7 
8  // Get the filename and histogram name
10  std::string histogramName = pset.getUntrackedParameter<std::string>("histogramName");
11 
12  // Read histo
13  edm::FileInPath myDataFile( fileName );
14  TFile * myFile = TFile::Open( myDataFile.fullPath().c_str() );
15 
16  gROOT->cd(); // create histogram in memory
17  auto obj = myFile->Get( histogramName.c_str() );
18 
19  // Check if histogram exists in file
20  if(!obj) {
21  throw cms::Exception( "FileReadError" )
22  << "Histogram \"" << histogramName
23  << "\" not found in file \"" << fileName
24  << "\".\n";
25  }
26  h3_ = new TH3F( *((TH3F*)obj) );
27 
28  delete myFile;
29 
30 }
31 
32 
33 float KKCorrectionFactors::getScale( float genE, float genEta, float simE ) const
34 {
35 
36  float r = simE / genE;
37  float scale = 1.;
38 
39  if( interpolate3D_
40  // TH3::Interpolate can only interpolate inside the bondaries of the histogram
41  && genE > h3_->GetXaxis()->GetXmin()
42  && genE < h3_->GetXaxis()->GetXmax()
43  && genEta > h3_->GetYaxis()->GetXmin()
44  && genEta < h3_->GetYaxis()->GetXmax()
45  && r < h3_->GetZaxis()->GetXmax()
46  && r > h3_->GetZaxis()->GetXmax() ) {
47 
48  scale = h3_->Interpolate( genE, genEta, r );
49 
50  } else { // interpolation in r is mandatory
51 
52  int binE = h3_->GetXaxis()->FindFixBin( genE );
53  int binEta = h3_->GetYaxis()->FindFixBin( genEta );
54 
55  // find the two bins which are closest to the actual value
56  auto binWidthR = h3_->GetZaxis()->GetBinWidth(0);
57  int binRup = h3_->GetZaxis()->FindFixBin( r + binWidthR/2. );
58  int binRdn = h3_->GetZaxis()->FindFixBin( r - binWidthR/2. );
59 
60  auto scaleUp = h3_->GetBinContent( binE, binEta, binRup );
61  auto scaleDn = h3_->GetBinContent( binE, binEta, binRdn );
62 
63  // make a linear extrapolation between neighbour bins if they are not zero
64  auto Rdn = h3_->GetZaxis()->GetBinCenter( binRdn );
65  scale = scaleDn + (scaleUp-scaleDn) * ( r - Rdn ) / binWidthR;
66 
67  }
68  return scale;
69 
70 }
71 
T getUntrackedParameter(std::string const &, T const &) const
KKCorrectionFactors(const edm::ParameterSet &pset)
bool exists(std::string const &parameterName) const
checks if a parameter exists
float getScale(float genEnergy, float genEta, float simEnergy) const