ATLAS Offline Software
GainUncertainty.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 #include <TFile.h>
7 #include <TH1.h>
8 
9 #include <memory>
10 #include <cstdio> //sprintf
11 #include <cmath> //abs()
12 
13 namespace egGain {
14 
15 //--------------------------------------
16 
17 GainUncertainty::GainUncertainty(const std::string& filename, bool splitGainUnc,
18  const std::string& thisname, bool setInterpolation)
19  : asg::AsgMessaging(thisname.c_str()) {
20 
21  ATH_MSG_INFO("opening file " << filename);
22  std::unique_ptr<TFile> gainFile(TFile::Open(filename.c_str(), "READ"));
23 
24  if (not(m_alpha_specialGainRun =
25  (TH1*)(gainFile->Get("alpha_specialGainRun"))))
26  ATH_MSG_FATAL("cannot open alpha_specialGainRun");
27  m_alpha_specialGainRun->SetDirectory(nullptr);
28  if (not(m_gain_impact_Zee = (TH1*)(gainFile->Get("gain_impact_Zee"))))
29  ATH_MSG_FATAL("cannot open gain_impact_Zee");
30  m_gain_impact_Zee->SetDirectory(nullptr);
31  for (int i = 0; i < s_nEtaBins; i++) {
32  char name[60];
33  sprintf(name, "gain_Impact_elec_%d", i);
34  if (not(m_gain_Impact_elec[i] = (TH1*)(gainFile->Get(name))))
35  ATH_MSG_FATAL("cannot open " << name);
36  m_gain_Impact_elec[i]->SetDirectory(nullptr);
37  sprintf(name, "gain_Impact_conv_%d", i);
38  if (not(m_gain_Impact_conv[i] = (TH1*)(gainFile->Get(name))))
39  ATH_MSG_FATAL("cannot open " << name);
40  m_gain_Impact_conv[i]->SetDirectory(nullptr);
41  sprintf(name, "gain_Impact_unco_%d", i);
42  if (not(m_gain_Impact_unco[i] = (TH1*)(gainFile->Get(name))))
43  ATH_MSG_FATAL("cannot open " << name);
44  m_gain_Impact_unco[i]->SetDirectory(nullptr);
45 
46  if (splitGainUnc) {
47  sprintf(name, "gain_Impact_elec_%d_medium", i);
48  if (not(m_gain_Impact_elec_medium[i] = (TH1*)(gainFile->Get(name))))
49  ATH_MSG_FATAL("cannot open " << name);
50  m_gain_Impact_elec_medium[i]->SetDirectory(nullptr);
51 
52  sprintf(name, "gain_Impact_conv_%d_medium", i);
53  if (not(m_gain_Impact_conv_medium[i] = (TH1*)(gainFile->Get(name))))
54  ATH_MSG_FATAL("cannot open " << name);
55  m_gain_Impact_conv_medium[i]->SetDirectory(nullptr);
56 
57  sprintf(name, "gain_Impact_unco_%d_medium", i);
58  if (not(m_gain_Impact_unco_medium[i] = (TH1*)(gainFile->Get(name))))
59  ATH_MSG_FATAL("cannot open " << name);
60  m_gain_Impact_unco_medium[i]->SetDirectory(nullptr);
61 
62  sprintf(name, "gain_Impact_elec_%d_low", i);
63  if (not(m_gain_Impact_elec_low[i] = (TH1*)(gainFile->Get(name))))
64  ATH_MSG_FATAL("cannot open " << name);
65  m_gain_Impact_elec_low[i]->SetDirectory(nullptr);
66 
67  sprintf(name, "gain_Impact_conv_%d_low", i);
68  if (not(m_gain_Impact_conv_low[i] = (TH1*)(gainFile->Get(name))))
69  ATH_MSG_FATAL("cannot open " << name);
70  m_gain_Impact_conv_low[i]->SetDirectory(nullptr);
71 
72  sprintf(name, "gain_Impact_unco_%d_low", i);
73  if (not(m_gain_Impact_unco_low[i] = (TH1*)(gainFile->Get(name))))
74  ATH_MSG_FATAL("cannot open " << name);
75  m_gain_Impact_unco_low[i]->SetDirectory(nullptr);
76  }
77  }
78 
79  m_useInterpolation = setInterpolation;
80 }
81 
82 //----------------------------------------------
83 
86  delete m_gain_impact_Zee;
87  for (int i = 0; i < s_nEtaBins; i++) {
88  delete m_gain_Impact_elec[i];
89  delete m_gain_Impact_conv[i];
90  delete m_gain_Impact_unco[i];
91 
95 
96  delete m_gain_Impact_elec_low[i];
97  delete m_gain_Impact_conv_low[i];
98  delete m_gain_Impact_unco_low[i];
99  }
100 }
101 
102 //----------------------------------------------
103 
104 // returns relative uncertainty on energy
105 
106 double GainUncertainty::getUncertainty(double etaCalo_input, double et_input,
108  bool useL2GainUncertainty,
109  GainType gainType) const {
110  double aeta = std::fabs(etaCalo_input);
111  int ibin = -1;
112  if (aeta < 0.8)
113  ibin = 0;
114  else if (aeta < 1.37)
115  ibin = 1;
116  else if (aeta < 1.52)
117  ibin = 2;
118  else if (aeta < 1.80)
119  ibin = 3;
120  else if (aeta < 2.50)
121  ibin = 4;
122  if (ibin < 0)
123  return 0.;
124  ATH_MSG_VERBOSE("GainUncertainty::getUncertainty " << etaCalo_input << " "
125  << et_input << " " << ptype
126  << " ibin " << ibin);
127 
128  // Protection needed as the histograms stops at 1 TeV
129  if (et_input > 999999.)
130  et_input = 999999.;
131 
132  // std::cout << " --- in GainUncertainty::getUncertainty " <<
133  // etaCalo_input << " " << et_input << " " << ptype << " ibin " << ibin
134  // << std::endl;
135 
136  TH1* hImpact = nullptr;
137  // Medium+Low gain effect
138  if (gainType == GainType::MEDIUMLOW) {
139  if (ptype == PATCore::ParticleType::Electron)
140  hImpact = m_gain_Impact_elec[ibin];
141  else if (ptype == PATCore::ParticleType::ConvertedPhoton)
142  hImpact = m_gain_Impact_conv[ibin];
144  hImpact = m_gain_Impact_unco[ibin];
145  else {
147  "Trying to get Gain correction of not allowed particle type");
148  return 0;
149  }
150  }
151 
152  // Medium gain effect
153  else if (gainType == GainType::MEDIUM) {
154  if (ptype == PATCore::ParticleType::Electron)
155  hImpact = m_gain_Impact_elec_medium[ibin];
156  else if (ptype == PATCore::ParticleType::ConvertedPhoton)
157  hImpact = m_gain_Impact_conv_medium[ibin];
159  hImpact = m_gain_Impact_unco_medium[ibin];
160  else {
162  "Trying to get Gain correction of not allowed particle type");
163  return 0;
164  }
165  }
166  // Low gain effect
167  else if (gainType == GainType::LOW) {
168  if (ptype == PATCore::ParticleType::Electron)
169  hImpact = m_gain_Impact_elec_low[ibin];
170  else if (ptype == PATCore::ParticleType::ConvertedPhoton)
171  hImpact = m_gain_Impact_conv_low[ibin];
173  hImpact = m_gain_Impact_unco_low[ibin];
174  else {
176  "Trying to get Gain correction of not allowed particle type");
177  return 0;
178  }
179  }
180 
181  double max_et = hImpact->GetXaxis()->GetBinUpEdge(hImpact->GetNbinsX());
182  // Protection needed to match maximum Et in the histogram
183  if (0.001 * et_input > max_et) {
184  et_input = (max_et - 1.) * 1000.;
185  }
186 
187  double impact = 0;
188  if (m_useInterpolation) {
189  impact = hImpact->Interpolate(0.001 * et_input);
191  "L2 gain impact without interpolation: "
192  << hImpact->GetBinContent(hImpact->FindFixBin(0.001 * et_input)));
193  ATH_MSG_DEBUG("L2 gain impact with interpolation: "
194  << hImpact->Interpolate(0.001 * et_input));
195  } else {
196  impact = hImpact->GetBinContent(hImpact->FindFixBin(0.001 * et_input));
197  }
198 
199  int ieta = m_alpha_specialGainRun->FindFixBin(aeta);
200  if (useL2GainUncertainty)
201  ATH_MSG_INFO("Applying 100% uncertainy on l2 gain corrections");
202 
203  double alphaG = m_alpha_specialGainRun->GetBinContent(ieta);
204 
205  double impactZee =
207 
208  double_t sigmaE = alphaG * impact / impactZee;
209 
210  ATH_MSG_VERBOSE("alpha_specialGainRun, gain_impact_Zee, impact, sigmaE = "
211  << alphaG << " " << impactZee << " " << impact << " "
212  << sigmaE);
213 
214  return sigmaE;
215 }
216 
217 } // namespace egGain
PATCore::ParticleType::UnconvertedPhoton
@ UnconvertedPhoton
Definition: PATCoreEnums.h:38
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
egGain::GainUncertainty::s_nEtaBins
static const int s_nEtaBins
Definition: GainUncertainty.h:37
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
PATCore::ParticleType::Type
Type
Definition: PATCoreEnums.h:35
egGain::GainUncertainty::m_gain_Impact_conv
TH1 * m_gain_Impact_conv[s_nEtaBins]
Definition: GainUncertainty.h:41
egGain::GainUncertainty::m_useInterpolation
bool m_useInterpolation
Definition: GainUncertainty.h:50
asg
Definition: DataHandleTestTool.h:28
egGain
Definition: EgammaCalibrationAndSmearingTool.h:33
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
egGain::GainUncertainty::GainType::MEDIUM
@ MEDIUM
egGain::GainUncertainty::m_gain_Impact_unco_medium
TH1 * m_gain_Impact_unco_medium[s_nEtaBins]
Definition: GainUncertainty.h:45
egGain::GainUncertainty::GainType::MEDIUMLOW
@ MEDIUMLOW
egGain::GainUncertainty::m_gain_impact_Zee
TH1 * m_gain_impact_Zee
Definition: GainUncertainty.h:39
egGain::GainUncertainty::GainType
GainType
Definition: GainUncertainty.h:23
PATCore::ParticleType::ConvertedPhoton
@ ConvertedPhoton
Definition: PATCoreEnums.h:39
egGain::GainUncertainty::getUncertainty
double getUncertainty(double etaCalo_input, double et_input, PATCore::ParticleType::Type ptype=PATCore::ParticleType::Electron, bool useUncertainty=false, GainType gainType=GainType::MEDIUMLOW) const
Definition: GainUncertainty.cxx:106
lumiFormat.i
int i
Definition: lumiFormat.py:92
GainUncertainty.h
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
egGain::GainUncertainty::GainUncertainty
GainUncertainty(const std::string &filename, bool splitGainUnc=false, const std::string &name="GainUncertainty", bool setInterpolation=false)
Definition: GainUncertainty.cxx:17
egGain::GainUncertainty::m_gain_Impact_unco
TH1 * m_gain_Impact_unco[s_nEtaBins]
Definition: GainUncertainty.h:42
egGain::GainUncertainty::m_alpha_specialGainRun
TH1 * m_alpha_specialGainRun
Definition: GainUncertainty.h:38
egGain::GainUncertainty::m_gain_Impact_elec_low
TH1 * m_gain_Impact_elec_low[s_nEtaBins]
Definition: GainUncertainty.h:46
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
egGain::GainUncertainty::GainType::LOW
@ LOW
TH1::GetBinContent
double GetBinContent(int) const
Definition: rootspy.cxx:298
egGain::GainUncertainty::m_gain_Impact_elec
TH1 * m_gain_Impact_elec[s_nEtaBins]
Definition: GainUncertainty.h:40
egGain::GainUncertainty::m_gain_Impact_elec_medium
TH1 * m_gain_Impact_elec_medium[s_nEtaBins]
Definition: GainUncertainty.h:43
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
egGain::GainUncertainty::~GainUncertainty
~GainUncertainty()
Definition: GainUncertainty.cxx:84
TH1
Definition: rootspy.cxx:268
CaloCellTimeCorrFiller.filename
filename
Definition: CaloCellTimeCorrFiller.py:24
PATCore::ParticleType::Electron
@ Electron
Definition: PATCoreEnums.h:40
egGain::GainUncertainty::m_gain_Impact_conv_low
TH1 * m_gain_Impact_conv_low[s_nEtaBins]
Definition: GainUncertainty.h:47
egGain::GainUncertainty::m_gain_Impact_conv_medium
TH1 * m_gain_Impact_conv_medium[s_nEtaBins]
Definition: GainUncertainty.h:44
egGain::GainUncertainty::m_gain_Impact_unco_low
TH1 * m_gain_Impact_unco_low[s_nEtaBins]
Definition: GainUncertainty.h:48