ATLAS Offline Software
BaseLinearFakeBkgTool.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
3 */
4 
9 
10 #ifndef FAKEBKGTOOLS_ATLAS_ENVIRONMENT
11  #define declareProperty(n, p, h) ExtraPropertyManager<BaseLinearFakeBkgTool>::declareProperty(n, &BaseLinearFakeBkgTool::p, h)
12 #endif
13 
14 #include "TTree.h"
15 #include "TDirectory.h"
16 #include "TFile.h"
17 #include "TH1.h"
18 #include "TH2.h"
19 #include "TH3.h"
20 
21 using namespace FakeBkgTools;
22 using namespace CP;
23 
24 namespace
25 {
26  const char savedYieldsKey[] = "BaseLinearFakeBkgTool__yields";
27  const char savedHistogramsKey[] = "BaseLinearFakeBkgTool__histograms";
28 }
29 
30 BaseLinearFakeBkgTool::BaseLinearFakeBkgTool(const std::string& toolname) : BaseFakeBkgTool(toolname), m_yields(1)
31 {
33 
35 }
36 
38 {
39 }
40 
42 {
44 }
45 
46 StatusCode BaseLinearFakeBkgTool::getEventWeight(float& weight, const std::string& selection, const std::string& process)
47 {
50  weight = w.value(this);
51  return sc;
52 }
53 
55 {
56  if(!m_initialized)
57  {
58  ATH_MSG_WARNING("the tool hasn't been initialized");
59  return StatusCode::FAILURE;
60  }
61  weight.uncertainties.clear();
62  bool success = true;
63  auto fs = getCachedFinalState(m_particles.size(), selection, process, success);
64  if(!success) return StatusCode::FAILURE;
65  auto itr = m_cachedWeights.find(fs);
66  if(itr != m_cachedWeights.end())
67  {
68  weight = itr->second;
69  return StatusCode::SUCCESS;
70  }
72  if(sc != StatusCode::SUCCESS) return sc;
73  m_cachedWeights.emplace(fs, weight);
74  return StatusCode::SUCCESS;
75 }
76 
77 StatusCode BaseLinearFakeBkgTool::getTotalYield(float& yield, float& yieldStatErrorUp, float& yieldStatErrorDown)
78 {
79  if(!m_initialized)
80  {
81  ATH_MSG_WARNING("the tool hasn't been initialized");
82  return StatusCode::FAILURE;
83  }
84  if(m_progressFileName.length() && m_progressFileName != "none")
85  {
87  }
88  auto& total = m_yields.at(0);
89  yield = total.value(this);
90  yieldStatErrorUp = sqrt(total.stat2.up);
91  yieldStatErrorDown = sqrt(total.stat2.down);
92  for(auto& kv : m_histogramYieldsRange)
93  {
94  for(int i=0;i<kv.first->GetNcells();++i)
95  {
96  auto& yield = m_yields.at(kv.second.first + i);
97  kv.first->SetBinContent(i, yield.value(this));
98  kv.first->SetBinError(i, yield.stat());
99  }
100  }
101  return StatusCode::SUCCESS;
102 }
103 
105 {
106  FakeBkgTools::Weight eventWeight;
109  m_yields.at(0).add(eventWeight, m_externalWeight);
110  for(auto& kv : m_values_1dhisto_map)
111  {
112  unsigned index = m_histogramYieldsRange.at(kv.first).first + kv.first->FindBin(*kv.second);
113  m_yields.at(index).add(eventWeight, m_externalWeight);
114  }
115  for(auto& kv : m_values_2dhisto_map)
116  {
117  unsigned index = m_histogramYieldsRange.at(kv.first).first + kv.first->FindBin(*kv.second.first, *kv.second.second);
118  m_yields.at(index).add(eventWeight, m_externalWeight);
119  }
120  for(auto& kv : m_values_3dhisto_map)
121  {
122  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));
123  m_yields.at(index).add(eventWeight, m_externalWeight);
124  }
125  return StatusCode::SUCCESS;
126 }
127 
129 {
131  if(sc != StatusCode::SUCCESS) return sc;
132  return assignYieldRange(h1);
133 }
134 
136 {
138  if(sc != StatusCode::SUCCESS) return sc;
139  return assignYieldRange(h2);
140 }
141 
142 StatusCode BaseLinearFakeBkgTool::register3DHistogram(TH3* h3, const float *xval, const float *yval, const float *zval)
143 {
145  if(sc != StatusCode::SUCCESS) return sc;
146  return assignYieldRange(h3);
147 }
148 
150 {
151  const std::string histname = h->GetName();
152  std::pair<uint32_t, uint32_t> range;
153  for(auto itr=m_histogramYieldsRange.begin();itr!=m_histogramYieldsRange.end();++itr)
154  {
155  if(histname != itr->first->GetName()) continue;
156  range = itr->second;
157  m_histogramYieldsRange.erase(itr);
158  m_histogramYieldsRange.emplace(h, range);
159  return StatusCode::SUCCESS;
160  }
161  range.first = m_yields.size();
162  m_yields.insert(m_yields.end(), std::max(1, h->GetNcells()), FakeBkgTools::Yield{});
163  range.second = m_yields.size() - 1;
164  m_histogramYieldsRange.emplace(h, range);
165  return StatusCode::SUCCESS;
166 }
167 
169 {
170  TTree tree(::savedYieldsKey, "binned yields saved by BaseLinearFakeBkgTool");
171  TObjArray histograms;
172 
173  ULong64_t histname = 0;
174  UInt_t bin=0, n=0;
175  float nominal, stat2Up, stat2Down;
176  const std::size_t maxSize = m_database->numberOfStats() + m_database->numberOfSysts();
177  std::unique_ptr<UShort_t[]> systUID(new UShort_t[maxSize]);
178  std::unique_ptr<float[]> systUp(new float[maxSize]), systDown(new float[maxSize]);
179  tree.Branch("Name", &histname, "Name/l");
180  tree.Branch("Bin", &bin, "Bin/i");
181  tree.Branch("nominal", &nominal, "nominal/F");
182  tree.Branch("stat2Up", &stat2Up, "stat2Up/F");
183  tree.Branch("stat2Down", &stat2Down, "stat2Down/F");
184  tree.Branch("N", &n, "N/i");
185  tree.Branch("systUID", systUID.get(), "systUID[N]/s");
186  tree.Branch("systUp", systUp.get(), "systUp[N]/F");
187  tree.Branch("systDown", systDown.get(), "systDown[N]/F");
188 
189  auto fillTree = [&](const auto& yield)
190  {
191  nominal = yield.nominal;
192  stat2Up = yield.stat2.up;
193  stat2Down = yield.stat2.down;
194  n = yield.uncertainties.size();
195  auto itr = yield.uncertainties.begin();
196  for(uint32_t j=0;j<n;++j)
197  {
198  systUID[j] = itr->first;
199  systUp[j] = itr->second.up;
200  systDown[j] = itr->second.down;
201  ++itr;
202  }
203  tree.Fill();
204  };
205 
206  fillTree(m_yields.at(0));
207 
208  std::hash<std::string> hasher;
209  for(auto& kv : m_histogramYieldsRange)
210  {
211  histograms.AddLast(kv.first);
212  histname = hasher(kv.first->GetName());
213  for(uint32_t i=kv.second.first;i<=kv.second.second;++i)
214  {
215  bin = i - kv.second.first;
216  fillTree(m_yields.at(i));
217  }
218  }
219 
220  dir->cd();
221  tree.Write();
222  dir->mkdir(::savedHistogramsKey);
223  dir->cd(::savedHistogramsKey);
224  histograms.Write();
225  dir->cd("..");
226  return StatusCode::SUCCESS;
227 }
228 
230 {
232 
233  auto file = std::unique_ptr<TFile>(TFile::Open(filename.c_str(), "READ"));
234  if(!file)
235  {
236  ATH_MSG_ERROR("Unable to open the file specified for the \"ProgressFileName\" property: " << m_progressFileName);
237  return StatusCode::FAILURE;
238  }
239  std::string path = ::savedYieldsKey;
240  if(m_progressFileDirectory.length())
241  {
243  }
244  auto tree = static_cast<TTree*>(file->Get(path.c_str()));
245  if(!tree)
246  {
247  ATH_MSG_ERROR("Unable to find the tree \"" << path << "\" in the input file " << m_progressFileName);
248  return StatusCode::FAILURE;
249  }
250 
253 
254  std::hash<std::string> hasher;
255  std::map<std::size_t, decltype(m_histogramYieldsRange.cbegin())> dictionary;
256  for(auto itr=m_histogramYieldsRange.cbegin();itr!=m_histogramYieldsRange.cend();++itr)
257  {
258  dictionary[hasher(itr->first->GetName())] = itr;
259  }
260 
261  ULong64_t histname = 0;
262  UInt_t bin=0, n=0;
263  float nominal, stat2Up, stat2Down;
264  const std::size_t maxSize = m_database->numberOfStats() + m_database->numberOfSysts();
265  std::unique_ptr<UShort_t[]> systUID(new UShort_t[maxSize]);
266  std::unique_ptr<float[]> systUp(new float[maxSize]), systDown(new float[maxSize]);
267  tree->SetBranchStatus("*", kTRUE);
268  tree->SetBranchAddress("Name", &histname);
269  tree->SetBranchAddress("Bin", &bin);
270  tree->SetBranchAddress("nominal", &nominal);
271  tree->SetBranchAddress("stat2Up", &stat2Up);
272  tree->SetBranchAddress("stat2Down", &stat2Down);
273  tree->SetBranchAddress("N", &n);
274  TBranch* branch = tree->FindBranch("N");
275  tree->SetBranchAddress("systUID", systUID.get());
276  tree->SetBranchAddress("systUp", systUp.get());
277  tree->SetBranchAddress("systDown", systDown.get());
278 
279  std::set<std::size_t> filledHistograms;
280  const long entries = tree->GetEntries();
281  for(long entry=0;entry<entries;++entry)
282  {
283  branch->GetEntry(entry);
284  if(n > maxSize)
285  {
286  ATH_MSG_ERROR("the tool configuration seems to have changed (number of systematics)!");
287  return StatusCode::FAILURE;
288  }
289  tree->GetEntry(entry);
290 
291  long index;
292  if(histname != 0)
293  {
294  auto itr = dictionary.find(histname);
295  if(itr == dictionary.end()) continue;
296  filledHistograms.emplace(histname);
297  const TH1* h = itr->second->first;
298  const auto& range = itr->second->second;
299  if(bin > (range.second - range.first))
300  {
301  ATH_MSG_ERROR("inconsistent binnings found for histogram " << h->GetName());
302  return StatusCode::FAILURE;
303  }
304  index = range.first + bin;
305  }
306  else
307  {
308  index = 0;
309  }
310 
311  FakeBkgTools::Yield yield;
312  yield.nominal = nominal;
313  yield.stat2.up = stat2Up;
314  yield.stat2.down = stat2Down;
315  for(unsigned j=0;j<n;++j)
316  {
318  u.up = systUp[j];
319  u.down= systDown[j];
320  auto emplaced = yield.uncertainties.emplace(systUID[j], u);
321  if(!emplaced.second) emplaced.first->second += u;
322  }
323  m_yields.at(index).add(yield);
324  }
325 
326  if(filledHistograms.size() != m_histogramYieldsRange.size())
327  {
328  ATH_MSG_ERROR("some of the registered histogram were not found in the input file " << m_progressFileName);
329  return StatusCode::FAILURE;
330  }
331 
332  m_progressFileName.clear();
333  return StatusCode::SUCCESS;
334 }
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:240
CP::BaseLinearFakeBkgTool::incrementTotalYield
StatusCode incrementTotalYield()
be sure to only call this once per event! (typically at the end of addEvent())
Definition: BaseLinearFakeBkgTool.cxx:104
CP::BaseFakeBkgTool::getCachedFinalState
FakeBkgTools::FinalState getCachedFinalState(uint8_t nparticles, const std::string &strPID, const std::string &strProc, bool &success)
Definition: BaseFakeBkgTool.cxx:219
FakeBkgTools::Uncertainty
Definition: FakeBkgInternals.h:27
CP::BaseFakeBkgTool
Definition: BaseFakeBkgTool.h:42
FakeBkgTools::Uncertainty::down
float down
Definition: FakeBkgInternals.h:28
athena.path
path
python interpreter configuration --------------------------------------—
Definition: athena.py:126
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:63
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:41
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:46
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:78
SUSY_SimplifiedModel_PostInclude.process
string process
Definition: SUSY_SimplifiedModel_PostInclude.py:42
Trk::u
@ u
Enums for curvilinear frames.
Definition: ParamDefs.h:83
AthenaPoolTestRead.sc
sc
Definition: AthenaPoolTestRead.py:27
dqt_zlumi_pandas.weight
int weight
Definition: dqt_zlumi_pandas.py:200
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:225
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:300
CP::BaseLinearFakeBkgTool::mergeSubjobs
StatusCode mergeSubjobs()
Definition: BaseLinearFakeBkgTool.cxx:229
CP::BaseLinearFakeBkgTool::~BaseLinearFakeBkgTool
virtual ~BaseLinearFakeBkgTool()
Definition: BaseLinearFakeBkgTool.cxx:37
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:135
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:142
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
PixelAthClusterMonAlgCfg.histname
histname
Definition: PixelAthClusterMonAlgCfg.py:106
lumiFormat.i
int i
Definition: lumiFormat.py:92
CP::BaseFakeBkgTool::m_initialized
bool m_initialized
Definition: BaseFakeBkgTool.h:78
h
FakeBkgTools
Definition: BaseFakeBkgTool.h:21
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
top::nominal
@ nominal
Definition: ScaleFactorRetriever.h:29
TH3
Definition: rootspy.cxx:440
CP::BaseLinearFakeBkgTool::assignYieldRange
StatusCode assignYieldRange(TH1 *h)
Definition: BaseLinearFakeBkgTool.cxx:149
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:195
file
TFile * file
Definition: tile_monitor.h:29
plotting.yearwise_efficiency.yval
float yval
Definition: yearwise_efficiency.py:43
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:270
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
plotting.yearwise_efficiency.xval
float xval
Definition: yearwise_efficiency.py:42
selection
std::string selection
Definition: fbtTestBasics.cxx:73
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
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:362
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:128
CP::BaseLinearFakeBkgTool::getEventWeightCommon
StatusCode getEventWeightCommon(FakeBkgTools::Weight &weight, const std::string &selection, const std::string &process)
Definition: BaseLinearFakeBkgTool.cxx:54
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:77
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:168
CP::BaseLinearFakeBkgTool::m_yields
std::vector< FakeBkgTools::Yield > m_yields
accumulated yield for all events (and histogram bins with uncertainties)
Definition: BaseLinearFakeBkgTool.h:68
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:72
FakeBkgTools::Efficiency::uncertainties
std::map< uint16_t, FakeBkgTools::Uncertainty > uncertainties
Definition: FakeBkgInternals.h:43