ATLAS Offline Software
FakeBkgInternals.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #ifndef FAKEBKGTOOLS_FAKEBKGINTERNALS_H
6 #define FAKEBKGTOOLS_FAKEBKGINTERNALS_H
7 
8 #include <string>
9 #include <vector>
10 #include <bitset>
11 #include <cmath>
12 #include <functional>
13 #include <memory>
14 #include <map>
15 #include <iostream>
16 #include "TBuffer.h"
17 #include "xAODBase/IParticle.h"
18 namespace CP
19 {
20  class BaseFakeBkgTool;
21 }
22 
23 namespace FakeBkgTools
24 {
25 
27 {
28  float up = 0, down = 0;
30  Uncertainty(float init_up, float init_down) : up(init_up), down(init_down) {}
31  inline Uncertainty& operator+=(const Uncertainty& rhs);
32  inline Uncertainty& operator+(const Uncertainty& rhs);
33  inline Uncertainty& operator*=(float rhs);
34  inline Uncertainty& operator*=(const Uncertainty& rhs);
35  friend Uncertainty operator*(float lhs, const Uncertainty& rhs) { return {lhs*rhs.up, lhs*rhs.down}; }
36 };
37 
39 struct Efficiency
40 {
41  float value(const CP::BaseFakeBkgTool* tool) const;
42  float nominal = 0;
43  std::map<uint16_t, FakeBkgTools::Uncertainty> uncertainties;
44 
46  inline Efficiency& add(const Efficiency& rhs, float weight = 1.f);
50  inline Efficiency& multiply(const Efficiency& rhs, float weight = 1.f);
51  inline Efficiency& multiply(float weight);
53  inline Efficiency& setToConst(float value = 1.f);
55  inline Efficiency& subFromOne();
56 };
57 
59 
61 struct Weight : public Efficiency
62 {
65  inline float syst() const;
66 };
67 
69 struct Yield : public Efficiency
70 {
73  inline Yield& add(const Yield& rhs, float weight = 1.f);
74  inline Yield& add(const Weight& rhs, float weight = 1.f);
77  inline float stat() const;
80  inline float syst() const;
81 };
82 
83 
85 {
88  bool tight;
89  int8_t charge;
91 };
92 
93 inline constexpr uint8_t maxParticles() { return 6; }
94 inline constexpr uint64_t maxCombinations() { return 1 << maxParticles(); }
95 using FSBitset = std::bitset<maxCombinations()>;
96 
98 {
99 public:
101  FinalState(size_t h) : m_hash(h) {} // for search by hash
102  FinalState(size_t h, const unsigned nparticles, const std::string& strSelection, const std::string& strProc, std::string& error); // main constructor
104  bool accept_selection(const FSBitset& tights, const FSBitset& charges) const
105  {
106  if(!selection[tights.to_ulong()]) return false;
107  auto nT = tights.count();
108  if(hasSS() && nT==2 && (tights&charges).count()==1) return false;
109  if(hasOS() && nT>=2 && ((tights&charges).count()%nT)==0) return false;
110  return true;
111  }
112  bool accept_process(uint8_t nparticles, const FSBitset& reals, const FSBitset& tights) const
113  {
114  auto nT = tights.count();
115  auto nR = reals.count(), nRT = (reals&tights).count(), nRL = nR - nRT;
116  auto nF = nparticles - nR, nFT = nT - nRT, nFL = nF - nFT;
117  uint32_t w = nRT | (nRL<<5) | (nR<<10) | (nFT<<15) | (nFL<<20) | (nF<<25);
118  if((m_wmax-w)&0x21084210 || (w-m_wmin)&0x21084210) return false;
119  return true;
120  }
121  FSBitset retrieveCharges(const std::vector<FakeBkgTools::ParticleData>& particles) const
122  {
123  FSBitset charges{0};
124  for(unsigned i=0;i<particles.size();++i) charges.set(i, particles[i].charge>0);
125  return charges;
126  }
127  bool hasSS() const { return m_wmin & 0x40000000; }
128  bool hasOS() const { return m_wmax & 0x40000000; }
129  void setSS(bool set = true) { m_wmin = set? (m_wmin|0x40000000) : m_wmin&~0x40000000; }
130  void setOS(bool set = true) { m_wmax = set? (m_wmax|0x40000000) : m_wmax&~0x40000000; }
131  bool hasChargeRequirement() const { return hasOS() || hasSS(); }
132  bool operator<(const FinalState& rhs) const { return m_hash < rhs.m_hash; }
133 private:
134  size_t m_hash;
135  uint32_t m_wmin = 0x0, m_wmax=0x3FFFFFFF;
136  bool parseProcess(std::string process, std::string& error);
137  bool parseSelection(const unsigned short nparticles, std::string strSelection, std::string& error);
138 };
139 
140 enum class Client
141 {
143 };
144 
146 {
147  up += rhs.up;
148  down += rhs.down;
149  return *this;
150 }
151 
153 {
154  up *= rhs.up;
155  down *= rhs.down;
156  return *this;
157 }
158 
160 {
161  up *= rhs;
162  down *= rhs;
163  return *this;
164 }
165 
166 inline float Weight::syst() const
167 {
168  double syst2 = 0;
169  for(auto& kv : uncertainties) syst2 += std::pow(0.5*(kv.second.up+kv.second.down), 2);
170  return sqrt(syst2);
171 }
172 
173 inline Yield& Yield::add(const Weight& rhs, float weight)
174 {
176  for(auto& kv : rhs.uncertainties)
177  {
178  auto r = uncertainties.emplace(kv);
179  if(r.second) r.first->second *= weight;
180  else r.first->second += weight * kv.second;
181  }
183  float ds2 = pow(weight * rhs.nominal, 2);
184  stat2.up += ds2;
185  stat2.down += ds2;
187  nominal += weight * rhs.nominal;
188  return *this;
189 }
190 
191 inline Yield& Yield::add(const Yield& rhs, float weight)
192 {
194  for(auto& kv : rhs.uncertainties)
195  {
196  auto r = uncertainties.emplace(kv);
197  if(r.second) r.first->second *= weight;
198  else r.first->second += weight * kv.second;
199  }
201  stat2.up += weight * weight * rhs.stat2.up;
202  stat2.down += weight * weight * rhs.stat2.down;
204  nominal += weight * rhs.nominal;
205  return *this;
206 }
207 
208 inline Efficiency& Efficiency::add(const Efficiency& rhs, float weight)
209 {
211  for(auto& kv : rhs.uncertainties)
212  {
213  auto r = uncertainties.emplace(kv);
214  if(r.second) r.first->second *= weight;
215  else r.first->second += weight * kv.second;
216  }
218  nominal += weight * rhs.nominal;
219  return *this;
220 }
221 
223 {
225  for(auto& kv : rhs.uncertainties)
226  {
227  auto r = uncertainties.emplace(kv);
228  if(!r.second) {
229  Uncertainty u_init = r.first->second;
230  r.first->second = nominal*kv.second;
231  r.first->second += (rhs.nominal*u_init);
232  r.first->second += (u_init*=kv.second);
233  }
234  }
236  nominal *= weight * rhs.nominal;
237  return *this;
238 }
239 
241 {
243  for(auto& kv : uncertainties)
244  {
245  kv.second*= weight;
246  }
248  nominal *= weight;
249  return *this;
250 }
251 
253 {
255  for(auto& kv : uncertainties)
256  {
257  kv.second.up = 0.;
258  kv.second.down = 0.;
259  }
261  nominal = val;
262  return *this;
263 }
264 
266 {
268  for(auto& kv : uncertainties)
269  {
270  kv.second.up = -kv.second.up;
271  kv.second.down = -kv.second.down;
272  }
274  nominal = 1.-nominal;;
275  return *this;
276 }
277 
278 
279 inline float Yield::stat() const
280 {
281  return 0.5f*sqrt(stat2.up) + 0.5f*sqrt(stat2.down);
282 }
283 
284 inline float Yield::syst() const
285 {
286  double syst2 = 0;
287  for(auto& kv : uncertainties) syst2 += std::pow(0.5*(kv.second.up+kv.second.down), 2);
288  return sqrt(syst2);
289 }
290 
291 }
292 
293 #endif
beamspotman.r
def r
Definition: beamspotman.py:676
FakeBkgTools::ParticleData::fake_factor
FakeFactor fake_factor
Definition: FakeBkgInternals.h:87
FakeBkgTools::FinalState::hasChargeRequirement
bool hasChargeRequirement() const
Definition: FakeBkgInternals.h:131
FakeBkgTools::Uncertainty
Definition: FakeBkgInternals.h:27
CP::BaseFakeBkgTool
Definition: BaseFakeBkgTool.h:41
FakeBkgTools::Uncertainty::down
float down
Definition: FakeBkgInternals.h:28
FakeBkgTools::FinalState::retrieveCharges
FSBitset retrieveCharges(const std::vector< FakeBkgTools::ParticleData > &particles) const
Definition: FakeBkgInternals.h:121
FakeBkgTools::Client::E2Y_FAKE
@ E2Y_FAKE
FakeBkgTools::FinalState::hasSS
bool hasSS() const
Definition: FakeBkgInternals.h:127
FakeBkgTools::FinalState::selection
FSBitset selection
Definition: FakeBkgInternals.h:100
IParticle.h
FakeBkgTools::Efficiency::add
Efficiency & add(const Efficiency &rhs, float weight=1.f)
key = source of uncertainty (ID), value = up/down
Definition: FakeBkgInternals.h:208
xAOD::uint8_t
uint8_t
Definition: Muon_v1.cxx:557
FakeBkgTools::Efficiency::multiply
Efficiency & multiply(const Efficiency &rhs, float weight=1.f)
the first version of multiply() takes the product of two Efficiencies, setting the up and down variat...
Definition: FakeBkgInternals.h:222
FakeBkgTools::Efficiency
a structure to hold an efficiency together with a variable number of uncertainties
Definition: FakeBkgInternals.h:40
ObjectType
ObjectType
Definition: BaseObject.h:11
xAOD::uint32_t
setEventNumber uint32_t
Definition: EventInfo_v1.cxx:127
FakeBkgTools::Client::FAKE_FACTOR
@ FAKE_FACTOR
FakeBkgTools::Yield::add
Yield & add(const Yield &rhs, float weight=1.f)
add() treats systematic uncertainties as correlated and updates (or creates) the total statistical un...
Definition: FakeBkgInternals.h:191
FakeBkgTools::FinalState::parseProcess
bool parseProcess(std::string process, std::string &error)
Definition: FakeBkgInternals.cxx:38
conifer::pow
constexpr int pow(int x)
Definition: conifer.h:20
FakeBkgTools::FinalState::parseSelection
bool parseSelection(const unsigned short nparticles, std::string strSelection, std::string &error)
Definition: FakeBkgInternals.cxx:105
FakeBkgTools::Client
Client
Definition: FakeBkgInternals.h:141
FakeBkgTools::Efficiency::value
float value(const CP::BaseFakeBkgTool *tool) const
Definition: FakeBkgInternals.cxx:17
FakeBkgTools::ParticleData
Definition: FakeBkgInternals.h:85
FakeBkgTools::ParticleData::tight
bool tight
Definition: FakeBkgInternals.h:88
FakeBkgTools::Weight::syst
float syst() const
Helper function to extract the total systematic uncertainty from the 'uncertainties' field; it is com...
Definition: FakeBkgInternals.h:166
FakeBkgTools::Client::MATRIX_METHOD
@ MATRIX_METHOD
CP
Select isolated Photons, Electrons and Muons.
Definition: Control/xAODRootAccess/xAODRootAccess/TEvent.h:48
SUSY_SimplifiedModel_PostInclude.process
string process
Definition: SUSY_SimplifiedModel_PostInclude.py:42
XMLtoHeader.count
count
Definition: XMLtoHeader.py:85
FakeBkgTools::FinalState::setOS
void setOS(bool set=true)
Definition: FakeBkgInternals.h:130
dqt_zlumi_pandas.weight
int weight
Definition: dqt_zlumi_pandas.py:189
FakeBkgTools::Uncertainty::operator+
Uncertainty & operator+(const Uncertainty &rhs)
FakeBkgTools::FinalState::m_wmin
uint32_t m_wmin
Definition: FakeBkgInternals.h:135
FakeBkgTools::Uncertainty::operator+=
Uncertainty & operator+=(const Uncertainty &rhs)
for older compilers
Definition: FakeBkgInternals.h:145
FakeBkgTools::FinalState::m_hash
size_t m_hash
Definition: FakeBkgInternals.h:134
lumiFormat.i
int i
Definition: lumiFormat.py:85
FakeBkgTools
Definition: BaseFakeBkgTool.h:23
FakeBkgTools::maxCombinations
constexpr uint64_t maxCombinations()
Definition: FakeBkgInternals.h:94
xAOD::uint64_t
uint64_t
Definition: EventInfo_v1.cxx:123
hist_file_dump.f
f
Definition: hist_file_dump.py:135
FakeBkgTools::ParticleData::type
xAOD::Type::ObjectType type
Definition: FakeBkgInternals.h:90
FakeBkgTools::FinalState::m_wmax
uint32_t m_wmax
Definition: FakeBkgInternals.h:135
CxxUtils::set
constexpr std::enable_if_t< is_bitmask_v< E >, E & > set(E &lhs, E rhs)
Convenience function to set bits in a class enum bitmask.
Definition: bitmask.h:232
FakeBkgTools::Efficiency::setToConst
Efficiency & setToConst(float value=1.f)
setToConst() sets the nominal and all varied values to the same constant
Definition: FakeBkgInternals.h:252
FakeBkgTools::Yield::syst
float syst() const
Helper function to extract the total systematic uncertainty from the 'uncertainties' field; it is com...
Definition: FakeBkgInternals.h:284
FakeBkgTools::FinalState::accept_selection
bool accept_selection(const FSBitset &tights, const FSBitset &charges) const
Important: the accept() function doesn't check the selection (for speed reason), this has to be done ...
Definition: FakeBkgInternals.h:104
FakeBkgTools::Uncertainty::operator*
friend Uncertainty operator*(float lhs, const Uncertainty &rhs)
Definition: FakeBkgInternals.h:35
WriteCellNoiseToCool.nF
nF
Definition: WriteCellNoiseToCool.py:541
AtlCoolConsole.tool
tool
Definition: AtlCoolConsole.py:453
FakeBkgTools::maxParticles
constexpr uint8_t maxParticles()
Definition: FakeBkgInternals.h:93
FakeBkgTools::Weight
a structure to hold a weight together with a variable number of systematic uncertainties
Definition: FakeBkgInternals.h:62
FakeBkgTools::ParticleData::real_efficiency
Efficiency real_efficiency
Definition: FakeBkgInternals.h:86
FakeBkgTools::Client::NONE
@ NONE
FakeBkgTools::FinalState
Definition: FakeBkgInternals.h:98
FakeBkgTools::Efficiency::nominal
float nominal
Definition: FakeBkgInternals.h:42
FakeBkgTools::FinalState::operator<
bool operator<(const FinalState &rhs) const
Definition: FakeBkgInternals.h:132
FakeBkgTools::Uncertainty::up
float up
Definition: FakeBkgInternals.h:28
FakeBkgTools::Efficiency::subFromOne
Efficiency & subFromOne()
subFromOne() sets nominal and varied values to 1 - previous value.
Definition: FakeBkgInternals.h:265
FakeBkgTools::Yield
a structure to hold an event yield together with a statistical uncertainty and a variable number of s...
Definition: FakeBkgInternals.h:70
FakeBkgTools::FinalState::accept_process
bool accept_process(uint8_t nparticles, const FSBitset &reals, const FSBitset &tights) const
Definition: FakeBkgInternals.h:112
FakeBkgTools::ParticleData::charge
int8_t charge
Definition: FakeBkgInternals.h:89
h
FakeBkgTools::Client::ALL_METHODS
@ ALL_METHODS
Pythia8_RapidityOrderMPI.val
val
Definition: Pythia8_RapidityOrderMPI.py:14
FakeBkgTools::Yield::stat2
Uncertainty stat2
Definition: FakeBkgInternals.h:71
FakeBkgTools::Yield::stat
float stat() const
Helper function to extract the total statistical uncertainty from the 'uncertainties' field; it retur...
Definition: FakeBkgInternals.h:279
LArG4FSStartPointFilter.particles
list particles
Definition: LArG4FSStartPointFilter.py:84
FakeBkgTools::Uncertainty::Uncertainty
Uncertainty(float init_up, float init_down)
for older compilers
Definition: FakeBkgInternals.h:30
FakeBkgTools::FinalState::FinalState
FinalState(size_t h)
Definition: FakeBkgInternals.h:101
FakeBkgTools::ParticleData::fake_efficiency
Efficiency fake_efficiency
Definition: FakeBkgInternals.h:86
FakeBkgTools::FinalState::hasOS
bool hasOS() const
Definition: FakeBkgInternals.h:128
python.IoTestsLib.w
def w
Definition: IoTestsLib.py:200
FakeBkgTools::Uncertainty::Uncertainty
Uncertainty()
Definition: FakeBkgInternals.h:29
FakeBkgTools::FSBitset
std::bitset< maxCombinations()> FSBitset
Definition: FakeBkgInternals.h:95
error
Definition: IImpactPoint3dEstimator.h:70
FakeBkgTools::Uncertainty::operator*=
Uncertainty & operator*=(float rhs)
Definition: FakeBkgInternals.h:159
FakeBkgTools::Efficiency::uncertainties
std::map< uint16_t, FakeBkgTools::Uncertainty > uncertainties
Definition: FakeBkgInternals.h:43
FakeBkgTools::FinalState::setSS
void setSS(bool set=true)
Definition: FakeBkgInternals.h:129