PandaRoot
BSEmcMCDepositToTimebasedWaveforms.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 BSEMCMCDEPOSITTOTIMEBASEDWAVEFORMS_HH
10 #define BSEMCMCDEPOSITTOTIMEBASEDWAVEFORMS_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 "PndTCAConstContainer.h"
31 
33 #include "BSEmcDigiPar.h"
34 #include "BSEmcMCDeposit.h"
35 #include "BSEmcWaveform.h"
36 #include "BSEmcWaveformBuffer.h"
37 #include "BSEmcWaveformData.h"
38 
48 template <class ParSet>
50  public:
51  // Constructors
52  BSEmcMCDepositToTimebasedWaveforms(const std::string &t_detectorname, Bool_t t_storewaves = kTRUE)
53  : PndPersistencyTask("BSEmcMCDepositToTimebasedWaveforms"), fDetectorName(t_detectorname)
54  {
55  SetPersistency(t_storewaves);
56  }
57 
58  // Destructor
60 
71  virtual InitStatus Init() /*override*/
72  {
73  if (fMCDepositBranchName == "") {
75  }
76 
77  if (fWaveformBranchName == "") {
79  }
80  if (fWaveformDataBranchName == "") {
82  }
83 
84  if (fSimulator == nullptr) {
85  LOG(debug) << "BSEmcMCDepositToTimebasedWaveforms for " << fMCDepositBranchName << " has no Simulator set. Calling SetupSimulator!";
88  }
89  if (fSimulator == nullptr) {
90  LOG(error) << "BSEmcMCDepositToTimebasedWaveforms for " << fMCDepositBranchName << " has no Simulator set. Aborting!";
91  exit(-1);
92  }
93 
94  // Get RootManager
95  FairRootManager *ioman = FairRootManager::Instance();
96  if (ioman == nullptr) {
97  LOG(error) << "BSEmcMCDepositToTimebasedWaveforms<ParSet>::Init: "
98  << "RootManager not instantiated!";
99  return kFATAL;
100  }
102  fMCDepositArray.SetTCA(dynamic_cast<TClonesArray *>(ioman->GetObject(fMCDepositBranchName)));
103  // Create and activiate output Buffer....choose between BSEmcWaveform and BSEmcMultiWaveform
104  fWaveformBuffer = new BSEmcWaveformBuffer(fWaveformBranchName, "BSEmcMultiWaveform", "Emc", GetPersistency());
105  LOG(debug) << "Using BSEmcMultiWaveform in WaveformBuffer.";
106 
107  fWaveformBuffer = (BSEmcWaveformBuffer *)ioman->RegisterWriteoutBuffer(fWaveformBranchName, fWaveformBuffer);
108  fWaveformBuffer->ActivateBuffering(fActivateBuffering);
109 
110  if (fStoreDataClass) {
112  }
113 
115 
116  if (fUse_photon_statistic) {
117  LOG(debug) << "BSEmcMCDepositToTimebasedWaveforms: " << fDetectorName << " using photon statistic";
121  } else {
123  fExcessNoiseFactor = 1;
124  }
126  LOG(debug) << "BSEmcMCDepositToTimebasedWaveforms::Init() for " << fMCDepositBranchName << " done.";
127  return kSUCCESS;
128  }
129 
139  virtual void Exec(Option_t * /*t_opt*/) /*override*/
140  {
141  LOG(debug) << "BSEmcMCDepositToTimebasedWaveforms<ParSet> " << fDetectorName << " Exec() ";
142  FairRootManager *ioman = FairRootManager::Instance();
143 
144  TStopwatch timer;
145  timer.Start();
146 
147  const BSEmcMCDeposit *theDeposit = nullptr;
148 
149  Int_t nDeposits = fMCDepositArray.GetSize();
150  LOG(debug) << "BSEmcMCDepositToTimebasedWaveforms<ParSet> " << fDetectorName << " Deposit array contains " << nDeposits << " deposits";
151 
152  for (Int_t iDeposit = 0; iDeposit < nDeposits; iDeposit++) {
153 
154  theDeposit = fMCDepositArray.GetConstElementPtr(iDeposit);
155  Double_t energy = theDeposit->GetEnergy();
156  if (energy == 0) {
157  continue;
158  }
159  LOG(debug3) << "deposit energy : " << energy;
160  if (fUse_photon_statistic) {
161  Double_t crystalPhotonsMeV = 1.0e3 * energy * fNPhotoElectronsPerMeV;
162  Double_t photonStatFactor = gRandom->Gaus(1, sqrt(fExcessNoiseFactor / crystalPhotonsMeV));
163  LOG(debug3) << "photonStatFactor: " << photonStatFactor;
164  energy *= photonStatFactor;
165  LOG(debug3) << "energy now: " << energy;
166  // energy *= 1e3;
167  LOG(debug3) << "energy now in MeV: " << energy;
168  }
169  // construct corresponding waveform data Object
170  BSEmcWaveformData wfData(theDeposit->GetDetectorID(), fSimulator);
172  // register deposit...timebased framework uses ns, whereas emc deals with seconds as time unit
173  FairLink linkToDeposit(-1, ioman->GetEntryNr(), fMCDepositBranchName, iDeposit, 1.0);
174  if (fActivateBuffering) {
175  wfData.AddDeposit(linkToDeposit, ioman->GetEventTime() + theDeposit->GetTime() * 1.0e9, energy);
176  } else {
177  wfData.AddDeposit(linkToDeposit, theDeposit->GetTime() * 1.0e9, energy);
178  }
179  LOG(debug) << "BSEmcMCDepositToTimebasedWaveforms " << fDetectorName << " adding deposit with event time " << ioman->GetEventTime()
180  << " and deposit time: " << theDeposit->GetTime() * 1.0e9 << " and energy: " << energy;
181  fWaveformBuffer->FillNewData(&wfData);
182  }
183  timer.Stop();
184  Double_t rtime = timer.RealTime();
185  Double_t ctime = timer.CpuTime();
186  LOG(debug) << "BSEmcMCDepositToTimebasedWaveforms, Real time " << rtime << " s, CPU time " << ctime << " s";
187  }
188 
189  void RunTimebased(Bool_t t_timebased = kTRUE) { fActivateBuffering = t_timebased; };
190  void SetSimulator(BSEmcAbsWaveformSimulator *t_simulator) { fSimulator = t_simulator; }
191  void SetStorageOfData(Bool_t t_storeWaves = kTRUE) { SetPersistency(t_storeWaves); };
192  void StoreDataClass(Bool_t t_storeData = kTRUE) { fStoreDataClass = t_storeData; };
193 
194  void SetMCDepositBranchName(const TString &t_branchName) { fMCDepositBranchName = t_branchName; }
195  void SetWaveformBranchName(const TString &t_branchName) { fWaveformBranchName = t_branchName; }
196  void SetWaveformDataBranchName(const TString &t_branchName) { fWaveformDataBranchName = t_branchName; }
197  void SetPhotonStatisticParName(const std::string &t_photonStatParName) { fPhotonStatisticParName = t_photonStatParName; }
198  void SetWaveformGenParName(const std::string &t_waveformGenParName) { fWaveformGenParName = t_waveformGenParName; }
199 
200  protected:
202  virtual void SetParContainers() /*override*/
203  {
204  // Get run and runtime database
205  FairRun *run = FairRun::Instance();
206  if (run == nullptr) {
207  Fatal("SetParContainers", "No analysis run");
208  }
209  FairRuntimeDb *db = run->GetRuntimeDb();
210  if (db == nullptr) {
211  Fatal("SetParContainers", "No runtime database");
212  }
213  // Get Emc digitisation parameter container
214  if (fWaveformGenParName == "") {
215  fWaveformGenParName = ParSet::fgParameterName;
216  }
217  fDigiPar = dynamic_cast<ParSet *>(db->getContainer(fWaveformGenParName.c_str()));
218 
219  if (fPhotonStatisticParName == "") {
221  }
222  fPhotonStatisticPar = dynamic_cast<BSEmcDigiPar *>(db->getContainer(fPhotonStatisticParName.c_str()));
223  }
224  virtual void SetupSimulator() = 0;
225 
227  TString fDetectorName{""};
228  TString fMCDepositBranchName{""};
229  TString fWaveformBranchName{""};
234 
235  Bool_t fStoreDataClass{kFALSE};
236  Bool_t fActivateBuffering{kFALSE};
237  std::string fPhotonStatisticParName{""};
239 
240  std::string fWaveformGenParName{""};
241  ParSet *fDigiPar{nullptr};
242  Bool_t fUse_photon_statistic{kFALSE};
244  Double_t fExcessNoiseFactor{0};
245  Double_t fOverlapTime{0};
247 
249 };
250 
251 #endif /*BSEMCMCDEPOSITTOTIMEBASEDWAVEFORMS_HH*/
Int_t GetUseDigiEffectiveSmearingMode() const
Definition: BSEmcDigiPar.h:31
void StoreDataClass(Bool_t t_storeData=kTRUE)
Method to specify whether waveforms are stored or not.
virtual T const * GetConstElementPtr(Int_t t_index) const
Get the Element object at t_index.
const std::string fgMultiWaveformBranchName
void SetTCA(TClonesArray *t_tca)
Set the TClonesArray address.
Int_t GetDetectorID() const
friend F32vec4 sqrt(const F32vec4 &a)
Definition: P4_F32vec4.h:28
virtual void Exec(Option_t *)
Runs the task.
Double_t GetRearCrystalSurfaceArea() const
Definition: BSEmcDigiPar.h:38
represents the deposited energy of one emc crystal from simulation
void SetPersistency(Bool_t val=kTRUE)
static const std::string fgParameterName
Definition: BSEmcDigiPar.h:26
void SetSimulator(BSEmcAbsWaveformSimulator *t_simulator)
BSEmcMCDepositToTimebasedWaveforms(const std::string &t_detectorname, Bool_t t_storewaves=kTRUE)
virtual void StoreWaveformData(TString t_branchName, TString t_folderName, Bool_t t_persistance)
virtual ssize_t GetSize() const final
Get the number of elements.
ClassDef(BSEmcMCDepositToTimebasedWaveforms, 2)
Double_t GetOverlapTime() const
Definition: BSEmcDigiPar.h:41
void SetWaveformDataBranchName(const TString &t_branchName)
void SetMCDepositBranchName(const TString &t_branchname)
void SetPhotonStatisticParName(const std::string &t_photonStatParName)
void SetWaveformBranchName(const TString &t_branchName)
void SetWaveformGenParName(const std::string &t_waveformGenParName)
void SetMCDepositBranchName(const TString &t_branchName)
ParSet * fDigiPar
Digitisation parameter container.
Abstract base class for waveform simulator.
virtual void FillNewData(BSEmcWaveformData *)
buffer for waveforms, used by BSEmcFwEndcapTimebasedWaveforms
const std::string fgWaveformDataBranchName
virtual Double_t GetTime() const
Double_t GetQuantumEfficiency() const
Definition: BSEmcDigiPar.h:35
Double_t GetSensitiveArea() const
Definition: BSEmcDigiPar.h:34
represents a simulated waveform in an emc crystal, used by BSEmcFwEndcapTimebasedWaveforms ...
void SetBranchName(const TString &t_branchname)
Set the Branch Name.
PndTCAConstContainer< BSEmcMCDeposit > fMCDepositArray
virtual Double_t GetEnergy() const
Input and Output Container implementation of PndConstContainerI using an underlying TClonesArray...
const std::string fgMCDepositBranchName
Double_t GetDetectedPhotonsPerMeV() const
Definition: BSEmcDigiPar.h:33
Double_t GetExcessNoiseFactor() const
Definition: BSEmcDigiPar.h:36
void SetOverlapTime(const Double_t t_overlap)
Container for runtime parameters that are required for the transformation from BSEmcMCDeposits to BSE...
Definition: BSEmcDigiPar.h:24
Taks to create waveforms from deposits.