BmnRoot
Loading...
Searching...
No Matches
BmnBdDigitizer.cxx
Go to the documentation of this file.
1// -------------------------------------------------------------------------
2// ----- BmnBdDigitizer source file -----
3// ----- Created 11/04/22 by S. Merts -----
4// ----- Updated 11/04/25 by N. Lashmanov -----
5// -------------------------------------------------------------------------
6
7#include "BmnBdDigitizer.h"
8
9#include "BmnBdPoint.h"
10#include "BmnTrigDigit.h"
11#include "FairRootManager.h"
12#include "TClonesArray.h"
13#include "TMath.h"
14#include "TRandom3.h"
15
16#include <iostream>
17#include <map>
18
19using namespace std;
20using namespace TMath;
21
22static Double_t workTime = 0.0;
23
24// Default constructor for BmnBdDigitizer.
26 : FairTask("Bmn BD Digitizer")
27 , fPointArray(nullptr)
28 , fDigitArray(nullptr)
29 , fNMod(40)
30 , fNoiseSigma(0.00001)
31 , // Default noise level (GeV)
32 fThreshold(0.0004)
33 , // Default signal threshold (GeV)
34 fFiredChannels(0)
35{}
36
37// Destructor for BmnBdDigitizer.
39{
40 if (fDigitArray) {
41 delete fDigitArray;
42 fDigitArray = nullptr;
43 }
44}
45
46// Converts amplitude (GeV) to signal duration (ns).
47Double_t BmnBdDigitizer::ConvertAmplitudeToTime(Double_t amplitude)
48{
49
50 const Double_t eMin = 0.0003; // 300 keV in GeV
51 const Double_t eMax = 0.05; // 50 MeV in GeV
52 const Double_t tMin = 6.0; // ns
53 const Double_t tMax = 18.0; // ns
54
55 // Linear interpolation
56 if (amplitude <= eMin)
57 return tMin;
58 if (amplitude >= eMax)
59 return tMax;
60
61 Double_t fraction = (amplitude - eMin) / (eMax - eMin);
62 Double_t timeNs = tMin + fraction * (tMax - tMin);
63 return timeNs;
64}
65
67{
68 // Get RootManager
69 FairRootManager* ioman = FairRootManager::Instance();
70 if (!ioman) {
71 cout << "-E- BmnBdDigitizer::Init: RootManager not instantiated!" << endl;
72 return kFATAL;
73 }
74
75 // Get input array
76 fPointArray = (TClonesArray*)ioman->GetObject("BdPoint");
77 if (!fPointArray) {
78 cout << "-E- BmnBdDigitizer::Init: Branch BdPoint not found! Task will be deactivated" << endl;
79 SetActive(kFALSE);
80 return kERROR;
81 }
82
83 // Create and register output array
84 fDigitArray = new TClonesArray("BmnTrigDigit");
85 ioman->Register("BdDigit", "BD", fDigitArray, kTRUE);
86
87 if (fVerbose)
88 cout << "BmnBdDigitizer::Init() finished successfully\n";
89 return kSUCCESS;
90}
91
92// Executes the digitization process for each event.
93void BmnBdDigitizer::Exec(Option_t* opt)
94{
95 if (!IsActive())
96 return;
97
98 TStopwatch sw;
99 sw.Start();
100
101 // Check output array
102 if (!fDigitArray)
103 Fatal("Exec", "No BdDigit array");
104
105 // Reset output array
106 fDigitArray->Delete();
107 fFiredChannels = 0;
108
109 // Map for storing energies at modules
110 map<Int_t, pair<Double_t, Double_t>> ampTimeMap;
111
112 // Random number generator for noise
113 TRandom3 rand(0);
114
115 // Loop over MCPoints
116 for (Int_t iPoint = 0; iPoint < fPointArray->GetEntriesFast(); iPoint++) {
117 BmnBdPoint* point = (BmnBdPoint*)fPointArray->At(iPoint);
118 if (!point)
119 continue;
120
121 Int_t mod = point->GetCopy() - 1;
122
123 // Validate module number
124 if (mod < 0 || mod >= fNMod) {
125 if (fVerbose)
126 cout << "-W- BmnBdDigitizer::Exec: Invalid module number " << mod << endl;
127 continue;
128 }
129
130 // Add energy loss with noise
131 Double_t energyLoss = point->GetEnergyLoss();
132 Double_t noise = rand.Gaus(0, fNoiseSigma);
133 Double_t totalEnergy = energyLoss + noise;
134 Double_t time = point->GetTime();
135
136 auto it = ampTimeMap.find(mod);
137 if (it != ampTimeMap.end()) {
138 it->second.first += totalEnergy;
139 if (time < it->second.second)
140 it->second.second = time;
141 } else {
142 ampTimeMap[mod] = {totalEnergy, time};
143 }
144 }
145
146 // Create digits for modules with energy above threshold
147 for (auto mit : ampTimeMap) {
148 if (mit.second.first < fThreshold)
149 continue; // Skip signals below threshold
150
151 BmnTrigDigit digi;
152 digi.SetMod((Short_t)mit.first);
153 Double_t duration = ConvertAmplitudeToTime(mit.second.first);
154 digi.SetAmp(duration);
155 digi.SetTime(mit.second.second);
156 // digi.SetFiredChannels(fFiredChannels + 1);
157 new ((*fDigitArray)[fDigitArray->GetEntriesFast()]) BmnTrigDigit(digi);
158 fFiredChannels++; // Increment fired channels counter
159 }
160
161 if (fVerbose) {
162 // printf("Number of BmnBdPoints = %d\n", fPointArray->GetEntriesFast());
163 // printf("Number of BmnBdDigits = %d\n", fDigitArray->GetEntriesFast());
164 // printf("Number of BmnBdDigits = %d\n", fFiredChannels);
165 // cout << "BmnBdDigitizer::Exec() finished\n";
166 }
167
168 sw.Stop();
169 workTime += sw.RealTime();
170}
171
172// Finalizes the digitizer task.
174{
175 printf("Work time of the BD digitizer: %.4f sec.\n", workTime);
176}
virtual InitStatus Init()
virtual void Finish()
virtual void Exec(Option_t *opt)
Short_t GetCopy() const
Definition BmnBdPoint.h:54
void SetTime(Double_t time)
void SetMod(Short_t mod)
void SetAmp(Double_t amp)
STL namespace.