source: HiSusy/trunk/Delphes-3.0.0/readers/DelphesLHEF.cpp @ 1

Last change on this file since 1 was 1, checked in by zerwas, 12 years ago

first import of structure, PYTHIA8 and DELPHES

File size: 9.7 KB
Line 
1#include <stdexcept>
2#include <iostream>
3#include <sstream>
4
5#include <stdlib.h>
6#include <signal.h>
7#include <stdio.h>
8
9#include "TROOT.h"
10#include "TApplication.h"
11
12#include "TFile.h"
13#include "TObjArray.h"
14#include "TStopwatch.h"
15#include "TDatabasePDG.h"
16#include "TParticlePDG.h"
17#include "TLorentzVector.h"
18
19#include "modules/Delphes.h"
20#include "classes/DelphesStream.h"
21#include "classes/DelphesClasses.h"
22#include "classes/DelphesFactory.h"
23
24#include "ExRootAnalysis/ExRootTreeWriter.h"
25#include "ExRootAnalysis/ExRootTreeBranch.h"
26#include "ExRootAnalysis/ExRootProgressBar.h"
27
28using namespace std;
29
30static const int kBufferSize  = 1024;
31
32class LHEFConverter
33{
34public:
35
36  LHEFConverter(DelphesFactory *factory, TObjArray *particleOutputArray,
37                TObjArray *candidateOutputArray, TObjArray *partonOutputArray);
38  ~LHEFConverter();
39
40  void Clear();
41  Bool_t EventReady();
42
43  Bool_t ReadLine(FILE *inputFile);
44
45  void AnalyzeEvent(ExRootTreeBranch *branch, Long64_t eventNumber,
46                    TStopwatch *readStopWatch, TStopwatch *procStopWatch);
47  void AnalyzeParticle();
48 
49private:
50
51  Int_t fParticleCounter, fProcessID;
52  Double_t fWeight, fScalePDF, fAlphaQCD, fAlphaQED;
53 
54  Int_t fPID, fStatus, fM1, fM2, fC1, fC2;
55  Double_t fPx, fPy, fPz, fE, fMass;
56 
57  Int_t fEventCounter;
58
59  char *fBuffer;
60
61  TDatabasePDG *fPDG;
62  DelphesFactory *fFactory;
63 
64  TIterator *fItParticleOutputArray;
65 
66  TObjArray *fParticleOutputArray;
67  TObjArray *fCandidateOutputArray;
68  TObjArray *fPartonOutputArray; 
69};
70
71//---------------------------------------------------------------------------
72
73void LHEFConverter::Clear()
74{
75  fEventCounter = -1;
76  fParticleCounter = -1;
77}
78
79//---------------------------------------------------------------------------
80
81Bool_t LHEFConverter::EventReady()
82{
83  return (fEventCounter == 0) && (fParticleCounter == 0);
84}
85
86//---------------------------------------------------------------------------
87
88Bool_t LHEFConverter::ReadLine(FILE *inputFile)
89{
90  int rc;
91
92  if(!fgets(fBuffer, kBufferSize, inputFile)) return kFALSE;
93
94  DelphesStream bufferStream(fBuffer);
95
96  if(strstr(fBuffer, "<event"))
97  {
98    Clear();
99    fEventCounter = 1;
100  }
101  else if(fEventCounter > 0)
102  {
103    rc = bufferStream.ReadInt(fParticleCounter)
104      && bufferStream.ReadInt(fProcessID)
105      && bufferStream.ReadDbl(fWeight)
106      && bufferStream.ReadDbl(fScalePDF)
107      && bufferStream.ReadDbl(fAlphaQED)
108      && bufferStream.ReadDbl(fAlphaQCD);
109
110    if(!rc)
111    {
112      cerr << "** ERROR: " << "invalid event format" << endl;
113      return kFALSE;
114    }
115
116    --fEventCounter;
117  }
118  else if(fParticleCounter > 0)
119  {
120    rc = bufferStream.ReadInt(fPID)
121      && bufferStream.ReadInt(fStatus)
122      && bufferStream.ReadInt(fM1)
123      && bufferStream.ReadInt(fM2)
124      && bufferStream.ReadInt(fC1)
125      && bufferStream.ReadInt(fC2)
126      && bufferStream.ReadDbl(fPx)
127      && bufferStream.ReadDbl(fPy)
128      && bufferStream.ReadDbl(fPz)
129      && bufferStream.ReadDbl(fE)
130      && bufferStream.ReadDbl(fMass);
131
132    if(!rc)
133    {
134      cerr << "** ERROR: " << "invalid particle format" << endl;
135      return kFALSE;
136    }
137
138    AnalyzeParticle();
139
140    --fParticleCounter;
141  }
142 
143  return kTRUE;
144}
145
146//---------------------------------------------------------------------------
147
148LHEFConverter::LHEFConverter(DelphesFactory *factory, TObjArray *particleOutputArray,
149                             TObjArray *candidateOutputArray, TObjArray *partonOutputArray) :
150  fBuffer(0), fPDG(0),
151  fFactory(factory),
152  fItParticleOutputArray(0),
153  fParticleOutputArray(particleOutputArray),
154  fCandidateOutputArray(candidateOutputArray),
155  fPartonOutputArray(partonOutputArray)
156{
157  fBuffer = new char[kBufferSize];
158   
159  fPDG = TDatabasePDG::Instance();
160  fItParticleOutputArray = fParticleOutputArray->MakeIterator();
161}
162
163//---------------------------------------------------------------------------
164
165LHEFConverter::~LHEFConverter()
166{
167  if(fItParticleOutputArray) delete fItParticleOutputArray;
168  if(fBuffer) delete[] fBuffer;
169}
170
171//---------------------------------------------------------------------------
172
173void LHEFConverter::AnalyzeEvent(ExRootTreeBranch *branch, Long64_t eventNumber,
174                                 TStopwatch *readStopWatch, TStopwatch *procStopWatch)
175{
176  LHEFEvent *element;
177 
178  element = static_cast<LHEFEvent *>(branch->NewEntry());
179  element->Number = eventNumber;
180
181  element->ProcessID = fProcessID;
182  element->Weight = fWeight;
183  element->ScalePDF = fScalePDF;
184  element->AlphaQED = fAlphaQED;
185  element->AlphaQCD = fAlphaQCD;
186
187  element->ReadTime = readStopWatch->RealTime();
188  element->ProcTime = procStopWatch->RealTime();
189}
190
191//---------------------------------------------------------------------------
192
193void LHEFConverter::AnalyzeParticle()
194{
195  Candidate *candidate;
196  TParticlePDG *pdgParticle;
197
198  candidate = fFactory->NewCandidate();
199 
200  candidate->PID = fPID;
201
202  candidate->Status = fStatus;
203
204  pdgParticle = fPDG->GetParticle(fPID);
205  candidate->Charge = pdgParticle ? Int_t(pdgParticle->Charge()/3.0) : -999;
206  candidate->Mass = pdgParticle ? pdgParticle->Mass() : -999.9;
207 
208  candidate->Momentum.SetPxPyPzE(fPx, fPy, fPz, fE);
209  candidate->Position.SetXYZT(0.0, 0.0, 0.0, 0.0);
210
211  candidate->M1 = fM1 - 1;
212  candidate->M2 = fM2 - 1;
213
214  candidate->D1 = -1;
215  candidate->D2 = -1;
216
217  fParticleOutputArray->Add(candidate);
218 
219  if(!pdgParticle) return;
220
221  if(fStatus == 1)
222  {
223    fCandidateOutputArray->Add(candidate);
224  }
225  else if(fStatus == 2)
226  {
227    fPartonOutputArray->Add(candidate);
228  }
229}
230
231//---------------------------------------------------------------------------
232
233static bool interrupted = false;
234
235void SignalHandler(int sig)
236{
237        interrupted = true;
238}
239
240//---------------------------------------------------------------------------
241
242int main(int argc, char *argv[])
243{
244  char appName[] = "DelphesLHEF";
245  stringstream message;
246  FILE *inputFile = 0;
247  TFile *outputFile = 0;
248  TStopwatch readStopWatch, procStopWatch;
249  ExRootTreeWriter *treeWriter = 0;
250  ExRootTreeBranch *branchEvent = 0;
251  ExRootConfReader *confReader = 0;
252  Delphes *modularDelphes = 0;
253  DelphesFactory *factory = 0;
254  TObjArray *candidateOutputArray = 0, *particleOutputArray = 0, *partonOutputArray = 0;
255  LHEFConverter *converter = 0;
256  Int_t i;
257  Long64_t length, eventCounter;
258
259  if(argc < 3)
260  {
261    cout << " Usage: " << appName << " config_file" << " output_file" << " [input_file(s)]" << endl;
262    cout << " config_file - configuration file in Tcl format," << endl;
263    cout << " output_file - output file in ROOT format," << endl;
264    cout << " input_file(s) - input file(s) in LHEF format," << endl;
265    cout << " with no input_file, or when input_file is -, read standard input." << endl;
266    return 1;
267  }
268
269  signal(SIGINT, SignalHandler);
270
271  gROOT->SetBatch();
272
273  int appargc = 1;
274  char *appargv[] = {appName};
275  TApplication app(appName, &appargc, appargv);
276       
277  try
278  {
279    outputFile = TFile::Open(argv[2], "RECREATE");
280
281    if(outputFile == NULL)
282    {
283      message << "can't open " << argv[2];
284      throw runtime_error(message.str());
285    }
286
287    treeWriter = new ExRootTreeWriter(outputFile, "Delphes");
288
289    branchEvent = treeWriter->NewBranch("Event", LHEFEvent::Class());
290
291    confReader = new ExRootConfReader;
292    confReader->ReadFile(argv[1]);
293   
294    modularDelphes = new Delphes("Delphes");
295    modularDelphes->SetConfReader(confReader);
296    modularDelphes->SetTreeWriter(treeWriter);
297   
298    factory = modularDelphes->GetFactory();
299    particleOutputArray = modularDelphes->ExportArray("particles");
300    candidateOutputArray = modularDelphes->ExportArray("candidates");
301    partonOutputArray = modularDelphes->ExportArray("partons");
302
303    converter = new LHEFConverter(factory, particleOutputArray, candidateOutputArray, partonOutputArray);
304
305    modularDelphes->InitTask();
306
307    i = 3;
308    do
309    {
310      if(interrupted) break;
311
312      if(i == argc || strcmp(argv[i], "-") == 0)
313      {
314        cout << "** Reading standard input" << endl;
315        inputFile = stdin;
316        length = -1;
317      }
318      else
319      {
320        cout << "** Reading " << argv[i] << endl;
321        inputFile = fopen(argv[i], "r");
322
323        if(inputFile == NULL)
324        {
325          message << "can't open " << argv[i];
326          throw runtime_error(message.str());
327        }
328
329        fseek(inputFile, 0L, SEEK_END);
330        length = ftell(inputFile);
331        fseek(inputFile, 0L, SEEK_SET);
332
333        if(length <= 0) continue;
334      }
335
336      ExRootProgressBar progressBar(length);
337 
338      // Loop over all objects
339      eventCounter = 0;
340      treeWriter->Clear();
341      modularDelphes->Clear();
342      converter->Clear();
343      readStopWatch.Start();
344      while(converter->ReadLine(inputFile) && !interrupted)
345      {
346        if(converter->EventReady())
347        {
348          ++eventCounter;
349
350          readStopWatch.Stop();
351          procStopWatch.Start();
352          modularDelphes->ProcessTask();
353          procStopWatch.Stop();
354
355          converter->AnalyzeEvent(branchEvent, eventCounter, &readStopWatch, &procStopWatch);
356   
357          treeWriter->Fill();
358
359          treeWriter->Clear();
360          modularDelphes->Clear();
361          converter->Clear();     
362
363          readStopWatch.Start();
364        }
365        progressBar.Update(ftell(inputFile), eventCounter);
366      }
367
368      fseek(inputFile, 0L, SEEK_END);
369      progressBar.Update(ftell(inputFile), eventCounter, kTRUE);
370      progressBar.Finish();
371
372      if(inputFile != stdin) fclose(inputFile);
373
374      ++i;
375    }
376    while(i < argc);
377
378    modularDelphes->FinishTask();
379    treeWriter->Write();
380
381    cout << "** Exiting..." << endl;
382
383    delete converter;
384    delete modularDelphes;
385    delete confReader;
386    delete treeWriter;
387    delete outputFile;
388   
389    return 0;
390  }
391  catch(runtime_error &e)
392  {
393    if(treeWriter) delete treeWriter;
394    if(outputFile) delete outputFile;
395    cerr << "** ERROR: " << e.what() << endl;
396    return 1;
397  }
398}
399
Note: See TracBrowser for help on using the repository browser.