Fork me on GitHub

source: svn/trunk/readers/DelphesHepMC.cpp

Last change on this file was 1130, checked in by Pavel Demin, 11 years ago

add MaxEvents and SkipEvents

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