PandaRoot
BSEmcMCDepositToTimebasedWaveforms.h
Go to the documentation of this file.
1 //****************************************************************************
2 //* This file is part of PandaRoot. *
3 //* *
4 //* PandaRoot is distributed under the terms of the *
5 //* GNU General Public License (GPL) version 3, *
6 //* copied verbatim in the file "LICENSE". *
7 //* *
8 //* Copyright (C) 2006 - 2024 FAIR GmbH and copyright holders of PandaRoot *
9 //* The copyright holders are listed in the file "COPYRIGHTHOLDERS". *
10 //* The authors are listed in the file "AUTHORS". *
11 //****************************************************************************
12 
13 //----------------------------------------------------------------------
14 // Author List:
15 // Phil Strother Original author
16 // Dima Melnichuk - adaption for PANDA
17 // Philipp Mahlberg - integration in timebased simulation concept
18 //----------------------------------------------------------------------
19 
20 // #pragma once
21 #ifndef BSEMCMCDEPOSITTOTIMEBASEDWAVEFORMS_HH
22 #define BSEMCMCDEPOSITTOTIMEBASEDWAVEFORMS_HH
23 
24 #include <PndPersistencyTask.h>
25 #include <cassert>
26 #include <string>
27 #include <vector>
28 
29 #include "TClonesArray.h"
30 #include "TROOT.h"
31 #include "TRandom.h"
32 #include "TStopwatch.h"
33 #include "TString.h"
34 
35 #include "FairLink.h"
36 #include "FairLogger.h"
37 #include "FairRootManager.h"
38 #include "FairRun.h"
39 #include "FairRunAna.h"
40 #include "FairRuntimeDb.h"
41 
42 #include "PndTCAConstContainer.h"
43 
45 #include "BSEmcDigiPar.h"
46 #include "BSEmcMCDeposit.h"
47 #include "BSEmcWaveform.h"
48 #include "BSEmcWaveformBuffer.h"
49 #include "BSEmcWaveformData.h"
50 
60 template <class ParSet>
62  public:
63  // Constructors
64  BSEmcMCDepositToTimebasedWaveforms(const std::string &t_detectorname, Bool_t t_storewaves = kTRUE)
65  : PndPersistencyTask("BSEmcMCDepositToTimebasedWaveforms"), fDetectorName(t_detectorname)
66  {
67  SetPersistency(t_storewaves);
68  }
69 
70  // Destructor
72 
83  virtual InitStatus Init() /*override*/
84  {
85  if (fMCDepositBranchName == "") {
87  }
88 
89  if (fWaveformBranchName == "") {
91  }
92  if (fWaveformDataBranchName == "") {
94  }
95 
96  if (fSimulator == nullptr) {
97  LOG(debug) << "BSEmcMCDepositToTimebasedWaveforms for " << fMCDepositBranchName << " has no Simulator set. Calling SetupSimulator!";
100  }
101  if (fSimulator == nullptr) {
102  LOG(error) << "BSEmcMCDepositToTimebasedWaveforms for " << fMCDepositBranchName << " has no Simulator set. Aborting!";
103  exit(-1);
104  }
105 
106  // Get RootManager
107  FairRootManager *ioman = FairRootManager::Instance();
108  if (ioman == nullptr) {
109  LOG(error) << "BSEmcMCDepositToTimebasedWaveforms<ParSet>::Init: "
110  << "RootManager not instantiated!";
111  return kFATAL;
112  }
114  fMCDepositArray.SetTCA(dynamic_cast<TClonesArray *>(ioman->GetObject(fMCDepositBranchName)));
115  // Create and activiate output Buffer....choose between BSEmcWaveform and BSEmcMultiWaveform
116  fWaveformBuffer = new BSEmcWaveformBuffer(fWaveformBranchName, "BSEmcMultiWaveform", "Emc", GetPersistency());
117  LOG(debug) << "Using BSEmcMultiWaveform in WaveformBuffer.";
118 
119  fWaveformBuffer = (BSEmcWaveformBuffer *)ioman->RegisterWriteoutBuffer(fWaveformBranchName, fWaveformBuffer);
120  fWaveformBuffer->ActivateBuffering(fActivateBuffering);
121 
122  if (fStoreDataClass) {
124  }
125 
127 
128  if (fUse_photon_statistic) {
129  LOG(debug) << "BSEmcMCDepositToTimebasedWaveforms: " << fDetectorName << " using photon statistic";
133  } else {
135  fExcessNoiseFactor = 1;
136  }
138  LOG(debug) << "BSEmcMCDepositToTimebasedWaveforms::Init() for " << fMCDepositBranchName << " done.";
139  return kSUCCESS;
140  }
141 
151  virtual void Exec(Option_t * /*t_opt*/) /*override*/
152  {
153  LOG(debug) << "BSEmcMCDepositToTimebasedWaveforms<ParSet> " << fDetectorName << " Exec() ";
154  FairRootManager *ioman = FairRootManager::Instance();
155 
156  TStopwatch timer;
157  timer.Start();
158 
159  const BSEmcMCDeposit *theDeposit = nullptr;
160 
161  Int_t nDeposits = fMCDepositArray.GetSize();
162  LOG(debug) << "BSEmcMCDepositToTimebasedWaveforms<ParSet> " << fDetectorName << " Deposit array contains " << nDeposits << " deposits";
163 
164  for (Int_t iDeposit = 0; iDeposit < nDeposits; iDeposit++) {
165 
166  theDeposit = fMCDepositArray.GetConstElementPtr(iDeposit);
167  Double_t energy = theDeposit->GetEnergy();
168  if (energy == 0) {
169  continue;
170  }
171  LOG(debug3) << "deposit energy : " << energy;
172  if (fUse_photon_statistic) {
173  Double_t crystalPhotonsMeV = 1.0e3 * energy * fNPhotoElectronsPerMeV;
174  Double_t photonStatFactor = gRandom->Gaus(1, sqrt(fExcessNoiseFactor / crystalPhotonsMeV));
175  LOG(debug3) << "photonStatFactor: " << photonStatFactor;
176  energy *= photonStatFactor;
177  LOG(debug3) << "energy now: " << energy;
178  // energy *= 1e3;
179  LOG(debug3) << "energy now in MeV: " << energy;
180  }
181  // construct corresponding waveform data Object
182  BSEmcWaveformData wfData(theDeposit->GetDetectorID(), fSimulator);
184  // register deposit...timebased framework uses ns, whereas emc deals with seconds as time unit
185  FairLink linkToDeposit(-1, ioman->GetEntryNr(), fMCDepositBranchName, iDeposit, 1.0);
186  if (fActivateBuffering) {
187  wfData.AddDeposit(linkToDeposit, ioman->GetEventTime() + theDeposit->GetTime() * 1.0e9, energy);
188  } else {
189  wfData.AddDeposit(linkToDeposit, theDeposit->GetTime() * 1.0e9, energy);
190  }
191  LOG(debug) << "BSEmcMCDepositToTimebasedWaveforms " << fDetectorName << " adding deposit with event time " << ioman->GetEventTime()
192  << " and deposit time: " << theDeposit->GetTime() * 1.0e9 << " and energy: " << energy;
193  fWaveformBuffer->FillNewData(&wfData);
194  }
195  timer.Stop();
196  Double_t rtime = timer.RealTime();
197  Double_t ctime = timer.CpuTime();
198  LOG(debug) << "BSEmcMCDepositToTimebasedWaveforms, Real time " << rtime << " s, CPU time " << ctime << " s";
199  }
200 
201  void RunTimebased(Bool_t t_timebased = kTRUE) { fActivateBuffering = t_timebased; };
202  void SetSimulator(BSEmcAbsWaveformSimulator *t_simulator) { fSimulator = t_simulator; }
203  void SetStorageOfData(Bool_t t_storeWaves = kTRUE) { SetPersistency(t_storeWaves); };
204  void StoreDataClass(Bool_t t_storeData = kTRUE) { fStoreDataClass = t_storeData; };
205 
206  void SetMCDepositBranchName(const TString &t_branchName) { fMCDepositBranchName = t_branchName; }
207  void SetWaveformBranchName(const TString &t_branchName) { fWaveformBranchName = t_branchName; }
208  void SetWaveformDataBranchName(const TString &t_branchName) { fWaveformDataBranchName = t_branchName; }
209  void SetPhotonStatisticParName(const std::string &t_photonStatParName) { fPhotonStatisticParName = t_photonStatParName; }
210  void SetWaveformGenParName(const std::string &t_waveformGenParName) { fWaveformGenParName = t_waveformGenParName; }
211 
212  protected:
214  virtual void SetParContainers() /*override*/
215  {
216  // Get run and runtime database
217  FairRun *run = FairRun::Instance();
218  if (run == nullptr) {
219  Fatal("SetParContainers", "No analysis run");
220  }
221  FairRuntimeDb *db = run->GetRuntimeDb();
222  if (db == nullptr) {
223  Fatal("SetParContainers", "No runtime database");
224  }
225  // Get Emc digitisation parameter container
226  if (fWaveformGenParName == "") {
227  fWaveformGenParName = ParSet::fgParameterName;
228  }
229  fDigiPar = dynamic_cast<ParSet *>(db->getContainer(fWaveformGenParName.c_str()));
230 
231  if (fPhotonStatisticParName == "") {
233  }
234  fPhotonStatisticPar = dynamic_cast<BSEmcDigiPar *>(db->getContainer(fPhotonStatisticParName.c_str()));
235  }
236  virtual void SetupSimulator() = 0;
237 
239  TString fDetectorName{""};
240  TString fMCDepositBranchName{""};
241  TString fWaveformBranchName{""};
246 
247  Bool_t fStoreDataClass{kFALSE};
248  Bool_t fActivateBuffering{kFALSE};
249  std::string fPhotonStatisticParName{""};
251 
252  std::string fWaveformGenParName{""};
253  ParSet *fDigiPar{nullptr};
254  Bool_t fUse_photon_statistic{kFALSE};
256  Double_t fExcessNoiseFactor{0};
257  Double_t fOverlapTime{0};
259 
261 };
262 
263 #endif /*BSEMCMCDEPOSITTOTIMEBASEDWAVEFORMS_HH*/
Int_t GetUseDigiEffectiveSmearingMode() const
Definition: BSEmcDigiPar.h:43
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:40
virtual void Exec(Option_t *)
Runs the task.
Double_t GetRearCrystalSurfaceArea() const
Definition: BSEmcDigiPar.h:50
represents the deposited energy of one emc crystal from simulation
void SetPersistency(Bool_t val=kTRUE)
static const std::string fgParameterName
Definition: BSEmcDigiPar.h:38
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:53
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:47
Double_t GetSensitiveArea() const
Definition: BSEmcDigiPar.h:46
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:45
Double_t GetExcessNoiseFactor() const
Definition: BSEmcDigiPar.h:48
void SetOverlapTime(const Double_t t_overlap)
Container for runtime parameters that are required for the transformation from BSEmcMCDeposits to BSE...
Definition: BSEmcDigiPar.h:36
Taks to create waveforms from deposits.