CbmRoot
Loading...
Searching...
No Matches
CbmTrdDigitizer.cxx
Go to the documentation of this file.
1/* Copyright (C) 2009-2024 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Florian Uhlig [committer], Alexandru Bercuci, Etienne Bechtel */
4
5#include "CbmTrdDigitizer.h"
6
7#include "CbmMCTrack.h"
8#include "CbmMatch.h"
9#include "CbmTrdAddress.h"
10#include "CbmTrdCheckUtil.h"
11#include "CbmTrdDigi.h"
12#include "CbmTrdGeoHandler.h"
13#include "CbmTrdModuleSim.h"
14#include "CbmTrdModuleSim2D.h"
15#include "CbmTrdModuleSimR.h"
16#include "CbmTrdPads.h"
17#include "CbmTrdParAsic.h"
18#include "CbmTrdParModAsic.h"
19#include "CbmTrdParModDigi.h"
20#include "CbmTrdParModGain.h"
21#include "CbmTrdParModGas.h"
22#include "CbmTrdParModGeo.h"
23#include "CbmTrdParSetAsic.h"
24#include "CbmTrdParSetDigi.h"
25#include "CbmTrdParSetGain.h"
26#include "CbmTrdParSetGas.h"
27#include "CbmTrdParSetGeo.h"
28#include "CbmTrdPoint.h"
29#include "CbmTrdRadiator.h"
30#include "CbmTrdRawToDigiR.h"
31
32#include <FairBaseParSet.h>
33#include <FairEventHeader.h>
34#include <FairRootManager.h>
35#include <FairRunAna.h>
36#include <FairRunSim.h>
37#include <FairRuntimeDb.h>
38#include <Logger.h>
39
40#include <TClonesArray.h>
41#include <TRandom.h>
42#include <TStopwatch.h>
43#include <TVector3.h>
44
45#include <cmath>
46#include <iomanip>
47#include <iostream>
48#include <memory>
49
50using std::map;
51using std::pair;
52using std::shared_ptr;
53using namespace std;
55
56//________________________________________________________________________________________
57CbmTrdDigitizer::CbmTrdDigitizer(shared_ptr<CbmTrdRadiator> radiator)
58 : CbmDigitize<CbmTrdDigi>("TrdDigitize")
59 , fLastEventTime(0)
60 , fpoints(0)
61 , nofBackwardTracks(0)
62 , fEfficiency(1.)
63 , fPoints(NULL)
64 , fTracks(NULL)
65 , fDigis(nullptr)
66 , fDigiMatches(nullptr)
67 , fAsicPar(NULL)
68 , fGasPar(NULL)
69 , fDigiPar(NULL)
70 , fGainPar(NULL)
71 , fGeoPar(NULL)
72 , fRadiator(radiator)
73 , fConverter(NULL)
74 , fQA(NULL)
75 // ,fConverter()
76 // ,fGeoHandler(new CbmTrdGeoHandler())
77 , fModuleMap()
78 , fDigiMap()
79{
80 if (fRadiator == NULL) fRadiator = make_shared<CbmTrdRadiator>(kTRUE, "tdr18");
81}
82
83//________________________________________________________________________________________
86
87
88//________________________________________________________________________________________
90{
92 delete fDigis;
93 delete fDigiMatches;
94 for (map<Int_t, CbmTrdModuleSim*>::iterator imod = fModuleMap.begin(); imod != fModuleMap.end(); imod++)
95 delete imod->second;
96 fModuleMap.clear();
97
98 delete fConverter;
99 delete fQA;
100}
101
102
103//________________________________________________________________________________________
105{
106 fAsicPar = static_cast<CbmTrdParSetAsic*>(FairRunAna::Instance()->GetRuntimeDb()->getContainer("CbmTrdParSetAsic"));
107 fGasPar = static_cast<CbmTrdParSetGas*>(FairRunAna::Instance()->GetRuntimeDb()->getContainer("CbmTrdParSetGas"));
108 fDigiPar = static_cast<CbmTrdParSetDigi*>(FairRunAna::Instance()->GetRuntimeDb()->getContainer("CbmTrdParSetDigi"));
109 fGainPar = static_cast<CbmTrdParSetGain*>(FairRunAna::Instance()->GetRuntimeDb()->getContainer("CbmTrdParSetGain"));
110 fGeoPar = new CbmTrdParSetGeo(); //fGeoPar->Print();
111}
112
113//________________________________________________________________________________________
115{
116 FairRootManager* ioman = FairRootManager::Instance();
117 if (!ioman) LOG(fatal) << "CbmTrdDigitizer::Init: No FairRootManager";
118
119 fPoints = (TClonesArray*) ioman->GetObject("TrdPoint");
120 if (!fPoints) LOG(fatal) << "CbmTrdDigitizer::Init(): No TrdPoint array!";
121
122 fTracks = (TClonesArray*) ioman->GetObject("MCTrack");
123 if (!fTracks) LOG(fatal) << "CbmTrdDigitizer::Init(): No MCTrack array!";
124
125 if (fRadiator) fRadiator->Init();
126
128 fQA = new CbmTrdCheckUtil();
129
130 // Set time-based mode if appropriate
131 SetTimeBased(fEventMode ? kFALSE : kTRUE);
132
133 // --- Read list of inactive channels
134 if (!fInactiveChannelFileName.IsNull()) {
135 LOG(info) << GetName() << ": Reading inactive channels from " << fInactiveChannelFileName;
136 auto result = ReadInactiveChannels();
137 if (!result.second) {
138 LOG(error) << GetName() << ": Error in reading from file! Task will be inactive.";
139 return kFATAL;
140 }
141 LOG(info) << GetName() << ": " << std::get<0>(result) << " lines read from file, " << fInactiveChannels.size()
142 << " channels set inactive";
143 }
144
146
147
148 LOG(info) << "================ TRD Digitizer ===============";
149 LOG(info) << " Free streaming : " << (IsTimeBased() ? "yes" : "no");
150 LOG(info) << " Add Noise : " << (AddNoise() ? "yes" : "no");
151 LOG(info) << " Weighted distance : " << (UseWeightedDist() ? "yes" : "no");
152
153 return kSUCCESS;
154}
155
156//________________________________________________________________________________________
158{
159 // start timer
160 TStopwatch timer;
161 timer.Start();
162
163 // get event info (once per event, used for matching)
164 GetEventInfo();
165
166 // reset private monitoring counters
168
169 // loop tracks in current event
170 CbmTrdModuleSim* mod(NULL);
171 Int_t nofPoints = fPoints->GetEntriesFast();
172 gGeoManager->CdTop();
173 for (Int_t iPoint = 0; iPoint < nofPoints; iPoint++) {
174 fpoints++;
175 //fMCPointId = iPoint;
176
177 CbmTrdPoint* point = static_cast<CbmTrdPoint*>(fPoints->At(iPoint));
178 if (!point) continue;
179 const CbmMCTrack* track = static_cast<const CbmMCTrack*>(fTracks->At(point->GetTrackID()));
180 if (!track) continue;
181
182 Double_t dz = point->GetZOut() - point->GetZIn();
183 if (dz < 0) {
184 LOG(debug2) << GetName() << "::Exec: MC-track points towards target!";
186 }
187
188 // get link to the module working class
189 map<Int_t, CbmTrdModuleSim*>::iterator imod = fModuleMap.find(point->GetDetectorID());
190 if (imod == fModuleMap.end()) {
191 // Looking for gas node corresponding to current point in geo manager
192 Double_t meanX = (point->GetXOut() + point->GetXIn()) / 2.;
193 Double_t meanY = (point->GetYOut() + point->GetYIn()) / 2.;
194 Double_t meanZ = (point->GetZOut() + point->GetZIn()) / 2.;
195 gGeoManager->FindNode(meanX, meanY, meanZ);
196 if (!TString(gGeoManager->GetPath()).Contains("gas")) {
197 LOG(error) << GetName() << "::Exec: MC-track not in TRD! Node:" << TString(gGeoManager->GetPath()).Data()
198 << " gGeoManager->MasterToLocal() failed!";
199 continue;
200 }
201 mod = AddModule(point->GetDetectorID());
202 }
203 else
204 mod = imod->second;
206 Double_t gamma = TMath::Sqrt(1 + TMath::Power(track->GetP() / (track->GetMass()), 2));
207 mod->SetGamma(gamma);
208 mod->MakeDigi(point, fCurrentEventTime, TMath::Abs(track->GetPdgCode()) == 11);
209 }
210
211 // Fill data from internally used stl map into CbmDaqBuffer.
212 // Calculate final event statistics
213 Int_t nDigis(0), nofElectrons(0), nofLatticeHits(0), nofPointsAboveThreshold(0), n0, n1, n2;
214 for (map<Int_t, CbmTrdModuleSim*>::iterator imod = fModuleMap.begin(); imod != fModuleMap.end(); imod++) {
215 // in streaming mode flush buffers only up to a certain point in time wrt to current event time (allow for event pile-ups)
216 //printf("Processing data for module %d\n", imod->first);
217 if (IsTimeBased()) nDigis += imod->second->FlushBuffer(fCurrentEventTime);
218 // in event-by-event mode flush all buffers
219 if (!IsTimeBased()) imod->second->FlushBuffer();
220 imod->second->GetCounters(n0, n1, n2);
221 nofElectrons += n0;
222 nofLatticeHits += n1;
223 nofPointsAboveThreshold += n2;
224 std::map<Int_t, std::pair<CbmTrdDigi*, CbmMatch*>>* digis = imod->second->GetDigiMap();
225 for (std::map<Int_t, pair<CbmTrdDigi*, CbmMatch*>>::iterator it = digis->begin(); it != digis->end(); it++) {
226 assert(it->second.second);
227 CbmTrdDigi* digi = it->second.first;
228 SendData(digi->GetTime(), digi, it->second.second);
229 nDigis++;
230 } //# modules
231 digis->clear();
232 } //# digis
234
235
236 Double_t digisOverPoints = (nofPoints > 0) ? Double_t(nDigis) / Double_t(nofPoints) : 0;
237 Double_t latticeHitsOverElectrons = (nofElectrons > 0) ? (Double_t) nofLatticeHits / (Double_t) nofElectrons : 0;
238 LOG(debug) << "CbmTrdDigitizer::Exec Points: " << nofPoints;
239 LOG(debug) << "CbmTrdDigitizer::Exec PointsAboveThreshold: " << nofPointsAboveThreshold;
240 LOG(debug) << "CbmTrdDigitizer::Exec Digis: " << nDigis;
241 LOG(debug) << "CbmTrdDigitizer::Exec digis/points: " << digisOverPoints;
242 LOG(debug) << "CbmTrdDigitizer::Exec BackwardTracks: " << nofBackwardTracks;
243 LOG(debug) << "CbmTrdDigitizer::Exec LatticeHits: " << nofLatticeHits;
244 LOG(debug) << "CbmTrdDigitizer::Exec Electrons: " << nofElectrons;
245 LOG(debug) << "CbmTrdDigitizer::Exec latticeHits/electrons:" << latticeHitsOverElectrons;
246 timer.Stop();
247 LOG(debug) << "CbmTrdDigitizer::Exec real time=" << timer.RealTime() << " CPU time=" << timer.CpuTime();
248
249 // --- Event log
250 LOG(info) << "+ " << setw(15) << GetName() << ": Event " << setw(6) << right << fCurrentEvent << " at " << fixed
251 << setprecision(3) << fCurrentEventTime << " ns, points: " << nofPoints << ", digis: " << nDigis
252 << ". Exec time " << setprecision(6) << timer.RealTime() << " s.";
253}
254
255//________________________________________________________________________________________
257{
258 LOG(info) << GetName() << ": Processing analogue buffers";
259 Int_t nDigis(0);
260 for (map<Int_t, CbmTrdModuleSim*>::iterator imod = fModuleMap.begin(); imod != fModuleMap.end(); imod++) {
261 nDigis += imod->second->FlushBuffer();
262 std::map<Int_t, std::pair<CbmTrdDigi*, CbmMatch*>>* digis = imod->second->GetDigiMap();
263 for (std::map<Int_t, pair<CbmTrdDigi*, CbmMatch*>>::iterator it = digis->begin(); it != digis->end(); it++) {
264 assert(it->second.second);
265 CbmTrdDigi* digi = it->second.first;
266 SendData(digi->GetTime(), digi, it->second.second);
267 nDigis++;
268 } //# modules
269 digis->clear();
270 } //# digis
271 LOG(info) << GetName() << ": " << nDigis << (nDigis == 1 ? " digi " : " digis ") << "created and sent to DAQ ";
272}
273
274//________________________________________________________________________________________
276{
277 // flush buffers in streaming mode
278 LOG(info) << "=====================================";
279 LOG(info) << GetName() << ": Finish run";
280 if (IsTimeBased()) FlushBuffers();
281 LOG(info) << GetName() << ": Run summary ";
282 LOG(info) << "=====================================";
283
284 fQA->DumpPlots();
285}
286
287//________________________________________________________________________________________
289{
305 const TString& path = gGeoManager->GetPath(); // decouple the local path variable from gGeoManager current path [AB]
306 LOG(debug) << GetName() << "::AddModule(" << path << ")"
307 << " det[" << detId << "]";
308
309 CbmTrdGeoHandler geoHandler;
310 Int_t moduleAddress = geoHandler.GetModuleAddress(path), moduleType = geoHandler.GetModuleType(path),
311 orientation = geoHandler.GetModuleOrientation(path), lyId = CbmTrdAddress::GetLayerId(detId);
312 if (moduleAddress != detId) {
313 LOG(error) << "CbmTrdDigitizer::AddModule: MC module ID " << detId << " does not match geometry definition "
314 << moduleAddress << ". Module init failed!";
315 return nullptr;
316 }
317 // try to load read-out parameters for module
318 const CbmTrdParModDigi* pDigi(NULL);
319 if (!fDigiPar || !(pDigi = (const CbmTrdParModDigi*) fDigiPar->GetModulePar(detId))) {
320 LOG(error) << GetName() << "::AddModule : No Read-Out params for module " << detId << " @ " << path
321 << ". Module init failed!";
322 return nullptr;
323 }
324
325 // find the type of TRD detector was hit. Temporary until a new scheme of setup parameters will be but in place. TODO
326 bool trd2d(false);
327 if (pDigi->GetPadPlaneType() >= 0)
328 trd2d = pDigi->IsPadPlane2D();
329 else
330 trd2d = (moduleType >= 9); // legacy support for old pad-plane addresing
331 LOG(debug) << GetName() << "::AddModule(" << path << " " << (trd2d ? '2' : '1') << "D] mod[" << moduleAddress;
332 CbmTrdModuleSim* module(NULL);
333 if (trd2d) {
334 // temporary fix for TRD-2Dh @ mCBM 2021 (legacy)
335 if (moduleType == 10)
336 SetUseFASP(kFALSE);
337 else
338 SetUseFASP();
339 module = fModuleMap[moduleAddress] = new CbmTrdModuleSim2D(moduleAddress, lyId, orientation, UseFASP());
340 // AB :: calibration wrt the Tof detector as the Bmon simulation is still in development (14.07.2022)
341 module->SetTimeSysOffset(-400);
342 Int_t rType(-1);
343 if ((rType = geoHandler.GetRadiatorType(path)) >= 0) {
344 if (!fRadiator2D) { // strong TRD-2D entrance window
345 // const Char_t *ewin = "Al;C;Air;C;Al";
346 const Char_t* ewin = "Al;C;HC;C;Al";
347 Float_t widths[] = {
348 1.2e-3, // 12 µm aluminized polyester foil
349 0.02, // carbon laminate sheets of 0.2 mm thickness
350 0.9, // 9mm Nomex honeycom
351 0.02, // carbon laminate sheets of 0.2 mm thickness
352 1.2e-3, // 12 µm aluminized polyester foil
353 };
354
355 // // light TRD-2D entrance window
356 // const Char_t *ewin = "Al;C;HC;Po;Al";
357 // Float_t widths[] = {
358 // 1.2e-3, // 12 µm aluminized polyester foil
359 // 0.02, // carbon laminate sheets of 0.2 mm thickness
360 // 0.9, // 9mm Nomex honeycom
361 // 0.0025, // polyethylen sheets of 50 µm thickness
362 // 1.2e-3, // 12 µm aluminized polyester foil
363 // }; pwidth = widths;
364 fRadiator2D = make_shared<CbmTrdRadiator>(kTRUE, "tdr18", ewin);
365 fRadiator2D->SetEWwidths(5, widths);
366 fRadiator2D->Init();
367 }
368 module->SetRadiator(fRadiator2D);
369 }
370 //((CbmTrdModuleSim2D*)module)->SetLabMeasurement();
371 }
372 else {
373 module = fModuleMap[moduleAddress] = new CbmTrdModuleSimR(moduleAddress, lyId, orientation);
374 module->SetMessageConverter(fConverter);
375 module->SetQA(fQA);
376 }
377 module->SetDigiPar(pDigi);
378
379 // try to load Geometry parameters for module
380 const CbmTrdParModGeo* pGeo(NULL);
381 if (!fGeoPar || !(pGeo = (const CbmTrdParModGeo*) fGeoPar->GetModulePar(detId))) {
382 LOG(info) << GetName() << "::AddModule : No Geo params for module " << detId << " @ " << path << ". Using default.";
383 module->SetGeoPar(new CbmTrdParModGeo(Form("TRD_%d", detId), path));
384 }
385 else
386 module->SetGeoPar(pGeo);
387
388 // TODO check if this works also for TRD1D modules
389 if (trd2d) {
390 // try to load ASIC parameters for module
391 CbmTrdParModAsic* pAsic(NULL);
392 if (!fAsicPar || !(pAsic = (CbmTrdParModAsic*) fAsicPar->GetModulePar(detId))) {
393 LOG(fatal) << GetName() << "::AddModule : No ASIC params for module " << detId << " @ " << path << ". Abort.";
394 }
395 else
396 module->SetAsicPar(pAsic);
397
398 // try to load Chamber parameters for module
399 const CbmTrdParModGas* pChmb(NULL);
400 if (!fGasPar || !(pChmb = (const CbmTrdParModGas*) fGasPar->GetModulePar(detId))) {
401 LOG(info) << GetName() << "::AddModule : No Gas params for module " << detId << " @ " << path
402 << ". Using default.";
403 }
404 else
405 module->SetChmbPar(pChmb);
406
407 // try to load Gain parameters for module
408 const CbmTrdParModGain* pGain(NULL);
409 if (!fGainPar || !(pGain = (const CbmTrdParModGain*) fGainPar->GetModulePar(detId))) {
410 LOG(debug) << GetName() << "::AddModule : No Gain params for module " << detId << " @ " << path
411 << ". Using default.";
412 }
413 else
414 module->SetGainPar(pGain);
415 }
416
417 if (fRadiator) module->SetRadiator(fRadiator);
418
419 // Register this class to the module. For data transport through SendData().
420 module->SetDigitizer(this);
421
422
423 return module;
424}
425
426//________________________________________________________________________________________
428{
431 fpoints = 0;
433 for (std::map<Int_t, CbmTrdModuleSim*>::iterator imod = fModuleMap.begin(); imod != fModuleMap.end(); imod++)
434 imod->second->ResetCounters();
435}
436
437
439{
440
441 if (fDigis) fDigis->clear();
442 if (fDigiMatches) fDigiMatches->clear();
443}
444
445
ClassImp(CbmConverterManager)
Helper class to convert unique channel ID back and forth.
TRD digitizer. Updated 24/04/2013 by Andrey Lebedev andrey.lebedev@gsi.de Updated 4/06/2018 by Alex B...
Helper class to extract information from the GeoManager.
virtual std::pair< size_t, bool > ReadInactiveChannels()
Set of inactive channels, indicated by CbmAddress.
Int_t fCurrentEvent
Number of current input.
TString fInactiveChannelFileName
Time of current MC event [ns].
std::set< uint32_t > fInactiveChannels
Name of file with inactive channels.
void GetEventInfo()
Get event information.
Int_t fCurrentInput
Start time of run [ns].
Double_t fCurrentEventTime
Number of current MC entry.
Int_t fCurrentMCEntry
Number of current MC event.
Base class template for CBM digitisation tasks.
Definition CbmDigitize.h:44
void SendData(Double_t time, CbmTrdDigi *digi, CbmMatch *match=nullptr)
double GetP() const
Definition CbmMCTrack.h:98
double GetMass() const
Mass of the associated particle.
int32_t GetPdgCode() const
Definition CbmMCTrack.h:68
static uint32_t GetLayerId(uint32_t address)
Return layer ID from address.
double GetTime() const
Getter for physical time [ns]. Accounts for clock representation of each ASIC. In SPADIC case physica...
Definition CbmTrdDigi.h:153
virtual void Exec(Option_t *option)
Inherited from FairTask.
static Bool_t AddNoise()
CbmTrdParSetAsic * fAsicPar
parameter list for ASIC characterization
std::shared_ptr< CbmTrdRadiator > fRadiator2D
parametrization of 2D radiator TR yield
static void SetTimeBased(Bool_t set=kTRUE)
virtual void SetParContainers()
Inherited from FairTask.
CbmTrdRawToDigiR * fConverter
virtual InitStatus Init()
Inherited from FairTask.
CbmTrdParSetGas * fGasPar
parameter list for HV status
void ResetCounters()
Recursive reset all private monitoring counters.
CbmTrdParSetGain * fGainPar
parameter list for keV->ADC gain conversion
virtual void Finish()
Inherited from FairTask.
std::shared_ptr< CbmTrdRadiator > fRadiator
parametrization of radiator TR yield
static Bool_t UseWeightedDist()
Double_t fLastEventTime
time of last event [ns]
CbmTrdModuleSim * AddModule(Int_t detId)
Create module for current MC point.
static void SetUseFASP(Bool_t set=kTRUE)
CbmTrdDigitizer(std::shared_ptr< CbmTrdRadiator > radiator=nullptr)
Constructor.
std::map< Int_t, CbmTrdModuleSim * > fModuleMap
list of modules being processed
TClonesArray * fTracks
MC Track information.
virtual void ResetArrays()
Clear data arrays.
CbmTrdParSetDigi * fDigiPar
parameter list for read-out geometry
std::vector< CbmTrdDigi > * fDigis
Output CbmTrdDigi array.
std::vector< CbmMatch > * fDigiMatches
Output CbmMatch array.
TClonesArray * fPoints
Trd MC points.
CbmTrdCheckUtil * fQA
static Bool_t IsTimeBased()
void FlushBuffers()
Flush local digi buffers to CbmDaqBuffer.
CbmTrdParSetGeo * fGeoPar
parameter list for geometry definitions
virtual ~CbmTrdDigitizer()
Destructor.
static Int_t fConfig
Configuration map for the digitizer. See CbmTrdSimDef for details.
Int_t GetModuleType(const TString &path)
Int_t GetRadiatorType(const TString &path)
Navigate to node and return the radiator type build in the geometry based on the naming convention.
Int_t GetModuleOrientation(const TString &path)
Return pad orientation of the current node in the TGeoManager.
Int_t GetModuleAddress()
Return module address calculated based on the current node in the TGeoManager.
virtual const Char_t * GetPath() const
Abstract class for module wise digitization and raw format producing.
virtual Bool_t MakeDigi(CbmTrdPoint *p, Double_t time, Bool_t TR=kFALSE)=0
Steering routine for converting MC point to digits.
virtual void SetLinkId(Int_t input, Int_t event=-1, Int_t point=-1)
virtual void SetGamma(Double_t gamma=0.)=0
Describe TRD module ASIC settings (electronic gain, delays, etc)
Definition of chamber gain conversion for one TRD module.
int GetPadPlaneType() const
Access the basic type of pad plane topology. For convenience also specific accessors are added for ea...
bool IsPadPlane2D() const
Definition of gain parameters for one TRD module.
Definition of gas parameters for one TRD module.
Definition of geometry for one TRD module.
Describe TRD module ASIC settings (electronic gain, delays, etc)
Describe TRD module working settings (HV, etc)
virtual const CbmTrdParMod * GetModulePar(Int_t detId) const
double GetYOut() const
Definition CbmTrdPoint.h:69
double GetXIn() const
Definition CbmTrdPoint.h:65
double GetZIn() const
Definition CbmTrdPoint.h:67
double GetXOut() const
Definition CbmTrdPoint.h:68
double GetYIn() const
Definition CbmTrdPoint.h:66
double GetZOut() const
Definition CbmTrdPoint.h:70
Hash for CbmL1LinkKey.