PandaRoot
BSEmcExtractDigisFromWaveforms.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 //#pragma once
14 
15 #ifndef BSEMCEXTRACTDIGISFROMWAVEFORMS_HH
16 #define BSEMCEXTRACTDIGISFROMWAVEFORMS_HH
17 
18 #include <PndPersistencyTask.h>
19 #include <iostream>
20 #include <string>
21 #include <utility>
22 #include <vector>
23 
24 #include "TClonesArray.h"
25 #include "TStopwatch.h"
26 
27 #include "FairLogger.h"
28 #include "FairRootManager.h"
29 #include "FairRunAna.h"
30 #include "FairRuntimeDb.h"
31 
32 #include "PndTCAMutableContainer.h"
33 
34 #include "BSEmcAbsPSA.h"
35 #include "BSEmcDigi.h"
36 #include "BSEmcDigiPar.h"
37 #include "BSEmcMCDeposit.h"
38 #include "BSEmcMultiWaveform.h"
39 #include "BSEmcWaveformData.h"
40 
41 #include "math.h"
42 
51 template <class ParSet>
53  public:
54  // Constructors
55  BSEmcExtractDigisFromWaveforms(const std::string &t_detectorname = "Barrel", Bool_t t_storedigis = kTRUE)
56  : PndPersistencyTask("BSEmcExtractDigisFromWaveforms"), fDetectorName(t_detectorname)
57  {
58  SetPersistency(t_storedigis);
59  }
60 
61  // Destructor
63  {
64  if (fPSA != nullptr) {
65  delete fPSA;
66  fPSA = nullptr;
67  }
68  }
69 
80  virtual InitStatus Init()
81  {
82 
83  LOG(debug) << "BSEmcExtractDigisFromWaveforms<ParSet>::Init";
85  if (fWaveformBranchName == "") {
87  }
88  if (fDigiBranchName == "") {
90  }
91  // Get RootManager
92  FairRootManager *ioman = FairRootManager::Instance();
93  if (ioman == nullptr) {
94  LOG(error) << "BSEmcExtractDigisFromWaveforms<ParSet>::Init: "
95  << "RootManager not instantiated!";
96  return kFATAL;
97  }
99  if (ioman->CheckBranch(fDigiBranchName) == 0) {
100  ioman->Register(fDigiBranchName, "BSEmcDigi", "EMC", GetPersistency());
101  }
102  fDigiArray.SetTCA(dynamic_cast<TClonesArray *>(ioman->GetObject(fDigiBranchName)));
103 
104  LOG(debug) << "BSEmcExtractDigisFromWaveforms<ParSet>::Init: Registered " << fDigiBranchName;
105  // Get input array
106  fWaveformArray = (TClonesArray *)ioman->GetObject(fWaveformBranchName);
107  if (fWaveformArray == nullptr) {
108  // check if EmcWaveform contains MultiWaveforms
109  fWaveformArray = (TClonesArray *)ioman->GetObject(fWaveformBranchName);
110  if ((fWaveformArray == nullptr) || (!fWaveformArray->GetClass()->InheritsFrom("BSEmcMultiWaveform"))) {
111  LOG(error) << "BSEmcExtractDigisFromWaveforms<ParSet>::Init: "
112  << "No BSEmcWaveform array containing multi waveforms!";
113  return kFATAL;
114  }
115  }
116 
117  DefinePSA();
118  if (fPSA == nullptr) {
119  LOG(error) << "No PSA was defined. Aborting!";
120  return kFATAL;
121  }
122 
123  fEnergyDigiThreshold = fDigiPar->GetEnergyDigiThreshold();
124 
125  LOG(debug) << "BSEmcExtractDigisFromWaveforms: Intialization successfull";
126  return kSUCCESS;
127  }
128 
140  virtual void Exec(Option_t * /*unused*/)
141  {
142  fDigiArray.Reset();
143  // fDigiArray->Delete();
144  TStopwatch timer;
145  timer.Start();
146 
147  Double_t energy = NAN;
148  Double_t digi_time = NAN;
149  Int_t nDeposits = 0;
150  Int_t detId = 0;
151  Int_t nWaveforms = fWaveformArray->GetEntriesFast();
152  BSEmcWaveform *theWaveform = nullptr;
153 
154  for (Int_t iWaveform = 0; iWaveform < nWaveforms; iWaveform++) {
155 
156  theWaveform = (BSEmcWaveform *)fWaveformArray->At(iWaveform);
157  detId = theWaveform->GetDetectorId();
158 
159  nDeposits = fPSA->Process(theWaveform);
160 
161  for (Int_t iDeposit = 0; iDeposit < nDeposits; ++iDeposit) {
162  fPSA->GetHit(iDeposit, energy, digi_time);
163 
164  // if (energy > fEnergyDigiThreshold) {
165  Double_t timestamp = GetTimeStamp(theWaveform, digi_time);
166  BSEmcDigi *myDigi = fDigiArray.CreateCopy(BSEmcDigi(detId, energy, timestamp - FairRootManager::Instance()->GetEventTime()));
167  // BSEmcDigi *myDigi = new ((*fDigiArray)[fDigiArray->GetEntriesFast()]) BSEmcDigi(detId, energy, timestamp);
168  myDigi->SetGainType(GetGainType(theWaveform, iDeposit));
169  myDigi->ResetLinks();
170  myDigi->AddLinks(theWaveform->GetLinksWithType(FairRootManager::Instance()->GetBranchId(fMCDepositBranchName)));
171  FairMultiLinkedData mcdeplinks = myDigi->GetLinksWithType(FairRootManager::Instance()->GetBranchId(fMCDepositBranchName));
172  for (const FairLink &link : mcdeplinks.GetLinks()) {
173  LOG(debug) << link;
174  }
175  LOG(debug) << "BSEmcExtractDigisFromWaveforms for " << fDetectorName << " created Digi(detId: " << detId << ", energy: " << energy << ", timestamp: " << timestamp
176  << ") and has " << myDigi->GetLinksWithType(FairRootManager::Instance()->GetBranchId(fMCDepositBranchName)).GetNLinks() << " links to EmcMCDeposits";
177  // }
178  }
179  }
180 
181  timer.Stop();
182  Double_t rtime = timer.RealTime();
183  Double_t ctime = timer.CpuTime();
184  LOG(debug) << "BSEmcExtractDigisFromWaveforms, Real time " << rtime << " s, CPU time " << ctime << " s";
185  }
186 
187  void SetStorageOfData(Bool_t t_val)
188  {
189  SetPersistency(t_val);
190  return;
191  }
192  void SetDigiBranchName(const TString &t_digiBranchName) { fDigiBranchName = t_digiBranchName; }
193  void SetWaveformBranchName(const TString &t_digiBranchName) { fWaveformBranchName = t_digiBranchName; }
194 
195  protected:
197  virtual void SetParContainers()
198  {
199  // Get run and runtime database
200  FairRun *run = FairRun::Instance();
201  if (run == nullptr) {
202  Fatal("SetParContainers", "No analysis run");
203  }
204  FairRuntimeDb *db = run->GetRuntimeDb();
205  if (db == nullptr) {
206  Fatal("SetParContainers", "No runtime database");
207  }
208  // Get Emc digitisation parameter container
209  fDigiPar = dynamic_cast<ParSet *>(db->getContainer(ParSet::fgParameterName.c_str()));
210  }
211  virtual Double_t GetTimeStamp(BSEmcWaveform *t_waveform, Double_t t_digi_time) const
212  {
213  Double_t sampleRate = t_waveform->GetSampleRate();
214 
215  t_digi_time /= sampleRate;
216  t_digi_time *= 1e9; // ns
217  return t_waveform->GetTimeStamp() + t_digi_time;
218  }
219 
220  virtual BSEmcDigi::eGAIN GetGainType(BSEmcWaveform *t_waveform, Int_t t_dep) const = 0;
221  virtual void DefinePSA() = 0;
222 
223  protected:
224  const std::string fDetectorName{""};
225  TString fMCDepositBranchName{""};
227  // PndTCAConstContainer<BSEmcWaveform> fWaveformArray;
229  TClonesArray *fWaveformArray{nullptr};
230  // TClonesArray *fDigiArray{nullptr};
231  TString fDigiBranchName{""};
232  TString fWaveformBranchName{""};
233  Double_t fEnergyDigiThreshold{0};
234 
235  BSEmcAbsPSA *fPSA{nullptr};
236 
237  ParSet *fDigiPar{nullptr};
239 };
240 
241 #endif /*BSEMCEXTRACTDIGISFROMWAVEFORMS_HH*/
Baseclass for pulseshapeanalysis ( featureextraction )
Definition: BSEmcAbsPSA.h:38
void SetWaveformBranchName(const TString &t_digiBranchName)
represents a simulated waveform in an emc crystal
Definition: BSEmcWaveform.h:75
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:96
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:59
void SetDigiBranchName(const TString &t_digiBranchName)
long GetDetectorId() const
Definition: BSEmcWaveform.h:84
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:99
virtual void Exec(Option_t *)
Runs the task.
virtual BSEmcDigi::eGAIN GetGainType(BSEmcWaveform *t_waveform, Int_t t_dep) const =0