CbmRoot
CbmPlutoGenerator.cxx
Go to the documentation of this file.
1 // -------------------------------------------------------------------------
2 // ----- CbmPlutoGenerator source file -----
3 // ----- Created 13/07/04 by V. Friese / D.Bertini -----
4 // -------------------------------------------------------------------------
5 #include "CbmPlutoGenerator.h"
6 
7 #include "FairLogger.h"
8 #include "FairPrimaryGenerator.h" // for FairPrimaryGenerator
9 
10 #include "PDataBase.h" // for PDataBase
11 #include "PParticle.h" // for PParticle
12 #include "PStaticData.h" // for PStaticData
13 
14 #include "TChain.h" // for TChain
15 #include "TClonesArray.h" // for TClonesArray
16 #include "TDatabasePDG.h" // for TDatabasePDG
17 #include "TFile.h" // for TFile
18 #include "TLorentzVector.h" // for TLorentzVector
19 #include "TTree.h" // for TTree
20 #include "TVector3.h" // for TVector3
21 #include <iosfwd> // for ostream
22 
23 //#include <stddef.h> // for NULL
24 #include <iostream> // for operator<<, basic_ostream, etc
25 #include <sys/stat.h>
26 
27 // ----- Default constructor ------------------------------------------
29  : FairGenerator()
30  , fdata(makeStaticData())
31  , fbase(makeDataBase())
32  , iEvent(0)
33  , fFileName(nullptr)
34  , fInputChain(nullptr)
35  , fParticles(nullptr)
36  , fPDGmanual(0) {}
37 // ------------------------------------------------------------------------
38 
39 // ----- Standard constructor -----------------------------------------
40 CbmPlutoGenerator::CbmPlutoGenerator(const Char_t* fileName)
41  : FairGenerator()
42  , fdata(makeStaticData())
43  , fbase(makeDataBase())
44  , iEvent(0)
45  , fFileName(fileName)
46  , fInputChain(nullptr)
47  , fParticles(new TClonesArray("PParticle", 100))
48  , fPDGmanual(0) {
49  fInputChain = new TChain("data");
50  CheckFileExist(fileName);
51 
52  fInputChain->Add(fileName);
53  fInputChain->SetBranchAddress("Particles", &fParticles);
54 }
55 // ------------------------------------------------------------------------
56 
57 // ----- Constructor with file list -----------------------------------------
58 CbmPlutoGenerator::CbmPlutoGenerator(std::vector<std::string> fileNames)
59  : FairGenerator()
60  , fdata(makeStaticData())
61  , fbase(makeDataBase())
62  , iEvent(0)
63  , fFileName()
64  , fInputChain(nullptr)
65  , fParticles(new TClonesArray("PParticle", 100))
66  , fPDGmanual(0) {
67  fInputChain = new TChain("data");
68  for (auto& name : fileNames) {
69  CheckFileExist(name);
70  fInputChain->Add(name.c_str());
71  }
72  fInputChain->SetBranchAddress("Particles", &fParticles);
73 }
74 
75 
76 // ----- Destructor ---------------------------------------------------
78  // remove Pluto database
79  delete fdata;
80  delete fbase;
81  CloseInput();
82 }
83 // ------------------------------------------------------------------------
84 
85 
86 // ----- Public method ReadEvent --------------------------------------
87 Bool_t CbmPlutoGenerator::ReadEvent(FairPrimaryGenerator* primGen) {
88 
89  // Check for input file
90  if (!fInputChain) {
91  LOG(error) << "CbmPlutoGenerator: Input file not open!";
92  return kFALSE;
93  }
94 
95  // Check for number of events in input file
96  if (iEvent > fInputChain->GetEntries()) {
97  LOG(error) << "CbmPlutoGenerator: No more events in input file!";
98  CloseInput();
99  return kFALSE;
100  }
101  fInputChain->GetEntry(iEvent++);
102 
103  // Get PDG database
104  TDatabasePDG* dataBase = TDatabasePDG::Instance();
105 
106  // Get number of particle in TClonesrray
107  Int_t nParts = fParticles->GetEntriesFast();
108 
109  // Loop over particles in TClonesArray
110  for (Int_t iPart = 0; iPart < nParts; iPart++) {
111  PParticle* part = (PParticle*) fParticles->At(iPart);
112 
113  Int_t* pdgType = 0x0;
114  Bool_t found = fbase->GetParamInt("pid", part->ID(), "pythiakf", &pdgType);
115  // TODO: replace by fdata->GetParticleKF(part->ID()); as soon as FairSoft uses pluto version 5.43 or higher and remove fbase
116 
117  // Check if particle type is known to database
118  if (!found) {
119  LOG(warn) << "CbmPlutoGenerator: Unknown type " << part->ID()
120  << ", skipping particle.";
121  continue;
122  }
123  LOG(info) << iPart << " Particle (geant " << part->ID() << " PDG "
124  << *pdgType << " -> "
125  << dataBase->GetParticle(*pdgType)->GetName();
126 
127  // set PDG by hand for pluto dilepton pairs and other not defined codes in pluto
128  Int_t dielectron = 99009911;
129  Int_t dimuon = 99009913;
130  if (fPDGmanual && *pdgType == 0) {
131  pdgType = &fPDGmanual;
132  LOG(warn) << "PDG code changed by user defintion to " << *pdgType;
133  } else if (part->ID() == 51)
134  pdgType = &dielectron;
135  else if (part->ID() == 50)
136  pdgType = &dimuon;
137 
138  // get the mother
139  Int_t parIdx = part->GetParentIndex();
140  // get daughter
141  Int_t idx = part->GetDaughterIndex();
142 
143  TLorentzVector mom = part->Vect4();
144  Double_t px = mom.Px();
145  Double_t py = mom.Py();
146  Double_t pz = mom.Pz();
147  Double_t ee = mom.E();
148 
149  TVector3 vertex = part->getVertex();
150  Double_t vx = vertex.x();
151  Double_t vy = vertex.y();
152  Double_t vz = vertex.z();
153 
154  Bool_t wanttracking = kTRUE;
155  if (idx > -1) wanttracking = kFALSE; // only tracking for decay products
156  Int_t parent = parIdx;
157  LOG(info) << "Add particle with parent at index " << parIdx
158  << " and do tracking " << wanttracking;
159 
160  // Give track to PrimaryGenerator
161  primGen->AddTrack(
162  *pdgType, px, py, pz, vx, vy, vz, parent, wanttracking, ee);
163 
164  } // Loop over particle in event
165 
166  return kTRUE;
167 }
168 // ------------------------------------------------------------------------
169 
170 
171 // ----- Private method CloseInput ------------------------------------
173  if (fInputChain) {
174  LOG(info) << "CbmPlutoGenerator: Closing input file " << fFileName;
175  delete fInputChain;
176  }
177  fInputChain = nullptr;
178 }
179 // ------------------------------------------------------------------------
180 
181 void CbmPlutoGenerator::CheckFileExist(std::string filename) {
182  struct stat buffer;
183  if (stat(filename.c_str(), &buffer) == 0) {
184  LOG(info) << "CbmPlutoGenerator: Add file " << filename
185  << " to input chain";
186  } else {
187  LOG(fatal) << "Input File " << filename << " not found";
188  }
189 }
190 
CbmPlutoGenerator::CbmPlutoGenerator
CbmPlutoGenerator()
Definition: CbmPlutoGenerator.cxx:28
PParticle
Definition: PParticle.h:22
PParticle.h
PParticle::Vect4
TLorentzVector Vect4() const
Definition: PParticle.h:62
CbmPlutoGenerator::ReadEvent
virtual Bool_t ReadEvent(FairPrimaryGenerator *primGen)
Definition: CbmPlutoGenerator.cxx:87
CbmPlutoGenerator::fbase
PDataBase * fbase
pluto static data
Definition: CbmPlutoGenerator.h:68
CbmPlutoGenerator::iEvent
Int_t iEvent
pluto data base
Definition: CbmPlutoGenerator.h:70
CbmPlutoGenerator::CloseInput
void CloseInput()
forced pdg value for undefined pluto codes
Definition: CbmPlutoGenerator.cxx:172
CbmPlutoGenerator.h
CbmPlutoGenerator::~CbmPlutoGenerator
virtual ~CbmPlutoGenerator()
Definition: CbmPlutoGenerator.cxx:77
PParticle::getVertex
TVector3 & getVertex()
Definition: PParticle.h:73
CbmPlutoGenerator
Definition: CbmPlutoGenerator.h:34
PDataBase.h
CbmPlutoGenerator::fParticles
TClonesArray * fParticles
Pointer to input file.
Definition: CbmPlutoGenerator.h:73
makeStaticData
PStaticData * makeStaticData()
Definition: PStaticData.cxx:29
makeDataBase
PDataBase * makeDataBase()
Definition: PDataBase.cxx:26
CbmPlutoGenerator::fFileName
const Char_t * fFileName
Event number.
Definition: CbmPlutoGenerator.h:71
buffer
@ buffer
Definition: CbmMvdSensorPlugin.h:22
PParticle::GetParentIndex
Int_t GetParentIndex() const
Definition: PParticle.h:69
PParticle::ID
Int_t ID() const
Definition: PParticle.h:60
ClassImp
ClassImp(CbmConverterManager) InitStatus CbmConverterManager
Definition: CbmConverterManager.cxx:12
PParticle::GetDaughterIndex
Int_t GetDaughterIndex() const
Definition: PParticle.h:70
CbmPlutoGenerator::fdata
PStaticData * fdata
Definition: CbmPlutoGenerator.h:67
PStaticData.h
PDataBase::GetParamInt
Int_t GetParamInt(const char *paramname, Int_t length=-1)
Definition: PDataBase.cxx:188
CbmPlutoGenerator::fInputChain
TChain * fInputChain
Input file name.
Definition: CbmPlutoGenerator.h:72
CbmPlutoGenerator::fPDGmanual
Int_t fPDGmanual
Particle array from PLUTO.
Definition: CbmPlutoGenerator.h:74
CbmPlutoGenerator::CheckFileExist
void CheckFileExist(std::string filename)
Definition: CbmPlutoGenerator.cxx:181