ATLAS Offline Software
Loading...
Searching...
No Matches
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"
18namespace CP
19{
20 class BaseFakeBkgTool;
21}
22
23namespace 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
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
61struct Weight : public Efficiency
62{
65 inline float syst() const;
66};
67
69struct 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
92
93inline constexpr uint8_t maxParticles() { return 6; }
94inline constexpr uint64_t maxCombinations() { return 1 << maxParticles(); }
95using FSBitset = std::bitset<maxCombinations()>;
96
98{
99public:
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; }
133private:
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
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
166inline 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
173inline 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
191inline 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
208inline 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
222inline Efficiency& Efficiency::multiply(const Efficiency& rhs, float weight)
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
240inline Efficiency& Efficiency::multiply(float weight)
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
279inline float Yield::stat() const
280{
281 return 0.5f*sqrt(stat2.up) + 0.5f*sqrt(stat2.down);
282}
283
284inline 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
constexpr int pow(int base, int exp) noexcept
Header file for AthHistogramAlgorithm.
bool operator<(const FinalState &rhs) const
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 ...
bool accept_process(uint8_t nparticles, const FSBitset &reals, const FSBitset &tights) const
void setSS(bool set=true)
void setOS(bool set=true)
bool parseSelection(const unsigned short nparticles, std::string strSelection, std::string &error)
bool parseProcess(std::string process, std::string &error)
FSBitset retrieveCharges(const std::vector< FakeBkgTools::ParticleData > &particles) const
STL class.
const std::string process
int r
Definition globals.cxx:22
int count(std::string s, const std::string &regx)
count how many occurances of a regx are in a string
Definition hcg.cxx:146
Select isolated Photons, Electrons and Muons.
constexpr uint64_t maxCombinations()
std::bitset< maxCombinations()> FSBitset
constexpr uint8_t maxParticles()
Efficiency FakeFactor
ObjectType
Type of objects that have a representation in the xAOD EDM.
Definition ObjectType.h:32
a structure to hold an efficiency together with a variable number of uncertainties
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...
std::map< uint16_t, FakeBkgTools::Uncertainty > uncertainties
Efficiency & subFromOne()
subFromOne() sets nominal and varied values to 1 - previous value.
Efficiency & add(const Efficiency &rhs, float weight=1.f)
key = source of uncertainty (ID), value = up/down
Efficiency & setToConst(float value=1.f)
setToConst() sets the nominal and all varied values to the same constant
float value(const CP::BaseFakeBkgTool *tool) const
xAOD::Type::ObjectType type
Uncertainty(float init_up, float init_down)
for older compilers
friend Uncertainty operator*(float lhs, const Uncertainty &rhs)
Uncertainty & operator*=(float rhs)
Uncertainty & operator+=(const Uncertainty &rhs)
for older compilers
Uncertainty & operator+(const Uncertainty &rhs)
a structure to hold a weight together with a variable number of systematic uncertainties
float syst() const
Helper function to extract the total systematic uncertainty from the 'uncertainties' field; it is com...
a structure to hold an event yield together with a statistical uncertainty and a variable number of s...
float syst() const
Helper function to extract the total systematic uncertainty from the 'uncertainties' field; it is com...
float stat() const
Helper function to extract the total statistical uncertainty from the 'uncertainties' field; it retur...
Yield & add(const Yield &rhs, float weight=1.f)
add() treats systematic uncertainties as correlated and updates (or creates) the total statistical un...