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  if (ioman->CheckBranch(fDigiBranchName) == 0) {
88  ioman->Register(fDigiBranchName, "BSEmcDigi", "EMC", GetPersistency());
89  }
90  fDigiArray.SetTCA(dynamic_cast<TClonesArray *>(ioman->GetObject(fDigiBranchName)));
91 
92  LOG(debug) << "BSEmcExtractDigisFromWaveforms<ParSet>::Init: Registered " << fDigiBranchName;
93  // Get input array
94  fWaveformArray = (TClonesArray *)ioman->GetObject(fWaveformBranchName);
95  if (fWaveformArray == nullptr) {
96  // check if EmcWaveform contains MultiWaveforms
97  fWaveformArray = (TClonesArray *)ioman->GetObject(fWaveformBranchName);
98  if ((fWaveformArray == nullptr) || (!fWaveformArray->GetClass()->InheritsFrom("BSEmcMultiWaveform"))) {
99  LOG(error) << "BSEmcExtractDigisFromWaveforms<ParSet>::Init: "
100  << "No BSEmcWaveform array containing multi waveforms!";
101  return kFATAL;
102  }
103  }
104 
105  DefinePSA();
106  if (fPSA == nullptr) {
107  LOG(error) << "No PSA was defined. Aborting!";
108  return kFATAL;
109  }
110 
111  fEnergyDigiThreshold = fDigiPar->GetEnergyDigiThreshold();
112 
113  LOG(debug) << "BSEmcExtractDigisFromWaveforms: Intialization successfull";
114  return kSUCCESS;
115  }
116 
128  virtual void Exec(Option_t * /*unused*/)
129  {
130  fDigiArray.Reset();
131  // fDigiArray->Delete();
132  TStopwatch timer;
133  timer.Start();
134 
135  Double_t energy = NAN;
136  Double_t digi_time = NAN;
137  Int_t nDeposits = 0;
138  Int_t detId = 0;
139  Int_t nWaveforms = fWaveformArray->GetEntriesFast();
140  BSEmcWaveform *theWaveform = nullptr;
141 
142  for (Int_t iWaveform = 0; iWaveform < nWaveforms; iWaveform++) {
143 
144  theWaveform = (BSEmcWaveform *)fWaveformArray->At(iWaveform);
145  detId = theWaveform->GetDetectorId();
146 
147  nDeposits = fPSA->Process(theWaveform);
148 
149  for (Int_t iDeposit = 0; iDeposit < nDeposits; ++iDeposit) {
150  fPSA->GetHit(iDeposit, energy, digi_time);
151 
152  // if (energy > fEnergyDigiThreshold) {
153  Double_t timestamp = GetTimeStamp(theWaveform, digi_time);
154  BSEmcDigi *myDigi = fDigiArray.CreateCopy(BSEmcDigi(detId, energy, timestamp - FairRootManager::Instance()->GetEventTime()));
155  // BSEmcDigi *myDigi = new ((*fDigiArray)[fDigiArray->GetEntriesFast()]) BSEmcDigi(detId, energy, timestamp);
156  myDigi->SetGainType(GetGainType(theWaveform, iDeposit));
157  myDigi->ResetLinks();
158  myDigi->AddLinks(theWaveform->GetLinksWithType(FairRootManager::Instance()->GetBranchId(fMCDepositBranchName)));
159  FairMultiLinkedData mcdeplinks = myDigi->GetLinksWithType(FairRootManager::Instance()->GetBranchId(fMCDepositBranchName));
160  for (const FairLink &link : mcdeplinks.GetLinks()) {
161  LOG(debug) << link;
162  }
163  LOG(debug) << "BSEmcExtractDigisFromWaveforms for " << fDetectorName << " created Digi(detId: " << detId << ", energy: " << energy << ", timestamp: " << timestamp
164  << ") and has " << myDigi->GetLinksWithType(FairRootManager::Instance()->GetBranchId(fMCDepositBranchName)).GetNLinks() << " links to EmcMCDeposits";
165  // }
166  }
167  }
168 
169  timer.Stop();
170  Double_t rtime = timer.RealTime();
171  Double_t ctime = timer.CpuTime();
172  LOG(debug) << "BSEmcExtractDigisFromWaveforms, Real time " << rtime << " s, CPU time " << ctime << " s";
173  }
174 
175  void SetStorageOfData(Bool_t t_val)
176  {
177  SetPersistency(t_val);
178  return;
179  }
180  void SetDigiBranchName(const TString &t_digiBranchName) { fDigiBranchName = t_digiBranchName; }
181  void SetWaveformBranchName(const TString &t_digiBranchName) { fWaveformBranchName = t_digiBranchName; }
182 
183  protected:
185  virtual void SetParContainers()
186  {
187  // Get run and runtime database
188  FairRun *run = FairRun::Instance();
189  if (run == nullptr) {
190  Fatal("SetParContainers", "No analysis run");
191  }
192  FairRuntimeDb *db = run->GetRuntimeDb();
193  if (db == nullptr) {
194  Fatal("SetParContainers", "No runtime database");
195  }
196  // Get Emc digitisation parameter container
197  fDigiPar = dynamic_cast<ParSet *>(db->getContainer(ParSet::fgParameterName.c_str()));
198  }
199  virtual Double_t GetTimeStamp(BSEmcWaveform *t_waveform, Double_t t_digi_time) const
200  {
201  Double_t sampleRate = t_waveform->GetSampleRate();
202 
203  t_digi_time /= sampleRate;
204  t_digi_time *= 1e9; // ns
205  return t_waveform->GetTimeStamp() + t_digi_time;
206  }
207 
208  virtual BSEmcDigi::eGAIN GetGainType(BSEmcWaveform *t_waveform, Int_t t_dep) const = 0;
209  virtual void DefinePSA() = 0;
210 
211  protected:
212  const std::string fDetectorName{""};
213  TString fMCDepositBranchName{""};
215  // PndTCAConstContainer<BSEmcWaveform> fWaveformArray;
217  TClonesArray *fWaveformArray{nullptr};
218  // TClonesArray *fDigiArray{nullptr};
219  TString fDigiBranchName{""};
220  TString fWaveformBranchName{""};
221  Double_t fEnergyDigiThreshold{0};
222 
223  BSEmcAbsPSA *fPSA{nullptr};
224 
225  ParSet *fDigiPar{nullptr};
227 };
228 
229 #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