ATLAS Offline Software
BaseLinearFakeBkgTool.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
9 #include "TTree.h"
10 #include "TDirectory.h"
11 #include "TFile.h"
12 #include "TH1.h"
13 #include "TH2.h"
14 #include "TH3.h"
15 
16 using namespace FakeBkgTools;
17 using namespace CP;
18 
19 namespace
20 {
21  const char savedYieldsKey[] = "BaseLinearFakeBkgTool__yields";
22  const char savedHistogramsKey[] = "BaseLinearFakeBkgTool__histograms";
23 }
24 
25 BaseLinearFakeBkgTool::BaseLinearFakeBkgTool(const std::string& toolname) : BaseFakeBkgTool(toolname), m_yields(1)
26 {
28 }
29 
31 {
32 }
33 
35 {
37 }
38 
39 StatusCode BaseLinearFakeBkgTool::getEventWeight(float& weight, const std::string& selection, const std::string& process)
40 {
43  weight = w.value(this);
44  return sc;
45 }
46 
48 {
49  if(!m_initialized)
50  {
51  ATH_MSG_WARNING("the tool hasn't been initialized");
52  return StatusCode::FAILURE;
53  }
54  weight.uncertainties.clear();
55  bool success = true;
56  auto fs = getCachedFinalState(m_particles.size(), selection, process, success);
57  if(!success) return StatusCode::FAILURE;
58  auto itr = m_cachedWeights.find(fs);
59  if(itr != m_cachedWeights.end())
60  {
61  weight = itr->second;
62  return StatusCode::SUCCESS;
63  }
65  if(sc != StatusCode::SUCCESS) return sc;
66  m_cachedWeights.emplace(fs, weight);
67  return StatusCode::SUCCESS;
68 }
69 
70 StatusCode BaseLinearFakeBkgTool::getTotalYield(float& yield, float& yieldStatErrorUp, float& yieldStatErrorDown)
71 {
72  if(!m_initialized)
73  {
74  ATH_MSG_WARNING("the tool hasn't been initialized");
75  return StatusCode::FAILURE;
76  }
77  if(m_progressFileName.length() && m_progressFileName != "none")
78  {
80  }
81  auto& total = m_yields.at(0);
82  yield = total.value(this);
83  yieldStatErrorUp = sqrt(total.stat2.up);
84  yieldStatErrorDown = sqrt(total.stat2.down);
85  for(auto& kv : m_histogramYieldsRange)
86  {
87  for(int i=0;i<kv.first->GetNcells();++i)
88  {
89  auto& thisYield = m_yields.at(kv.second.first + i);
90  kv.first->SetBinContent(i, thisYield.value(this));
91  kv.first->SetBinError(i, thisYield.stat());
92  }
93  }
94  return StatusCode::SUCCESS;
95 }
96 
98 {
99  FakeBkgTools::Weight eventWeight;
102  m_yields.at(0).add(eventWeight, m_externalWeight);
103  for(auto& kv : m_values_1dhisto_map)
104  {
105  unsigned index = m_histogramYieldsRange.at(kv.first).first + kv.first->FindBin(*kv.second);
106  m_yields.at(index).add(eventWeight, m_externalWeight);
107  }
108  for(auto& kv : m_values_2dhisto_map)
109  {
110  unsigned index = m_histogramYieldsRange.at(kv.first).first + kv.first->FindBin(*kv.second.first, *kv.second.second);
111  m_yields.at(index).add(eventWeight, m_externalWeight);
112  }
113  for(auto& kv : m_values_3dhisto_map)
114  {
115  unsigned index = m_histogramYieldsRange.at(kv.first).first + kv.first->FindBin(*std::get<0>(kv.second), *std::get<1>(kv.second), *std::get<2>(kv.second));
116  m_yields.at(index).add(eventWeight, m_externalWeight);
117  }
118  return StatusCode::SUCCESS;
119 }
120 
122 {
124  if(sc != StatusCode::SUCCESS) return sc;
125  return assignYieldRange(h1);
126 }
127 
129 {
131  if(sc != StatusCode::SUCCESS) return sc;
132  return assignYieldRange(h2);
133 }
134 
135 StatusCode BaseLinearFakeBkgTool::register3DHistogram(TH3* h3, const float *xval, const float *yval, const float *zval)
136 {
138  if(sc != StatusCode::SUCCESS) return sc;
139  return assignYieldRange(h3);
140 }
141 
143 {
144  const std::string histname = h->GetName();
145  std::pair<uint32_t, uint32_t> range;
146  for(auto itr=m_histogramYieldsRange.begin();itr!=m_histogramYieldsRange.end();++itr)
147  {
148  if(histname != itr->first->GetName()) continue;
149  range = itr->second;
150  m_histogramYieldsRange.erase(itr);
151  m_histogramYieldsRange.emplace(h, range);
152  return StatusCode::SUCCESS;
153  }
154  range.first = m_yields.size();
155  m_yields.insert(m_yields.end(), std::max(1, h->GetNcells()), FakeBkgTools::Yield{});
156  range.second = m_yields.size() - 1;
157  m_histogramYieldsRange.emplace(h, range);
158  return StatusCode::SUCCESS;
159 }
160 
162 {
163  TTree tree(::savedYieldsKey, "binned yields saved by BaseLinearFakeBkgTool");
164  TObjArray histograms;
165 
166  ULong64_t histname = 0;
167  UInt_t bin=0, n=0;
168  float nominal, stat2Up, stat2Down;
169  const std::size_t maxSize = m_database->numberOfStats() + m_database->numberOfSysts();
170  std::unique_ptr<UShort_t[]> systUID(new UShort_t[maxSize]);
171  std::unique_ptr<float[]> systUp(new float[maxSize]), systDown(new float[maxSize]);
172  tree.Branch("Name", &histname, "Name/l");
173  tree.Branch("Bin", &bin, "Bin/i");
174  tree.Branch("nominal", &nominal, "nominal/F");
175  tree.Branch("stat2Up", &stat2Up, "stat2Up/F");
176  tree.Branch("stat2Down", &stat2Down, "stat2Down/F");
177  tree.Branch("N", &n, "N/i");
178  tree.Branch("systUID", systUID.get(), "systUID[N]/s");
179  tree.Branch("systUp", systUp.get(), "systUp[N]/F");
180  tree.Branch("systDown", systDown.get(), "systDown[N]/F");
181 
182  auto fillTree = [&](const auto& yield)
183  {
184  nominal = yield.nominal;
185  stat2Up = yield.stat2.up;
186  stat2Down = yield.stat2.down;
187  n = yield.uncertainties.size();
188  auto itr = yield.uncertainties.begin();
189  for(uint32_t j=0;j<n;++j)
190  {
191  systUID[j] = itr->first;
192  systUp[j] = itr->second.up;
193  systDown[j] = itr->second.down;
194  ++itr;
195  }
196  tree.Fill();
197  };
198 
199  fillTree(m_yields.at(0));
200 
201  std::hash<std::string> hasher;
202  for(auto& kv : m_histogramYieldsRange)
203  {
204  histograms.AddLast(kv.first);
205  histname = hasher(kv.first->GetName());
206  for(uint32_t i=kv.second.first;i<=kv.second.second;++i)
207  {
208  bin = i - kv.second.first;
209  fillTree(m_yields.at(i));
210  }
211  }
212 
213  dir->cd();
214  tree.Write();
215  dir->mkdir(::savedHistogramsKey);
216  dir->cd(::savedHistogramsKey);
217  histograms.Write();
218  dir->cd("..");
219  return StatusCode::SUCCESS;
220 }
221 
223 {
225 
226  auto file = std::unique_ptr<TFile>(TFile::Open(filename.c_str(), "READ"));
227  if(!file)
228  {
229  ATH_MSG_ERROR("Unable to open the file specified for the \"ProgressFileName\" property: " << m_progressFileName);
230  return StatusCode::FAILURE;
231  }
232  std::string path = ::savedYieldsKey;
233  if(m_progressFileDirectory.length())
234  {
236  }
237  auto tree = static_cast<TTree*>(file->Get(path.c_str()));
238  if(!tree)
239  {
240  ATH_MSG_ERROR("Unable to find the tree \"" << path << "\" in the input file " << m_progressFileName);
241  return StatusCode::FAILURE;
242  }
243 
246 
247  std::hash<std::string> hasher;
248  std::map<std::size_t, decltype(m_histogramYieldsRange.cbegin())> dictionary;
249  for(auto itr=m_histogramYieldsRange.cbegin();itr!=m_histogramYieldsRange.cend();++itr)
250  {
251  dictionary[hasher(itr->first->GetName())] = itr;
252  }
253 
254  ULong64_t histname = 0;
255  UInt_t bin=0, n=0;
256  float nominal, stat2Up, stat2Down;
257  const std::size_t maxSize = m_database->numberOfStats() + m_database->numberOfSysts();
258  std::unique_ptr<UShort_t[]> systUID(new UShort_t[maxSize]);
259  std::unique_ptr<float[]> systUp(new float[maxSize]), systDown(new float[maxSize]);
260  tree->SetBranchStatus("*", kTRUE);
261  tree->SetBranchAddress("Name", &histname);
262  tree->SetBranchAddress("Bin", &bin);
263  tree->SetBranchAddress("nominal", &nominal);
264  tree->SetBranchAddress("stat2Up", &stat2Up);
265  tree->SetBranchAddress("stat2Down", &stat2Down);
266  tree->SetBranchAddress("N", &n);
267  TBranch* branch = tree->FindBranch("N");
268  tree->SetBranchAddress("systUID", systUID.get());
269  tree->SetBranchAddress("systUp", systUp.get());
270  tree->SetBranchAddress("systDown", systDown.get());
271 
272  std::set<std::size_t> filledHistograms;
273  const long entries = tree->GetEntries();
274  for(long entry=0;entry<entries;++entry)
275  {
276  branch->GetEntry(entry);
277  if(n > maxSize)
278  {
279  ATH_MSG_ERROR("the tool configuration seems to have changed (number of systematics)!");
280  return StatusCode::FAILURE;
281  }
282  tree->GetEntry(entry);
283 
284  long index;
285  if(histname != 0)
286  {
287  auto itr = dictionary.find(histname);
288  if(itr == dictionary.end()) continue;
289  filledHistograms.emplace(histname);
290  const TH1* h = itr->second->first;
291  const auto& range = itr->second->second;
292  if(bin > (range.second - range.first))
293  {
294  ATH_MSG_ERROR("inconsistent binnings found for histogram " << h->GetName());
295  return StatusCode::FAILURE;
296  }
297  index = range.first + bin;
298  }
299  else
300  {
301  index = 0;
302  }
303 
304  FakeBkgTools::Yield yield;
305  yield.nominal = nominal;
306  yield.stat2.up = stat2Up;
307  yield.stat2.down = stat2Down;
308  for(unsigned j=0;j<n;++j)
309  {
311  u.up = systUp[j];
312  u.down= systDown[j];
313  auto emplaced = yield.uncertainties.emplace(systUID[j], u);
314  if(!emplaced.second) emplaced.first->second += u;
315  }
316  m_yields.at(index).add(yield);
317  }
318 
319  if(filledHistograms.size() != m_histogramYieldsRange.size())
320  {
321  ATH_MSG_ERROR("some of the registered histogram were not found in the input file " << m_progressFileName);
322  return StatusCode::FAILURE;
323  }
324 
325  m_progressFileName.clear();
326  return StatusCode::SUCCESS;
327 }
CP::BaseFakeBkgTool::register1DHistogram
virtual StatusCode register1DHistogram(TH1 *h1, const float *val) override
associates a 1D histogram to the tool, to obtain a binned estimate of the fake lepton background the ...
Definition: BaseFakeBkgTool.cxx:269
CP::BaseLinearFakeBkgTool::incrementTotalYield
StatusCode incrementTotalYield()
be sure to only call this once per event! (typically at the end of addEvent())
Definition: BaseLinearFakeBkgTool.cxx:97
CP::BaseFakeBkgTool::getCachedFinalState
FakeBkgTools::FinalState getCachedFinalState(uint8_t nparticles, const std::string &strPID, const std::string &strProc, bool &success)
Definition: BaseFakeBkgTool.cxx:248
FakeBkgTools::Uncertainty
Definition: FakeBkgInternals.h:27
CP::BaseFakeBkgTool
Definition: BaseFakeBkgTool.h:41
FakeBkgTools::Uncertainty::down
float down
Definition: FakeBkgInternals.h:28
athena.path
path
python interpreter configuration --------------------------------------—
Definition: athena.py:128
max
#define max(a, b)
Definition: cfImp.cxx:41
covarianceTool.histograms
dictionary histograms
Definition: covarianceTool.py:53
xAOD::uint32_t
setEventNumber uint32_t
Definition: EventInfo_v1.cxx:127
index
Definition: index.py:1
tree
TChain * tree
Definition: tile_monitor.h:30
bin
Definition: BinsDiffFromStripMedian.h:43
CP::BaseFakeBkgTool::initialize
virtual StatusCode initialize() override
Dummy implementation of the initialisation function.
Definition: BaseFakeBkgTool.cxx:80
CP::BaseFakeBkgTool::m_database
std::unique_ptr< FakeBkgTools::Database > m_database
Definition: BaseFakeBkgTool.h:91
CP::BaseLinearFakeBkgTool::initialize
virtual StatusCode initialize() override
Dummy implementation of the initialisation function.
Definition: BaseLinearFakeBkgTool.cxx:34
read_hist_ntuple.h1
h1
Definition: read_hist_ntuple.py:21
CP::BaseLinearFakeBkgTool::getEventWeight
virtual StatusCode getEventWeight(float &weight, const std::string &selection, const std::string &process) override final
returns an event weight addEvent() must have been called before hand.
Definition: BaseLinearFakeBkgTool.cxx:39
CP
Select isolated Photons, Electrons and Muons.
Definition: Control/xAODRootAccess/xAODRootAccess/TEvent.h:48
CP::BaseLinearFakeBkgTool::m_histogramYieldsRange
std::map< TH1 *, std::pair< uint32_t, uint32_t > > m_histogramYieldsRange
Definition: BaseLinearFakeBkgTool.h:73
SUSY_SimplifiedModel_PostInclude.process
string process
Definition: SUSY_SimplifiedModel_PostInclude.py:42
Trk::u
@ u
Enums for curvilinear frames.
Definition: ParamDefs.h:77
AthenaPoolTestRead.sc
sc
Definition: AthenaPoolTestRead.py:27
dqt_zlumi_pandas.weight
int weight
Definition: dqt_zlumi_pandas.py:189
fillTree
void fillTree(AccumulateMap &map, TTree *tree, int nLayers, int nCoords)
Writes the contents of an AccumulateMap into the supplied tree (one entry per sector).
Definition: FPGATrackSimMatrixIO.cxx:226
CP::BaseFakeBkgTool::register3DHistogram
virtual StatusCode register3DHistogram(TH3 *h3, const float *xval, const float *yval, const float *zval) override
associates a 3D histogram to the tool, to obtain a binned estimate of the fake lepton background
Definition: BaseFakeBkgTool.cxx:329
CP::BaseLinearFakeBkgTool::mergeSubjobs
StatusCode mergeSubjobs()
Definition: BaseLinearFakeBkgTool.cxx:222
CP::BaseLinearFakeBkgTool::~BaseLinearFakeBkgTool
virtual ~BaseLinearFakeBkgTool()
Definition: BaseLinearFakeBkgTool.cxx:30
CP::BaseLinearFakeBkgTool::register2DHistogram
virtual StatusCode register2DHistogram(TH2 *h2, const float *xval, const float *yval) override
associates a 2D histogram to the tool, to obtain a binned estimate of the fake lepton background the ...
Definition: BaseLinearFakeBkgTool.cxx:128
CP::BaseLinearFakeBkgTool::register3DHistogram
virtual StatusCode register3DHistogram(TH3 *h3, const float *xval, const float *yval, const float *zval) override
associates a 3D histogram to the tool, to obtain a binned estimate of the fake lepton background
Definition: BaseLinearFakeBkgTool.cxx:135
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
PixelAthClusterMonAlgCfg.histname
histname
Definition: PixelAthClusterMonAlgCfg.py:106
plotting.yearwise_efficiency_vs_mu.xval
float xval
Definition: yearwise_efficiency_vs_mu.py:35
lumiFormat.i
int i
Definition: lumiFormat.py:85
CP::BaseFakeBkgTool::m_initialized
bool m_initialized
Definition: BaseFakeBkgTool.h:78
h
FakeBkgTools
Definition: BaseFakeBkgTool.h:23
beamspotman.n
n
Definition: beamspotman.py:731
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
CP::BaseFakeBkgTool::m_progressFileDirectory
std::string m_progressFileDirectory
property ProgressFileDirectory
Definition: BaseFakeBkgTool.h:139
CP::BaseLinearFakeBkgTool::assignYieldRange
StatusCode assignYieldRange(TH1 *h)
Definition: BaseLinearFakeBkgTool.cxx:142
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:195
file
TFile * file
Definition: tile_monitor.h:29
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
CP::BaseFakeBkgTool::m_process
std::string m_process
'process' settings used to compute the total yield / fill histograms
Definition: BaseFakeBkgTool.h:118
CP::BaseFakeBkgTool::register2DHistogram
virtual StatusCode register2DHistogram(TH2 *h2, const float *xval, const float *yval) override
associates a 2D histogram to the tool, to obtain a binned estimate of the fake lepton background the ...
Definition: BaseFakeBkgTool.cxx:299
CP::BaseFakeBkgTool::m_unlimitedSystematicVariations
bool m_unlimitedSystematicVariations
used to prevent multiple calls to applySystematicVariation() when unsupported set to true in a partic...
Definition: BaseFakeBkgTool.h:170
selection
std::string selection
Definition: fbtTestBasics.cxx:75
CP::BaseFakeBkgTool::m_values_1dhisto_map
std::map< TH1 *, const float * > m_values_1dhisto_map
Definition: BaseFakeBkgTool.h:108
beamspotman.dir
string dir
Definition: beamspotman.py:623
GetAllXsec.entry
list entry
Definition: GetAllXsec.py:132
PathResolver.h
Database.h
master.dictionary
dictionary
Definition: master.py:47
python.hypoToolDisplay.toolname
def toolname(tool)
Definition: hypoToolDisplay.py:13
plotBeamSpotVxVal.bin
int bin
Definition: plotBeamSpotVxVal.py:83
FakeBkgInternals.h
CP::BaseFakeBkgTool::m_externalWeight
float m_externalWeight
Definition: BaseFakeBkgTool.h:95
FakeBkgTools::Weight
a structure to hold a weight together with a variable number of systematic uncertainties
Definition: FakeBkgInternals.h:62
CP::BaseFakeBkgTool::m_particles
std::vector< FakeBkgTools::ParticleData > m_particles
Definition: BaseFakeBkgTool.h:85
plotting.yearwise_efficiency_vs_mu.yval
float yval
Definition: yearwise_efficiency_vs_mu.py:36
FakeBkgTools::Efficiency::nominal
float nominal
Definition: FakeBkgInternals.h:42
FakeBkgTools::Uncertainty::up
float up
Definition: FakeBkgInternals.h:28
FakeBkgTools::Yield
a structure to hold an event yield together with a statistical uncertainty and a variable number of s...
Definition: FakeBkgInternals.h:70
PathResolverFindDataFile
std::string PathResolverFindDataFile(const std::string &logical_file_name)
Definition: PathResolver.cxx:379
RTTAlgmain.branch
branch
Definition: RTTAlgmain.py:61
DeMoScan.index
string index
Definition: DeMoScan.py:364
BaseLinearFakeBkgTool.h
CP::BaseLinearFakeBkgTool::register1DHistogram
virtual StatusCode register1DHistogram(TH1 *h1, const float *val) override
associates a 1D histogram to the tool, to obtain a binned estimate of the fake lepton background the ...
Definition: BaseLinearFakeBkgTool.cxx:121
CP::BaseLinearFakeBkgTool::getEventWeightCommon
StatusCode getEventWeightCommon(FakeBkgTools::Weight &weight, const std::string &selection, const std::string &process)
Definition: BaseLinearFakeBkgTool.cxx:47
CP::BaseLinearFakeBkgTool::getEventWeightCustom
virtual StatusCode getEventWeightCustom(FakeBkgTools::Weight &weight, const FakeBkgTools::FinalState &fs)=0
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
CP::BaseLinearFakeBkgTool::getTotalYield
virtual StatusCode getTotalYield(float &yield, float &statErrorUp, float &statErrorDown) override final
returns the accumulated fake lepton background yield (or compute it, in the case of the likelihood ma...
Definition: BaseLinearFakeBkgTool.cxx:70
Pythia8_RapidityOrderMPI.val
val
Definition: Pythia8_RapidityOrderMPI.py:14
FakeBkgTools::Yield::stat2
Uncertainty stat2
Definition: FakeBkgInternals.h:71
Herwig7_QED_EvtGen_ll.fs
dictionary fs
Definition: Herwig7_QED_EvtGen_ll.py:17
CaloCellTimeCorrFiller.filename
filename
Definition: CaloCellTimeCorrFiller.py:24
entries
double entries
Definition: listroot.cxx:49
CP::BaseFakeBkgTool::m_selection
std::string m_selection
'selection' settings used to compute the total yield / fill histograms
Definition: BaseFakeBkgTool.h:115
CP::BaseFakeBkgTool::m_values_2dhisto_map
std::map< TH2 *, std::pair< const float *, const float * > > m_values_2dhisto_map
Definition: BaseFakeBkgTool.h:109
CP::BaseLinearFakeBkgTool::saveProgress
virtual StatusCode saveProgress(TDirectory *dir) override
Definition: BaseLinearFakeBkgTool.cxx:161
CP::BaseLinearFakeBkgTool::m_yields
std::vector< FakeBkgTools::Yield > m_yields
accumulated yield for all events (and histogram bins with uncertainties)
Definition: BaseLinearFakeBkgTool.h:63
python.IoTestsLib.w
def w
Definition: IoTestsLib.py:200
CP::BaseFakeBkgTool::m_values_3dhisto_map
std::map< TH3 *, std::tuple< const float *, const float *, const float * > > m_values_3dhisto_map
Definition: BaseFakeBkgTool.h:110
CP::BaseFakeBkgTool::m_progressFileName
std::string m_progressFileName
property ProgressFileName
Definition: BaseFakeBkgTool.h:136
CP::BaseLinearFakeBkgTool::m_cachedWeights
std::map< FakeBkgTools::FinalState, FakeBkgTools::Weight > m_cachedWeights
cached weight+uncertainties for a single event Each tool derived from this base class MUST clear the ...
Definition: BaseLinearFakeBkgTool.h:67
FakeBkgTools::Efficiency::uncertainties
std::map< uint16_t, FakeBkgTools::Uncertainty > uncertainties
Definition: FakeBkgInternals.h:43