cepgen is hosted by Hepforge, IPPP Durham
CepGen 1.2.5
Central exclusive processes event generator
Loading...
Searching...
No Matches
DelphesHandler.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 <ExRootAnalysis/ExRootTreeBranch.h>
20#include <ExRootAnalysis/ExRootTreeWriter.h>
21#include <TFile.h>
22#include <classes/DelphesClasses.h>
23#include <classes/DelphesFactory.h>
24#include <modules/Delphes.h>
25
28#include "CepGen/Event/Event.h"
31#include "CepGen/Utils/Timer.h"
32#include "CepGen/Utils/Value.h"
33
34namespace cepgen {
41 public:
42 explicit DelphesHandler(const ParametersList&);
44
46 auto desc = EventExporter::description();
47 desc.setDescription("Delphes interfacing module");
48 desc.add<std::string>("filename", "output.delphes.root");
49 desc.add<std::string>("inputCard", "input.tcl");
50 desc.add<bool>("compress", false);
51 return desc;
52 }
53
54 void setCrossSection(const Value& cross_section) override { cross_section_ = cross_section; }
55 bool operator<<(const Event&) override;
56
57 private:
58 void initialise() override;
59
60 std::unique_ptr<TFile> output_;
61 const std::string input_card_;
62 const bool compress_;
63 std::unique_ptr<Delphes> delphes_;
64 //--- initialised here, but deleted elsewhere
65 ExRootConfReader* conf_reader_; // deleted at destructor
66 ExRootTreeWriter* tree_writer_; // deleted at destructor
67 //--- non-owning
68 DelphesFactory* factory_{nullptr};
69 ExRootTreeBranch* evt_branch_{nullptr};
70 TObjArray *out_all_parts_{nullptr}, *out_stab_parts_{nullptr}, *out_partons_{nullptr};
71 Value cross_section_{0., 1.};
72 };
73
75 : EventExporter(params),
76 output_(new TFile(steer<std::string>("filename").c_str(), "recreate")),
77 input_card_(steer<std::string>("inputCard")),
78 compress_(steer<bool>("compress")),
79 delphes_(new Delphes),
80 conf_reader_(new ExRootConfReader),
81 tree_writer_(new ExRootTreeWriter(output_.get(), "Delphes")) {
82 CG_DEBUG("DelphesHandler") << "Initialising Delphes with configuration card at \"" << input_card_ << "\".";
83 try {
84 conf_reader_->ReadFile(input_card_.c_str());
85 } catch (const std::runtime_error& err) {
86 throw CG_FATAL("DelphesHandler") << "Failed to parse the Delphes configuration card!\n\t" << err.what();
87 }
88 delphes_->SetTreeWriter(tree_writer_);
89 delphes_->SetConfReader(conf_reader_);
90 }
91
93 delphes_->FinishTask();
94 tree_writer_->Write();
95 }
96
97 void DelphesHandler::initialise() {
98 factory_ = delphes_->GetFactory();
99 if (!factory_)
100 throw CG_FATAL("DelphesHandler") << "Failed to retrieve factory object!";
101 out_all_parts_ = delphes_->ExportArray("allParticles");
102 out_stab_parts_ = delphes_->ExportArray("stableParticles");
103 out_partons_ = delphes_->ExportArray("partons");
104 evt_branch_ = tree_writer_->NewBranch("Event", LHEFEvent::Class());
105 delphes_->InitTask();
106 }
107
109 delphes_->Clear();
110 tree_writer_->Clear();
111 //--- auxiliary event quantities
112 auto evt_aux = static_cast<LHEFEvent*>(evt_branch_->NewEntry());
113 evt_aux->Number = event_num_++;
114 evt_aux->ProcessID = 0;
115 evt_aux->Weight = ev.metadata("weight"); // events are normally unweighted in CepGen
116 //evt_aux->CrossSection = (double)cross_section_; // not yet fully supported
117 evt_aux->ScalePDF = 0.; // for the time being
118 evt_aux->AlphaQED = ev.metadata("alphaEM");
119 evt_aux->AlphaQCD = ev.metadata("alphaS");
120 evt_aux->ReadTime = ev.metadata("time:generation");
121 const auto& parts = compress_ ? ev.compress().particles() : ev.particles();
122 //--- particles content
123 for (const auto& part : parts) {
124 auto cand = factory_->NewCandidate();
125 cand->PID = part.integerPdgId();
126 cand->Status = (int)part.status();
127 cand->Charge = part.charge();
128 //--- kinematics part
129 const auto& mom = part.momentum();
130 cand->Mass = mom.mass();
131 cand->Momentum.SetPxPyPzE(mom.px(), mom.py(), mom.pz(), mom.energy());
132 // no cand->Position specified (particles produced at origin)
133 //--- parentage part
134 cand->M1 = part.primary() ? 0 : *part.mothers().begin();
135 cand->M2 = part.mothers().size() < 2 ? 0 : *part.mothers().rbegin();
136 cand->D1 = part.daughters().empty() ? -1 : *part.daughters().begin();
137 cand->D2 = part.daughters().size() < 2 ? -1 : *part.daughters().rbegin();
138 //--- add to the proper collection(s)
139 out_all_parts_->Add(cand);
140 if (cand->Status == 1)
141 out_stab_parts_->Add(cand);
142 else if (cand->PID <= 5 || cand->PID == 21 || cand->PID == 15)
143 out_partons_->Add(cand);
144 }
145 utils::Timer tmr;
146 delphes_->ProcessTask();
147 evt_aux->ProcTime = tmr.elapsed();
148 tree_writer_->Fill();
149 return true;
150 }
151} // namespace cepgen
152
153REGISTER_EXPORTER("delphes", DelphesHandler);
#define REGISTER_EXPORTER(name, obj)
Add a generic export module definition to the factory.
#define CG_FATAL(mod)
Definition Exception.h:61
#define CG_DEBUG(mod)
Definition Message.h:220
Export handler for Delphes.
bool operator<<(const Event &) override
Writer operator.
DelphesHandler(const ParametersList &)
static ParametersDescription description()
void setCrossSection(const Value &cross_section) override
Specify the cross section value, in pb.
Output format handler for events export.
unsigned long long event_num_
Event index.
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
Particles particles() const
Vector of all particles in the event.
Definition Event.cpp:353
EventMetadata metadata
List of auxiliary information.
Definition Event.h:136
A description object for parameters collection.
static ParametersDescription description()
Description of all object parameters.
Definition Steerable.cpp:42
A scalar value with its uncertainty.
Definition Value.h:26
A generic timer to extract the processing time between two steps in this software's flow.
Definition Timer.h:30
double elapsed() const
Time elapsed since the last reset call (or class construction)
Definition Timer.h:37
Common namespace for this Monte Carlo generator.