PandaRoot
BSEmcMCHitToTimebasedWaveforms.h
Go to the documentation of this file.
1 //----------------------------------------------------------------------
2 // Author List:
3 // Phil Strother Original author
4 // Dima Melnichuk - adaption for PANDA
5 // Philipp Mahlberg - integration in timebased simulation concept
6 //----------------------------------------------------------------------
7 
8 //#pragma once
9 #ifndef BSEMCMCHITTOTIMEBASEDWAVEFORMS_HH
10 #define BSEMCMCHITTOTIMEBASEDWAVEFORMS_HH
11 
12 #include <PndPersistencyTask.h>
13 #include <cassert>
14 #include <string>
15 #include <vector>
16 
17 #include "TClonesArray.h"
18 #include "TROOT.h"
19 #include "TRandom.h"
20 #include "TStopwatch.h"
21 #include "TString.h"
22 
23 #include "FairLink.h"
24 #include "FairLogger.h"
25 #include "FairRootManager.h"
26 #include "FairRun.h"
27 #include "FairRunAna.h"
28 #include "FairRuntimeDb.h"
29 
30 #include "PndTCAInputContainer.h"
31 
33 #include "BSEmcDigiPar.h"
34 #include "BSEmcMCHit.h"
36 #include "BSEmcWaveform.h"
37 #include "BSEmcWaveformBuffer.h"
38 #include "BSEmcWaveformData.h"
39 
49 template <class ParSet>
51  public:
52  // Constructors
53  BSEmcMCHitToTimebasedWaveforms(const std::string &t_detectorname, Bool_t t_storewaves = kTRUE)
54  : PndPersistencyTask("BSEmcMCHitToTimebasedWaveforms"), fDetectorName(t_detectorname)
55  {
56  SetPersistency(t_storewaves);
57  }
58 
59  // Destructor
61 
71  virtual InitStatus Init() /*override*/
72  {
73 
75  if (fWaveformBranchName == "") {
77  }
78  if (fWaveformDataBranchName == "") {
80  }
82 
83  if (fSimulator == nullptr) {
84  LOG(debug) << "BSEmcMCHitToTimebasedWaveforms for " << fHitBranchName << " has no Simulator set. Calling SetupSimulator!";
87  }
88  if (fSimulator == nullptr) {
89  LOG(error) << "BSEmcMCHitToTimebasedWaveforms for " << fHitBranchName << " has no Simulator set. Aborting!";
90  exit(-1);
91  }
92 
93  // Get RootManager
94  FairRootManager *ioman = FairRootManager::Instance();
95  if (ioman == nullptr) {
96  LOG(error) << "BSEmcMCHitToTimebasedWaveforms<ParSet>::Init: "
97  << "RootManager not instantiated!";
98  return kFATAL;
99  }
100 
101  // Create and activiate output Buffer....choose between BSEmcWaveform and BSEmcMultiWaveform
102  fWaveformBuffer = new BSEmcWaveformBuffer(fWaveformBranchName, "BSEmcMultiWaveform", "Emc", GetPersistency());
103  LOG(debug) << "Using BSEmcMultiWaveform in WaveformBuffer.";
104 
105  fWaveformBuffer = (BSEmcWaveformBuffer *)ioman->RegisterWriteoutBuffer(fWaveformBranchName, fWaveformBuffer);
106  fWaveformBuffer->ActivateBuffering(fActivateBuffering);
107 
108  if (fStoreDataClass) {
110  }
111 
113 
114  if (fUse_photon_statistic) {
115  LOG(debug) << "BSEmcMCHitToTimebasedWaveforms: " << fDetectorName << " using photon statistic";
119  } else {
121  fExcessNoiseFactor = 1;
122  }
124  LOG(debug) << "BSEmcMCHitToTimebasedWaveforms::Init() for " << fHitBranchName << " done.";
125  return kSUCCESS;
126  }
127 
137  virtual void Exec(Option_t */*t_opt*/) /*override*/
138  {
139  LOG(debug) << "BSEmcMCHitToTimebasedWaveforms<ParSet> " << fDetectorName << " Exec() ";
140  FairRootManager *ioman = FairRootManager::Instance();
141 
142  TStopwatch timer;
143  timer.Start();
144 
145  const BSEmcMCHit *theHit = nullptr;
146 
147  Int_t nHits = fHitArray.GetSize();
148  LOG(debug) << "BSEmcMCHitToTimebasedWaveforms<ParSet> " << fDetectorName << " Hit array contains " << nHits << " hits";
149 
150  for (Int_t iHit = 0; iHit < nHits; iHit++) {
151 
152  theHit = fHitArray.GetConstElementPtr(iHit);
153  Double_t energy = theHit->GetEnergy();
154  if (energy == 0) {
155  continue;
156  }
157  LOG(DEBUG3) << "hit energy : " << energy;
158  if (fUse_photon_statistic) {
159  Double_t crystalPhotonsMeV = 1.0e3 * energy * fNPhotoElectronsPerMeV;
160  Double_t photonStatFactor = gRandom->Gaus(1, sqrt(fExcessNoiseFactor / crystalPhotonsMeV));
161  LOG(DEBUG3) << "photonStatFactor: " << photonStatFactor;
162  energy *= photonStatFactor;
163  LOG(DEBUG3) << "energy now: " << energy;
164  // energy *= 1e3;
165  LOG(DEBUG3) << "energy now in MeV: " << energy;
166  }
167  // construct corresponding waveform data Object
168  BSEmcWaveformData wfData(theHit->GetDetectorID(), fSimulator);
170  // register hit...timebased framework uses ns, whereas emc deals with seconds as time unit
171  FairLink linkToHit(-1, ioman->GetEntryNr(), fHitBranchName, iHit, 1.0);
172  if (fActivateBuffering) {
173  wfData.AddHit(linkToHit, ioman->GetEventTime() + theHit->GetTime() * 1.0e9, energy);
174  } else {
175  wfData.AddHit(linkToHit, theHit->GetTime() * 1.0e9, energy);
176  }
177  LOG(debug) << "BSEmcMCHitToTimebasedWaveforms " << fDetectorName << " adding hit with event time " << ioman->GetEventTime() << " and hit time: " << theHit->GetTime() * 1.0e9
178  << " and energy: " << energy;
179  fWaveformBuffer->FillNewData(&wfData);
180  }
181  timer.Stop();
182  Double_t rtime = timer.RealTime();
183  Double_t ctime = timer.CpuTime();
184  LOG(debug) << "BSEmcMCHitToTimebasedWaveforms, Real time " << rtime << " s, CPU time " << ctime << " s";
185  }
186 
187  void RunTimebased(Bool_t t_timebased = kTRUE) { fActivateBuffering = t_timebased; };
188  void SetSimulator(BSEmcAbsWaveformSimulator *t_simulator) { fSimulator = t_simulator; }
189  void SetStorageOfData(Bool_t t_storeWaves = kTRUE) { SetPersistency(t_storeWaves); };
190  void StoreDataClass(Bool_t t_storeData = kTRUE) { fStoreDataClass = t_storeData; };
191 
192  void SetWaveformBranchName(const TString &t_branchName) { fWaveformBranchName = t_branchName; }
193  void SetWaveformDataBranchName(const TString &t_branchName) { fWaveformDataBranchName = t_branchName; }
194  void SetPhotonStatisticParName(const std::string &t_photonStatParName) { fPhotonStatisticParName = t_photonStatParName; }
195  void SetWaveformGenParName(const std::string &t_waveformGenParName) { fWaveformGenParName = t_waveformGenParName; }
196 
197  protected:
199  virtual void SetParContainers() /*override*/
200  {
201  // Get run and runtime database
202  FairRun *run = FairRun::Instance();
203  if (run == nullptr) {
204  Fatal("SetParContainers", "No analysis run");
205  }
206  FairRuntimeDb *db = run->GetRuntimeDb();
207  if (db == nullptr) {
208  Fatal("SetParContainers", "No runtime database");
209  }
210  // Get Emc digitisation parameter container
211  if (fWaveformGenParName == "") {
212  fWaveformGenParName = ParSet::fgParameterName;
213  }
214  fDigiPar = dynamic_cast<ParSet *>(db->getContainer(fWaveformGenParName.c_str()));
215 
216  if (fPhotonStatisticParName == "") {
218  }
219  fPhotonStatisticPar = dynamic_cast<BSEmcDigiPar *>(db->getContainer(fPhotonStatisticParName.c_str()));
220  }
221  virtual void SetupSimulator() = 0;
222 
224  TString fDetectorName{""};
225  TString fHitBranchName{""};
226  TString fWaveformBranchName{""};
231 
232  Bool_t fStoreDataClass{kFALSE};
233  Bool_t fActivateBuffering{kFALSE};
234  std::string fPhotonStatisticParName{""};
236 
237  std::string fWaveformGenParName{""};
238  ParSet *fDigiPar{nullptr};
239  Bool_t fUse_photon_statistic{kFALSE};
241  Double_t fExcessNoiseFactor{0};
242  Double_t fOverlapTime{0};
244 
246 };
247 
248 #endif /*BSEMCMCHITTOTIMEBASEDWAVEFORMS_HH*/
virtual InitStatus Init()
Init Task.
Int_t GetUseDigiEffectiveSmearingMode() const
Definition: BSEmcDigiPar.h:30
virtual ssize_t GetSize() const final
Get the number of elements.
const std::string fgMultiWaveformBranchName
void SetHitBranchName(const TString &t_branchname)
Int_t GetDetectorID() const
Definition: BSEmcMCHit.h:61
virtual T const * GetConstElementPtr(Int_t t_index) const
Get the Element object at t_index.
friend F32vec4 sqrt(const F32vec4 &a)
Definition: P4_F32vec4.h:28
void SetSimulator(BSEmcAbsWaveformSimulator *t_simulator)
Double_t GetRearCrystalSurfaceArea() const
Definition: BSEmcDigiPar.h:37
void SetPersistency(Bool_t val=kTRUE)
virtual void SetupSimulator()=0
static const std::string fgParameterName
Definition: BSEmcDigiPar.h:25
virtual void StoreWaveformData(TString t_branchName, TString t_folderName, Bool_t t_persistance)
ClassDef(BSEmcMCHitToTimebasedWaveforms, 2)
void SetWaveformGenParName(const std::string &t_waveformGenParName)
Double_t GetOverlapTime() const
Definition: BSEmcDigiPar.h:40
Input and Output Container implementation of PndInputContainerI using an underlying TClonesArray...
void SetWaveformBranchName(const TString &t_branchName)
Abstract base class for waveform simulator.
ParSet * fDigiPar
Digitisation parameter container.
virtual Bool_t Init(const TString &t_branchname) final
Fetch the data t_branchname in form of TClonesArray * from the FairRootManager.
void SetWaveformDataBranchName(const TString &t_branchName)
virtual void FillNewData(BSEmcWaveformData *)
buffer for waveforms, used by BSEmcFwEndcapTimebasedWaveforms
const std::string fgWaveformDataBranchName
virtual void Exec(Option_t *)
Runs the task.
Double_t GetQuantumEfficiency() const
Definition: BSEmcDigiPar.h:34
Double_t GetSensitiveArea() const
Definition: BSEmcDigiPar.h:33
represents a simulated waveform in an emc crystal, used by BSEmcFwEndcapTimebasedWaveforms ...
virtual Double_t GetEnergy() const
void RunTimebased(Bool_t t_timebased=kTRUE)
const std::string fgMCHitBranchName
PndTCAInputContainer< BSEmcMCHit > fHitArray
void SetStorageOfData(Bool_t t_storeWaves=kTRUE)
virtual Double_t GetTime() const
Definition: BSEmcMCHit.h:54
BSEmcMCHitToTimebasedWaveforms(const std::string &t_detectorname, Bool_t t_storewaves=kTRUE)
void SetPhotonStatisticParName(const std::string &t_photonStatParName)
Double_t GetDetectedPhotonsPerMeV() const
Definition: BSEmcDigiPar.h:32
represents the deposited energy of one emc crystal from simulation
Definition: BSEmcMCHit.h:32
void StoreDataClass(Bool_t t_storeData=kTRUE)
Method to specify whether waveforms are stored or not.
Double_t GetExcessNoiseFactor() const
Definition: BSEmcDigiPar.h:35
Taks to create waveforms from hits.
void SetOverlapTime(const Double_t t_overlap)
Container for runtime parameters that are required for the transformation from BSEmcMCHits to BSEmcDi...
Definition: BSEmcDigiPar.h:23