CbmRoot
Loading...
Searching...
No Matches
CbmCaMCModule.h
Go to the documentation of this file.
1/* Copyright (C) 2022-2023 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Sergei Zharko [committer] */
4
9
10#ifndef CbmCaMCModule_h
11#define CbmCaMCModule_h 1
12
13#include "CaMonitor.h"
14#include "CaParameters.h"
15#include "CaToolsMCData.h"
16#include "CaToolsMCPoint.h"
17#include "CaVector.h"
18#include "CbmL1DetectorID.h"
19#include "CbmL1Hit.h"
20#include "CbmL1Track.h"
21#include "CbmLink.h"
22#include "CbmMCDataArray.h"
23#include "CbmMCEventList.h"
24#include "CbmMCTrack.h"
25#include "CbmMatch.h"
26#include "CbmMuchPoint.h"
28#include "CbmMvdPoint.h"
30#include "CbmStsPoint.h"
32#include "CbmTimeSlice.h"
33#include "CbmTofPoint.h"
35#include "CbmTrdPoint.h"
37#include "TClonesArray.h"
38#include "TDatabasePDG.h"
39
40#include <numeric>
41#include <optional>
42#include <string>
43#include <string_view>
44#include <type_traits>
45
46class CbmEvent;
47class CbmMCDataObject;
49class CbmL1Track;
50
51namespace cbm::ca
52{
53 namespace ca = cbm::algo::ca;
54
57 class MCModule {
58
59
60 public:
64 MCModule(int verb = 1, int perfMode = 1) : fVerbose(verb), fPerformanceMode(perfMode)
65 {
66 LOG(info) << "cbm::ca::MCModule: performance mode = " << fPerformanceMode;
67 }
68
70 ~MCModule() = default;
71
73 MCModule(const MCModule&) = delete;
74
76 MCModule(MCModule&&) = delete;
77
79 MCModule& operator=(const MCModule&) = delete;
80
83
84
86 void Finish();
87
89 const tools::MCData* GetMCData() const { return fpMCData; }
90
94 void InitEvent(CbmEvent* pEvent);
95
98 bool InitRun();
99
107 template<ca::EDetectorID DetId>
108 std::tuple<int, std::vector<int>> MatchHitWithMc(int iHitExt);
109
111 void MatchHits();
112
116 void MatchTracks();
117
121 void ProcessEvent(CbmEvent* pEvent);
122
125 void RegisterFirstHitIndexes(const std::array<int, constants::size::MaxNdetectors + 1>& source)
126 {
127 fpvFstHitId = &source;
128 }
129
134 void SetDetector(ca::EDetectorID detID, bool flag) { fvbUseDet[detID] = flag; }
135
138 void RegisterMCData(tools::MCData& mcData) { fpMCData = &mcData; }
139
143
147
150 void RegisterParameters(std::shared_ptr<ca::Parameters<float>>& pParameters) { fpParameters = pParameters; }
151
155
157 int GetVerbosity() const { return fVerbose; }
158
161 void CheckInit() const;
162
168 void InitTrackInfo();
169
174 template<ca::EDetectorID DetID>
175 void MatchPointsAndHits();
176
186
187 private:
189 void ReadMCTracks();
190
192 void ReadMCPoints();
193
195 template<ca::EDetectorID DetID>
197
206 template<ca::EDetectorID DetID>
207 std::optional<tools::MCPoint> FillMCPoint(int iExtId, int iEvent, int iFile);
208
225
226 // ------ Flags
228 int fVerbose = 1;
230
231 std::shared_ptr<ca::Parameters<float>> fpParameters = nullptr;
232 // ------ Input data branches
233 const CbmTimeSlice* fpTimeSlice = nullptr;
237
239
242
243 // Matching information
244 std::set<std::pair<int, int>> fFileEventIDs;
245 int fBestMcFile = -1;
246 int fBestMcEvent = -1;
247
248
249 // ----- Internal MC data
251
252 // ----- Internal reconstructed data
256
260 const std::array<int, constants::size::MaxNdetectors + 1>* fpvFstHitId = nullptr;
261 };
262
263
264 // **********************************************
265 // ** Template function implementation **
266 // **********************************************
267
268 // -------------------------------------------------------------------------------------------------------------------
269 //
270 template<ca::EDetectorID DetID>
271 std::tuple<int, std::vector<int>> MCModule::MatchHitWithMc(int iHitExt)
272 {
273 int iPoint = -1;
274 std::vector<int> vPoints;
275 const auto* pHitMatch = dynamic_cast<CbmMatch*>(fvpBrHitMatches[DetID]->At(iHitExt));
276 if (!pHitMatch) {
277 LOG(warn) << "Hit match with index " << iHitExt << " is missing for " << kDetName[DetID];
278 if constexpr (ca::EDetectorID::kMvd == DetID) {
280 }
281 else if constexpr (ca::EDetectorID::kSts == DetID) {
283 }
284 else if constexpr (ca::EDetectorID::kMuch == DetID) {
286 }
287 else if constexpr (ca::EDetectorID::kTrd == DetID) {
289 }
290 else if constexpr (ca::EDetectorID::kTof == DetID) {
292 }
293 return std::tuple(iPoint, vPoints);
294 }
295
296 for (int iLink = 0; iLink < pHitMatch->GetNofLinks(); ++iLink) {
297 const auto& link = pHitMatch->GetLink(iLink);
298 int iPointExt = link.GetIndex();
299 int iEvent = link.GetEntry();
300 int iFile = link.GetFile();
301 if (iPointExt < 0) continue;
302 int id = fpMCData->FindInternalPointIndex(DetID, iPointExt, iEvent, iFile);
303 vPoints.push_back(id);
304 }
305
306 if (pHitMatch->GetNofLinks() > 0) {
307 const auto& link = pHitMatch->GetMatchedLink();
308 if (link.GetIndex() > -1) {
309 int index = link.GetIndex();
310 int event = link.GetEntry();
311 int file = link.GetFile();
312 iPoint = fpMCData->FindInternalPointIndex(DetID, index, event, file);
313 }
314 }
315
316 return std::tuple(iPoint, vPoints);
317 }
318
319 // -------------------------------------------------------------------------------------------------------------------
320 //
321 template<ca::EDetectorID DetID>
322 std::optional<tools::MCPoint> MCModule::FillMCPoint(int iExtId, int iEvent, int iFile)
323 {
324 auto oPoint = std::make_optional<tools::MCPoint>();
325
326 // ----- Get positions, momenta, time and track ID
327 TVector3 posIn; // Position at entrance to station [cm]
328 TVector3 posOut; // Position at exist of station [cm]
329 TVector3 momIn; // 3-momentum at entrance to station [GeV/c]
330 TVector3 momOut; // 3-momentum at exit of station [GeV/c]
331
332 auto* pBrPoints = fvpBrPoints[DetID];
333
334 PointTypes_t::at<DetID>* pExtPoint = dynamic_cast<PointTypes_t::at<DetID>*>(pBrPoints->Get(iFile, iEvent, iExtId));
335 if (!pExtPoint) {
336 LOG(warn) << "CbmCaMCModule: " << kDetName[DetID] << " MC point with iExtId = " << iExtId
337 << ", iEvent = " << iEvent << ", iFile = " << iFile << " does not exist";
338 return std::nullopt;
339 }
340 if constexpr (ca::EDetectorID::kMvd == DetID) {
341 pExtPoint->Position(posIn);
342 pExtPoint->PositionOut(posOut);
343 pExtPoint->Momentum(momIn);
344 pExtPoint->MomentumOut(momOut);
345 }
346 // STS
347 else if constexpr (ca::EDetectorID::kSts == DetID) {
348 pExtPoint->Position(posIn);
349 pExtPoint->PositionOut(posOut);
350 pExtPoint->Momentum(momIn);
351 pExtPoint->MomentumOut(momOut);
352 }
353 // MuCh
354 else if constexpr (ca::EDetectorID::kMuch == DetID) {
355 pExtPoint->Position(posIn);
356 pExtPoint->PositionOut(posOut);
357 pExtPoint->Momentum(momIn);
358 pExtPoint->Momentum(momOut);
359 }
360 // TRD
361 else if constexpr (ca::EDetectorID::kTrd == DetID) {
362 pExtPoint->Position(posIn);
363 pExtPoint->PositionOut(posOut);
364 pExtPoint->Momentum(momIn);
365 pExtPoint->MomentumOut(momOut);
366 }
367 // TOF
368 else if constexpr (ca::EDetectorID::kTof == DetID) {
369 pExtPoint->Position(posIn);
370 pExtPoint->Position(posOut);
371 pExtPoint->Momentum(momIn);
372 pExtPoint->Momentum(momOut);
373 }
374 double time = pExtPoint->GetTime();
375 int iTmcExt = pExtPoint->GetTrackID();
376
377 if (iTmcExt < 0) {
378 LOG(warn) << "CbmCaMCModule: For MC point with iExtId = " << iExtId << ", iEvent = " << iEvent
379 << ", iFile = " << iFile << " MC track is undefined (has ID = " << iTmcExt << ')';
380 return std::nullopt;
381 }
382 TVector3 posMid = 0.5 * (posIn + posOut);
383 TVector3 momMid = 0.5 * (momIn + momOut);
384
385 // // ----- Get station index
386 int iStLoc = fvpDetInterface[DetID]->GetTrackingStationIndex(pExtPoint);
387 if (iStLoc < 0) {
388 return std::nullopt;
389 }
390
391 int stationID = fpParameters->GetStationIndexActive(iStLoc, DetID);
392 if (stationID == -1) {
393 return std::nullopt;
394 } // Skip points from inactive stations
395
396 // Update point time with event time
397 time += fpMCEventList->GetEventTime(iEvent, iFile);
398
399 // ----- Reject MC points falling out of the time slice
400 // STS, MuCh, TRD, TOF
401 if constexpr (DetID != ca::EDetectorID::kMvd) {
402 // TODO: SZh 18.06.2024: Avoid dependency from CbmTimeSlice, if possible (no TimeSlice branch in the online unpack)
403 double startT = fpTimeSlice->GetStartTime();
404 double endT = fpTimeSlice->GetEndTime();
405
406 if ((startT > 0. && time < startT) || (endT > 0. && time > endT)) {
407 LOG(warn) << "CbmCaMCModule: MC point with iExtId = " << iExtId << ", iEvent = " << iEvent
408 << ", iFile = " << iFile << " and det id " << int(DetID) << " fell out of the TS duration [" << startT
409 << ", " << endT << "] with measured time = " << time << " [ns]";
410 return std::nullopt;
411 }
412 }
413
414 // ----- Fill MC point
415 oPoint->SetExternalId(fpMCData->GetPointGlobExtIndex(DetID, iExtId));
416 oPoint->SetEventId(iEvent);
417 oPoint->SetFileId(iFile);
418 oPoint->SetTime(time);
419 oPoint->SetX(posMid.X());
420 oPoint->SetY(posMid.Y());
421 oPoint->SetZ(posMid.Z());
422 oPoint->SetXIn(posIn.X());
423 oPoint->SetYIn(posIn.Y());
424 oPoint->SetZIn(posIn.Z());
425 oPoint->SetXOut(posOut.X());
426 oPoint->SetYOut(posOut.Y());
427 oPoint->SetZOut(posOut.Z());
428 oPoint->SetPx(momMid.X());
429 oPoint->SetPy(momMid.Y());
430 oPoint->SetPz(momMid.Z());
431 oPoint->SetPxIn(momIn.X());
432 oPoint->SetPyIn(momIn.Y());
433 oPoint->SetPzIn(momIn.Z());
434 oPoint->SetPxOut(momOut.X());
435 oPoint->SetPyOut(momOut.Y());
436 oPoint->SetPzOut(momOut.Z());
437
438 // Select current number of points as a local id of points
439 oPoint->SetId(fpMCData->GetNofPoints());
440
441 // Match MC track and point to each other
442 int iTmcInt = fpMCData->FindInternalTrackIndex(iTmcExt, iEvent, iFile);
443
444 oPoint->SetTrackId(iTmcInt);
445 if (iTmcInt > -1) {
446 fpMCData->GetTrack(iTmcInt).AddPointIndex(oPoint->GetId());
447 }
448
449 oPoint->SetStationId(stationID);
450 oPoint->SetDetectorId(DetID);
451
452 auto* pExtTrk = dynamic_cast<CbmMCTrack*>(fpMCTracks->Get(iFile, iEvent, iTmcExt));
453 if (!pExtTrk) {
454 LOG(warn) << "CbmCaMCModule: MC track with iTmcExt = " << iTmcExt << ", iEvent = " << iEvent
455 << ", iFile = " << iFile << " MC track is undefined (nullptr)";
456 return std::nullopt;
457 }
458 oPoint->SetPdgCode(pExtTrk->GetPdgCode());
459 oPoint->SetMotherId(pExtTrk->GetMotherId());
460
461 auto* pPdgDB = TDatabasePDG::Instance()->GetParticle(oPoint->GetPdgCode());
462 oPoint->SetMass(pPdgDB ? pPdgDB->Mass() : 0.);
463 oPoint->SetCharge(pPdgDB ? pPdgDB->Charge() / 3. : 0.);
464
465 return oPoint;
466 }
467
468 // -------------------------------------------------------------------------------------------------------------------
469 //
470 template<ca::EDetectorID DetID>
472 {
473 if (!fvbUseDet[DetID]) {
474 return;
475 }
476
477 for (int iH = (*fpvFstHitId)[static_cast<int>(DetID)]; iH < (*fpvFstHitId)[static_cast<int>(DetID) + 1]; ++iH) {
478 auto& hit = (*fpvQaHits)[iH];
479 auto [iBestP, vAllP] = MatchHitWithMc<DetID>(hit.ExtIndex);
480 if (iBestP >= 0) {
481 hit.SetBestMcPointId(iBestP);
482 }
483 for (auto iP : vAllP) {
484 if (iP >= 0) {
485 hit.AddMcPointId(iP);
486 fpMCData->GetPoint(iP).AddHitID(iH);
487 }
488 }
489 }
490 }
491 // -------------------------------------------------------------------------------------------------------------------
492 // NOTE: template is used, because another template function FillMCPoint is used inside
493 template<ca::EDetectorID DetID>
495 {
496 if (!fvbUseDet[DetID]) {
497 return;
498 }
499 for (const auto& [iFile, iEvent] : fFileEventIDs) {
500 int nPointsEvent = fvpBrPoints[DetID]->Size(iFile, iEvent);
501 for (int iP = 0; iP < nPointsEvent; ++iP) {
502 std::optional<tools::MCPoint> oPoint = FillMCPoint<DetID>(iP, iEvent, iFile);
503 if (oPoint) {
504 fpMCData->AddPoint(*oPoint);
505 }
506 } // iP: end
507 } // key: end
508 }
509} // namespace cbm::ca
510
511#endif // CbmCaMCModule_h
CA Tracking monitor class.
Data structure for internal tracking MC-information (header)
Internal class describing a MC point for CA tracking QA and performance (header)
Implementation of L1DetectorID enum class for CBM.
Class characterising one event by a collection of links (indices) to data objects,...
Definition CbmEvent.h:34
Access to a MC data branch for time-based analysis.
TObject * Get(const CbmLink *lnk)
Access to a MC data branch for time-based analysis.
Container class for MC events with number, file and start time.
double GetEventTime(uint32_t event, uint32_t file)
Event start time.
void SetMotherId(int32_t id)
Definition CbmMCTrack.h:110
Bookkeeping of time-slice content.
double GetEndTime() const
double GetStartTime() const
Monitor class for the CA tracking.
Definition CaMonitor.h:35
void IncrementCounter(ECounterKey key)
Increments key counter by 1.
Definition CaMonitor.h:99
A container for all external parameters of the CA tracking algorithm.
Class CbmCaPerformance is an interface to communicate between.
std::tuple< int, std::vector< int > > MatchHitWithMc(int iHitExt)
Matches hit with MC point.
DetIdArr_t< CbmTrackingDetectorInterfaceBase * > fvpDetInterface
Tracking detector interface.
void ReadMCTracks()
Reads MC tracks from external trees and saves them to MCDataObject.
MCModule & operator=(const MCModule &)=delete
Copy assignment operator.
void InitEvent(CbmEvent *pEvent)
Defines performance action in the beginning of each event or time slice.
void Finish()
Defines performance action in the end of the run.
MCModule & operator=(MCModule &&)=delete
Move assignment operator.
void CheckInit() const
Check class initialization.
CbmMCEventList * fpMCEventList
MC event list.
int fBestMcFile
Index of bestly matched MC file.
void ProcessEvent(CbmEvent *pEvent)
Processes event.
ca::Vector< CbmL1HitId > * fpvHitIds
Pointer to hit index container.
void ReadMCPoints()
Reads MC points from external trees and saves them to MCDataObject.
void RegisterFirstHitIndexes(const std::array< int, constants::size::MaxNdetectors+1 > &source)
Sets first hit indexes container in different detectors.
ca::Monitor< EMonitorKey > fMonitor
Monitor.
const tools::MCData * GetMCData() const
Gets a pointer to MC data object.
int GetVerbosity() const
Gets verbosity level.
DetIdArr_t< bool > fvbUseDet
Flag: is detector subsystem used.
const std::array< int, constants::size::MaxNdetectors+1 > * fpvFstHitId
Pointer to array of first hit indexes in the detector subsystem.
void SetDetector(ca::EDetectorID detID, bool flag)
Sets used detector subsystems.
int fVerbose
Verbosity level.
tools::MCData * fpMCData
MC information (hits and tracks) instance.
~MCModule()=default
Destructor.
DetIdArr_t< CbmMCDataArray * > fvpBrPoints
Array of points vs. detector.
void RegisterMCData(tools::MCData &mcData)
Registers MC data object.
void MatchRecoAndMCTracks()
Matches reconstructed tracks with MC tracks.
CbmMCDataObject * fpMCEventHeader
MC event header.
void ReadMCPointsForDetector()
Reads MC points in particular detector.
std::optional< tools::MCPoint > FillMCPoint(int iExtId, int iEvent, int iFile)
Fills a single detector-specific MC point.
EMonitorKey
Monitor keys.
@ kMissedMatchesMvd
Number of missed matches in MVD.
@ kMissedMatchesTof
Number of missed TOF matches.
@ kMissedMatchesTrd
Number of missed matches in TRD.
@ kMcPoint
Number of MC points.
@ kMissedMatchesMuch
Number of missed matches in MuCh.
@ kRecoNevents
Number of events.
@ kMissedMatchesSts
Number of missed matches in STS.
@ kMcTrack
Number of MC tracks.
@ kMcTrackReconstructable
Number of reconstructable MC tracks.
void MatchPointsAndHits()
Match sets of MC points and reconstructed hits for a given detector.
ca::Vector< CbmL1HitDebugInfo > * fpvQaHits
Pointer to QA hit container.
std::set< std::pair< int, int > > fFileEventIDs
Set of file and event indexes: first - iFile, second - iEvent.
bool InitRun()
Defines action on the module in the beginning of the run.
int fBestMcEvent
Index of bestly matched MC event.
void RegisterQaHitContainer(ca::Vector< CbmL1HitDebugInfo > &vQaHits)
Registers debug hit container.
ca::Vector< CbmL1Track > * fpvRecoTracks
Pointer to reconstructed track container.
MCModule(const MCModule &)=delete
Copy constructor.
void RegisterRecoTrackContainer(ca::Vector< CbmL1Track > &vRecoTracks)
Registers reconstructed track container.
DetIdArr_t< TClonesArray * > fvpBrHitMatches
Array of hit match branches vs. detector.
CbmMCDataArray * fpMCTracks
MC tracks input.
void MatchHits()
Match reconstructed hits and MC points.
MCModule(int verb=1, int perfMode=1)
Constructor.
MCModule(MCModule &&)=delete
Move constructor.
void RegisterHitIndexContainer(ca::Vector< CbmL1HitId > &vHitIds)
Registers hit index container.
const CbmTimeSlice * fpTimeSlice
Current time slice.
void MatchTracks()
Match reconstructed and MC data.
int fPerformanceMode
Mode of performance.
void RegisterParameters(std::shared_ptr< ca::Parameters< float > > &pParameters)
Registers CA parameters object.
void InitTrackInfo()
Initializes MC track.
std::shared_ptr< ca::Parameters< float > > fpParameters
Pointer to tracking parameters object.
This class represents a package for tracking-related data.
void AddPoint(const MCPoint &point)
int GetNofPoints() const
Gets number of points in this event/TS.
const auto & GetTrack(int idx) const
Gets a reference to MC track by its internal index.
int GetPointGlobExtIndex(ca::EDetectorID detID, int iPointLocal) const
Calculates global index of MC point.
int FindInternalTrackIndex(int index, int event, int file) const
Finds an index of MC track in internal track container.
int FindInternalPointIndex(ca::EDetectorID detID, int index, int event, int file) const
Finds an index of MC point in internal point container.
const auto & GetPoint(int idx) const
Gets a reference to MC point by its index.
TODO: SZh 8.11.2022: add selection of parameterisation.
Definition CaBranch.h:14
EDetectorID
Enumeration for the tracking detector subsystems in CBM-CA.
Definition CbmDefs.h:176
constexpr DetIdArr_t< const char * > kDetName
Names of detector subsystems.
std::tuple_element_t< static_cast< std::size_t >(DetID), std::tuple< Types... > > at