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
21
22namespace ROOT {
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
38 sqrt_s = -1.;
39 xsect = errxsect = -1.;
41 process_name.clear();
42 process_parameters.clear();
43 }
44
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
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
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
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
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
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;
216 part.setPdgId((long)pdg_id[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
#define CG_FATAL(mod)
Definition Exception.h:61
#define CG_ERROR(mod)
Definition Exception.h:60
#define CG_WARNING(mod)
Definition Message.h:228
All useful information about a generated event.
double m[MAX_PART]
Particles mass, in GeV/c .
static CepGenEvent load(TFile *, const std::string &events_tree=TREE_NAME)
double rapidity[MAX_PART]
Particles rapidity.
double charge[MAX_PART]
Particles charges, in e.
bool next(cepgen::Event &)
Read the next event in the file.
double eta[MAX_PART]
Particles pseudo-rapidity.
int parent1[MAX_PART]
First particles mother.
void fill(const cepgen::Event &, bool compress=false)
Fill the tree with a new event.
cepgen::Event::EventMetadata metadata
int status[MAX_PART]
Integer status code.
double phi[MAX_PART]
Particles azimuthal angle.
float weight
Event weight.
double E[MAX_PART]
Particles energy, in GeV.
int np
Number of particles in the event.
float gen_time
Event generation time.
float tot_time
Total event generation time.
int parent2[MAX_PART]
Last particles mother.
int role[MAX_PART]
Particles role in the event.
static constexpr size_t MAX_PART
Maximal number of particles in event.
void attach()
Attach the event tree reader to a tree.
int pdg_id[MAX_PART]
Integer particles PDG id.
void clear()
Reinitialise the event content.
void create()
Populate the tree and all associated branches.
static constexpr const char * TREE_NAME
Output tree name.
int stable[MAX_PART]
Whether the particle must decay or not.
double pt[MAX_PART]
Particles transverse momentum.
All useful information about a generation run.
std::string process_name
Unique name of the process generated in this run.
unsigned int litigious_events
Number of litigious events in run.
double xsect
Process cross section, in pb.
double errxsect
Uncertainty on process cross section, in pb.
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.
void clear()
Reinitialise the run tree.
std::string process_parameters
Serialised process parameters.
unsigned int num_events
Number of events generated in run.
static CepGenRun load(TFile *, const std::string &run_tree=TREE_NAME)
void create()
Populate the run tree.
static constexpr const char * TREE_NAME
Output tree name.
void fill()
Fill the run tree.
double sqrt_s
Centre of mass energy for beam particles.
Container for the information on the in- and outgoing particles' kinematics.
Definition Event.h:28
Event compress() const
Compress the event record.
Definition Event.cpp:115
ParticleRef addParticle(Particle &part, bool replace=false)
Set the information on one particle in the process.
Definition Event.cpp:323
Particles particles() const
Vector of all particles in the event.
Definition Event.cpp:353
EventMetadata metadata
List of auxiliary information.
Definition Event.h:136
void clear()
Empty the whole event content.
Definition Event.cpp:90
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
Kinematic information for one particle.
Definition Particle.h:33
Particle & setMomentum(const Momentum &, bool offshell=false)
Associate a momentum object to this particle.
Definition Particle.cpp:77
Status
Internal status code for a particle.
Definition Particle.h:36
Particle & setRole(const Role &role)
Definition Particle.h:90
Particle & setStatus(Status status)
Definition Particle.h:96
Particle & setPdgId(pdgid_t pdg, short ch=0)
Set the PDG identifier (along with the particle's electric charge)
Definition Particle.cpp:93