cepgen
is hosted by
Hepforge
,
IPPP Durham
CepGen
1.2.5
Central exclusive processes event generator
Loading...
Searching...
No Matches
ROOTTreeInfo.cpp
Go to the documentation of this file.
1
/*
2
* CepGen: a central exclusive processes event generator
3
* Copyright (C) 2019-2024 Laurent Forthomme
4
*
5
* This program is free software: you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation, either version 3 of the License, or
8
* any later version.
9
*
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
14
*
15
* You should have received a copy of the GNU General Public License
16
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17
*/
18
19
#include "
CepGen/Core/Exception.h
"
20
#include "
CepGenAddOns/ROOTWrapper/ROOTTreeInfo.h
"
21
22
namespace
ROOT
{
23
CepGenRun::CepGenRun
() {
clear
(); }
24
25
CepGenRun
CepGenRun::load
(TFile* file,
const
std::string& run_tree) {
26
CepGenRun
run;
27
run.
attach
(file, run_tree.data());
28
return
run;
29
}
30
31
CepGenRun
CepGenRun::load
(
const
std::string& filename,
const
std::string& run_tree) {
32
CepGenRun
run;
33
run.
attach
(filename.data(), run_tree.data());
34
return
run;
35
}
36
37
void
CepGenRun::clear
() {
38
sqrt_s
= -1.;
39
xsect
=
errxsect
= -1.;
40
num_events
=
litigious_events
= 0;
41
process_name
.clear();
42
process_parameters
.clear();
43
}
44
45
void
CepGenRun::create
() {
46
tree_ = std::make_shared<TTree>(
TREE_NAME
,
"a tree containing information on the previous run"
);
47
if
(!tree_)
48
throw
CG_FATAL
(
"CepGenRun:create"
) <<
"Failed to create the run TTree!"
;
49
tree_->Branch(
"xsect"
, &
xsect
,
"xsect/D"
);
50
tree_->Branch(
"errxsect"
, &
errxsect
,
"errxsect/D"
);
51
tree_->Branch(
"num_events"
, &
num_events
,
"num_events/i"
);
52
tree_->Branch(
"litigious_events"
, &
litigious_events
,
"litigious_events/i"
);
53
tree_->Branch(
"sqrt_s"
, &
sqrt_s
,
"sqrt_s/D"
);
54
tree_->Branch(
"process_name"
, &
process_name
);
55
tree_->Branch(
"process_parameters"
, &
process_parameters
);
56
}
57
58
void
CepGenRun::fill
() {
59
if
(!tree_)
60
throw
CG_FATAL
(
"CepGenRun:fill"
) <<
"Trying to fill a non-existent tree!"
;
61
tree_->Fill();
62
}
63
64
void
CepGenRun::attach
(TFile* file,
const
std::string& run_tree) {
65
//--- special constructor to avoid the memory to be cleared at destruction time
66
tree_ = std::shared_ptr<TTree>(
dynamic_cast<
TTree*
>
(file->Get(run_tree.data())), [=](TTree*) {});
67
if
(!tree_)
68
throw
CG_FATAL
(
"CepGenRun:attach"
) <<
"Failed to attach to the run TTree!"
;
69
tree_->SetBranchAddress(
"xsect"
, &
xsect
);
70
tree_->SetBranchAddress(
"errxsect"
, &
errxsect
);
71
tree_->SetBranchAddress(
"num_events"
, &
num_events
);
72
tree_->SetBranchAddress(
"litigious_events"
, &
litigious_events
);
73
tree_->SetBranchAddress(
"sqrt_s"
, &
sqrt_s
);
74
auto
*process_name_view =
new
std::string(), *process_params_view =
new
std::string();
75
tree_->SetBranchAddress(
"process_name"
, &process_name_view);
76
tree_->SetBranchAddress(
"process_parameters"
, &process_params_view);
77
if
(
const
auto
num_entries = tree_->GetEntriesFast(); num_entries != 1) {
78
if
(num_entries <= 0) {
79
CG_ERROR
(
"CepGenRun:attach"
) <<
"No entries retrieved from the run tree. Aborting the 'attach' method."
;
80
return
;
81
}
82
CG_WARNING
(
"CepGenRun:attach"
) <<
"The run tree has more than one entry. Number of entries retrieved: "
83
<< num_entries <<
"."
;
84
}
85
tree_->GetEntry(0);
86
process_name
= *process_name_view;
87
process_parameters
= *process_params_view;
88
}
89
90
CepGenEvent
CepGenEvent::load
(TFile* file,
const
std::string& evt_tree) {
91
CepGenEvent
evt;
92
evt.
attach
(file, evt_tree.data());
93
return
evt;
94
}
95
96
CepGenEvent
CepGenEvent::load
(
const
std::string& filename,
const
std::string& evt_tree) {
97
CepGenEvent
evt;
98
evt.
attach
(filename.data(), evt_tree.data());
99
return
evt;
100
}
101
102
void
CepGenEvent::clear
() {
103
gen_time
=
tot_time
= 0.;
104
np
= 0;
105
for
(
size_t
i = 0; i <
MAX_PART
; ++i) {
106
pt
[i] =
eta
[i] =
phi
[i] =
rapidity
[i] =
E
[i] =
m
[i] =
charge
[i] = 0.;
107
pdg_id
[i] =
parent1
[i] =
parent2
[i] =
stable
[i] =
role
[i] =
status
[i] = 0;
108
}
109
}
110
111
void
CepGenEvent::create
() {
112
tree_ = std::make_shared<TTree>(
TREE_NAME
,
"a tree containing information on events generated in previous run"
);
113
if
(!tree_)
114
throw
CG_FATAL
(
"CepGenEvent:create"
) <<
"Failed to create the events TTree!"
;
115
tree_->Branch(
"npart"
, &
np
,
"npart/I"
);
116
tree_->Branch(
"role"
,
role
,
"role[npart]/I"
);
117
tree_->Branch(
"pt"
,
pt
,
"pt[npart]/D"
);
118
tree_->Branch(
"eta"
,
eta
,
"eta[npart]/D"
);
119
tree_->Branch(
"phi"
,
phi
,
"phi[npart]/D"
);
120
tree_->Branch(
"rapidity"
,
rapidity
,
"rapidity[npart]/D"
);
121
tree_->Branch(
"E"
,
E
,
"E[npart]/D"
);
122
tree_->Branch(
"m"
,
m
,
"m[npart]/D"
);
123
tree_->Branch(
"charge"
,
charge
,
"charge[npart]/D"
);
124
tree_->Branch(
"pdg_id"
,
pdg_id
,
"pdg_id[npart]/I"
);
125
tree_->Branch(
"parent1"
,
parent1
,
"parent1[npart]/I"
);
126
tree_->Branch(
"parent2"
,
parent2
,
"parent2[npart]/I"
);
127
tree_->Branch(
"stable"
,
stable
,
"stable[npart]/I"
);
128
tree_->Branch(
"status"
,
status
,
"status[npart]/I"
);
129
tree_->Branch(
"weight"
, &
weight
,
"weight/F"
);
130
tree_->Branch(
"generation_time"
, &
gen_time
,
"generation_time/F"
);
131
tree_->Branch(
"total_time"
, &
tot_time
,
"total_time/F"
);
132
tree_->Branch(
"metadata"
, &
metadata
);
133
}
134
135
void
CepGenEvent::attach
() {
136
if
(!tree_)
137
throw
CG_FATAL
(
"CepGenEvent:attach"
) <<
"Failed to attach to the events TTree!"
;
138
tree_->SetBranchAddress(
"npart"
, &
np
);
139
tree_->SetBranchAddress(
"role"
,
role
);
140
tree_->SetBranchAddress(
"pt"
,
pt
);
141
tree_->SetBranchAddress(
"eta"
,
eta
);
142
tree_->SetBranchAddress(
"phi"
,
phi
);
143
tree_->SetBranchAddress(
"rapidity"
,
rapidity
);
144
tree_->SetBranchAddress(
"E"
,
E
);
145
tree_->SetBranchAddress(
"m"
,
m
);
146
tree_->SetBranchAddress(
"charge"
,
charge
);
147
tree_->SetBranchAddress(
"pdg_id"
,
pdg_id
);
148
tree_->SetBranchAddress(
"parent1"
,
parent1
);
149
tree_->SetBranchAddress(
"parent2"
,
parent2
);
150
tree_->SetBranchAddress(
"stable"
,
stable
);
151
tree_->SetBranchAddress(
"status"
,
status
);
152
tree_->SetBranchAddress(
"weight"
, &
weight
);
153
tree_->SetBranchAddress(
"generation_time"
, &
gen_time
);
154
tree_->SetBranchAddress(
"total_time"
, &
tot_time
);
155
tree_->SetBranchAddress(
"metadata"
, &
metadata
);
156
tree_attached_ =
true
;
157
}
158
159
void
CepGenEvent::fill
(
const
cepgen::Event
& ev,
bool
compress) {
160
if
(!tree_)
161
throw
CG_FATAL
(
"CepGenEvent:fill"
) <<
"Trying to fill a non-existent tree!"
;
162
163
clear
();
164
gen_time
= ev.
metadata
(
"time:generation"
);
165
tot_time
= ev.
metadata
(
"time:total"
);
166
weight
= ev.
metadata
(
"weight"
);
167
np
= 0;
168
const
auto
& parts = compress ? ev.
compress
().
particles
() : ev.
particles
();
169
//--- loop over all particles in event
170
for
(
const
auto
& part : parts) {
171
const
auto
& mom = part.momentum();
172
rapidity
[
np
] = mom.rapidity();
173
pt
[
np
] = mom.pt();
174
eta
[
np
] = mom.eta();
175
phi
[
np
] = mom.phi();
176
E
[
np
] = mom.energy();
177
m
[
np
] = mom.mass();
178
pdg_id
[
np
] = part.integerPdgId();
179
parent1
[
np
] = (part.mothers().size() > 0) ? *part.mothers().begin() : -1;
180
parent2
[
np
] = (part.mothers().size() > 1) ? *part.mothers().rbegin() : -1;
181
status
[
np
] = (int)part.status();
182
stable
[
np
] = ((short)part.status() > 0);
183
charge
[
np
] = part.charge();
184
role
[
np
] = part.role();
185
np
++;
186
}
187
metadata
= ev.
metadata
;
188
tree_->Fill();
189
clear
();
190
}
191
192
void
CepGenEvent::attach
(TFile* f,
const
std::string& events_tree) {
193
//--- special constructor to avoid the memory to be cleared at destruction time
194
tree_ = std::shared_ptr<TTree>(
dynamic_cast<
TTree*
>
(f->Get(events_tree.data())), [=](TTree*) {});
195
attach
();
196
}
197
198
void
CepGenEvent::attach
(
const
std::string& filename,
const
std::string& events_tree) {
199
file_.reset(TFile::Open(filename.data()));
200
attach
(file_.get(), events_tree);
201
}
202
203
bool
CepGenEvent::next
(
cepgen::Event
& ev) {
204
if
(!tree_attached_)
205
attach
();
206
if
(tree_->GetEntry(num_read_events_++) <= 0)
207
return
false
;
208
ev.
clear
();
209
ev.
metadata
[
"time:generation"
] =
gen_time
;
210
ev.
metadata
[
"time:total"
] =
tot_time
;
211
ev.
metadata
[
"weight"
] =
weight
;
212
//--- first loop to populate the particles content
213
for
(
unsigned
short
i = 0; i <
np
; ++i) {
214
cepgen::Particle
part;
215
part.
setRole
((
cepgen::Particle::Role
)
role
[i]);
216
part.
setPdgId
((
long
)
pdg_id
[i]);
217
part.
setStatus
((
cepgen::Particle::Status
)
status
[i]);
218
part.
setMomentum
(
cepgen::Momentum::fromPtEtaPhiE
(
pt
[i],
eta
[i],
phi
[i],
E
[i]));
219
ev.
addParticle
(part);
220
}
221
//--- second loop to associate the parentage
222
for
(
unsigned
short
i = 0; i <
np
; ++i) {
223
auto
& part = ev[i];
224
if
(
parent1
[i] > 0)
225
part.addMother(ev[
parent1
[i]]);
226
if
(
parent2
[i] >
parent1
[i])
227
for
(
unsigned
short
j =
parent1
[i] + 1; j <=
parent2
[i]; ++j)
228
part.addMother(ev[j]);
229
}
230
return
true
;
231
}
232
}
// namespace ROOT
Exception.h
CG_FATAL
#define CG_FATAL(mod)
Definition
Exception.h:61
CG_ERROR
#define CG_ERROR(mod)
Definition
Exception.h:60
CG_WARNING
#define CG_WARNING(mod)
Definition
Message.h:228
ROOTTreeInfo.h
ROOT::CepGenEvent
All useful information about a generated event.
Definition
ROOTTreeInfo.h:60
ROOT::CepGenEvent::m
double m[MAX_PART]
Particles mass, in GeV/c .
Definition
ROOTTreeInfo.h:81
ROOT::CepGenEvent::load
static CepGenEvent load(TFile *, const std::string &events_tree=TREE_NAME)
Definition
ROOTTreeInfo.cpp:90
ROOT::CepGenEvent::rapidity
double rapidity[MAX_PART]
Particles rapidity.
Definition
ROOTTreeInfo.h:79
ROOT::CepGenEvent::charge
double charge[MAX_PART]
Particles charges, in e.
Definition
ROOTTreeInfo.h:82
ROOT::CepGenEvent::next
bool next(cepgen::Event &)
Read the next event in the file.
Definition
ROOTTreeInfo.cpp:203
ROOT::CepGenEvent::eta
double eta[MAX_PART]
Particles pseudo-rapidity.
Definition
ROOTTreeInfo.h:77
ROOT::CepGenEvent::parent1
int parent1[MAX_PART]
First particles mother.
Definition
ROOTTreeInfo.h:84
ROOT::CepGenEvent::fill
void fill(const cepgen::Event &, bool compress=false)
Fill the tree with a new event.
Definition
ROOTTreeInfo.cpp:159
ROOT::CepGenEvent::metadata
cepgen::Event::EventMetadata metadata
Definition
ROOTTreeInfo.h:71
ROOT::CepGenEvent::status
int status[MAX_PART]
Integer status code.
Definition
ROOTTreeInfo.h:88
ROOT::CepGenEvent::phi
double phi[MAX_PART]
Particles azimuthal angle.
Definition
ROOTTreeInfo.h:78
ROOT::CepGenEvent::weight
float weight
Event weight.
Definition
ROOTTreeInfo.h:74
ROOT::CepGenEvent::E
double E[MAX_PART]
Particles energy, in GeV.
Definition
ROOTTreeInfo.h:80
ROOT::CepGenEvent::np
int np
Number of particles in the event.
Definition
ROOTTreeInfo.h:75
ROOT::CepGenEvent::gen_time
float gen_time
Event generation time.
Definition
ROOTTreeInfo.h:72
ROOT::CepGenEvent::tot_time
float tot_time
Total event generation time.
Definition
ROOTTreeInfo.h:73
ROOT::CepGenEvent::parent2
int parent2[MAX_PART]
Last particles mother.
Definition
ROOTTreeInfo.h:85
ROOT::CepGenEvent::role
int role[MAX_PART]
Particles role in the event.
Definition
ROOTTreeInfo.h:87
ROOT::CepGenEvent::MAX_PART
static constexpr size_t MAX_PART
Maximal number of particles in event.
Definition
ROOTTreeInfo.h:65
ROOT::CepGenEvent::attach
void attach()
Attach the event tree reader to a tree.
Definition
ROOTTreeInfo.cpp:135
ROOT::CepGenEvent::pdg_id
int pdg_id[MAX_PART]
Integer particles PDG id.
Definition
ROOTTreeInfo.h:83
ROOT::CepGenEvent::clear
void clear()
Reinitialise the event content.
Definition
ROOTTreeInfo.cpp:102
ROOT::CepGenEvent::create
void create()
Populate the tree and all associated branches.
Definition
ROOTTreeInfo.cpp:111
ROOT::CepGenEvent::TREE_NAME
static constexpr const char * TREE_NAME
Output tree name.
Definition
ROOTTreeInfo.h:66
ROOT::CepGenEvent::stable
int stable[MAX_PART]
Whether the particle must decay or not.
Definition
ROOTTreeInfo.h:86
ROOT::CepGenEvent::pt
double pt[MAX_PART]
Particles transverse momentum.
Definition
ROOTTreeInfo.h:76
ROOT::CepGenRun
All useful information about a generation run.
Definition
ROOTTreeInfo.h:29
ROOT::CepGenRun::CepGenRun
CepGenRun()
Definition
ROOTTreeInfo.cpp:23
ROOT::CepGenRun::process_name
std::string process_name
Unique name of the process generated in this run.
Definition
ROOTTreeInfo.h:41
ROOT::CepGenRun::litigious_events
unsigned int litigious_events
Number of litigious events in run.
Definition
ROOTTreeInfo.h:40
ROOT::CepGenRun::xsect
double xsect
Process cross section, in pb.
Definition
ROOTTreeInfo.h:37
ROOT::CepGenRun::errxsect
double errxsect
Uncertainty on process cross section, in pb.
Definition
ROOTTreeInfo.h:38
ROOT::CepGenRun::attach
void attach(TFile *file, const std::string &run_tree=TREE_NAME)
Attach the run tree reader to a given tree Attach the run tree reader to a given file.
Definition
ROOTTreeInfo.cpp:64
ROOT::CepGenRun::clear
void clear()
Reinitialise the run tree.
Definition
ROOTTreeInfo.cpp:37
ROOT::CepGenRun::process_parameters
std::string process_parameters
Serialised process parameters.
Definition
ROOTTreeInfo.h:42
ROOT::CepGenRun::num_events
unsigned int num_events
Number of events generated in run.
Definition
ROOTTreeInfo.h:39
ROOT::CepGenRun::load
static CepGenRun load(TFile *, const std::string &run_tree=TREE_NAME)
Definition
ROOTTreeInfo.cpp:25
ROOT::CepGenRun::create
void create()
Populate the run tree.
Definition
ROOTTreeInfo.cpp:45
ROOT::CepGenRun::TREE_NAME
static constexpr const char * TREE_NAME
Output tree name.
Definition
ROOTTreeInfo.h:31
ROOT::CepGenRun::fill
void fill()
Fill the run tree.
Definition
ROOTTreeInfo.cpp:58
ROOT::CepGenRun::sqrt_s
double sqrt_s
Centre of mass energy for beam particles.
Definition
ROOTTreeInfo.h:36
cepgen::Event
Container for the information on the in- and outgoing particles' kinematics.
Definition
Event.h:28
cepgen::Event::compress
Event compress() const
Compress the event record.
Definition
Event.cpp:115
cepgen::Event::addParticle
ParticleRef addParticle(Particle &part, bool replace=false)
Set the information on one particle in the process.
Definition
Event.cpp:323
cepgen::Event::particles
Particles particles() const
Vector of all particles in the event.
Definition
Event.cpp:353
cepgen::Event::metadata
EventMetadata metadata
List of auxiliary information.
Definition
Event.h:136
cepgen::Event::clear
void clear()
Empty the whole event content.
Definition
Event.cpp:90
cepgen::Momentum::fromPtEtaPhiE
static Momentum fromPtEtaPhiE(double pt, double eta, double phi, double e=-1.)
Build a 3-momentum from its three pseudo-cylindrical coordinates.
Definition
Momentum.cpp:69
cepgen::Particle
Kinematic information for one particle.
Definition
Particle.h:33
cepgen::Particle::setMomentum
Particle & setMomentum(const Momentum &, bool offshell=false)
Associate a momentum object to this particle.
Definition
Particle.cpp:77
cepgen::Particle::Status
Status
Internal status code for a particle.
Definition
Particle.h:36
cepgen::Particle::setRole
Particle & setRole(const Role &role)
Definition
Particle.h:90
cepgen::Particle::Role
Role
Definition
Particle.h:50
cepgen::Particle::setStatus
Particle & setStatus(Status status)
Definition
Particle.h:96
cepgen::Particle::setPdgId
Particle & setPdgId(pdgid_t pdg, short ch=0)
Set the PDG identifier (along with the particle's electric charge)
Definition
Particle.cpp:93
ROOT
Definition
ROOTTreeInfo.cpp:22
CepGenAddOns
ROOTWrapper
ROOTTreeInfo.cpp
Generated on Mon Jul 29 2024 for CepGen by
1.9.7