forked from UCATLAS/xAODAnaHelpers
/
ParticleContainer.h
231 lines (187 loc) · 6.09 KB
/
ParticleContainer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#ifndef xAODAnaHelpers_ParticleContainer_H
#define xAODAnaHelpers_ParticleContainer_H
#include <TTree.h>
#include <TLorentzVector.h>
#include <vector>
#include <string>
#include <xAODAnaHelpers/HelperClasses.h>
#include <xAODAnaHelpers/HelperFunctions.h>
#include <xAODAnaHelpers/Particle.h>
#include <xAODBase/IParticle.h>
namespace xAH {
template <class T_PARTICLE, class T_INFOSWITCH>
class ParticleContainer
{
public:
ParticleContainer(const std::string& name,
const std::string& detailStr="",
float units = 1e3,
bool mc = false,
bool useMass=false,
bool useTheS = true,
const std::string& suffix="")
: m_name(name),
m_infoSwitch(detailStr),
m_mc(mc),
m_debug(false),
m_units(units),
m_useMass(useMass),
m_useTheS(useTheS),
m_suffix(suffix)
{
m_n = 0;
// kinematic
m_pt =new std::vector<float>();
m_eta =new std::vector<float>();
m_phi =new std::vector<float>();
m_E =new std::vector<float>();
m_M =new std::vector<float>();
}
virtual ~ParticleContainer()
{
// kinematic
if(m_infoSwitch.m_kinematic){
delete m_pt;
delete m_eta;
delete m_phi;
delete m_E;
delete m_M;
}
}
virtual void setTree(TTree *tree)
{
std::string counterName = "n"+m_name;
if (!m_suffix.empty()) { counterName += "_" + m_suffix; }
if (m_useTheS) { counterName += "s"; }
tree->SetBranchStatus (counterName.c_str() , 1);
tree->SetBranchAddress (counterName.c_str() , &m_n);
if(m_infoSwitch.m_kinematic)
{
connectBranch<float>(tree,"pt" ,&m_pt);
connectBranch<float>(tree,"eta",&m_eta);
connectBranch<float>(tree,"phi",&m_phi);
if(m_useMass) connectBranch<float>(tree,"m" ,&m_M);
else connectBranch<float>(tree,"E" ,&m_E);
}
}
virtual void setBranches(TTree *tree)
{
std::string counterName = "n"+m_name;
if (!m_suffix.empty()) { counterName += "_" + m_suffix; }
if (m_useTheS) { counterName += "s"; }
tree->Branch(counterName.c_str(), &m_n, (counterName+"/I").c_str());
if(m_infoSwitch.m_kinematic) {
if(m_useMass) setBranch<float>(tree,"m", m_M );
else setBranch<float>(tree,"E", m_E );
setBranch<float>(tree,"pt", m_pt );
setBranch<float>(tree,"phi", m_phi );
setBranch<float>(tree,"eta", m_eta );
}
}
virtual void clear()
{
m_n = 0;
if(m_infoSwitch.m_kinematic) {
if(m_useMass) m_M->clear();
else m_E->clear();
m_pt ->clear();
m_phi ->clear();
m_eta ->clear();
}
}
virtual void FillParticle(const xAOD::IParticle* particle)
{
m_n++;
if( m_infoSwitch.m_kinematic ){
m_pt -> push_back ( particle->pt() / m_units );
m_eta -> push_back( particle->eta() );
m_phi -> push_back( particle->phi() );
if(m_useMass) m_M->push_back ( particle->m() / m_units );
else m_E->push_back ( particle->e() / m_units );
}
}
void updateEntry()
{
m_particles.clear();
for(int i=0;i<m_n;i++)
{
T_PARTICLE particle;
updateParticle(i,particle);
m_particles.push_back(particle);
}
}
T_PARTICLE& at_nonConst(uint idx)
{ return m_particles[idx]; }
const T_PARTICLE& at(uint idx) const
{ return m_particles[idx]; }
const T_PARTICLE& operator[](uint idx) const
{ return m_particles[idx]; }
uint size() const
{ return m_particles.size(); }
protected:
template <typename T_BR> void connectBranch(TTree *tree, const std::string& branch, std::vector<T_BR> **variable)
{
std::string name = m_name + "_" + branch;
if (! m_suffix.empty()) { name += "_" + m_suffix; }
tree->SetBranchStatus (name.c_str() , 1);
tree->SetBranchAddress (name.c_str() , variable);
}
template<typename T> void setBranch(TTree* tree, std::string varName, std::vector<T>* localVectorPtr){
std::string name = m_name + "_" + varName;
if (! m_suffix.empty()) { name += "_" + m_suffix; }
tree->Branch(name.c_str(), localVectorPtr);
}
template<typename T, typename U, typename V> void safeFill(const V* xAODObj, SG::AuxElement::ConstAccessor<T>& accessor, std::vector<U>* destination, U defaultValue, int units = 1){
if ( accessor.isAvailable( *xAODObj ) ) {
destination->push_back( accessor( *xAODObj ) / units );
} else {
destination->push_back( defaultValue );
}
}
template<typename T, typename U, typename V> void safeVecFill(const V* xAODObj, SG::AuxElement::ConstAccessor<std::vector<T> >& accessor, std::vector<std::vector<U> >* destination, int units = 1){
destination->push_back( std::vector<U>() );
if ( accessor.isAvailable( *xAODObj ) ) {
for(U itemInVec : accessor(*xAODObj)) destination->back().push_back(itemInVec / units);
}
return;
}
virtual void updateParticle(uint idx, T_PARTICLE& particle)
{
if(m_infoSwitch.m_kinematic)
{
if(m_useMass){
particle.p4.SetPtEtaPhiM(m_pt ->at(idx),
m_eta->at(idx),
m_phi->at(idx),
m_M ->at(idx));
} else{
particle.p4.SetPtEtaPhiE(m_pt ->at(idx),
m_eta->at(idx),
m_phi->at(idx),
m_E ->at(idx));
}
}
}
std::string m_name;
std::vector<T_PARTICLE> m_particles;
public:
T_INFOSWITCH m_infoSwitch;
bool m_mc;
bool m_debug;
float m_units;
int m_n;
private:
bool m_useMass;
bool m_useTheS;
std::string m_suffix;
//
// Vector branches
// kinematic
std::vector<float> *m_pt;
std::vector<float> *m_eta;
std::vector<float> *m_phi;
std::vector<float> *m_E;
std::vector<float> *m_M;
};
}//xAH
#endif // xAODAnaHelpers_ParticleContainer_H