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 
75  if (fWaveformBranchName == "") {
77  }
78  if (fWaveformDataBranchName == "") {
80  }
81 
82  if (fSimulator == nullptr) {
83  LOG(debug) << "BSEmcMCDepositToTimebasedWaveforms for " << fMCDepositBranchName << " has no Simulator set. Calling SetupSimulator!";
86  }
87  if (fSimulator == nullptr) {
88  LOG(error) << "BSEmcMCDepositToTimebasedWaveforms for " << fMCDepositBranchName << " has no Simulator set. Aborting!";
89  exit(-1);
90  }
91 
92  // Get RootManager
93  FairRootManager *ioman = FairRootManager::Instance();
94  if (ioman == nullptr) {
95  LOG(error) << "BSEmcMCDepositToTimebasedWaveforms<ParSet>::Init: "
96  << "RootManager not instantiated!";
97  return kFATAL;
98  }
100  fMCDepositArray.SetTCA(dynamic_cast<TClonesArray *>(ioman->GetObject(fMCDepositBranchName)));
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) << "BSEmcMCDepositToTimebasedWaveforms: " << fDetectorName << " using photon statistic";
119  } else {
121  fExcessNoiseFactor = 1;
122  }
124  LOG(debug) << "BSEmcMCDepositToTimebasedWaveforms::Init() for " << fMCDepositBranchName << " done.";
125  return kSUCCESS;
126  }
127 
137  virtual void Exec(Option_t * /*t_opt*/) /*override*/
138  {
139  LOG(debug) << "BSEmcMCDepositToTimebasedWaveforms<ParSet> " << fDetectorName << " Exec() ";
140  FairRootManager *ioman = FairRootManager::Instance();
141 
142  TStopwatch timer;
143  timer.Start();
144 
145  const BSEmcMCDeposit *theDeposit = nullptr;
146 
147  Int_t nDeposits = fMCDepositArray.GetSize();
148  LOG(debug) << "BSEmcMCDepositToTimebasedWaveforms<ParSet> " << fDetectorName << " Deposit array contains " << nDeposits << " deposits";
149 
150  for (Int_t iDeposit = 0; iDeposit < nDeposits; iDeposit++) {
151 
152  theDeposit = fMCDepositArray.GetConstElementPtr(iDeposit);
153  Double_t energy = theDeposit->GetEnergy();
154  if (energy == 0) {
155  continue;
156  }
157  LOG(debug3) << "deposit 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(theDeposit->GetDetectorID(), fSimulator);
170  // register deposit...timebased framework uses ns, whereas emc deals with seconds as time unit
171  FairLink linkToDeposit(-1, ioman->GetEntryNr(), fMCDepositBranchName, iDeposit, 1.0);
172  if (fActivateBuffering) {
173  wfData.AddDeposit(linkToDeposit, ioman->GetEventTime() + theDeposit->GetTime() * 1.0e9, energy);
174  } else {
175  wfData.AddDeposit(linkToDeposit, theDeposit->GetTime() * 1.0e9, energy);
176  }
177  LOG(debug) << "BSEmcMCDepositToTimebasedWaveforms " << fDetectorName << " adding deposit with event time " << ioman->GetEventTime()
178  << " and deposit time: " << theDeposit->GetTime() * 1.0e9 << " 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) << "BSEmcMCDepositToTimebasedWaveforms, 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 fMCDepositBranchName{""};
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 /*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)
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.