PandaRoot
BSEmcExtractDigisFromWaveforms.h
Go to the documentation of this file.
1 //#pragma once
2 
3 #ifndef BSEMCEXTRACTDIGISFROMWAVEFORMS_HH
4 #define BSEMCEXTRACTDIGISFROMWAVEFORMS_HH
5 
6 #include <PndPersistencyTask.h>
7 #include <iostream>
8 #include <string>
9 #include <utility>
10 #include <vector>
11 
12 #include "TClonesArray.h"
13 #include "TStopwatch.h"
14 
15 #include "FairLogger.h"
16 #include "FairRootManager.h"
17 #include "FairRunAna.h"
18 #include "FairRuntimeDb.h"
19 
20 #include "PndTCAMutableContainer.h"
21 
22 #include "BSEmcAbsPSA.h"
23 #include "BSEmcDigi.h"
24 #include "BSEmcDigiPar.h"
25 #include "BSEmcMCDeposit.h"
26 #include "BSEmcMultiWaveform.h"
27 #include "BSEmcWaveformData.h"
28 
29 #include "math.h"
30 
39 template <class ParSet>
41  public:
42  // Constructors
43  BSEmcExtractDigisFromWaveforms(const std::string &t_detectorname = "Barrel", Bool_t t_storedigis = kTRUE)
44  : PndPersistencyTask("BSEmcExtractDigisFromWaveforms"), fDetectorName(t_detectorname)
45  {
46  SetPersistency(t_storedigis);
47  }
48 
49  // Destructor
51  {
52  if (fPSA != nullptr) {
53  delete fPSA;
54  fPSA = nullptr;
55  }
56  }
57 
68  virtual InitStatus Init()
69  {
70 
71  LOG(debug) << "BSEmcExtractDigisFromWaveforms<ParSet>::Init";
73  if (fWaveformBranchName == "") {
75  }
76  if (fDigiBranchName == "") {
78  }
79  // Get RootManager
80  FairRootManager *ioman = FairRootManager::Instance();
81  if (ioman == nullptr) {
82  LOG(error) << "BSEmcExtractDigisFromWaveforms<ParSet>::Init: "
83  << "RootManager not instantiated!";
84  return kFATAL;
85  }
87  fDigiArray.SetTCA(dynamic_cast<TClonesArray *>(ioman->GetObject(fDigiBranchName)));
88 
89  LOG(debug) << "BSEmcExtractDigisFromWaveforms<ParSet>::Init: Registered " << fDigiBranchName;
90  // Get input array
91  fWaveformArray = (TClonesArray *)ioman->GetObject(fWaveformBranchName);
92  if (fWaveformArray == nullptr) {
93  // check if EmcWaveform contains MultiWaveforms
94  fWaveformArray = (TClonesArray *)ioman->GetObject(fWaveformBranchName);
95  if ((fWaveformArray == nullptr) || (!fWaveformArray->GetClass()->InheritsFrom("BSEmcMultiWaveform"))) {
96  LOG(error) << "BSEmcExtractDigisFromWaveforms<ParSet>::Init: "
97  << "No BSEmcWaveform array containing multi waveforms!";
98  return kFATAL;
99  }
100  }
101 
102  DefinePSA();
103  if (fPSA == nullptr) {
104  LOG(error) << "No PSA was defined. Aborting!";
105  return kFATAL;
106  }
107 
108  fEnergyDigiThreshold = fDigiPar->GetEnergyDigiThreshold();
109 
110  LOG(debug) << "BSEmcExtractDigisFromWaveforms: Intialization successfull";
111  return kSUCCESS;
112  }
113 
125  virtual void Exec(Option_t * /*unused*/)
126  {
127  fDigiArray.Reset();
128  // fDigiArray->Delete();
129  TStopwatch timer;
130  timer.Start();
131 
132  Double_t energy = NAN;
133  Double_t digi_time = NAN;
134  Int_t nDeposits = 0;
135  Int_t detId = 0;
136  Int_t nWaveforms = fWaveformArray->GetEntriesFast();
137  BSEmcWaveform *theWaveform = nullptr;
138 
139  for (Int_t iWaveform = 0; iWaveform < nWaveforms; iWaveform++) {
140 
141  theWaveform = (BSEmcWaveform *)fWaveformArray->At(iWaveform);
142  detId = theWaveform->GetDetectorId();
143 
144  nDeposits = fPSA->Process(theWaveform);
145 
146  for (Int_t iDeposit = 0; iDeposit < nDeposits; ++iDeposit) {
147  fPSA->GetHit(iDeposit, energy, digi_time);
148 
149  // if (energy > fEnergyDigiThreshold) {
150  Double_t timestamp = GetTimeStamp(theWaveform, digi_time);
151  BSEmcDigi *myDigi = fDigiArray.CreateCopy(BSEmcDigi(detId, energy, timestamp));
152  // BSEmcDigi *myDigi = new ((*fDigiArray)[fDigiArray->GetEntriesFast()]) BSEmcDigi(detId, energy, timestamp);
153  myDigi->SetGainType(GetGainType(theWaveform, iDeposit));
154  myDigi->ResetLinks();
155  myDigi->AddLinks(theWaveform->GetLinksWithType(FairRootManager::Instance()->GetBranchId(fMCDepositBranchName)));
156  FairMultiLinkedData mcdeplinks = myDigi->GetLinksWithType(FairRootManager::Instance()->GetBranchId(fMCDepositBranchName));
157  for (const FairLink &link : mcdeplinks.GetLinks()) {
158  LOG(debug) << link;
159  }
160  LOG(debug) << "BSEmcExtractDigisFromWaveforms for " << fDetectorName << " created Digi(detId: " << detId << ", energy: " << energy << ", timestamp: " << timestamp
161  << ") and has " << myDigi->GetLinksWithType(FairRootManager::Instance()->GetBranchId(fMCDepositBranchName)).GetNLinks() << " links to EmcMCDeposits";
162  // }
163  }
164  }
165 
166  timer.Stop();
167  Double_t rtime = timer.RealTime();
168  Double_t ctime = timer.CpuTime();
169  LOG(debug) << "BSEmcExtractDigisFromWaveforms, Real time " << rtime << " s, CPU time " << ctime << " s";
170  }
171 
172  void SetStorageOfData(Bool_t t_val)
173  {
174  SetPersistency(t_val);
175  return;
176  }
177  void SetDigiBranchName(const TString &t_digiBranchName) { fDigiBranchName = t_digiBranchName; }
178  void SetWaveformBranchName(const TString &t_digiBranchName) { fWaveformBranchName = t_digiBranchName; }
179 
180  protected:
182  virtual void SetParContainers()
183  {
184  // Get run and runtime database
185  FairRun *run = FairRun::Instance();
186  if (run == nullptr) {
187  Fatal("SetParContainers", "No analysis run");
188  }
189  FairRuntimeDb *db = run->GetRuntimeDb();
190  if (db == nullptr) {
191  Fatal("SetParContainers", "No runtime database");
192  }
193  // Get Emc digitisation parameter container
194  fDigiPar = dynamic_cast<ParSet *>(db->getContainer(ParSet::fgParameterName.c_str()));
195  }
196  virtual Double_t GetTimeStamp(BSEmcWaveform *t_waveform, Double_t t_digi_time) const
197  {
198  Double_t sampleRate = t_waveform->GetSampleRate();
199 
200  t_digi_time /= sampleRate;
201  t_digi_time *= 1e9; // ns
202  return t_waveform->GetTimeStamp() + t_digi_time;
203  }
204 
205  virtual BSEmcDigi::eGAIN GetGainType(BSEmcWaveform *t_waveform, Int_t t_dep) const = 0;
206  virtual void DefinePSA() = 0;
207 
208  protected:
209  const std::string fDetectorName{""};
210  TString fMCDepositBranchName{""};
212  // PndTCAConstContainer<BSEmcWaveform> fWaveformArray;
214  TClonesArray *fWaveformArray{nullptr};
215  // TClonesArray *fDigiArray{nullptr};
216  TString fDigiBranchName{""};
217  TString fWaveformBranchName{""};
218  Double_t fEnergyDigiThreshold{0};
219 
220  BSEmcAbsPSA *fPSA{nullptr};
221 
222  ParSet *fDigiPar{nullptr};
224 };
225 
226 #endif /*BSEMCEXTRACTDIGISFROMWAVEFORMS_HH*/
Baseclass for pulseshapeanalysis ( featureextraction )
Definition: BSEmcAbsPSA.h:26
void SetWaveformBranchName(const TString &t_digiBranchName)
represents a simulated waveform in an emc crystal
Definition: BSEmcWaveform.h:63
const std::string fgMultiWaveformBranchName
BSEmcExtractDigisFromWaveforms(const std::string &t_detectorname="Barrel", Bool_t t_storedigis=kTRUE)
virtual Double_t GetTimeStamp(BSEmcWaveform *t_waveform, Double_t t_digi_time) const
virtual void GetHit(Int_t t_i, Double_t &t_energy, Double_t &t_time)=0
Get energy and time of hit.
void SetGainType(eGAIN t_type)
Definition: BSEmcDigi.h:84
Input and Output Container implementation of PndMutableContainerI using an underlying TClonesArray...
virtual InitStatus Init()
Init Task.
void SetPersistency(Bool_t val=kTRUE)
void SetTCA(TClonesArray *t_tca)
Set the TClonesArray address.
virtual Int_t Process(const BSEmcWaveform *t_waveform)=0
Find Hits in Waveform.
virtual void Reset() final
Delete all elements.
PndTCAMutableContainer< BSEmcDigi > fDigiArray
Task to create digis from waveforms.
represents the reconstructed hit of one emc crystal
Definition: BSEmcDigi.h:47
void SetDigiBranchName(const TString &t_digiBranchName)
long GetDetectorId() const
Definition: BSEmcWaveform.h:72
void SetBranchName(const TString &t_branchname)
Set the Branch Name.
ParSet * fDigiPar
Timebased Digitisation parameter container.
ClassDef(BSEmcExtractDigisFromWaveforms, 1)
virtual T * CreateCopy(const T &t_element)
Create a copy of t_element in the TClonesArray and return a pointer to it.
const std::string fgDigiBranchName
const std::string fgMCDepositBranchName
Double_t GetSampleRate() const
Definition: BSEmcWaveform.h:87
virtual void Exec(Option_t *)
Runs the task.
virtual BSEmcDigi::eGAIN GetGainType(BSEmcWaveform *t_waveform, Int_t t_dep) const =0