Bartender LYSO
Digitizer simulation for the LYSO calorimeter prototype
Loading...
Searching...
No Matches
bar.cc
Go to the documentation of this file.
1
5#include "bar.hh"
6
7using namespace std;
8using namespace TMath;
9
10
11BarLYSO::BarLYSO(const char*inputFilename, Int_t threadID)
12{
13 // Set the threadID
14 fThreadID = threadID;
15
16 // Initialize the random generators
17 fRandPars = new TRandom3(0);
18 fRandNoise = new TRandom3(0);
19
20 // Initialize DAQ
21 fDAQ = new DAQ();
22
23 // Initialize whole WF containers [CHANNELS]x[SAMPLINGS]
24 fEvent = -1;
25 fFront.resize(CHANNELS, vector<Double_t>(SAMPLINGS, 0));
26 fBack.resize(CHANNELS, vector<Double_t>(SAMPLINGS, 0));
27 fTimes_F.resize(CHANNELS, vector<Double_t>(SAMPLINGS, 0));
28 fTimes_B.resize(CHANNELS, vector<Double_t>(SAMPLINGS, 0));
29
30 // Determine the output filename based on the BarLYSO ID
31 string outputFilename = GenerateOutputFilename(inputFilename);
32
33 // Create the file.root and the TTree
34 fOutFile = TFile::Open(outputFilename.c_str(), "RECREATE");
35 fOutTree = new TTree("lyso_wfs", "lyso_wfs");
36 fOutTree->Branch("Event", &fEvent);
37 fOutTree->Branch("Time_F", &fTimes_F);
38 fOutTree->Branch("Time_B", &fTimes_B);
39 fOutTree->Branch("Front", &fFront);
40 fOutTree->Branch("Back", &fBack);
41}
42
43
44
46{
47 // Delete DAQ and random generators
48 delete fDAQ;
49 delete hPars;
50 delete fRandPars;
51 delete fRandNoise;
52
53 // Ensure that we delete the TTree and TFile objects only if they are not null
54 if(fOutTree)
55 {
56 fOutTree->ResetBranchAddresses(); // Reset any attached branches if needed
57 delete fOutTree;
58 }
59 if(fOutFile)
60 {
61 fOutFile->Close(); // Make sure to close the file properly
62 delete fOutFile;
63 }
64}
65
66
67
68string BarLYSO::GenerateOutputFilename(const char* inputFilename)
69{
70 // Regular expression to extract digits following "MCID_"
71 regex regex("\\bMCID_(\\d+)");
72
73 // Storage for matched results
74 smatch match;
75
76 // Convert inputFilename to string
77 string filename = inputFilename;
78
79 // Check if there is a match for digits following "MCID_" in the filename
80 if(regex_search(filename, match, regex))
81 {
82 // Extract the matched digits as a string
83 string runID = match[1].str();
84
85 if (!runID.empty())
86 {
87 // Convert the extracted string to an integer and assign it to the fID member variable
88 fID = stoi(runID);
89 }
90 }
91
92 // Determine the output filename based on the Bar ID
93 string outputFilename;
94 if(fID)
95 {
96 // If fThreadID is -1, omit the thread ID part in the filename
97 if(fThreadID == -1)
98 {
99 outputFilename = "BarID_" + to_string(fID) + ".root";
100 }
101 else
102 {
103 outputFilename = "BarID_" + to_string(fID) + "_t" + to_string(fThreadID) + ".root";
104 }
105 }
106 else
107 {
108 outputFilename = "output.root";
109 }
110
111 return outputFilename;
112}
113
114
115
116void BarLYSO::SetSamplingTimes()
117{
118 // Set the times
119 for(Int_t j = 0; j < CHANNELS; j++)
120 {
121 if(fDAQ->fIsBinSizeConstant)
122 {
123 for(Int_t i = 0; i < SAMPLINGS; i++)
124 {
125 fTimes_F[j][i] = (Double_t) i / fDAQ->fSamplingSpeed;
126 fTimes_B[j][i] = (Double_t) i / fDAQ->fSamplingSpeed;
127 }
128 }
129 else
130 {
131 // Initialize the first time points
132 fTimes_F[j][0] = 0.0;
133 fTimes_B[j][0] = 0.0;
134
135 for(Int_t i = 0; i < SAMPLINGS - 1; i++) // Up to SAMPLINGS - 1
136 {
137 Double_t bin_F, bin_B;
138 do
139 {
140 bin_F = fDAQ->binRand->Gaus(1.0 / fDAQ->fSamplingSpeed, fDAQ->fSigmaBinSize);
141 } while (bin_F < 0.5 * (1.0 / fDAQ->fSamplingSpeed) || bin_F > 1.5 * (1.0 / fDAQ->fSamplingSpeed));
142
143 do
144 {
145 bin_B = fDAQ->binRand->Gaus(1.0 / fDAQ->fSamplingSpeed, fDAQ->fSigmaBinSize);
146 } while (bin_B < 0.5 * (1.0 / fDAQ->fSamplingSpeed) || bin_B > 1.5 * (1.0 / fDAQ->fSamplingSpeed));
147
148 fTimes_F[j][i+1] = fTimes_F[j][i] + bin_F;
149 fTimes_B[j][i+1] = fTimes_B[j][i] + bin_B;
150 }
151 }
152 }
153}
154
155
156
158{
159 Int_t status;
160 Double_t my_charge, A, tau_rise, tau_dec;
161
162 // Get a TTree from the text file of parameters
163 TTree *tree = new TTree("tree", "mytree");
164 tree->ReadFile(fInputFilename.c_str());
165
166 tree->SetBranchAddress("Status", &status);
167 tree->SetBranchAddress("my_charge", &my_charge);
168 tree->SetBranchAddress("A", &A);
169 tree->SetBranchAddress("Tau_rise", &tau_rise);
170 tree->SetBranchAddress("Tau_dec", &tau_dec);
171
172 // Create hPars
173 hPars = new TH3D("hPars", "Fitted All", fHisto_A[0], fHisto_A[1], fHisto_A[2], fHisto_Tau_rise[0], fHisto_Tau_rise[1], fHisto_Tau_rise[2], fHisto_Tau_dec[0], fHisto_Tau_dec[1], fHisto_Tau_dec[2]);
174
175 // Fill hPars
176 for(Int_t i = 0; i < tree->GetEntries(); i++)
177 {
178 tree->GetEntry(i);
179
180 // Check conditions about charge
181 if(status==0 && my_charge >= fChargeCuts[0] && my_charge <= fChargeCuts[1])
182 {
183 hPars->Fill(A, tau_rise, tau_dec);
184 }
185 }
186
187 // Delete the TTree
188 delete tree;
189}
190
191
192
193Double_t BarLYSO::Wave_OnePhel(Double_t t, Double_t A, Double_t tau_rise, Double_t tau_dec, Double_t timePhel)
194{
195 Double_t funcVal;
196 Double_t expRise = Exp(-(t-timePhel)/tau_rise);
197 Double_t expDec = Exp(-(t-timePhel)/tau_dec);
198
199 // It happens sometimes when t << timePhel or t >> timePhel, so it's just a numerical fixing
200 if(expRise > DBL_MAX || expRise < DBL_MIN) expRise = 0;
201 if(expDec > DBL_MAX || expDec < DBL_MIN) expDec = 0;
202
203 // 1-Phel function
204 funcVal = A*(expRise-expDec)*((t > timePhel) ? 1:0);
205 return funcVal;
206}
207
208
209
211{
212 fEvent = event;
213 fFront.assign(CHANNELS, std::vector<Double_t>(SAMPLINGS, 0.));
214 fBack.assign(CHANNELS, std::vector<Double_t>(SAMPLINGS, 0.));
215}
216
217
218
220{
221 for(Int_t ch = 0; ch < CHANNELS; ch++)
222 {
223 fFront[ch].clear();
224 fBack[ch].clear();
225 }
226
227 fFront.clear();
228 fBack.clear();
229}
230
231
232
233void BarLYSO::SetFrontWaveform(Int_t channel, Double_t start)
234{
235 // Sample from hPars the parameters of 1-Phel WF
236 Double_t A, tau_rise, tau_dec;
237 hPars->GetRandom3(A, tau_rise, tau_dec, fRandPars);
238
239 // Evaluate and sum the new 1-Phel WF to the existing one
240 for(Int_t bin = 0; bin < SAMPLINGS; bin++)
241 {
242 fFront[channel][bin] += Wave_OnePhel(fTimes_F[channel][bin], A, tau_rise, tau_dec, start + ZERO_TIME_BIN);
243 }
244}
245
246
247
248void BarLYSO::SetBackWaveform(Int_t channel, Double_t start)
249{
250 // Sample from hPars the parameters of 1-Phel WF
251 Double_t A, tau_rise, tau_dec;
252 hPars->GetRandom3(A, tau_rise, tau_dec, fRandPars);
253
254 // Evaluate and sum the new 1-Phel WF to the existing one
255 for(Int_t bin = 0; bin < SAMPLINGS; bin++)
256 {
257 fBack[channel][bin] += Wave_OnePhel(fTimes_B[channel][bin], A, tau_rise, tau_dec, start + ZERO_TIME_BIN);
258 }
259}
260
261
262
264{
265 // Recompute the gain and add noise
266 Double_t k = fDAQ->ComputeFactorOfGainConversion();
267 for(Int_t ch = 0; ch < CHANNELS; ch++)
268 {
269 for(Int_t bin = 0; bin < SAMPLINGS; bin++)
270 {
271 // Set every bin with gaussian noise
272 fFront[ch][bin] = k*fFront[ch][bin] + Add_Noise();
273 fBack[ch][bin] = k*fBack[ch][bin] + Add_Noise();
274 }
275 }
276
277 fOutTree->Fill();
278}
279
280
281
282void BarLYSO::SaveBar()
283{
284 fOutFile->cd();
285 fOutTree->Write("lyso_wfs");
286}
Declaration of the class BarLYSO.
Double_t fHisto_Tau_dec[3]
Settings for histogram hPars related to parameter Tau_dec: [0] for number of bins,...
Definition bar.hh:180
TRandom3 * fRandNoise
Random generator for Add_Noise()
Definition bar.hh:172
Int_t fEvent
Number of events in the run.
Definition bar.hh:159
std::string fInputFilename
Name of the txt file of the best fit parameters data. See the introduction for more details about the...
Definition bar.hh:176
std::vector< std::vector< Double_t > > fBack
Container for Back-Detector waveforms: a 3-dimensional matrix with indices for event,...
Definition bar.hh:165
Double_t fChargeCuts[2]
Cuts in the charge spectrum of input best fit parameters data; [0] represents the minimum,...
Definition bar.hh:177
void SaveEvent()
Saves all the samples from fFront and fBack into a text file.
Definition bar.cc:263
void InitializeBaselines(Int_t event)
Method to initialize the entire fFront and fBack with a noise baseline.
Definition bar.cc:210
void SetParsDistro()
Sets the 3D histogram hPars.
Definition bar.cc:157
Double_t Add_Noise()
Returns the value of the noise.
Definition bar.hh:197
BarLYSO(const char *inputFilename, Int_t threadID)
Constructor of the class.
Definition bar.cc:11
void SetBackWaveform(Int_t channel, Double_t start)
Method to add a I-Phel waveform to the corresponding event and channel of the Back-Detector.
Definition bar.cc:248
TH3D * hPars
3D Histogram of One-Phel waveform parameters from which sampling will occur
Definition bar.hh:169
Double_t fHisto_A[3]
Settings for histogram hPars related to parameter A: [0] for number of bins, [1] for the lower limit,...
Definition bar.hh:178
void ClearContainers()
Method to add a I-Phel waveform to the corresponding event and channel of the Front-Detector.
Definition bar.cc:219
~BarLYSO()
Destructor of the class.
Definition bar.cc:45
Double_t Wave_OnePhel(Double_t t, Double_t A, Double_t tau_rise, Double_t tau_dec, Double_t timePhel)
Returns the value at t of the analytical form of the One Photo-Electron waveform.
Definition bar.cc:193
TRandom3 * fRandPars
Random generator for SetFrontWaveform() and SetBackWaveform()
Definition bar.hh:171
Double_t fHisto_Tau_rise[3]
Settings for histogram hPars related to parameter Tau_rise: [0] for number of bins,...
Definition bar.hh:179
Int_t fID
Run ID of the Monte Carlo.
Definition bar.hh:160
std::vector< std::vector< Double_t > > fFront
Container for Front-Detector waveforms: a 3-dimensional matrix with indices for event,...
Definition bar.hh:164
Definition daq.hh:12
Float_t fSamplingSpeed
Sampling speed [GSPS] of the DAQ.
Definition daq.hh:18
constexpr Int_t SAMPLINGS
Number of samplings for one waveform.
Definition globals.hh:10
constexpr Int_t ZERO_TIME_BIN
Delay of all waveforms in the [0, 1023] bins window.
Definition globals.hh:12
constexpr Int_t CHANNELS
Number of channels of the detectors.
Definition globals.hh:9