source: HiSusy/trunk/PythiaDelphes/main41.cc @ 1

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

first import of structure, PYTHIA8 and DELPHES

File size: 15.3 KB
Line 
1#include <stdexcept>
2#include <iostream>
3#include <sstream>
4
5#include <map>
6
7#include <stdlib.h>
8#include <signal.h>
9#include <stdio.h>
10
11#include "TROOT.h"
12#include "TApplication.h"
13
14#include "TFile.h"
15#include "TObjArray.h"
16#include "TStopwatch.h"
17#include "TDatabasePDG.h"
18#include "TParticlePDG.h"
19#include "TLorentzVector.h"
20
21#include "modules/Delphes.h"
22#include "classes/DelphesStream.h"
23#include "classes/DelphesClasses.h"
24#include "classes/DelphesFactory.h"
25
26#include "ExRootAnalysis/ExRootTreeWriter.h"
27#include "ExRootAnalysis/ExRootTreeBranch.h"
28#include "ExRootAnalysis/ExRootProgressBar.h"
29
30using namespace std;
31
32static const int kBufferSize  = 1024;
33
34// main41.cc is a part of the PYTHIA event generator.
35// Copyright (C) 2012 Mikhail Kirsanov, Torbjorn Sjostrand.
36// PYTHIA is licenced under the GNU GPL version 2, see COPYING for details.
37// Please respect the MCnet Guidelines, see GUIDELINES for details.
38
39// This is a simple test program.
40// It illustrates how HepMC can be interfaced to Pythia8.
41// It studies the charged multiplicity distribution at the LHC.
42// HepMC events are output to the hepmcout41.dat file.
43// Written by Mikhail Kirsanov based on main01.cc.
44
45// WARNING: typically one needs 25 MB/100 events at the LHC.
46// Therefore large event samples may be impractical.
47
48#include "Pythia.h"
49#include "HepMCInterface.h"
50
51#include "HepMC/GenEvent.h"
52#include "HepMC/IO_GenEvent.h"
53
54// Following line is a deprecated alternative, removed in recent versions
55//#include "HepMC/IO_Ascii.h"
56//#include "HepMC/IO_AsciiParticles.h"
57
58// Following line to be used with HepMC 2.04 onwards.
59#ifdef HEPMC_HAS_UNITS
60#include "HepMC/Units.h"
61#endif
62
63using namespace Pythia8; 
64
65class HepMCConverter
66{
67public:
68
69  HepMCConverter(DelphesFactory *factory, TObjArray *particleOutputArray,
70                 TObjArray *candidateOutputArray, TObjArray *partonOutputArray);
71  ~HepMCConverter();
72
73  void Clear();
74  Bool_t EventReady();
75
76  Bool_t ReadLine(FILE *inputFile);
77
78  void AnalyzeEvent(ExRootTreeBranch *branch,
79                    TStopwatch *readStopWatch, TStopwatch *procStopWatch);
80  void AnalyzeParticle();
81  void FinalizeParticles();
82 
83private:
84
85  Int_t fEventNumber, fMPI, fProcessID, fSignalCode, fVertexCounter;
86  Double_t fScale, fAlphaQCD, fAlphaQED;
87
88  Int_t fID1, fID2;
89  Double_t fX1, fX2, fScalePDF, fPDF1, fPDF2;
90
91  Int_t fOutVertexCode, fVertexID, fInCounter, fOutCounter;
92  Double_t fX, fY, fZ, fT;
93 
94  Int_t fParticleCode, fPID, fStatus, fInVertexCode;
95  Double_t fPx, fPy, fPz, fE, fMass, fTheta, fPhi;
96 
97  Int_t fParticleCounter;
98
99  char *fBuffer;
100
101  TDatabasePDG *fPDG;
102  DelphesFactory *fFactory;
103 
104  TIterator *fItParticleOutputArray;
105 
106  TObjArray *fParticleOutputArray;
107  TObjArray *fCandidateOutputArray;
108  TObjArray *fPartonOutputArray;
109
110  map< Int_t, pair < Int_t, Int_t > > fMotherMap;   
111  map< Int_t, pair < Int_t, Int_t > > fDaughterMap;   
112};
113
114//---------------------------------------------------------------------------
115
116void HepMCConverter::Clear()
117{
118  fVertexCounter = -1;
119  fInCounter = -1;
120  fOutCounter = -1;
121  fMotherMap.clear();
122  fParticleCounter = 0;
123}
124
125//---------------------------------------------------------------------------
126
127Bool_t HepMCConverter::EventReady()
128{
129  return (fVertexCounter == 0) && (fInCounter == 0) && (fOutCounter == 0);
130}
131
132//---------------------------------------------------------------------------
133
134Bool_t HepMCConverter::ReadLine(FILE *inputFile)
135{
136  map< Int_t, pair< Int_t, Int_t > >::iterator itMotherMap;
137  map< Int_t, pair< Int_t, Int_t > >::iterator itDaughterMap;
138  char key;
139  int rc;
140
141  if(!fgets(fBuffer, kBufferSize, inputFile)) return kFALSE;
142
143  DelphesStream bufferStream(fBuffer + 1);
144
145  key = fBuffer[0];
146
147  if(key == 'E')
148  {
149    Clear();
150
151    rc = bufferStream.ReadInt(fEventNumber)
152      && bufferStream.ReadInt(fMPI)
153      && bufferStream.ReadDbl(fScale)
154      && bufferStream.ReadDbl(fAlphaQCD)
155      && bufferStream.ReadDbl(fAlphaQED)
156      && bufferStream.ReadInt(fProcessID)
157      && bufferStream.ReadInt(fSignalCode)
158      && bufferStream.ReadInt(fVertexCounter);
159
160    if(!rc)
161    {
162      cerr << "** ERROR: " << "invalid event format" << endl;
163      return kFALSE;
164    }
165  }
166  else if(key == 'F')
167  {
168    rc = bufferStream.ReadInt(fID1)
169      && bufferStream.ReadInt(fID2)
170      && bufferStream.ReadDbl(fX1)
171      && bufferStream.ReadDbl(fX2)
172      && bufferStream.ReadDbl(fScalePDF)
173      && bufferStream.ReadDbl(fPDF1)
174      && bufferStream.ReadDbl(fPDF2);
175
176    if(!rc)
177    {
178      cerr << "** ERROR: " << "invalid PDF format" << endl;
179      return kFALSE;
180    }
181  }
182  else if(key == 'V' && fVertexCounter > 0)
183  {
184    rc = bufferStream.ReadInt(fOutVertexCode)
185      && bufferStream.ReadInt(fVertexID)
186      && bufferStream.ReadDbl(fX)
187      && bufferStream.ReadDbl(fY)
188      && bufferStream.ReadDbl(fZ)
189      && bufferStream.ReadDbl(fT)
190      && bufferStream.ReadInt(fInCounter)
191      && bufferStream.ReadInt(fOutCounter);
192
193    if(!rc)
194    {
195      cerr << "** ERROR: " << "invalid vertex format" << endl;
196      return kFALSE;
197    }
198    --fVertexCounter;
199  }
200  else if(key == 'P' && fOutCounter > 0)
201  {
202    rc = bufferStream.ReadInt(fParticleCode)
203      && bufferStream.ReadInt(fPID)
204      && bufferStream.ReadDbl(fPx)
205      && bufferStream.ReadDbl(fPy)
206      && bufferStream.ReadDbl(fPz)
207      && bufferStream.ReadDbl(fE)
208      && bufferStream.ReadDbl(fMass)
209      && bufferStream.ReadInt(fStatus)
210      && bufferStream.ReadDbl(fTheta)
211      && bufferStream.ReadDbl(fPhi)
212      && bufferStream.ReadInt(fInVertexCode);
213
214    if(!rc)
215    {
216      cerr << "** ERROR: " << "invalid particle format" << endl;
217      return kFALSE;
218    }
219
220    if(fInVertexCode < 0)
221    {
222      itMotherMap = fMotherMap.find(fInVertexCode);
223      if(itMotherMap == fMotherMap.end())
224      {
225        fMotherMap[fInVertexCode] = make_pair(fParticleCounter, -1);
226      }
227      else
228      {
229        itMotherMap->second.second = fParticleCounter;
230      }
231    }
232
233    if(fInCounter <= 0)
234    {
235      itDaughterMap = fDaughterMap.find(fOutVertexCode);
236      if(itDaughterMap == fDaughterMap.end())
237      {
238        fDaughterMap[fOutVertexCode] = make_pair(fParticleCounter, fParticleCounter);
239      }
240      else
241      {
242        itDaughterMap->second.second = fParticleCounter;
243      }
244    }
245
246    AnalyzeParticle();
247
248    if(fInCounter > 0)
249    {
250      --fInCounter;
251    }
252    else
253    {
254      --fOutCounter;
255    }
256
257    ++fParticleCounter;
258  }
259 
260  return kTRUE;
261 
262}
263
264//---------------------------------------------------------------------------
265
266HepMCConverter::HepMCConverter(DelphesFactory *factory, TObjArray *particleOutputArray,
267                               TObjArray *candidateOutputArray, TObjArray *partonOutputArray) :
268  fBuffer(0), fPDG(0),
269  fFactory(factory),
270  fItParticleOutputArray(0),
271  fParticleOutputArray(particleOutputArray),
272  fCandidateOutputArray(candidateOutputArray),
273  fPartonOutputArray(partonOutputArray)
274{
275  fBuffer = new char[kBufferSize];
276   
277  fPDG = TDatabasePDG::Instance();
278  fItParticleOutputArray = fParticleOutputArray->MakeIterator();
279}
280
281//---------------------------------------------------------------------------
282
283HepMCConverter::~HepMCConverter()
284{
285  if(fItParticleOutputArray) delete fItParticleOutputArray;
286  if(fBuffer) delete[] fBuffer;
287}
288
289//---------------------------------------------------------------------------
290
291void HepMCConverter::AnalyzeEvent(ExRootTreeBranch *branch,
292                                  TStopwatch *readStopWatch, TStopwatch *procStopWatch)
293{
294  HepMCEvent *element;
295 
296  element = static_cast<HepMCEvent *>(branch->NewEntry());
297  element->Number = fEventNumber;
298
299  element->ProcessID = fProcessID;
300  element->MPI = fMPI;
301  element->Scale = fScale;
302  element->AlphaQED = fAlphaQED;
303  element->AlphaQCD = fAlphaQCD;
304
305  element->ID1 = fID1;
306  element->ID2 = fID2;
307  element->X1 = fX1;
308  element->X2 = fX2;
309  element->ScalePDF = fScalePDF;
310  element->PDF1 = fPDF1;
311  element->PDF2 = fPDF2;
312 
313  element->ReadTime = readStopWatch->RealTime();
314  element->ProcTime = procStopWatch->RealTime();
315}
316
317//---------------------------------------------------------------------------
318
319void HepMCConverter::AnalyzeParticle()
320{
321  Candidate *candidate;
322  TParticlePDG *pdgParticle;
323
324  candidate = fFactory->NewCandidate();
325 
326  candidate->PID = fPID;
327
328  candidate->Status = fStatus;
329
330  pdgParticle = fPDG->GetParticle(fPID);
331  candidate->Charge = pdgParticle ? Int_t(pdgParticle->Charge()/3.0) : -999;
332  candidate->Mass = pdgParticle ? pdgParticle->Mass() : -999.9;
333 
334  candidate->Momentum.SetPxPyPzE(fPx, fPy, fPz, fE);
335
336  candidate->M2 = 1;
337  candidate->D2 = 1;
338  if(fInCounter > 0)
339  {
340    candidate->M1 = 1;
341    candidate->Position.SetXYZT(0.0, 0.0, 0.0, 0.0);
342  }
343  else
344  {
345    candidate->M1 = fOutVertexCode;
346    candidate->Position.SetXYZT(fX, fY, fZ, fT);
347  }
348  if(fInVertexCode < 0)
349  {
350    candidate->D1 = fInVertexCode;
351  }
352  else
353  {
354    candidate->D1 = 1;
355  }
356
357  fParticleOutputArray->Add(candidate);
358 
359  if(!pdgParticle) return;
360
361  if(fStatus == 1)
362  {
363    fCandidateOutputArray->Add(candidate);
364  }
365  else if(fStatus == 2)
366  {
367    fPartonOutputArray->Add(candidate);
368  }
369}
370
371//---------------------------------------------------------------------------
372
373void HepMCConverter::FinalizeParticles()
374{
375  Candidate *candidate;
376  map< Int_t, pair< Int_t, Int_t > >::iterator itMotherMap;
377  map< Int_t, pair< Int_t, Int_t > >::iterator itDaughterMap;
378 
379  fItParticleOutputArray->Reset();
380  while((candidate = static_cast<Candidate *>(fItParticleOutputArray->Next())))
381  {
382    if(candidate->M1 > 0)
383    {
384      candidate->M1 = -1;
385      candidate->M2 = -1;
386    }
387    else
388    {
389      itMotherMap = fMotherMap.find(candidate->M1);
390      if(itMotherMap == fMotherMap.end())
391      {
392        candidate->M1 = -1;
393        candidate->M2 = -1;
394      }
395      else
396      {
397        candidate->M1 = itMotherMap->second.first;
398        candidate->M2 = itMotherMap->second.second;
399      }
400    }
401    if(candidate->D1 > 0)
402    {
403      candidate->D1 = -1;
404      candidate->D2 = -1;
405    }
406    else
407    {
408      itDaughterMap = fDaughterMap.find(candidate->D1);
409      if(itDaughterMap == fDaughterMap.end())
410      {
411        candidate->D1 = -1;
412        candidate->D2 = -1;
413      }
414      else
415      {
416        candidate->D1 = itDaughterMap->second.first;
417        candidate->D2 = itDaughterMap->second.second;
418      }
419    }
420  }
421}
422
423//---------------------------------------------------------------------------
424
425static bool interrupted = false;
426
427void SignalHandler(int sig)
428{
429        interrupted = true;
430}
431
432//---------------------------------------------------------------------------
433
434int main(int argc, char *argv[])
435{
436  char appName[] = "DelphesHepMC";
437  stringstream message;
438  FILE *inputFile = 0;
439  TFile *outputFile = 0;
440  TStopwatch readStopWatch, procStopWatch;
441  ExRootTreeWriter *treeWriter = 0;
442  ExRootTreeBranch *branchEvent = 0;
443  ExRootConfReader *confReader = 0;
444  Delphes *modularDelphes = 0;
445  DelphesFactory *factory = 0;
446  TObjArray *candidateOutputArray = 0, *particleOutputArray = 0, *partonOutputArray = 0;
447  HepMCConverter *converter = 0;
448  Int_t i;
449  Long64_t length, eventCounter;
450
451  //PYTHIA8 BEGIN
452  // Interface for conversion from Pythia8::Event to HepMC one.
453  HepMC::I_Pythia8 ToHepMC;
454  //  ToHepMC.set_crash_on_problem();
455
456  // Specify file where HepMC events will be stored.
457  HepMC::IO_GenEvent ascii_io("hepmcout41.dat", std::ios::out);
458  // Following line is a deprecated alternative, removed in recent versions
459  // HepMC::IO_Ascii ascii_io("hepmcout31.dat", std::ios::out);
460  // Line below is an eye-readable one-way output, uncomment the include above
461  // HepMC::IO_AsciiParticles ascii_io("hepmcout31.dat", std::ios::out);
462
463  // Generator. Process selection. LHC initialization. Histogram.
464  Pythia pythia;
465  pythia.readString("Beams:eCM = 8000.");   
466  pythia.readString("HardQCD:all = on");   
467  pythia.readString("PhaseSpace:pTHatMin = 20.");   
468  pythia.init();
469  //PYTHIA8 END
470
471  //DELPHES
472  if(argc < 3)
473  {
474    cout << " Usage: " << appName << " config_file" << " output_file" << " [input_file(s)]" << endl;
475    cout << " config_file - configuration file in Tcl format," << endl;
476    cout << " output_file - output file in ROOT format," << endl;
477    cout << " input_file(s) - input file(s) in HepMC format," << endl;
478    cout << " with no input_file, or when input_file is -, read standard input." << endl;
479    return 1;
480  }
481
482  signal(SIGINT, SignalHandler);
483
484  gROOT->SetBatch();
485
486  int appargc = 1;
487  char *appargv[] = {appName};
488  TApplication app(appName, &appargc, appargv);
489       
490  try
491  {
492    outputFile = TFile::Open(argv[2], "RECREATE");
493
494    if(outputFile == NULL)
495    {
496      message << "can't open " << argv[2];
497      throw runtime_error(message.str());
498    }
499
500    treeWriter = new ExRootTreeWriter(outputFile, "Delphes");
501
502    branchEvent = treeWriter->NewBranch("Event", HepMCEvent::Class());
503
504    confReader = new ExRootConfReader;
505    confReader->ReadFile(argv[1]);
506   
507    modularDelphes = new Delphes("Delphes");
508    modularDelphes->SetConfReader(confReader);
509    modularDelphes->SetTreeWriter(treeWriter);
510   
511    factory = modularDelphes->GetFactory();
512    particleOutputArray = modularDelphes->ExportArray("particles");
513    candidateOutputArray = modularDelphes->ExportArray("candidates");
514    partonOutputArray = modularDelphes->ExportArray("partons");
515
516    converter = new HepMCConverter(factory, particleOutputArray, candidateOutputArray, partonOutputArray);
517
518    modularDelphes->InitTask();
519 
520    i = 3;
521    do
522    {
523      if(interrupted) break;
524
525      if(i == argc || strcmp(argv[i], "-") == 0)
526      {
527        cout << "** Reading in memory" << endl;
528        inputFile = stdin;
529        length = -1;
530      }
531
532      ExRootProgressBar progressBar(length);
533 
534      // Loop over all objects
535      eventCounter = 0;
536      treeWriter->Clear();
537      modularDelphes->Clear();
538      converter->Clear();
539      readStopWatch.Start();
540
541      pythia.next();
542
543#ifdef HEPMC_HAS_UNITS
544      // This form with arguments is only meaningful for HepMC 2.04 onwards,
545      // and even then unnecessary if HepMC was built with GeV and mm as units.
546      HepMC::GenEvent* hepmcevt = new HepMC::GenEvent(HepMC::Units::GEV, HepMC::Units::MM);
547#else
548      // This form is needed for backwards compatibility.
549      // In HepMCInterface.cc a conversion from GeV to MeV will be done.
550      // To retain units in GeV uncomment line below.
551      //ToHepMC.set_convert_to_mev( false);
552      HepMC::GenEvent* hepmcevt = new HepMC::GenEvent();
553#endif
554     
555      // Fill HepMC event, including PDF info.
556      ToHepMC.fill_next_event( pythia, hepmcevt );
557       
558      while( converter->ReadLine(inputFile)  && !interrupted)
559        {
560          if(converter->EventReady())
561            {
562              ++eventCounter;
563             
564              converter->FinalizeParticles();
565             
566              readStopWatch.Stop();
567              procStopWatch.Start();
568              modularDelphes->ProcessTask();
569              procStopWatch.Stop();
570             
571              converter->AnalyzeEvent(branchEvent, &readStopWatch, &procStopWatch);
572             
573              treeWriter->Fill();
574             
575              treeWriter->Clear();
576              modularDelphes->Clear();
577              converter->Clear();
578             
579              readStopWatch.Start();
580            }
581        }
582      delete hepmcevt;
583
584      ++i;
585    }
586    while(i < argc);
587
588    modularDelphes->FinishTask();
589    treeWriter->Write();
590
591    cout << "** Exiting..." << endl;
592
593    delete converter;
594    delete modularDelphes;
595    delete confReader;
596    delete treeWriter;
597    delete outputFile;
598   
599    return 0;
600  }
601  catch(runtime_error &e)
602  {
603    if(treeWriter) delete treeWriter;
604    if(outputFile) delete outputFile;
605    cerr << "** ERROR: " << e.what() << endl;
606    return 1;
607  }
608}
609
Note: See TracBrowser for help on using the repository browser.