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 | * @author S.Chekanov (ANL)
|
---|
18 | */
|
---|
19 |
|
---|
20 | #include <stdexcept>
|
---|
21 | #include <iostream>
|
---|
22 | #include <sstream>
|
---|
23 | #include <memory>
|
---|
24 |
|
---|
25 | #include <map>
|
---|
26 |
|
---|
27 | #include <stdlib.h>
|
---|
28 | #include <signal.h>
|
---|
29 | #include <stdio.h>
|
---|
30 |
|
---|
31 | #include "TROOT.h"
|
---|
32 | #include "TApplication.h"
|
---|
33 |
|
---|
34 | #include "TFile.h"
|
---|
35 | #include "TObjArray.h"
|
---|
36 | #include "TStopwatch.h"
|
---|
37 | #include "TDatabasePDG.h"
|
---|
38 | #include "TParticlePDG.h"
|
---|
39 | #include "TLorentzVector.h"
|
---|
40 |
|
---|
41 | #include "modules/Delphes.h"
|
---|
42 | #include "classes/DelphesStream.h"
|
---|
43 | #include "classes/DelphesClasses.h"
|
---|
44 | #include "classes/DelphesFactory.h"
|
---|
45 |
|
---|
46 | #include "ExRootAnalysis/ExRootTreeWriter.h"
|
---|
47 | #include "ExRootAnalysis/ExRootTreeBranch.h"
|
---|
48 | #include "ExRootAnalysis/ExRootProgressBar.h"
|
---|
49 |
|
---|
50 | #include <proio/reader.h>
|
---|
51 | #include <proio/event.h>
|
---|
52 | #include <proio/model/mc.pb.h>
|
---|
53 | namespace model=proio::model::mc;
|
---|
54 |
|
---|
55 |
|
---|
56 | using namespace std;
|
---|
57 |
|
---|
58 | //---------------------------------------------------------------------------
|
---|
59 |
|
---|
60 | void ConvertInput(proio::Event *event,
|
---|
61 | ExRootTreeBranch *branch, DelphesFactory *factory,
|
---|
62 | TObjArray *allParticleOutputArray, TObjArray *stableParticleOutputArray,
|
---|
63 | TObjArray *partonOutputArray, TStopwatch *readStopWatch, TStopwatch *procStopWatch)
|
---|
64 | {
|
---|
65 |
|
---|
66 | HepMCEvent *element;
|
---|
67 | Candidate *candidate;
|
---|
68 | TDatabasePDG *pdg;
|
---|
69 | TParticlePDG *pdgParticle;
|
---|
70 | Int_t pdgCode;
|
---|
71 |
|
---|
72 | Int_t pid, status;
|
---|
73 | Double_t px, py, pz, mass;
|
---|
74 | Double_t x, y, z, t;
|
---|
75 |
|
---|
76 | pdg = TDatabasePDG::Instance();
|
---|
77 |
|
---|
78 |
|
---|
79 | // event information
|
---|
80 | element = static_cast<HepMCEvent *>(branch->NewEntry());
|
---|
81 |
|
---|
82 |
|
---|
83 | int nID=0;
|
---|
84 | double weight=0;
|
---|
85 | int process_id=0;
|
---|
86 | auto mcentries = event->TaggedEntries("MCParameters");
|
---|
87 | for (uint64_t mcentryID : mcentries) {
|
---|
88 | auto mcpar = dynamic_cast<proio::model::mc::MCParameters *>(event->GetEntry(mcentryID));
|
---|
89 | nID=mcpar->number();
|
---|
90 | weight=mcpar->weight();
|
---|
91 | process_id=mcpar->processid();
|
---|
92 | break; // consider only most generic from 1st entry
|
---|
93 | };
|
---|
94 |
|
---|
95 | element->Number = nID;
|
---|
96 | element->ProcessID =process_id;
|
---|
97 | element->Weight = weight;
|
---|
98 |
|
---|
99 | /*
|
---|
100 | // Pythia8 specific
|
---|
101 | element->Number = mutableEvent->number();
|
---|
102 | element->ProcessID = mutableEvent->process_id();
|
---|
103 | element->MPI = mutableEvent->mpi();
|
---|
104 | element->Weight = mutableEvent->weight();
|
---|
105 | element->Scale = mutableEvent->scale();
|
---|
106 | element->AlphaQED = mutableEvent->alpha_qed();
|
---|
107 | element->AlphaQCD = mutableEvent->alpha_qcd();
|
---|
108 | element->ID1 = mutableEvent->id1();
|
---|
109 | element->ID2 = mutableEvent->id2();
|
---|
110 | element->X1 = mutableEvent->x1();
|
---|
111 | element->X2 = mutableEvent->x2();
|
---|
112 | element->ScalePDF = mutableEvent->scale_pdf();
|
---|
113 | element->PDF1 = mutableEvent->pdf1();
|
---|
114 | element->PDF2 = mutableEvent->pdf2();
|
---|
115 | */
|
---|
116 |
|
---|
117 | element->ReadTime = readStopWatch->RealTime();
|
---|
118 | element->ProcTime = procStopWatch->RealTime();
|
---|
119 |
|
---|
120 |
|
---|
121 | auto entries = event->TaggedEntries("Particle");
|
---|
122 |
|
---|
123 | for (uint64_t entryID : entries) {
|
---|
124 | auto part = dynamic_cast<model::Particle *>(event->GetEntry(entryID));
|
---|
125 | pid = part->pdg();
|
---|
126 | status = part->status();
|
---|
127 | model::XYZF pvector=part->p();
|
---|
128 | px=pvector.x();
|
---|
129 | py=pvector.y();
|
---|
130 | pz=pvector.z();
|
---|
131 | mass = part->mass();
|
---|
132 | auto v =part->vertex();
|
---|
133 | x=v.x();
|
---|
134 | y=v.y();
|
---|
135 | z=v.z();
|
---|
136 | t=v.t();
|
---|
137 |
|
---|
138 | candidate = factory->NewCandidate();
|
---|
139 | candidate->PID = pid;
|
---|
140 | pdgCode = TMath::Abs(candidate->PID);
|
---|
141 | candidate->Status = status;
|
---|
142 |
|
---|
143 | int M1=0;
|
---|
144 | int M2=0;
|
---|
145 | for (int k1=0; k1<part->parent_size(); k1++){
|
---|
146 | if (k1==0) {
|
---|
147 | auto mother = dynamic_cast<model::Particle *>(event->GetEntry(part->parent(0)));
|
---|
148 | M1=mother->barcode();
|
---|
149 | }
|
---|
150 | if (k1==1) {
|
---|
151 | auto mother = dynamic_cast<model::Particle *>(event->GetEntry(part->parent(1)));
|
---|
152 | M2=mother->barcode();
|
---|
153 | }
|
---|
154 | }
|
---|
155 |
|
---|
156 |
|
---|
157 | int D1=0;
|
---|
158 | int D2=0;
|
---|
159 | for (int k1=0; k1<part->child_size(); k1++){
|
---|
160 | if (k1==0) {
|
---|
161 | auto child = dynamic_cast<model::Particle *>(event->GetEntry(part->child(0)));
|
---|
162 | D1=child->barcode();
|
---|
163 | }
|
---|
164 |
|
---|
165 | if (k1==1) {
|
---|
166 | auto child = dynamic_cast<model::Particle *>(event->GetEntry(part->child(1)));
|
---|
167 | D2=child->barcode();
|
---|
168 | };
|
---|
169 | }
|
---|
170 |
|
---|
171 |
|
---|
172 | candidate->M1 = M1;
|
---|
173 | candidate->M2 = M2;
|
---|
174 | candidate->D1 = D1;
|
---|
175 | candidate->D2 = D2;
|
---|
176 |
|
---|
177 | pdgParticle = pdg->GetParticle(pid);
|
---|
178 | candidate->Charge = pdgParticle ? Int_t(pdgParticle->Charge()/3.0) : -999;
|
---|
179 | candidate->Mass = mass;
|
---|
180 |
|
---|
181 | candidate->Momentum.SetXYZM(px, py, pz, mass);
|
---|
182 |
|
---|
183 | candidate->Position.SetXYZT(x, y, z, t);
|
---|
184 |
|
---|
185 | allParticleOutputArray->Add(candidate);
|
---|
186 |
|
---|
187 | if(!pdgParticle) continue;
|
---|
188 |
|
---|
189 | if(status == 1)
|
---|
190 | {
|
---|
191 | stableParticleOutputArray->Add(candidate);
|
---|
192 | }
|
---|
193 | else if(pdgCode <= 5 || pdgCode == 21 || pdgCode == 15)
|
---|
194 | {
|
---|
195 | partonOutputArray->Add(candidate);
|
---|
196 | }
|
---|
197 |
|
---|
198 | }
|
---|
199 |
|
---|
200 |
|
---|
201 | }
|
---|
202 |
|
---|
203 | //---------------------------------------------------------------------------
|
---|
204 |
|
---|
205 | static bool interrupted = false;
|
---|
206 |
|
---|
207 | void SignalHandler(int sig)
|
---|
208 | {
|
---|
209 | interrupted = true;
|
---|
210 | }
|
---|
211 |
|
---|
212 | //---------------------------------------------------------------------------
|
---|
213 |
|
---|
214 | int main(int argc, char *argv[])
|
---|
215 | {
|
---|
216 | char appName[] = "DelphesProIO";
|
---|
217 | stringstream message;
|
---|
218 | proio::Reader *inputFile = 0;
|
---|
219 | TFile *outputFile = 0;
|
---|
220 | TStopwatch readStopWatch, procStopWatch;
|
---|
221 | ExRootTreeWriter *treeWriter = 0;
|
---|
222 | ExRootTreeBranch *branchEvent = 0;
|
---|
223 | ExRootConfReader *confReader = 0;
|
---|
224 | Delphes *modularDelphes = 0;
|
---|
225 | DelphesFactory *factory = 0;
|
---|
226 | TObjArray *allParticleOutputArray = 0, *stableParticleOutputArray = 0, *partonOutputArray = 0;
|
---|
227 | Int_t i;
|
---|
228 | Long64_t eventCounter, numberOfEvents;
|
---|
229 |
|
---|
230 | if(argc < 4)
|
---|
231 | {
|
---|
232 | cout << " Usage: " << appName << " config_file" << " output_file" << " input_file(s)" << endl;
|
---|
233 | cout << " config_file - configuration file in Tcl format," << endl;
|
---|
234 | cout << " output_file - output file in ROOT format," << endl;
|
---|
235 | cout << " input_file(s) - input file(s) in ProIO format." << endl;
|
---|
236 | return 1;
|
---|
237 | }
|
---|
238 |
|
---|
239 | signal(SIGINT, SignalHandler);
|
---|
240 |
|
---|
241 | gROOT->SetBatch();
|
---|
242 |
|
---|
243 | int appargc = 1;
|
---|
244 | char *appargv[] = {appName};
|
---|
245 | TApplication app(appName, &appargc, appargv);
|
---|
246 |
|
---|
247 | try
|
---|
248 | {
|
---|
249 | outputFile = TFile::Open(argv[2], "CREATE");
|
---|
250 |
|
---|
251 | if(outputFile == NULL)
|
---|
252 | {
|
---|
253 | message << "can't open " << argv[2] << endl;
|
---|
254 | throw runtime_error(message.str());
|
---|
255 | }
|
---|
256 |
|
---|
257 | treeWriter = new ExRootTreeWriter(outputFile, "Delphes");
|
---|
258 |
|
---|
259 | branchEvent = treeWriter->NewBranch("Event", HepMCEvent::Class());
|
---|
260 |
|
---|
261 | confReader = new ExRootConfReader;
|
---|
262 | confReader->ReadFile(argv[1]);
|
---|
263 |
|
---|
264 | modularDelphes = new Delphes("Delphes");
|
---|
265 | modularDelphes->SetConfReader(confReader);
|
---|
266 | modularDelphes->SetTreeWriter(treeWriter);
|
---|
267 |
|
---|
268 | factory = modularDelphes->GetFactory();
|
---|
269 | allParticleOutputArray = modularDelphes->ExportArray("allParticles");
|
---|
270 | stableParticleOutputArray = modularDelphes->ExportArray("stableParticles");
|
---|
271 | partonOutputArray = modularDelphes->ExportArray("partons");
|
---|
272 |
|
---|
273 | modularDelphes->InitTask();
|
---|
274 |
|
---|
275 | for(i = 3; i < argc && !interrupted; ++i)
|
---|
276 | {
|
---|
277 | cout << "** INFO: Reading " << argv[i] << endl;
|
---|
278 |
|
---|
279 | inputFile=new proio::Reader( argv[i] );
|
---|
280 |
|
---|
281 | if(inputFile == NULL)
|
---|
282 | {
|
---|
283 | message << "can't open " << argv[i] << endl;
|
---|
284 | throw runtime_error(message.str());
|
---|
285 | }
|
---|
286 |
|
---|
287 |
|
---|
288 | /*
|
---|
289 | // this is slow method, but general
|
---|
290 | inputFile->SeekToStart();
|
---|
291 | int nn=0;
|
---|
292 | auto event = new proio::Event();
|
---|
293 | while(inputFile->Next(event)){
|
---|
294 | auto entries = event->TaggedEntries("Particle");
|
---|
295 | if (entries.size()>0) nn++;
|
---|
296 | }
|
---|
297 | */
|
---|
298 |
|
---|
299 |
|
---|
300 | auto event = new proio::Event();
|
---|
301 | auto max_n_events = std::numeric_limits<uint64_t>::max();
|
---|
302 | auto nn = inputFile->Skip(max_n_events);
|
---|
303 | cout << "** INFO: " << nn-1 << " events found in ProIO file" << endl;
|
---|
304 | inputFile->SeekToStart();
|
---|
305 | numberOfEvents = nn-1; // last event has only metadata (no particle record)
|
---|
306 |
|
---|
307 | if(numberOfEvents <= 0) continue;
|
---|
308 |
|
---|
309 | ExRootProgressBar progressBar(numberOfEvents - 1);
|
---|
310 |
|
---|
311 |
|
---|
312 | // Loop over all objects
|
---|
313 | modularDelphes->Clear();
|
---|
314 | treeWriter->Clear();
|
---|
315 | readStopWatch.Start();
|
---|
316 |
|
---|
317 | for(eventCounter = 0; eventCounter < numberOfEvents && !interrupted; ++eventCounter)
|
---|
318 | {
|
---|
319 | inputFile->Next(event);
|
---|
320 | if(event == 0) continue;
|
---|
321 |
|
---|
322 | readStopWatch.Stop();
|
---|
323 |
|
---|
324 | procStopWatch.Start();
|
---|
325 |
|
---|
326 | ConvertInput(event,
|
---|
327 | branchEvent, factory,
|
---|
328 | allParticleOutputArray, stableParticleOutputArray,
|
---|
329 | partonOutputArray, &readStopWatch, &procStopWatch);
|
---|
330 |
|
---|
331 | modularDelphes->ProcessTask();
|
---|
332 | procStopWatch.Stop();
|
---|
333 |
|
---|
334 | treeWriter->Fill();
|
---|
335 |
|
---|
336 | modularDelphes->Clear();
|
---|
337 | treeWriter->Clear();
|
---|
338 |
|
---|
339 | readStopWatch.Start();
|
---|
340 | progressBar.Update(eventCounter);
|
---|
341 | }
|
---|
342 |
|
---|
343 |
|
---|
344 | progressBar.Update(eventCounter, eventCounter, kTRUE);
|
---|
345 | progressBar.Finish();
|
---|
346 |
|
---|
347 | delete inputFile;
|
---|
348 | }
|
---|
349 |
|
---|
350 |
|
---|
351 | modularDelphes->FinishTask();
|
---|
352 | treeWriter->Write();
|
---|
353 |
|
---|
354 | cout << "** Exiting..." << endl;
|
---|
355 |
|
---|
356 | delete modularDelphes;
|
---|
357 | delete confReader;
|
---|
358 | delete treeWriter;
|
---|
359 | return 0;
|
---|
360 | }
|
---|
361 | catch(runtime_error &e)
|
---|
362 | {
|
---|
363 | if(treeWriter) delete treeWriter;
|
---|
364 | if(outputFile) delete outputFile;
|
---|
365 | cerr << "** ERROR: " << e.what() << endl;
|
---|
366 | return 1;
|
---|
367 | }
|
---|
368 | }
|
---|