Fork me on GitHub

source: git/readers/DelphesROOT.cpp@ 5eda2de5

ImprovedOutputFile Timing dual_readout llp
Last change on this file since 5eda2de5 was e576ef60, checked in by Michele Selvaggi <michele.selvaggi@…>, 8 years ago

fix maxEvents in DelphesROOT reader

  • Property mode set to 100644
File size: 7.6 KB
Line 
1/*
2 * Delphes: a framework for fast simulation of a generic collider experiment
3 * Copyright (C) 2012-2014 Universite catholique de Louvain (UCL), Belgium
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 * (at your option) 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 <algorithm>
20#include <stdexcept>
21#include <iostream>
22#include <sstream>
23#include <memory>
24
25#include <map>
26#include <vector>
27
28#include <stdlib.h>
29#include <signal.h>
30#include <stdio.h>
31
32#include "TROOT.h"
33#include "TApplication.h"
34
35#include "TFile.h"
36#include "TClonesArray.h"
37#include "TObjArray.h"
38#include "TStopwatch.h"
39#include "TDatabasePDG.h"
40#include "TParticlePDG.h"
41#include "TLorentzVector.h"
42
43#include "modules/Delphes.h"
44#include "classes/DelphesStream.h"
45#include "classes/DelphesClasses.h"
46#include "classes/DelphesFactory.h"
47
48#include "ExRootAnalysis/ExRootTreeWriter.h"
49#include "ExRootAnalysis/ExRootTreeReader.h"
50#include "ExRootAnalysis/ExRootTreeBranch.h"
51#include "ExRootAnalysis/ExRootProgressBar.h"
52
53
54
55using namespace std;
56
57//---------------------------------------------------------------------------
58
59
60//---------------------------------------------------------------------------
61
62static bool interrupted = false;
63
64void SignalHandler(int sig)
65{
66 interrupted = true;
67}
68
69//---------------------------------------------------------------------------
70
71int main(int argc, char *argv[])
72{
73 char appName[] = "DelphesROOT";
74 stringstream message;
75 TFile *inputFile = 0;
76 TFile *outputFile = 0;
77 TStopwatch eventStopWatch;
78 ExRootTreeWriter *treeWriter = 0;
79 ExRootTreeBranch *branchEvent = 0;
80 ExRootConfReader *confReader = 0;
81 Delphes *modularDelphes = 0;
82 DelphesFactory *factory = 0;
83 GenParticle *gen;
84 HepMCEvent *element, *eve;
85 Candidate *candidate;
86 Int_t pdgCode;
87 Int_t maxEvents;
88
89 const Double_t c_light = 2.99792458E8;
90
91 TObjArray *allParticleOutputArray = 0, *stableParticleOutputArray = 0, *partonOutputArray = 0;
92 Int_t i;
93 Long64_t eventCounter, numberOfEvents;
94
95 if(argc < 4)
96 {
97 cout << " Usage: " << appName << " config_file" << " output_file" << " input_file(s)" << endl;
98 cout << " config_file - configuration file in Tcl format," << endl;
99 cout << " output_file - output file in ROOT format," << endl;
100 cout << " input_file(s) - input file(s) in ROOT format." << endl;
101 return 1;
102 }
103
104 signal(SIGINT, SignalHandler);
105
106 gROOT->SetBatch();
107
108 int appargc = 1;
109 char *appargv[] = {appName};
110 TApplication app(appName, &appargc, appargv);
111
112 try
113 {
114 outputFile = TFile::Open(argv[2], "CREATE");
115
116 if(outputFile == NULL)
117 {
118 message << "can't open " << argv[2] << endl;
119 throw runtime_error(message.str());
120 }
121
122 treeWriter = new ExRootTreeWriter(outputFile, "Delphes");
123
124 branchEvent = treeWriter->NewBranch("Event", HepMCEvent::Class());
125
126 confReader = new ExRootConfReader;
127 confReader->ReadFile(argv[1]);
128
129 maxEvents = confReader->GetInt("::MaxEvents", 0);
130
131 if(maxEvents < 0)
132 {
133 throw runtime_error("MaxEvents must be zero or positive");
134 }
135
136 modularDelphes = new Delphes("Delphes");
137 modularDelphes->SetConfReader(confReader);
138 modularDelphes->SetTreeWriter(treeWriter);
139
140 TChain *chain = new TChain("Delphes");
141
142 factory = modularDelphes->GetFactory();
143 allParticleOutputArray = modularDelphes->ExportArray("allParticles");
144 stableParticleOutputArray = modularDelphes->ExportArray("stableParticles");
145 partonOutputArray = modularDelphes->ExportArray("partons");
146
147 modularDelphes->InitTask();
148
149 for(i = 3; i < argc && !interrupted; ++i)
150 {
151 cout << "** Reading " << argv[i] << endl;
152
153 chain->Add(argv[i]);
154 ExRootTreeReader *treeReader = new ExRootTreeReader(chain);
155
156 inputFile = TFile::Open(argv[i]);
157
158 if(inputFile == NULL)
159 {
160 message << "can't open " << argv[i] << endl;
161 throw runtime_error(message.str());
162 }
163
164 numberOfEvents = treeReader->GetEntries();
165 TClonesArray *branchParticle = treeReader->UseBranch("Particle");
166 TClonesArray *branchHepMCEvent = treeReader->UseBranch("Event");
167
168 if(numberOfEvents <= 0) continue;
169
170 // ExRootProgressBar progressBar(numberOfEvents - 1);
171 ExRootProgressBar progressBar(-1);
172
173 // Loop over all objects
174 eventCounter = 0;
175 modularDelphes->Clear();
176 treeWriter->Clear();
177 for(Int_t entry = 0; entry < numberOfEvents && !interrupted; ++entry)
178 {
179
180 if(entry >= maxEvents) break;
181 treeReader->ReadEntry(entry);
182
183 // -- TBC need also to include event weights --
184
185 eve = (HepMCEvent*) branchHepMCEvent->At(0);
186 element = static_cast<HepMCEvent *>(branchEvent->NewEntry());
187
188 element->Number = eventCounter;
189
190 element->ProcessID = eve->ProcessID;
191 element->MPI = eve->MPI;
192 element->Weight = eve->Weight;
193 element->Scale = eve->Scale;
194 element->AlphaQED = eve->AlphaQED;
195 element->AlphaQCD = eve->AlphaQCD;
196
197 element->ID1 = eve->ID1;
198 element->ID2 = eve->ID2;
199 element->X1 = eve->X1;
200 element->X2 = eve->X2;
201 element->ScalePDF = eve->ScalePDF;
202 element->PDF1 = eve->PDF1;
203 element->PDF2 = eve->PDF2;
204
205 element->ReadTime = eve->ReadTime;
206 element->ProcTime = eve->ProcTime;
207
208 for(Int_t j=0; j < branchParticle->GetEntriesFast(); j++)
209 {
210
211 gen = (GenParticle*) branchParticle->At(j);
212 candidate = factory->NewCandidate();
213
214 candidate->Momentum = gen->P4();
215 candidate->Position.SetXYZT(gen->X, gen->Y, gen->Z, gen->T*1.0E3*c_light);
216
217 candidate->PID = gen->PID;
218 candidate->Status = gen->Status;
219
220 candidate->M1 = gen->M1;
221 candidate->M2 = gen->M2;
222
223 candidate->D1 = gen->D1;
224 candidate->D2 = gen->D2;
225
226 candidate->Charge = gen->Charge;
227 candidate->Mass = gen->Mass;
228
229 allParticleOutputArray->Add(candidate);
230
231 pdgCode = TMath::Abs(gen->PID);
232
233 if(gen->Status == 1)
234 {
235 stableParticleOutputArray->Add(candidate);
236 }
237 else if(pdgCode <= 5 || pdgCode == 21 || pdgCode == 15)
238 {
239 partonOutputArray->Add(candidate);
240 }
241 }
242
243 modularDelphes->ProcessTask();
244
245 treeWriter->Fill();
246
247 modularDelphes->Clear();
248 treeWriter->Clear();
249
250 progressBar.Update(eventCounter, eventCounter);
251 ++eventCounter;
252 }
253
254 progressBar.Update(eventCounter, eventCounter, kTRUE);
255 progressBar.Finish();
256
257 inputFile->Close();
258
259 delete treeReader;
260
261 }
262
263 modularDelphes->FinishTask();
264 treeWriter->Write();
265
266 cout << "** Exiting..." << endl;
267
268 delete modularDelphes;
269 delete confReader;
270 delete treeWriter;
271 delete outputFile;
272 delete chain;
273
274 return 0;
275 }
276 catch(runtime_error &e)
277 {
278 if(treeWriter) delete treeWriter;
279 if(outputFile) delete outputFile;
280 cerr << "** ERROR: " << e.what() << endl;
281 return 1;
282 }
283}
Note: See TracBrowser for help on using the repository browser.