er  dev
ERNDPID.cxx
1 /********************************************************************************
2  * Copyright (C) Joint Institute for Nuclear Research *
3  * *
4  * This software is distributed under the terms of the *
5  * GNU Lesser General Public Licence version 3 (LGPL) version 3, *
6  * copied verbatim in the file "LICENSE" *
7  ********************************************************************************/
8 
9 #include "ERNDPID.h"
10 
11 #include "G4IonTable.hh"
12 #include "G4ParticleDefinition.hh"
13 #include "G4Neutron.hh"
14 #include "G4EmCalculator.hh"
15 #include "G4NistManager.hh"
16 
17 #include "TGeoManager.h"
18 
19 #include "FairLogger.h"
20 
21 #include "ERNDTrack.h"
22 #include "ERBeamDetParticle.h"
23 #include "ERSupport.h"
24 //--------------------------------------------------------------------------------------------------
25 ERNDPID::ERNDPID()
26  : ERTask("ER ND particle reconstruction.")
27 {
28 }
29 //--------------------------------------------------------------------------------------------------
30 InitStatus ERNDPID::Init() {
31  if (ERTask::Init() != kSUCCESS)
32  return kFATAL;
33  FairRootManager* ioman = FairRootManager::Instance();
34  if ( ! ioman ) Fatal("Init", "No FairRootManager");
35  fNDTracks = (TClonesArray*) ioman->GetObject("NDTrack");
36  if (!fNDTracks) Fatal("Init", "Can`t find collection NDTrack!");
37  fNDParticles = new TClonesArray("ERNDParticle",1000);
38  ioman->Register("NDParticle", "ND particle", fNDParticles, kTRUE);
39  fSetup = ERNDSetup::Instance();
40  fSetup->ReadGeoParamsFromParContainer();
41  fBeamDetParticle = (TClonesArray*) ioman->GetObject("BeamDetParticle.");
42  if (!fBeamDetParticle) {
43  LOG(FATAL) << "Branch BeamDetParticle not found." << FairLogger::endl;
44  }
45  return kSUCCESS;
46 }
47 //--------------------------------------------------------------------------------------------------
48 Double_t KineticEnergyOnTarget(const ERNDTrack& track, Double_t kineticEnergy) {
49  // Init track in back direction.
50  auto backDirection = (track.TargetVertex() - track.DetectorVertex());
51  backDirection.SetMag(1.);
52  TGeoNode* node = gGeoManager->InitTrack(
53  track.DetectorVertex().X(), track.DetectorVertex().Y(), track.DetectorVertex().Z(),
54  backDirection.X(), backDirection.Y(), backDirection.Z());
55  Bool_t targetHasPassed = kFALSE;
56  const auto particle = G4Neutron::Definition();
57  while(!gGeoManager->IsOutside()) {
58  gGeoManager->FindNextBoundary();
59  const bool trackInTarget = TString(gGeoManager->GetPath()).Contains("target");
60  if (targetHasPassed && !trackInTarget)
61  break;
62  targetHasPassed = trackInTarget;
63  const auto step = gGeoManager->GetStep();
64  // We take into account only half of step in target.
65  const auto range = trackInTarget ? step / 2 : step;
66  const TString materialName = node->GetMedium()->GetMaterial()->GetName();
67  if (materialName == TString("Stilbene")) {
68  node = gGeoManager->Step();
69  continue;
70  }
71  const auto* material = G4NistManager::Instance()->FindOrBuildMaterial(materialName.Data());
72  LOG(DEBUG) <<"[ERNDPID] [CalcEnergyDeposites]"
73  <<" Calc energy deposite for range " << range << " in materail "
74  << materialName << " with kinetic energy " << kineticEnergy << FairLogger::endl;
75  const auto deadDeposite = CalcElossIntegralVolStep(kineticEnergy, *particle, *material, range);
76  kineticEnergy += deadDeposite;
77  node = gGeoManager->Step();
78  }
79  return kineticEnergy;
80 }
81 //--------------------------------------------------------------------------------------------------
82 void ERNDPID::Exec(Option_t* opt) {
83  Reset();
84  ERBeamDetParticle* beamdet_particle = (ERBeamDetParticle*)fBeamDetParticle->At(0);
85  if (!beamdet_particle) {
86  //fRun->MarkFill(kFALSE);
87  return ;
88  }
89  const Double_t mass = 939.57;
90  const auto calcState = [mass](const ERNDTrack* track, Double_t kineticEnergy) -> TLorentzVector {
91  const Double_t momentumMag = sqrt(pow(kineticEnergy, 2) + 2 * mass * kineticEnergy);
92  TVector3 direction = (track->DetectorVertex()-track->TargetVertex());
93  direction.SetMag(1.);
94  const auto momentum = momentumMag * direction;
95  const Double_t fullEnergy = sqrt(pow(momentumMag, 2)+pow(mass, 2));
96  return TLorentzVector(momentum, fullEnergy);
97  };
98  for (Int_t iTrack(0); iTrack < fNDTracks->GetEntriesFast(); iTrack++) {
99  const auto* track = static_cast<ERNDTrack*>(fNDTracks->At(iTrack));
100  auto kineticEnergy = track->Edep();
101  TLorentzVector detectorState = calcState(track, kineticEnergy);
102  auto time_on_target = beamdet_particle->time_on_target();
103  auto time = track->Time();
104  auto tof = time - time_on_target;
105  auto distance_between_target_and_detector = (track->DetectorVertex() - track->TargetVertex()).Mag();
106  auto beta = distance_between_target_and_detector * 1e-2 / (tof * 1e-9) / TMath::C();
107 
108  if(beta <= 0 || beta >= 1) {
109  LOG(DEBUG) << "[ERNDPID] Wrong beta " << beta << FairLogger::endl;
110  return ;
111  }
112  auto gamma = 1. / TMath::Sqrt(1.- beta * beta);
113  auto p = beta * gamma * mass;
114  auto E = TMath::Sqrt(p * p + mass * mass);
115  TLorentzVector lv(p * TMath::Sin(track->Direction().Theta()) * TMath::Cos(track->Direction().Phi()),
116  p * TMath::Sin(track->Direction().Theta()) * TMath::Sin(track->Direction().Phi()),
117  p * TMath::Cos(track->Direction().Theta()),
118  E);
119  AddParticle(lv, tof);
120  }
121 }
122 //--------------------------------------------------------------------------------------------------
124  fNDParticles->Clear();
125 }
126 //--------------------------------------------------------------------------------------------------
127 ERNDParticle* ERNDPID::AddParticle(const TLorentzVector& lv, float tof) {
128  return new((*fNDParticles)[fNDParticles->GetEntriesFast()])
129  ERNDParticle(lv, tof);
130 }
131 //--------------------------------------------------------------------------------------------------
132 ClassImp(ERNDPID)
TVector3 DetectorVertex() const
Definition: ERNDTrack.h:19
virtual InitStatus Init()
Definition: ERNDPID.cxx:30
virtual void Reset()
virtual InitStatus Init()
Definition: ERTask.cxx:31
Base abstract class for all tasks in er.
Definition: ERTask.h:27
virtual void Reset()
Definition: ERNDPID.cxx:123