Fork me on GitHub

source: svn/trunk/readers/DelphesLHEF.cpp@ 1177

Last change on this file since 1177 was 1130, checked in by Pavel Demin, 12 years ago

add MaxEvents and SkipEvents

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