Fork me on GitHub

source: git/readers/DelphesCMSFWLite.cpp@ 480f9ed

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

added #974 suggested modification

  • Property mode set to 100644
File size: 10.2 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 "TObjArray.h"
37#include "TStopwatch.h"
38#include "TDatabasePDG.h"
39#include "TParticlePDG.h"
40#include "TLorentzVector.h"
41
42#include "modules/Delphes.h"
43#include "classes/DelphesStream.h"
44#include "classes/DelphesClasses.h"
45#include "classes/DelphesFactory.h"
46
47#include "ExRootAnalysis/ExRootTreeWriter.h"
48#include "ExRootAnalysis/ExRootTreeBranch.h"
49#include "ExRootAnalysis/ExRootProgressBar.h"
50
51#include "FWCore/FWLite/interface/FWLiteEnabler.h"
52#include "DataFormats/FWLite/interface/Event.h"
53#include "DataFormats/FWLite/interface/Handle.h"
54#include "DataFormats/HepMCCandidate/interface/GenParticle.h"
55#include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h"
56#include "SimDataFormats/GeneratorProducts/interface/GenEventInfoProduct.h"
57#include "SimDataFormats/GeneratorProducts/interface/LHEEventProduct.h"
58#include "SimDataFormats/GeneratorProducts/interface/WeightsInfo.h"
59
60using namespace std;
61
62//---------------------------------------------------------------------------
63
64void ConvertInput(fwlite::Event &event, Long64_t eventCounter,
65 ExRootTreeBranch *branchEvent, ExRootTreeBranch *branchRwgt,
66 DelphesFactory *factory, TObjArray *allParticleOutputArray,
67 TObjArray *stableParticleOutputArray, TObjArray *partonOutputArray)
68{
69
70 fwlite::Handle< GenEventInfoProduct > handleGenEventInfo;
71 fwlite::Handle< LHEEventProduct > handleLHEEvent;
72 fwlite::Handle< vector< reco::GenParticle > > handleParticle;
73
74 vector< reco::GenParticle >::const_iterator itParticle;
75
76 vector< const reco::Candidate * > vectorCandidate;
77 vector< const reco::Candidate * >::iterator itCandidate;
78
79 handleGenEventInfo.getByLabel(event, "generator");
80
81 if (!((handleLHEEvent.getBranchNameFor(event, "source")).empty()))
82 {
83 handleLHEEvent.getByLabel(event, "source");
84 }
85 else if (!((handleLHEEvent.getBranchNameFor(event, "externalLHEProducer")).empty()))
86 {
87 handleLHEEvent.getByLabel(event, "externalLHEProducer");
88 }
89 else
90 {
91 std::cout<<"Wrong LHEEvent Label! Please, check the input file."<<std::endl;
92 exit(-1);
93 }
94
95 if (!((handleParticle.getBranchNameFor(event, "genParticles")).empty()))
96 {
97 handleParticle.getByLabel(event, "genParticles");
98 }
99 else if (!((handleParticle.getBranchNameFor(event, "prunedGenParticles")).empty()))
100 {
101 handleParticle.getByLabel(event, "prunedGenParticles");
102 }
103 else
104 {
105 std::cout<<"Wrong GenParticle Label! Please, check the input file."<<std::endl;
106 exit(-1);
107 }
108
109 HepMCEvent *element;
110 Weight *weight;
111 Candidate *candidate;
112 TDatabasePDG *pdg;
113 TParticlePDG *pdgParticle;
114 Int_t pdgCode;
115
116 Int_t pid, status;
117 Double_t px, py, pz, e, mass;
118 Double_t x, y, z;
119
120 const vector< gen::WeightsInfo > &vectorWeightsInfo = handleLHEEvent->weights();
121 vector< gen::WeightsInfo >::const_iterator itWeightsInfo;
122
123 element = static_cast<HepMCEvent *>(branchEvent->NewEntry());
124
125 element->Number = eventCounter;
126
127 element->ProcessID = handleGenEventInfo->signalProcessID();
128 element->MPI = 1;
129 element->Weight = handleGenEventInfo->weight();
130 element->Scale = handleGenEventInfo->qScale();
131 element->AlphaQED = handleGenEventInfo->alphaQED();
132 element->AlphaQCD = handleGenEventInfo->alphaQCD();
133
134 element->ID1 = 0;
135 element->ID2 = 0;
136 element->X1 = 0.0;
137 element->X2 = 0.0;
138 element->ScalePDF = 0.0;
139 element->PDF1 = 0.0;
140 element->PDF2 = 0.0;
141
142 element->ReadTime = 0.0;
143 element->ProcTime = 0.0;
144
145 for(itWeightsInfo = vectorWeightsInfo.begin(); itWeightsInfo != vectorWeightsInfo.end(); ++itWeightsInfo)
146 {
147 weight = static_cast<Weight *>(branchRwgt->NewEntry());
148 weight->Weight = itWeightsInfo->wgt;
149 }
150
151 pdg = TDatabasePDG::Instance();
152
153 for(itParticle = handleParticle->begin(); itParticle != handleParticle->end(); ++itParticle)
154 {
155 vectorCandidate.push_back(&*itParticle);
156 }
157
158 for(itParticle = handleParticle->begin(); itParticle != handleParticle->end(); ++itParticle)
159 {
160 const reco::GenParticle &particle = *itParticle;
161
162 pid = particle.pdgId();
163 status = particle.status();
164 px = particle.px(); py = particle.py(); pz = particle.pz(); e = particle.energy(); mass = particle.mass();
165 x = particle.vx(); y = particle.vy(); z = particle.vz();
166
167 candidate = factory->NewCandidate();
168
169 candidate->PID = pid;
170 pdgCode = TMath::Abs(candidate->PID);
171
172 candidate->Status = status;
173
174 if(particle.mother())
175 {
176 itCandidate = find(vectorCandidate.begin(), vectorCandidate.end(), particle.mother());
177 if(itCandidate != vectorCandidate.end()) candidate->M1 = distance(vectorCandidate.begin(), itCandidate);
178 }
179
180 itCandidate = find(vectorCandidate.begin(), vectorCandidate.end(), particle.daughter(0));
181 if(itCandidate != vectorCandidate.end()) candidate->D1 = distance(vectorCandidate.begin(), itCandidate);
182
183 itCandidate = find(vectorCandidate.begin(), vectorCandidate.end(), particle.daughter(particle.numberOfDaughters() - 1));
184 if(itCandidate != vectorCandidate.end()) candidate->D2 = distance(vectorCandidate.begin(), itCandidate);
185
186 pdgParticle = pdg->GetParticle(pid);
187 candidate->Charge = pdgParticle ? Int_t(pdgParticle->Charge()/3.0) : -999;
188 candidate->Mass = mass;
189
190 candidate->Momentum.SetPxPyPzE(px, py, pz, e);
191
192 candidate->Position.SetXYZT(x*10.0, y*10.0, z*10.0, 0.0);
193
194 allParticleOutputArray->Add(candidate);
195
196 if(!pdgParticle) continue;
197
198 if(status == 1)
199 {
200 stableParticleOutputArray->Add(candidate);
201 }
202 else if(pdgCode <= 5 || pdgCode == 21 || pdgCode == 15)
203 {
204 partonOutputArray->Add(candidate);
205 }
206 }
207}
208
209//---------------------------------------------------------------------------
210
211static bool interrupted = false;
212
213void SignalHandler(int sig)
214{
215 interrupted = true;
216}
217
218//---------------------------------------------------------------------------
219
220int main(int argc, char *argv[])
221{
222 char appName[] = "DelphesCMSFWLite";
223 stringstream message;
224 TFile *inputFile = 0;
225 TFile *outputFile = 0;
226 TStopwatch eventStopWatch;
227 ExRootTreeWriter *treeWriter = 0;
228 ExRootTreeBranch *branchEvent = 0, *branchRwgt = 0;
229 ExRootConfReader *confReader = 0;
230 Delphes *modularDelphes = 0;
231 DelphesFactory *factory = 0;
232 TObjArray *allParticleOutputArray = 0, *stableParticleOutputArray = 0, *partonOutputArray = 0;
233 Int_t i;
234 Long64_t eventCounter, numberOfEvents;
235
236 if(argc < 4)
237 {
238 cout << " Usage: " << appName << " config_file" << " output_file" << " input_file(s)" << endl;
239 cout << " config_file - configuration file in Tcl format," << endl;
240 cout << " output_file - output file in ROOT format," << endl;
241 cout << " input_file(s) - input file(s) in ROOT format." << endl;
242 return 1;
243 }
244
245 signal(SIGINT, SignalHandler);
246
247 gROOT->SetBatch();
248
249 int appargc = 1;
250 char *appargv[] = {appName};
251 TApplication app(appName, &appargc, appargv);
252
253 FWLiteEnabler::enable();
254
255 try
256 {
257 outputFile = TFile::Open(argv[2], "CREATE");
258
259 if(outputFile == NULL)
260 {
261 message << "can't open " << argv[2] << endl;
262 throw runtime_error(message.str());
263 }
264
265 treeWriter = new ExRootTreeWriter(outputFile, "Delphes");
266
267 branchEvent = treeWriter->NewBranch("Event", HepMCEvent::Class());
268 branchRwgt = treeWriter->NewBranch("Rwgt", Weight::Class());
269
270 confReader = new ExRootConfReader;
271 confReader->ReadFile(argv[1]);
272
273 modularDelphes = new Delphes("Delphes");
274 modularDelphes->SetConfReader(confReader);
275 modularDelphes->SetTreeWriter(treeWriter);
276
277 factory = modularDelphes->GetFactory();
278 allParticleOutputArray = modularDelphes->ExportArray("allParticles");
279 stableParticleOutputArray = modularDelphes->ExportArray("stableParticles");
280 partonOutputArray = modularDelphes->ExportArray("partons");
281
282 modularDelphes->InitTask();
283
284 for(i = 3; i < argc && !interrupted; ++i)
285 {
286 cout << "** Reading " << argv[i] << endl;
287
288 inputFile = TFile::Open(argv[i]);
289
290 if(inputFile == NULL)
291 {
292 message << "can't open " << argv[i] << endl;
293 throw runtime_error(message.str());
294 }
295
296 fwlite::Event event(inputFile);
297
298 numberOfEvents = event.size();
299
300 if(numberOfEvents <= 0) continue;
301
302 // ExRootProgressBar progressBar(numberOfEvents - 1);
303 ExRootProgressBar progressBar(-1);
304
305 // Loop over all objects
306 eventCounter = 0;
307 modularDelphes->Clear();
308 treeWriter->Clear();
309 for(event.toBegin(); !event.atEnd() && !interrupted; ++event)
310 {
311 ConvertInput(event, eventCounter, branchEvent, branchRwgt, factory,
312 allParticleOutputArray, stableParticleOutputArray, partonOutputArray);
313 modularDelphes->ProcessTask();
314
315 treeWriter->Fill();
316
317 modularDelphes->Clear();
318 treeWriter->Clear();
319
320 progressBar.Update(eventCounter, eventCounter);
321 ++eventCounter;
322 }
323
324 progressBar.Update(eventCounter, eventCounter, kTRUE);
325 progressBar.Finish();
326
327 inputFile->Close();
328 }
329
330 modularDelphes->FinishTask();
331 treeWriter->Write();
332
333 cout << "** Exiting..." << endl;
334
335 delete modularDelphes;
336 delete confReader;
337 delete treeWriter;
338 delete outputFile;
339
340 return 0;
341 }
342 catch(runtime_error &e)
343 {
344 if(treeWriter) delete treeWriter;
345 if(outputFile) delete outputFile;
346 cerr << "** ERROR: " << e.what() << endl;
347 return 1;
348 }
349}
Note: See TracBrowser for help on using the repository browser.