R3BROOT
R3B analysis software
Loading...
Searching...
No Matches
R3BNeulandCal2HitPar.cxx
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) 2019 GSI Helmholtzzentrum für Schwerionenforschung GmbH *
3 * Copyright (C) 2019-2025 Members of R3B Collaboration *
4 * *
5 * This software is distributed under the terms of the *
6 * GNU General Public Licence (GPL) version 3, *
7 * copied verbatim in the file "LICENSE". *
8 * *
9 * In applying this license GSI does not waive the privileges and immunities *
10 * granted to it by virtue of its status as an Intergovernmental Organization *
11 * or submit itself to any jurisdiction. *
12 ******************************************************************************/
13
15#include <FairRootManager.h>
16
17#include "R3BEventHeader.h"
18#include "R3BNeulandCalData.h"
22#include "R3BNeulandHitPar.h"
23
24#include "FairLogger.h"
25#include "FairRuntimeDb.h"
26
27#include "TCanvas.h"
28#include "TClonesArray.h"
29#include "TH2F.h"
30#include "TROOT.h"
31
32using namespace R3B::Neuland; // NOLINT
33
34R3BNeulandCal2HitPar::R3BNeulandCal2HitPar(const char* name, const Int_t iVerbose)
35 : FairTask(name, iVerbose)
36 , fHitCalEngine(new Calibration::HitCalibrationEngine())
37 , fCosmicTracker(new Calibration::CosmicTracker())
38 , fNeulandHitPar(nullptr)
39 , fCalNeuland(nullptr)
40 , fMappedLos(nullptr)
41 , fCosmicTpat(0)
42 , fSavePlots(kFALSE)
43{
44}
45
47{
48 FairRootManager* mgr = FairRootManager::Instance();
49
50 if (!mgr)
51 {
52 LOG(fatal) << "R3BNeulandCal2HitPar::Init: FairRootManager not found";
53 return kFATAL;
54 }
55
56 fEventHeader = static_cast<R3BEventHeader*>(mgr->GetObject("EventHeader."));
57
58 if (!fEventHeader)
59 {
60 LOG(info) << "R3BNeulandCal2HitPar::Init Branch EventHeader not found, its ok";
61 // return kFATAL;
62 }
63
64 fCalNeuland = static_cast<TClonesArray*>(mgr->GetObject("NeulandCalData"));
65
66 if (!fCalNeuland)
67 {
68 LOG(fatal) << "R3BNeulandCal2HitPar::Init: Branch NeulandCalData not found";
69 return kFATAL;
70 }
71
72 fNeulandHitPar = static_cast<R3BNeulandHitPar*>(FairRuntimeDb::instance()->getContainer("NeulandHitPar"));
73 LOG(info) << "R3BNeulandCal2HitPar::Init: Number of Hit-Paramteres found: " << fNeulandHitPar->GetNumModulePar();
74
75 LOG(info) << "R3BNeulandCal2HitPar::Init: Initializing NeulandHitCalibrationEngine.";
76 fHitCalEngine->Init(fNeulandHitPar);
77
78 if (fNeulandHitPar)
79 fCosmicTracker->SetDistances(fNeulandHitPar->GetDistancesToFirstPlane());
80
81 return kSUCCESS;
82}
83
84void R3BNeulandCal2HitPar::Exec(Option_t* option)
85{
86 if (++fEventNumber % 100000 == 0 && fVerbose) // 10000
87 {
88 const auto msg = TString::Format(
89 "R3BNeulandCal2HitPar::Exec : Event: %10d, accepted Events: %10d", fEventNumber, fAcceptedEventNumber);
90 std::cout << msg << "\r" << std::flush;
91
92 std::cout << " cnts " << fIgorcnt0 << " ---- " << fIgorcnt1 << " ---- " << fIgorcnt2 << " " << std::endl;
93 }
94
95 // Skip if this is not a pure cosmic event
96 if (!IsCosmicEvent())
97 {
98 return;
99 }
100
101 ++fIgorcnt0;
102
103 LOG(debug) << "R3BNeulandCal2HitPar::Exec: Event " << fEventNumber - 1;
104 const auto nItems = fCalNeuland->GetEntriesFast();
105
106 if (nItems < 8)
107 {
108 LOG(debug) << " Event cannot be used: too few signals : " << nItems << "!";
109 return;
110 }
111
112 fHitCalEngine->Reset();
113 fCosmicTracker->Reset();
114
115 Int_t addedPoints = 0;
116
117 for (Int_t i = 0; i < nItems; i++)
118 {
119 const auto pmt = static_cast<R3BNeulandCalData*>(fCalNeuland->At(i));
120
121 const auto id = pmt->GetBarId() - 1;
122 const auto side = pmt->GetSide() - 1;
123
124 if (std::isnan(pmt->GetTriggerTime()))
125 {
126 fHitCalEngine->Set(id, side, pmt->GetTime(), pmt->GetQdc());
127 }
128 else
129 {
130 fHitCalEngine->Set(id, side, pmt->GetTime() - pmt->GetTriggerTime(), pmt->GetQdc());
131 }
132
133 if (fHitCalEngine->IsValid(id))
134 {
135 ++addedPoints;
136 fCosmicTracker->AddPoint(id, fHitCalEngine->GetPosition(id));
137 }
138 }
139
140 ++fIgorcnt1;
141
142 if (addedPoints < 6)
143 {
144 LOG(debug) << " Event cannot be used: too few Points : " << addedPoints << " (" << nItems << ")!";
145 return;
146 }
147
148 ++fIgorcnt2;
149
150 const auto& cosmicTrack = fCosmicTracker->GetTrack();
151
152 if (cosmicTrack.Interactions.size() == 0)
153 {
154 LOG(debug) << " Getting Cosmic Track : Failure!";
155 return;
156 }
157
158 LOG(debug) << " Getting Cosmic Track: Success!";
159 ++fAcceptedEventNumber;
160
161 LOG(debug) << " Adding data to calibration";
162 fHitCalEngine->Add(cosmicTrack, fEventNumber);
163}
164
166{
167 LOG(info) << "R3BNeulandCal2HitPar::FinishTask: " << "Saved " << fAcceptedEventNumber << " Events.";
168
169 LOG(info) << "R3BNeulandCal2HitPar::FinishTask: " << "Starting Neuland Hit Calibration with "
170 << fAcceptedEventNumber << " Events.";
171
172 const auto batchMode = gROOT->IsBatch();
173 gROOT->SetBatch(kTRUE);
174 const auto defdir = gDirectory;
175
176 TDirectory* neulandDir = nullptr;
177 if (fSavePlots)
178 neulandDir = defdir->mkdir("NeuLAND");
179
180 auto parameters = fHitCalEngine->Calibrate(neulandDir);
181
182 defdir->cd();
183 gROOT->SetBatch(batchMode);
184
185 fNeulandHitPar->GetListOfModulePar()->Clear();
186
187 auto maxThreshold = 0.;
188 for (auto& parameter : parameters)
189 {
190 fNeulandHitPar->AddModulePar(new R3BNeulandHitModulePar(parameter));
191
192 for (auto side = 1; side <= 2; ++side)
193 {
194 const auto threshold =
195 parameter.GetPMTThreshold(side) * exp(TotalBarLength / parameter.GetLightAttenuationLength());
196 if (threshold > maxThreshold)
197 maxThreshold = threshold;
198 }
199 }
200
201 fNeulandHitPar->SetEnergyCutoff(maxThreshold);
202 LOG(info) << TString::Format("R3BNeulandCal2HitPar::FinishTask: Recommended Minimum Energy Cutoff : >%4.2f MeV\n",
203 maxThreshold);
204
205 fNeulandHitPar->setChanged();
206
207 LOG(info) << "R3BNeulandCal2HitPar::FinishTask: " << "Number of calibrated Bars: "
208 << fNeulandHitPar->GetNumModulePar();
209}
210
211bool R3BNeulandCal2HitPar::IsCosmicEvent() const { return ((fEventHeader->GetTpat() & fCosmicTpat) == fCosmicTpat); }
212
ClassImp(R3B::Neuland::Cal2HitPar)
int GetTpat() const
virtual void Exec(Option_t *option)
virtual InitStatus Init()
Int_t GetBarId() const
Int_t GetNumModulePar() const
Method to get number of modules storred in array.
Simulation of NeuLAND Bar/Paddle.
constexpr auto TotalBarLength