BmnRoot 26.10.0-18-g4e3414f9
Software framework of the BM@N experiment
Loading...
Searching...
No Matches
BmnFillTrigInfoDst.cxx
Go to the documentation of this file.
1/********************************************************************************
2 * BmnFillTrigInfoDst.cxx *
3 *******************************************************************************/
4
6
7#include "BmnFDPoint.h"
8#include "BmnTrigDigit.h"
9#include "BmnTrigInfoDst.h"
10#include "BmnTrigWaveDigit.h"
11#include "FairRootManager.h"
12#include "UniDetectorParameter.h"
13#include "uni_db_structures.h"
14
15#include <TBufferJSON.h>
16#include <TStopwatch.h>
17#include <exception>
18#include <iostream>
19#include <nlohmann/json.hpp>
20using namespace std;
21
22static Double_t workTime = 0.0;
23
24// ---- Default constructor -------------------------------------------
26 : BmnFillTrigInfoDst(false)
27{
28 LOG(debug) << "Defaul Constructor of BmnFillTrigInfoDst";
29}
30
31// ---- Constructor with flag whether experimental data are used --------
33 : BmnTask("BmnFillTrigInfoDst")
34 , isExpData(isExp)
35 , doProcessTriggers(true)
36 , fDigiEventHeader(nullptr)
37 , fVCDigits(nullptr)
38 , fBC0Digits(nullptr)
39 , fBC1Digits(nullptr)
40 , fBC2Digits(nullptr)
41 , fBDDigits(nullptr)
42 , fSiMDDigits(nullptr)
43 , fFDDigits(nullptr)
44 , fBC1Digits_Top(nullptr)
45 , fBC1Digits_Bottom(nullptr)
46 , fTrigInfo(nullptr)
47{
48 fnRun = 0;
49 fnPeriod = 0;
50
51 LOG(debug) << "Constructor with experimental data flag of BmnFillTrigInfoDst";
52}
53
54// ---- Destructor ----------------------------------------------------
56{
57 LOG(debug) << "Destructor of BmnFillTrigInfoDst";
58 if (fTrigInfo)
59 delete fTrigInfo;
60}
61
62// ---- Init ----------------------------------------------------------
64{
65 LOG(debug) << "Initialization of BmnFillTrigInfoDst";
66
67 // Get a handle from the IO manager
68 FairRootManager* ioman = FairRootManager::Instance();
69 if (!ioman) {
70 LOG(error) << "Init: FairRootManager is not instantiated! BmnFillTrigInfoDst task will be deactivated";
71 SetActive(kFALSE);
72 return kERROR;
73 }
74
75 // Get input branches
76 if (isExpData) {
77 fDigiEventHeader = (BmnEventHeader*)ioman->GetObject("BmnEventHeader.");
78 fVCDigits = (TClonesArray*)ioman->GetObject("TQDC_VCS");
79 fBC0Digits = (TClonesArray*)ioman->GetObject("TQDC_BC0");
80 fBC1Digits = (TClonesArray*)ioman->GetObject("TQDC_BC1S");
81 if (!fBC1Digits)
82 fBC1Digits = (TClonesArray*)ioman->GetObject("TQDC_BC1");
83 fBC2Digits = (TClonesArray*)ioman->GetObject("TQDC_BC2AS");
84 if (!fBC2Digits)
85 fBC2Digits = (TClonesArray*)ioman->GetObject("TQDC_BC2");
86 fBDDigits = (TClonesArray*)ioman->GetObject("BD");
87 fSiMDDigits = (TClonesArray*)ioman->GetObject("SiMD");
88 fFDDigits = (TClonesArray*)ioman->GetObject("TQDC_FD");
89
90 fDigiRunHeader = static_cast<DigiRunHeader*>(ioman->GetInFile()->Get("DigiRunHeader"));
91 fnRun = fDigiRunHeader->GetRunId();
92 fnPeriod = fDigiRunHeader->GetPeriodId();
93 // std::cout << "fnRun = " << fnRun << "; fnPeriod = " << fnPeriod << std::endl;
94 if (fnPeriod == 9) {
95 fGr4FitBC1Top = new TGraph();
97 f_gaus = new TF1("f_gaus", "gaus", 0, 500);
98 fBC1Digits_Top = (TClonesArray*)ioman->GetObject("TQDC_BC1T");
99 fBC1Digits_Bottom = (TClonesArray*)ioman->GetObject("TQDC_BC1B");
101 }
102
103 } else {
104 fBDDigits = (TClonesArray*)ioman->GetObject("BdDigit");
105 fFDDigits = (TClonesArray*)ioman->GetObject("FDPoint");
106 }
107
109 && !fFDDigits)
110 {
111 LOG(error) << "Init: all trigger branches not found! BmnFillTrigInfoDst task will process only basic data";
112 doProcessTriggers = false;
113 // SetActive(kFALSE);
114 // return kERROR;
115 }
116
117 // Create and register TrigInfo object for trigger output data
119 ioman->Register("BmnTrigInfo.", "TRIGGER", fTrigInfo, kTRUE);
120 if (!fTrigInfo) {
121 LOG(error) << "Init: BmnTrigInfo not created! BmnFillTrigInfoDst task will be deactivated";
122 SetActive(kFALSE);
123 return kERROR;
124 }
125
126 return kSUCCESS;
127}
128
129// ---- Exec ----------------------------------------------------------
131{
132 if (!IsActive())
133 return;
134
135 TStopwatch sw;
136 sw.Start();
137
138 if (fVerbose > 1)
139 cout << "\n======================== BmnFillTrigInfoDst exec started =====================\n" << endl;
140
141 fTrigInfo->Reset();
142
143 if (fDigiEventHeader) {
144 fTrigInfo->SetInputSignalsAR(fDigiEventHeader->GetInputSignalsAR());
145 fTrigInfo->SetInputSignalsBR(fDigiEventHeader->GetInputSignalsBR());
146 fTrigInfo->SetInputSignalsVector(fDigiEventHeader->GetInputSignalsVector());
147 }
148
149 if (doProcessTriggers) {
150 if (fVCDigits && fVCDigits->GetEntriesFast() > 0) {
152 fTrigInfo->SetVCAmp(digiVC->GetPeak());
153 }
154 if (fBC0Digits && fBC0Digits->GetEntriesFast() > 0) {
155 BmnTrigWaveDigit* digiBC0 = (BmnTrigWaveDigit*)fBC0Digits->At(0);
156 fTrigInfo->SetBC0Amp(digiBC0->GetPeak());
157 fTrigInfo->GetBC0Digits()->AbsorbObjects(fBC0Digits);
158 }
159 if (fBC1Digits && fBC1Digits->GetEntriesFast() > 0) {
160 BmnTrigWaveDigit* digiBC1 = (BmnTrigWaveDigit*)fBC1Digits->At(0);
161 fTrigInfo->SetBC1Integral(digiBC1->GetIntegral());
162 fTrigInfo->SetBC1Amp(digiBC1->GetPeak());
163 fTrigInfo->GetBC1Digits()->AbsorbObjects(fBC1Digits);
164 }
165 if (fBC2Digits && fBC2Digits->GetEntriesFast() > 0) {
166 BmnTrigWaveDigit* digiBC2 = (BmnTrigWaveDigit*)fBC2Digits->At(0);
167 fTrigInfo->SetBC2Amp(digiBC2->GetPeak());
168 fTrigInfo->GetBC2Digits()->AbsorbObjects(fBC2Digits);
169 }
170 if (fBDDigits) {
171 fTrigInfo->SetBDMult(fBDDigits->GetEntriesFast());
172 fTrigInfo->GetBDDigits()->AbsorbObjects(fBDDigits);
173 }
174 if (fSiMDDigits)
175 fTrigInfo->SetSiMDMult(fSiMDDigits->GetEntriesFast());
176 if (fFDDigits && fFDDigits->GetEntriesFast() > 0) {
177 if (isExpData) {
179 fTrigInfo->SetFDAmp(digiFD->GetPeak());
180 }
181 fTrigInfo->GetFDDigits()->AbsorbObjects(fFDDigits);
182 }
183 if (fnPeriod == 9 && isExpData && fBC1Digits_Top && fBC1Digits_Top->GetEntriesFast() > 0 && fBC1Digits_Bottom
184 && fBC1Digits_Bottom->GetEntriesFast() > 0)
185 {
186 Float_t amp_temp, time_temp;
187 time_temp = CalculateT0(&amp_temp);
188 fTrigInfo->SetT0_time(time_temp);
189 fTrigInfo->SetT0_amp(amp_temp);
190 } else {
191 fTrigInfo->SetT0_time(0.);
192 fTrigInfo->SetT0_amp(0.);
193 }
194 }
195
196 sw.Stop();
197 workTime += sw.RealTime();
198}
199
200// ---- Finish --------------------------------------------------------
202{
203 LOG(debug) << "Finish of BmnFillTrigInfoDst";
204 printf("Work time of BmnFillTrigInfoDst: %4.2f sec.\n", workTime);
205}
206
207// ---- CalculateT0 ----------------------------------------------------
209{
210 Float_t tempT0 = 0.;
211 if (amp)
212 *amp = 0;
213
214 // Get WaveDigit for BC1_Top and BC1_Bottom detectors
215 BmnTrigWaveDigit* digBC1T = (BmnTrigWaveDigit*)fBC1Digits_Top->UncheckedAt(0);
216 BmnTrigWaveDigit* digBC1B = (BmnTrigWaveDigit*)fBC1Digits_Bottom->UncheckedAt(0);
217 Int_t nSemplesWF = digBC1T->GetNSamples();
218 auto& valBC1T = digBC1T->GetValue();
219 auto& valBC1B = digBC1B->GetValue();
220 vector<Double_t> vtimeBC1T = digBC1T->TdcVector();
221 vector<Double_t> vtimeBC1B = digBC1B->TdcVector();
222
223 if (nSemplesWF <= 0 || LimitWvHgh >= nSemplesWF)
224 return 0;
225
226 if (vtimeBC1T.size() == 0 || vtimeBC1B.size() == 0)
227 return 0;
228
229 if (valBC1T.size() == 0 || valBC1B.size() == 0)
230 return 0;
231
232 Double_t timeBC1 = 0, timeBC1T = 0, timeBC1B = 0;
233
234 // Find TDC time for Top and Bottom in TdcDigitsArray
235 timeBC1T = FindTimeFromVector(vtimeBC1T);
236 timeBC1B = FindTimeFromVector(vtimeBC1B);
237 timeBC1 = (timeBC1T + timeBC1B) * 0.5;
238
239 // calculate the signal amplitude of Top and Bottom
240 Double_t maxBC1T = 0, maxBC1B = 0, maxBC1T_fit = 0, maxBC1B_fit = 0;
241 Double_t maxStBC1T = 0, maxStBC1B = 0;
242
245 for (Int_t ist = (int)LimitWvLow; ist < (int)LimitWvHgh; ist++) {
246 fGr4FitBC1Top->AddPoint(ist, valBC1T[ist]);
247 fGr4FitBC1Bottom->AddPoint(ist, valBC1B[ist]);
248 if (maxBC1T < valBC1T[ist]) {
249 maxBC1T = valBC1T[ist];
250 maxStBC1T = (Double_t)ist;
251 }
252 if (maxBC1B < valBC1B[ist]) {
253 maxBC1B = valBC1B[ist];
254 maxStBC1B = (Double_t)ist;
255 }
256 }
257
258 if (maxBC1T < LimitLowAmpBC1T || maxBC1B < LimitLowAmpBC1B)
259 return 0;
260
261 f_gaus->SetParameters(maxBC1T, maxStBC1T, 1);
262 fGr4FitBC1Top->Fit(f_gaus, "QW", "", maxStBC1T - GateFitLow, maxStBC1T + GateFitHgh);
263 maxBC1T_fit = f_gaus->GetParameter(0);
264
265 f_gaus->SetParameters(maxBC1B, maxStBC1B, 1);
266 fGr4FitBC1Bottom->Fit(f_gaus, "QW", "", maxStBC1B - GateFitLow, maxStBC1B + GateFitHgh);
267 maxBC1B_fit = f_gaus->GetParameter(0);
268
269 // calculate the geometric mean of amplitude
270 Double_t gAmpBC1 = TMath::Sqrt(maxBC1T_fit * maxBC1B_fit);
271
272 // correct the time on amplitude
273 // tempT0 = timeBC1;
274 tempT0 = timeBC1 + f_BC1->Eval(gAmpBC1);
275 if (amp)
276 *amp = gAmpBC1;
277
278 return tempT0;
279}
280
281// ---- find time from vector of TDC hits ----------------------------------
283{
284
285 for (UInt_t i = 0; i < vtime.size(); i++) {
286 if (vtime.at(i) > LimitTdcLow && vtime.at(i) < LimitTdcHgh)
287 return vtime.at(i);
288 }
289 return 0;
290}
291
292// ---- clear points in TGraph ----------------------------------
294{
295 if (g1 == nullptr)
296 return;
297
298 for (int ip = g1->GetN() - 1; ip >= 0; ip--) {
299 g1->RemovePoint(ip);
300 }
301}
302
303// ---- clear points in TGraph ----------------------------------
305{
306 // defauilt parameters for run 9
307 // The shape of the curve and its parameters depend on the properties of the T0 detectors
308 f_BC1 = new TF1("f_BC1", "[0] + [1]*TMath::Exp([2]*([3]+x))", 0, 20000);
309 f_BC1->SetParameters(0.5327, -3.611, -0.0002426, 855.5);
310
311 // These constants depends on DAQ settings and WR sinchronization
312 LimitTdcLow = 4225.;
313 LimitTdcHgh = 4249.;
314
315 // These constants depends on DAQ settings
316 LimitWvLow = 320;
317 LimitWvHgh = 350;
318
319 // These constants depends on waveform of the signal
320 GateFitLow = 5.5;
321 GateFitHgh = 2.5;
322
323 // These constants depends on waveform of the signal
324 LimitLowAmpBC1T = 1000.;
325 LimitLowAmpBC1B = 1000.;
326
327 UniDetectorParameter* pDetectorParameter =
329 if (pDetectorParameter == nullptr) {
330 LOG(info) << "BmnFillTrigInfoDst::GetT0SlewingFromDb() :\n\t No data for T0 correction was found in the "
331 "database. The default values will be used.";
332 return false;
333 }
334
335 JsonValue* json_value = (JsonValue*)pDetectorParameter->GetValue();
336 Bool_t flagOK = true;
337 // the record can be written by another ROOT version or be malformed, then the JSON conversion
338 // throws an exception, which must not terminate the reconstruction: default values are used
339 try {
340 if (json_value->value.contains("SlewingFunction")) {
341 // std::cout << json_value->value["SlewingFunction"] << std::endl;
342 std::string s1 = json_value->value["SlewingFunction"].dump();
343 TObject* slewingObject = TBufferJSON::ConvertFromJSON(s1.c_str());
344 TF1* slewingFunction = dynamic_cast<TF1*>(slewingObject);
345 if (slewingFunction != nullptr) {
346 delete f_BC1; // replace the default slewing function by the one from the database
347 f_BC1 = slewingFunction;
348 } else {
349 delete slewingObject;
350 LOG(error)
351 << "BmnFillTrigInfoDst::GetT0SlewingFromDb() :\n\t The \"SlewingFunction\" field for T0 correction "
352 "was not read as TF1 from the database record. The default function will be used.";
353 flagOK = false;
354 }
355 } else
356 LOG(info)
357 << "BmnFillTrigInfoDst::GetT0SlewingFromDb() :\n\t The \"SlewingFunction\" field for T0 correction "
358 "was not found in the database record. The "
359 "default values will be used.";
360
361 if (json_value->value.contains("Parameters")) {
362 // std::cout << json_value->value["Parameters"] << std::endl;
363
364 if (json_value->value["Parameters"].contains("LimitTdcLow"))
365 LimitTdcLow = json_value->value["Parameters"]["LimitTdcLow"];
366 else
367 flagOK = false;
368
369 if (json_value->value["Parameters"].contains("LimitTdcHgh"))
370 LimitTdcHgh = json_value->value["Parameters"]["LimitTdcHgh"];
371 else
372 flagOK = false;
373
374 if (json_value->value["Parameters"].contains("LimitWvLow"))
375 LimitWvLow = json_value->value["Parameters"]["LimitWvLow"];
376 else
377 flagOK = false;
378
379 if (json_value->value["Parameters"].contains("LimitWvHgh"))
380 LimitWvHgh = json_value->value["Parameters"]["LimitWvHgh"];
381 else
382 flagOK = false;
383
384 if (json_value->value["Parameters"].contains("GateFitLow"))
385 GateFitLow = json_value->value["Parameters"]["GateFitLow"];
386 else
387 flagOK = false;
388
389 if (json_value->value["Parameters"].contains("GateFitHgh"))
390 GateFitHgh = json_value->value["Parameters"]["GateFitHgh"];
391 else
392 flagOK = false;
393
394 if (json_value->value["Parameters"].contains("LimitLowAmpBC1T"))
395 LimitLowAmpBC1T = json_value->value["Parameters"]["LimitLowAmpBC1T"];
396 else
397 flagOK = false;
398
399 if (json_value->value["Parameters"].contains("LimitLowAmpBC1B"))
400 LimitLowAmpBC1B = json_value->value["Parameters"]["LimitLowAmpBC1B"];
401 else
402 flagOK = false;
403
404 } else
405 LOG(info)
406 << "BmnFillTrigInfoDst::GetT0SlewingFromDb() :\n\t The \"Parameters\" field for T0 correction was "
407 "not found in the database record. The default "
408 "values will be used.";
409 } catch (const std::exception& exc) {
410 LOG(error) << "BmnFillTrigInfoDst::GetT0SlewingFromDb() :\n\t The database record for T0 correction was not "
411 "parsed: "
412 << exc.what() << ". The default values will be used.";
413 delete json_value;
414 delete pDetectorParameter;
415 return false;
416 }
417
418 if (flagOK == false)
419 LOG(info) << "BmnFillTrigInfoDst::GetT0SlewingFromDb() :\n\t Some parameters are not found in the "
420 "\"Parameters\" field. The default values will be used.";
421
422 // clean memory after work
423 delete json_value;
424 delete pDetectorParameter;
425
426 LOG(info) << "BmnFillTrigInfoDst::GetT0SlewingFromDb() :\n\t The parameters for calculating T0 have been "
427 "loaded from the database";
428
429 return true;
430}
static Double_t workTime
int i
Definition P4_F32vec4.h:22
bool Bool_t
int Int_t
float Float_t
constexpr Bool_t kFALSE
double Double_t
constexpr Bool_t kTRUE
const char Option_t
TClonesArray * fBDDigits
Double_t FindTimeFromVector(vector< Double_t > vtime)
TClonesArray * fVCDigits
TClonesArray * fBC1Digits_Bottom
DigiRunHeader * fDigiRunHeader
Arrays for T0 calculation.
virtual void Exec(Option_t *opt)
Executed for each event.
~BmnFillTrigInfoDst()
Destructor.
Float_t CalculateT0(Float_t *amp)
TClonesArray * fFDDigits
TClonesArray * fSiMDDigits
TClonesArray * fBC1Digits
void ClearTGraph(TGraph *g1)
virtual void Finish()
Finish task called at the end.
BmnEventHeader * fDigiEventHeader
TClonesArray * fBC2Digits
BmnTrigInfoDst * fTrigInfo
TClonesArray * fBC1Digits_Top
BmnFillTrigInfoDst()
Default constructor.
TClonesArray * fBC0Digits
virtual InitStatus Init()
Initialization of task at the beginning.
BmnTask()
Default constructor.
Definition BmnTask.cxx:3
class for TQDC data including waveform can be initialized both from an std::vector and raw pointer to...
vector< int16_t > & GetValue()
size_t GetNSamples() const
int32_t GetIntegral(size_t start=0, size_t stop=SIZE_MAX) const
int16_t GetPeak(size_t start=0, size_t stop=SIZE_MAX) const
vector< Double_t > & TdcVector()
static TObject * ConvertFromJSON(const char *str)
Int_t GetN() const
virtual Int_t RemovePoint()
Double_t RealTime()
void Start(Bool_t reset=kTRUE)
void Stop()
int GetValue(vector< UniValue * > &parameter_value)
get value of detector parameter presented by an array
static UniDetectorParameter * GetDetectorParameter(int value_id)
get detector parameter from the database
Double_t Sqrt(Double_t x)
Definition json.hpp:4446