er  dev
ERTextDecay.cxx
1 #include "ERTextDecay.h"
2 
3 #include <iostream>
4 #include <fstream>
5 #include <string>
6 
7 #include "TVirtualMC.h"
8 #include "TMCProcess.h"
9 #include "TMath.h"
10 
11 #include "FairRunSim.h"
12 #include "FairLogger.h"
13 
14 #include "ERDecayMCEventHeader.h"
15 
16 using namespace std;
17 
18 #include "ERMCEventHeader.h" //for ERMCEventHeader
19 
20 ERTextDecay::ERTextDecay(TString name):
21 ERDecay(name),
22 fFileName(""),
23 fNOutputs(0)
24 {
25 }
26 
27 ERTextDecay::~ERTextDecay(){
28 
29 }
30 
31 void ERTextDecay::AddOutputIon(Int_t A, Int_t Z, Int_t Q){
32  FairRunSim* run = FairRunSim::Instance();
33  TString outputIonName = fName + TString("_OutputIon");
34  FairIon* ion = new FairIon(outputIonName,A,Z,Q);
35  fOutputIons.push_back(ion);
36  run->AddNewIon(ion);
37  fOutputs.push_back(ion);
38  fNOutputs++;
39 }
40 
41 void ERTextDecay::AddOutputParticle(Int_t pdg){
42  fNOutputs++;
43  fOutputParticlesPDG.push_back(TDatabasePDG::Instance()->GetParticle(pdg));
44  fOutputs.push_back(TDatabasePDG::Instance()->GetParticle(pdg));
45 }
46 
47 Bool_t ERTextDecay::Init(){
48  if (!ERDecay::Init())
49  return kFALSE;
50 
51  for (Int_t iOut = 0; iOut < fOutputs.size(); iOut++){
52  if (TString(fOutputs[iOut]->ClassName()).Contains("FairIon")){
53  FairIon* ion = (FairIon*)fOutputs[iOut];
54  TParticlePDG* ionPDG = TDatabasePDG::Instance()->GetParticle(ion->GetName());
55  if ( ! ionPDG ) {
56  std::cerr << "ERTextDecay: Ion "<< ion->GetName() <<" not found in database!" << endl;
57  return kFALSE;
58  }
59  fOutputs[iOut] = ionPDG;
60  }
61  }
62 
63  if (fFileName == ""){
64  cerr << "File for " << fName << " decay not defined!" << endl;
65  return kFALSE;
66  }
67 
68  if (!ReadFile()){
69  cerr << "Problem in "<< fName << " decay file reading!" << endl;
70  return kFALSE;
71  }
72 
73  if (fVolumeName == ""){
74  cerr << "Volume name for decay is not defined!" << endl;
75  return kFALSE;
76  }
77 
78  return kTRUE;
79 }
80 
81 Bool_t ERTextDecay::ReadFile(){
82  TString path = TString(gSystem->Getenv("VMCWORKDIR")) + "/input/" + fFileName;
83  ifstream f;
84  f.open(path.Data());
85  if (!f.is_open()){
86  cerr << "Can`t open file " << path << endl;
87  return kFALSE;
88  }
89  //Пропускаем шапку файла
90  string header;
91  std::getline(f,header);
92 
93  Float_t E, Px, Py, Pz, Angle;
94  while(!f.eof()){
95  vector<TLorentzVector> decay;
96  f >> E;
97  for (Int_t iOutput = 0; iOutput < fNOutputs; iOutput++){
98  f >> Px >> Py >> Pz;
99  decay.push_back(TLorentzVector(Px,Py,Pz,0.));
100  if (iOutput == fNOutputs-1)
101  f >> Angle;
102  }
103  fDecays.push_back(decay);
104  }
105  cout << fDecays.size() << " events have read from file " << path << endl;
106  return kTRUE;
107 }
108 
109 Bool_t ERTextDecay::Stepping() {
110  if (gMC->CurrentEvent() > fDecays.size()-1) {
111  //Fatal("ERTextDecay::BeginEvent",TString("Events count in file")
112  // + fFileName + TString(" less currenet event number"));
113  Fatal("ERTextDecay::BeginEvent", "Events count in file %s less currenet event number", fFileName.Data());
114  }
115 
116  if(!fDecayFinish && gMC->TrackPid() == fInputIonPDG->PdgCode() && gMC->CurrentVolName() == fVolumeName){
117 
118  gMC->SetMaxStep(fStep);
119  gMC->TrackPosition(fDecayPos);
120 
121  vector<TLorentzVector> decay = fDecays[gMC->CurrentEvent()];
122 
123  if (fDecayPos.Z() > fDecayPosZ){
124 
125  gMC->TrackMomentum(fInputIonV);
126  if (fTargetMass){
127  TLorentzVector target(0,0,0,fTargetMass);
128  fInputIonV += target;
129  }
130  LOG(INFO) << "ERTextDecay: Decay with beta = " << fInputIonV.Beta() << "; Gamma = " << fInputIonV.Gamma() <<endl;
131  LOG(INFO) << "ERTextDecay: Primary ion with mom = (" << fInputIonV.Px() << "," << fInputIonV.Py()
132  << "," << fInputIonV.Pz() << ")" << endl;
133  for (Int_t iOut = 0; iOut < fOutputs.size(); iOut++){
134 
135  TParticlePDG* particle = (TParticlePDG*)fOutputs[iOut];
136  TLorentzVector particleV = decay[iOut];
137 
138  particleV.SetE(TMath::Sqrt(particleV.Px()*particleV.Px() + particleV.Py()*particleV.Py() +
139  particleV.Pz()* particleV.Pz() + particle->Mass()*particle->Mass()));
140  particleV.Boost(fInputIonV.BoostVector());
141 
142  Int_t newTrackNb;
143  gMC->GetStack()->PushTrack(1,gMC->GetStack()->GetCurrentTrackNumber(), particle->PdgCode(),
144  particleV.Px(),particleV.Py(),particleV.Pz(),
145  particleV.E(), fDecayPos.X(), fDecayPos.Y(), fDecayPos.Z(),
146  gMC->TrackTime(), 0., 0., 0.,
147  kPDecay, newTrackNb, particle->Mass(), 0);
148 
149  cout << "ERTextDecay: Added output particle with PDG = " << particle->PdgCode() << "; pos=("
150  << fDecayPos.X() << "," << fDecayPos.Y() << "," << fDecayPos.Z() << "); mom=("
151  << particleV.Px() << "," << particleV.Py() << "," << particleV.Pz() << ")" << endl;
152 
153  decay[iOut] = particleV;
154  }
155 
156  fDecayFinish = kTRUE;
157  gMC->StopTrack();
158  gMC->SetMaxStep(10000.);
159  SaveToEventHeader();
160  }
161  }
162  return kTRUE;
163 }
164 
165 void ERTextDecay::SaveToEventHeader(){
166  //@TODO
167  /*
168  FairRunSim* run = FairRunSim::Instance();
169  if (TString(run->GetMCEventHeader()->ClassName()).Contains("ERDecayMCEventHeader")){
170  ERDecayMCEventHeader* header = (ERDecayMCEventHeader*)run->GetMCEventHeader();
171  vector<TLorentzVector> decay = fDecays[gMC->CurrentEvent()];
172  header->SetDecayPos(fDecayPos.Vect());
173  header->SetInputIon(fInputIonV);
174  for (Int_t iOutput = 0; iOutput < fNOutputs; iOutput++){
175  header->AddOutputParticle(decay[iOutput]);
176  }
177  }
178  */
179 }
180 
181 
182 ClassImp(ERTextDecay)