Fork me on GitHub

source: git/readers/DelphesSTDHEP.cpp@ f4900ab

Last change on this file since f4900ab was 341014c, checked in by Pavel Demin <pavel-demin@…>, 6 years ago

apply .clang-format to all .h, .cc and .cpp files

  • Property mode set to 100644
File size: 6.3 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 <iostream>
20#include <sstream>
21#include <stdexcept>
22
23#include <signal.h>
24
25#include "TApplication.h"
26#include "TROOT.h"
27
28#include "TDatabasePDG.h"
29#include "TFile.h"
30#include "TLorentzVector.h"
31#include "TObjArray.h"
32#include "TParticlePDG.h"
33#include "TStopwatch.h"
34
35#include "classes/DelphesClasses.h"
36#include "classes/DelphesFactory.h"
37#include "classes/DelphesSTDHEPReader.h"
38#include "modules/Delphes.h"
39
40#include "ExRootAnalysis/ExRootProgressBar.h"
41#include "ExRootAnalysis/ExRootTreeBranch.h"
42#include "ExRootAnalysis/ExRootTreeWriter.h"
43
44using namespace std;
45
46//---------------------------------------------------------------------------
47
48static bool interrupted = false;
49
50void SignalHandler(int sig)
51{
52 interrupted = true;
53}
54
55//---------------------------------------------------------------------------
56
57int main(int argc, char *argv[])
58{
59 char appName[] = "DelphesSTDHEP";
60 stringstream message;
61 FILE *inputFile = 0;
62 TFile *outputFile = 0;
63 TStopwatch readStopWatch, procStopWatch;
64 ExRootTreeWriter *treeWriter = 0;
65 ExRootTreeBranch *branchEvent = 0;
66 ExRootConfReader *confReader = 0;
67 Delphes *modularDelphes = 0;
68 DelphesFactory *factory = 0;
69 TObjArray *stableParticleOutputArray = 0, *allParticleOutputArray = 0, *partonOutputArray = 0;
70 DelphesSTDHEPReader *reader = 0;
71 Int_t i, maxEvents, skipEvents;
72 Long64_t length, eventCounter;
73
74 if(argc < 3)
75 {
76 cout << " Usage: " << appName << " config_file"
77 << " output_file"
78 << " [input_file(s)]" << endl;
79 cout << " config_file - configuration file in Tcl format," << endl;
80 cout << " output_file - output file in ROOT format," << endl;
81 cout << " input_file(s) - input file(s) in STDHEP format," << endl;
82 cout << " with no input_file, or when input_file is -, read standard input." << endl;
83 return 1;
84 }
85
86 signal(SIGINT, SignalHandler);
87
88 gROOT->SetBatch();
89
90 int appargc = 1;
91 char *appargv[] = {appName};
92 TApplication app(appName, &appargc, appargv);
93
94 try
95 {
96 outputFile = TFile::Open(argv[2], "CREATE");
97
98 if(outputFile == NULL)
99 {
100 message << "can't create output file " << argv[2];
101 throw runtime_error(message.str());
102 }
103
104 treeWriter = new ExRootTreeWriter(outputFile, "Delphes");
105
106 branchEvent = treeWriter->NewBranch("Event", LHEFEvent::Class());
107
108 confReader = new ExRootConfReader;
109 confReader->ReadFile(argv[1]);
110
111 maxEvents = confReader->GetInt("::MaxEvents", 0);
112 skipEvents = confReader->GetInt("::SkipEvents", 0);
113
114 if(maxEvents < 0)
115 {
116 throw runtime_error("MaxEvents must be zero or positive");
117 }
118
119 if(skipEvents < 0)
120 {
121 throw runtime_error("SkipEvents must be zero or positive");
122 }
123
124 modularDelphes = new Delphes("Delphes");
125 modularDelphes->SetConfReader(confReader);
126 modularDelphes->SetTreeWriter(treeWriter);
127
128 factory = modularDelphes->GetFactory();
129 allParticleOutputArray = modularDelphes->ExportArray("allParticles");
130 stableParticleOutputArray = modularDelphes->ExportArray("stableParticles");
131 partonOutputArray = modularDelphes->ExportArray("partons");
132
133 reader = new DelphesSTDHEPReader;
134
135 modularDelphes->InitTask();
136
137 i = 3;
138 do
139 {
140 if(interrupted) break;
141
142 if(i == argc || strncmp(argv[i], "-", 2) == 0)
143 {
144 cout << "** Reading standard input" << endl;
145 inputFile = stdin;
146 length = -1;
147 }
148 else
149 {
150 cout << "** Reading " << argv[i] << endl;
151 inputFile = fopen(argv[i], "rb");
152
153 if(inputFile == NULL)
154 {
155 message << "can't open " << argv[i];
156 throw runtime_error(message.str());
157 }
158
159 fseek(inputFile, 0L, SEEK_END);
160 length = ftello(inputFile);
161 fseek(inputFile, 0L, SEEK_SET);
162
163 if(length <= 0)
164 {
165 fclose(inputFile);
166 ++i;
167 continue;
168 }
169 }
170
171 reader->SetInputFile(inputFile);
172
173 ExRootProgressBar progressBar(length);
174
175 // Loop over all objects
176 eventCounter = 0;
177 treeWriter->Clear();
178 modularDelphes->Clear();
179 reader->Clear();
180 readStopWatch.Start();
181 while((maxEvents <= 0 || eventCounter - skipEvents < maxEvents) && reader->ReadBlock(factory, allParticleOutputArray, stableParticleOutputArray, partonOutputArray) && !interrupted)
182 {
183 if(reader->EventReady())
184 {
185 ++eventCounter;
186
187 readStopWatch.Stop();
188
189 if(eventCounter > skipEvents)
190 {
191 procStopWatch.Start();
192 modularDelphes->ProcessTask();
193 procStopWatch.Stop();
194
195 reader->AnalyzeEvent(branchEvent, eventCounter, &readStopWatch, &procStopWatch);
196
197 treeWriter->Fill();
198
199 treeWriter->Clear();
200 }
201
202 modularDelphes->Clear();
203 reader->Clear();
204
205 readStopWatch.Start();
206 }
207 progressBar.Update(ftello(inputFile), eventCounter);
208 }
209
210 fseek(inputFile, 0L, SEEK_END);
211 progressBar.Update(ftello(inputFile), eventCounter, kTRUE);
212 progressBar.Finish();
213
214 if(inputFile != stdin) fclose(inputFile);
215
216 ++i;
217 } while(i < argc);
218
219 modularDelphes->FinishTask();
220 treeWriter->Write();
221
222 cout << "** Exiting..." << endl;
223
224 delete reader;
225 delete modularDelphes;
226 delete confReader;
227 delete treeWriter;
228 delete outputFile;
229
230 return 0;
231 }
232 catch(runtime_error &e)
233 {
234 if(treeWriter) delete treeWriter;
235 if(outputFile) delete outputFile;
236 cerr << "** ERROR: " << e.what() << endl;
237 return 1;
238 }
239}
Note: See TracBrowser for help on using the repository browser.