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