er  dev
ERDigitizer.cxx
1 #include "ERDigitizer.h"
2 
3 #include <vector>
4 
5 #include "TVector3.h"
6 #include "TGeoMatrix.h"
7 #include "TMath.h"
8 #include "TRandom3.h"
9 
10 #include "FairRootManager.h"
11 #include "FairRunSim.h"
12 #include "FairRuntimeDb.h"
13 #include "FairLogger.h"
14 
15 #include "ERPoint.h"
16 #include "ERDecayMCEventHeader.h"
17 
18 #include <iostream>
19 #include <algorithm>
20 using namespace std;
21 
22 #include "ERDetectorList.h"
23 
24 // ----------------------------------------------------------------------------
26  : ERTask("ER common digitization")
27 {
28  fAvailibleRunManagers.push_back("ERRunSim");
29  fAvailibleRunManagers.push_back("ERRunAna");
30 }
31 // ----------------------------------------------------------------------------
32 
33 // ----------------------------------------------------------------------------
35  : ERTask(name)
36 {
37  fAvailibleRunManagers.push_back("ERRunSim");
38  fAvailibleRunManagers.push_back("ERRunAna");
39 }
40 // ----------------------------------------------------------------------------
41 
42 // ----------------------------------------------------------------------------
43 ERDigitizer::ERDigitizer(TString name, Int_t verbose)
44  : ERTask(name, verbose)
45 {
46  fAvailibleRunManagers.push_back("ERRunSim");
47  fAvailibleRunManagers.push_back("ERRunAna");
48 }
49 // ----------------------------------------------------------------------------
50 
51 // ----------------------------------------------------------------------------
53 {
54 }
55 // ----------------------------------------------------------------------------
56 
57 // ----------------------------------------------------------------------------
58 InitStatus ERDigitizer::Init()
59 {
60  // Get input array
61  FairRootManager* ioman = FairRootManager::Instance();
62  if ( ! ioman ) Fatal("Init", "No FairRootManager");
63 
64  TList* allBranchNames = ioman->GetBranchNameList();;
65  TIter nextBranch(allBranchNames);
66  TObjString* bName;
67  vector<TString> pointBranches;
68  while (bName = (TObjString*)nextBranch()){
69  if (bName->GetString().Contains(fName) && bName->GetString().Contains("Point")){
70  pointBranches.push_back(bName->GetString());
71  }
72  }
73 
74  for (Int_t iBranch = 0; iBranch < pointBranches.size(); iBranch++){
75  TString pBranch = pointBranches[iBranch];
76  fSenVolPoints[pBranch] = (TClonesArray*) ioman->GetObject(pBranch);
77  fSenVolDigis[pBranch] = new TClonesArray("ERDigi");
78  ioman->Register(pBranch(0,pBranch.Length()-5)+"Digi", fName, fSenVolDigis[pBranch], kTRUE);
79  }
80  return kSUCCESS;
81 }
82 // -------------------------------------------------------------------------
83 
84 // ----- Public method Exec --------------------------------------------
85 void ERDigitizer::Exec(Option_t* opt){
86 
87  Reset();
88 
89 
90  for(const auto &itSenVol: fSenVolPoints){
91  TClonesArray* senVolPoints = itSenVol.second;
92  //Sort the points by volNb
93  map<Int_t, vector<Int_t>> points;
94  for (Int_t iPoint = 0; iPoint < senVolPoints->GetEntriesFast(); iPoint++){
95  ERPoint* point = (ERPoint*)senVolPoints->At(iPoint);
96  points[point->GetVolNb()].push_back(iPoint);
97  }
98 
99  for(const auto &itVol: points){
100  fEdep = 0.; //sum edep in vol
101  fTime = numeric_limits<float>::max(); // min time in vol
102 
103  for (const auto itPoint: itVol.second){
104  ERPoint* point = (ERPoint*)(senVolPoints->At(itPoint));
105  fEdep += point->GetEnergyLoss();
106 
107  if (point->GetTime() < fTime){
108  fTime = point->GetTime();
109  }
110  }
111 
112 
113  TString volName = itSenVol.first(fName.Length(),itSenVol.first.Length()-fName.Length()-5);
114 
115  if (fSenVolErrors.find(volName) == fSenVolErrors.end()){
116  cout << "Error rule for vol name " << volName << " not found!!!" << endl;
117  }
118  else{
119  ERDigitizerError error = fSenVolErrors[volName];
120  Float_t sigma = error.a + error.b*TMath::Sqrt(fEdep) + error.c*fEdep;
121  fEdep = gRandom->Gaus(fEdep, sigma);
122  }
123  /*
124  if (edep < fElossThreshold)
125  continue;
126 
127  time = gRandom->Gaus(time, fTimeSigma);
128  */
129  fVolNb = itVol.first;
130  ERDigi *digi = AddDigi(fSenVolDigis[itSenVol.first]);
131  /*
132  for (const auto itPoint: itSensor.second){
133  digi->AddLink(FairLink("RTelescopePoint", itPoint));
134  }*/
135  }
136  }
137 }
138 //----------------------------------------------------------------------------
139 
140 //----------------------------------------------------------------------------
141 void ERDigitizer::AddTrigger(TString stationSID, Int_t value, Int_t priority)
142 {
143 
144  for (const auto& trigger : fTriggers){
145  if (trigger.first == stationSID)
146  LOG(FATAL) << "Trigger with station SID " << stationSID
147  << " already exist!" << FairLogger::endl;
148  if (trigger.second.fValue == value)
149  LOG(FATAL) << "Trigger with value " << value
150  << " already exist!" << FairLogger::endl;
151  if (trigger.second.fPriority == priority)
152  LOG(FATAL) << "Trigger with priority " << priority
153  << " already exist!" << FairLogger::endl;
154  }
155 
156  fTriggers[stationSID] = ERTrigger(value, priority);
157 
158 }
159 //----------------------------------------------------------------------------
160 
161 //----------------------------------------------------------------------------
162 void ERDigitizer::ApplyTrigger(TString stationSID, TClonesArray* digiCollection)
163 {
164  // The functionality of working with triggers is currently implemented only for ERDecayMCEventHeader
165  // and, accordingly, ERRunSim. In the future, this functionality should be made available for use
166  // from ERRunAna.
167 
168  if (fRun->ClassName() == TString("ERRunSim")){ // check Run class
169  // check header class
170  if ((((FairRunSim*)fRun)->GetMCEventHeader())->ClassName() != TString("ERDecayMCEventHeader"))
171  return;
172 
173  ERDecayMCEventHeader* header = (ERDecayMCEventHeader*)((FairRunSim*)fRun)->GetMCEventHeader();
174 
175  // Trigger only applied if stationSID is SID of trigger station
176  // and if there is signal in station (which means there are digis)
177  if ( (fTriggers.find(stationSID) != fTriggers.end()) && (digiCollection->GetEntriesFast() > 0) ){
178 
179  // It is possible to overwrite the current trigger value only if the priority is higher
180  // than the recorded one.
181  if (fTriggers[stationSID].fPriority > header->GetTriggerPriority()){
182 
183  header->SetTrigger(fTriggers[stationSID].fValue);
184  header->SetTriggerPriority(fTriggers[stationSID].fPriority);
185  }
186 
187  }
188 
189  }
190 
191 }
192 //----------------------------------------------------------------------------
193 
194 //----------------------------------------------------------------------------
196 {
197  for(const auto &itSenVolDigi: fSenVolDigis){
198  itSenVolDigi.second->Clear();
199  }
200 }
201 // ----------------------------------------------------------------------------
202 
203 // ----------------------------------------------------------------------------
205 {
206 
207 }
208 // ----------------------------------------------------------------------------
209 
210 // ----------------------------------------------------------------------------
211 ERDigi* ERDigitizer::AddDigi(TClonesArray* digis) {
212  ERDigi *digi = new((*digis)[digis->GetEntriesFast()])
213  ERDigi(fEdep, fTime, fVolNb);
214  return digi;
215 }
216 // ----------------------------------------------------------------------------
217 void ERDigitizer::AddError(TString volName,Float_t a, Float_t b, Float_t c) {
218  fSenVolErrors[volName] = ERDigitizerError(a,b,c);
219 }
220 //-----------------------------------------------------------------------------
221 ClassImp(ERDigitizer)
Definition: ERDigi.h:15
virtual void Exec(Option_t *opt)
Definition: ERDigitizer.cxx:85
virtual void Reset()
FairRun * fRun
Pointer to run manager object.
Definition: ERTask.h:63
The data class for storing pieces of charged tracks in sensitive volumes in NeuRad.
Definition: ERPoint.h:23
std::vector< TString > fAvailibleRunManagers
Run managers that are availible for this task.
Definition: ERTask.h:64
virtual InitStatus Init()
Definition: ERDigitizer.cxx:58
Base abstract class for all tasks in er.
Definition: ERTask.h:27
virtual void Finish()