FairRoot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FairAsciiGenerator.cxx
Go to the documentation of this file.
1 /********************************************************************************
2  * Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
3  * *
4  * This software is distributed under the terms of the *
5  * GNU Lesser General Public Licence (LGPL) version 3, *
6  * copied verbatim in the file "LICENSE" *
7  ********************************************************************************/
8 // -------------------------------------------------------------------------
9 // ----- FairAsciiGenerator source file -----
10 // ----- Created 09/06/04 by V. Friese / D.Bertini -----
11 // -------------------------------------------------------------------------
12 #include "FairAsciiGenerator.h"
13 
14 #include "FairLogger.h"
15 #include "FairPrimaryGenerator.h" // for FairPrimaryGenerator
16 
17 #include <climits> // for INT_MAX
18 #include <fstream>
19 
21  : FairGenerator()
22  , fInputFile(nullptr)
23  , fFileName("")
24 {}
25 
27  : FairGenerator()
28  , fInputFile(0)
29  , fFileName(fileName)
30 {
31  // fFileName = fileName;
32  LOG(info) << "FairAsciiGenerator: Opening input file " << fileName;
33  fInputFile = new std::ifstream(fFileName);
34  if (!fInputFile->is_open()) {
35  LOG(fatal) << "Cannot open input file.";
36  }
37 
38  // fPDG=TDatabasePDG::Instance();
39 }
40 
42 
44 {
45  // Check for input file
46  if (!fInputFile->is_open()) {
47  LOG(error) << "FairAsciiGenerator: Input file not open!";
48  return kFALSE;
49  }
50 
51  // Define event variable to be read from file
52  Int_t ntracks = 0, eventID = 0;
53  Double_t vx = 0., vy = 0., vz = 0.;
54 
55  // Define track variables to be read from file
56  Int_t pdgID = 0;
57  Double_t px = 0., py = 0., pz = 0.;
58 
59  // Read event header line from input file
60  *fInputFile >> ntracks;
61  if (fInputFile->fail() || ntracks < 0 || ntracks > (INT_MAX - 1))
62  LOG(fatal) << "Error reading the number of events from event header.";
63  *fInputFile >> eventID >> vx >> vy >> vz;
64 
65  // If end of input file is reached : close it and abort run
66  if (fInputFile->eof()) {
67  LOG(info) << "FairAsciiGenerator: End of input file reached ";
68  CloseInput();
69  return kFALSE;
70  }
71 
72  LOG(info) << "FairAsciiGenerator: Event " << eventID << ", vertex = (" << vx << "," << vy << "," << vz
73  << ") cm, multiplicity " << ntracks;
74 
75  // Loop over tracks in the current event
76  for (Int_t itrack = 0; itrack < ntracks; itrack++) {
77 
78  // Read PID and momentum from file
79  *fInputFile >> pdgID >> px >> py >> pz;
80  // convert Geant3 code to PDG code
81 
82  // Int_t pdg= fPDG->ConvertGeant3ToPdg(pdgID);
83 
84  // Give track to PrimaryGenerator
85  primGen->AddTrack(pdgID, px, py, pz, vx, vy, vz);
86  }
87 
88  return kTRUE;
89 }
90 
91 void FairAsciiGenerator::CloseInput()
92 {
93  if (fInputFile) {
94  if (fInputFile->is_open()) {
95  LOG(info) << "FairAsciiGenerator: Closing input file " << fFileName;
96  fInputFile->close();
97  }
98  delete fInputFile;
99  fInputFile = nullptr;
100  }
101 }
102 
virtual Bool_t ReadEvent(FairPrimaryGenerator *primGen)
ClassImp(FairEventBuilder)
virtual void AddTrack(Int_t pdgid, Double_t px, Double_t py, Double_t pz, Double_t vx, Double_t vy, Double_t vz, Int_t parent=-1, Bool_t wanttracking=true, Double_t e=-9e9, Double_t tof=0., Double_t weight=0., TMCProcess proc=kPPrimary)