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 "CaMcData.h"
14#include "CaMcHitInfo.h"
15#include "CaMcPoint.h"
16#include "CaMonitor.h"
17#include "CaParameters.h"
18#include "CaVector.h"
19#include "CbmL1DetectorID.h"
20#include "CbmL1HitId.h"
21#include "CbmL1Track.h"
22#include "CbmLink.h"
23#include "CbmMCDataArray.h"
24#include "CbmMCEventList.h"
25#include "CbmMCTrack.h"
26#include "CbmMatch.h"
27#include "CbmMuchPoint.h"
28#include "CbmMvdPoint.h"
29#include "CbmRecoSetupManager.h"
30#include "CbmStsPoint.h"
31#include "CbmTimeSlice.h"
32#include "CbmTofPoint.h"
33#include "CbmTrdPoint.h"
34#include "TClonesArray.h"
35#include "TDatabasePDG.h"
36
37#include <numeric>
38#include <optional>
39#include <string>
40#include <string_view>
41#include <type_traits>
42
43class CbmEvent;
44class CbmMCDataObject;
45class CbmL1Track;
46
47
48namespace cbm::ca
49{
50 namespace ca = cbm::algo::ca;
51
54 class MCModule {
55
56
57 public:
61 MCModule(int verb = 1, int perfMode = 1) : fVerbose(verb), fPerformanceMode(perfMode)
62 {
63 LOG(info) << "cbm::ca::MCModule: performance mode = " << fPerformanceMode;
64 }
65
67 ~MCModule() = default;
68
70 MCModule(const MCModule&) = delete;
71
73 MCModule(MCModule&&) = delete;
74
76 MCModule& operator=(const MCModule&) = delete;
77
80
81
83 void Finish();
84
86 const cbm::algo::ca::McData* GetMcData() const { return fpMcData; }
87
91 void InitEvent(CbmEvent* pEvent);
92
95 bool InitRun();
96
104 template<ca::EDetectorID DetId>
105 std::tuple<int, std::vector<int>> MatchHitWithMc(int iHitExt);
106
108 void MatchHits();
109
113 void MatchTracks();
114
118 void ProcessEvent(CbmEvent* pEvent);
119
122 void RegisterFirstHitIndexes(const std::array<int, constants::size::MaxNdetectors + 1>& source)
123 {
124 fpvFstHitId = &source;
125 }
126
131 void SetDetector(ca::EDetectorID detID, bool flag) { fvbUseDet[detID] = flag; }
132
135 void RegisterMcData(cbm::algo::ca::McData& mcData) { fpMcData = &mcData; }
136
140
144
147 void RegisterParameters(std::shared_ptr<const ca::Parameters<double>>& pParameters) { fpParameters = pParameters; }
148
152
154 int GetVerbosity() const { return fVerbose; }
155
158 void CheckInit() const;
159
165 void InitMcTrackInfo();
166
171 template<ca::EDetectorID DetID>
172 void MatchPointsAndHits();
173
183
184 private:
185
186
203
204 // ------ Flags
206 int fVerbose = 1;
208
209 std::shared_ptr<const ca::Parameters<double>> fpParameters = nullptr;
210 // ------ Input data branches
211 const CbmTimeSlice* fpTimeSlice = nullptr;
215
218
219 // Matching information
220 std::set<std::pair<int, int>> fFileEventIDs;
221 int fBestMcFile = -1;
222 int fBestMcEvent = -1;
223
224
225 // ----- Internal MC data
227
228 // ----- Internal reconstructed data
232
236 std::unordered_map<uint32_t, double> fmTofRpcZpos;
237
241 const std::array<int, constants::size::MaxNdetectors + 1>* fpvFstHitId = nullptr;
242
251 template<ca::EDetectorID DetID>
252 std::optional<algo::ca::McPoint> FillMCPoint(int iExtId, int iEvent, int iFile);
253
255 void ReadMCTracks();
256
258 void ReadMCPoints();
259
261 template<ca::EDetectorID DetID>
263 };
264
265
266 // **********************************************
267 // ** Template function implementation **
268 // **********************************************
269
270 // -------------------------------------------------------------------------------------------------------------------
271 //
272 template<ca::EDetectorID DetID>
273 std::tuple<int, std::vector<int>> MCModule::MatchHitWithMc(int iHitExt)
274 {
275 int iPoint = -1;
276 std::vector<int> vPoints;
277 const auto* pHitMatch = dynamic_cast<CbmMatch*>(fvpBrHitMatches[DetID]->At(iHitExt));
278 if (!pHitMatch) {
279 LOG(warn) << "Hit match with index " << iHitExt << " is missing for " << kDetName[DetID];
280 if constexpr (ca::EDetectorID::kMvd == DetID) {
282 }
283 else if constexpr (ca::EDetectorID::kSts == DetID) {
285 }
286 else if constexpr (ca::EDetectorID::kMuch == DetID) {
288 }
289 else if constexpr (ca::EDetectorID::kTrd == DetID) {
291 }
292 else if constexpr (ca::EDetectorID::kTof == DetID) {
294 }
295 return std::tuple(iPoint, vPoints);
296 }
297
298 for (int iLink = 0; iLink < pHitMatch->GetNofLinks(); ++iLink) {
299 const auto& link = pHitMatch->GetLink(iLink);
300 int iPointExt = link.GetIndex();
301 int iEvent = link.GetEntry();
302 int iFile = link.GetFile();
303 if (iPointExt < 0) continue;
304 int id = fpMcData->FindInternalPointIndex(DetID, iPointExt, iEvent, iFile);
305 vPoints.push_back(id);
306 }
307
308 if (pHitMatch->GetNofLinks() > 0) {
309 const auto& link = pHitMatch->GetMatchedLink();
310 if (link.GetIndex() > -1) {
311 int index = link.GetIndex();
312 int event = link.GetEntry();
313 int file = link.GetFile();
314 iPoint = fpMcData->FindInternalPointIndex(DetID, index, event, file);
315 }
316 }
317
318 return std::tuple(iPoint, vPoints);
319 }
320
321 // -------------------------------------------------------------------------------------------------------------------
322 //
323 template<ca::EDetectorID DetID>
324 std::optional<ca::McPoint> MCModule::FillMCPoint(int iExtId, int iEvent, int iFile)
325 {
326 auto oPoint = std::make_optional<ca::McPoint>();
327
328 // ----- Get positions, momenta, time and track ID
329 TVector3 posIn; // Position at entrance to station [cm]
330 TVector3 posOut; // Position at exist of station [cm]
331 TVector3 momIn; // 3-momentum at entrance to station [GeV/c]
332 TVector3 momOut; // 3-momentum at exit of station [GeV/c]
333
334 auto* pBrPoints = fvpBrPoints[DetID];
335
336 const auto* pRecoUnit = cbm::RecoSetupManager::Instance()->GetSetup().Get<ToCbmModuleId(DetID)>();
337 assert(pRecoUnit); // TODO: Provide a proper validation
338
339 PointTypes_t::at<DetID>* pExtPoint = dynamic_cast<PointTypes_t::at<DetID>*>(pBrPoints->Get(iFile, iEvent, iExtId));
340 if (!pExtPoint) {
341 LOG(warn) << "CbmCaMCModule: " << kDetName[DetID] << " MC point with iExtId = " << iExtId
342 << ", iEvent = " << iEvent << ", iFile = " << iFile << " does not exist";
343 return std::nullopt;
344 }
345 if constexpr (ca::EDetectorID::kMvd == DetID) {
346 pExtPoint->Position(posIn);
347 pExtPoint->PositionOut(posOut);
348 pExtPoint->Momentum(momIn);
349 pExtPoint->MomentumOut(momOut);
350 }
351 // STS
352 else if constexpr (ca::EDetectorID::kSts == DetID) {
353 pExtPoint->Position(posIn);
354 pExtPoint->PositionOut(posOut);
355 pExtPoint->Momentum(momIn);
356 pExtPoint->MomentumOut(momOut);
357 }
358 // MuCh
359 else if constexpr (ca::EDetectorID::kMuch == DetID) {
360 pExtPoint->Position(posIn);
361 pExtPoint->PositionOut(posOut);
362 pExtPoint->Momentum(momIn);
363 pExtPoint->Momentum(momOut);
364 }
365 // TRD
366 else if constexpr (ca::EDetectorID::kTrd == DetID) {
367 pExtPoint->Position(posIn);
368 pExtPoint->PositionOut(posOut);
369 pExtPoint->Momentum(momIn);
370 pExtPoint->MomentumOut(momOut);
371 }
372 // TOF
373 else if constexpr (ca::EDetectorID::kTof == DetID) {
374 pExtPoint->Position(posIn);
375 pExtPoint->Position(posOut);
376 pExtPoint->Momentum(momIn);
377 pExtPoint->Momentum(momOut);
378 }
379 double time = pExtPoint->GetTime();
380 int iTmcExt = pExtPoint->GetTrackID();
381
382 if (iTmcExt < 0) {
383 LOG(warn) << "CbmCaMCModule: For MC point with iExtId = " << iExtId << ", iEvent = " << iEvent
384 << ", iFile = " << iFile << " MC track is undefined (has ID = " << iTmcExt << ')';
385 return std::nullopt;
386 }
387 TVector3 posMid = 0.5 * (posIn + posOut);
388 TVector3 momMid = 0.5 * (momIn + momOut);
389
390 // // ----- Get station index
391 int iStLoc = pRecoUnit->GetTrackingStationId(pExtPoint->GetDetectorID());
392 if (iStLoc < 0) {
393 return std::nullopt;
394 }
395
396 int stationID = fpParameters->GetStationIndexActive(iStLoc, DetID);
397
398 // Update point time with event time
399 time += fpMCEventList->GetEventTime(iEvent, iFile);
400
401 // ----- Reject MC points falling out of the time slice
402 // STS, MuCh, TRD, TOF
403 if constexpr (DetID != ca::EDetectorID::kMvd) {
404 // TODO: SZh 18.06.2024: Avoid dependency from CbmTimeSlice, if possible (no TimeSlice branch in the online unpack)
405 double startT = fpTimeSlice->GetStartTime();
406 double endT = fpTimeSlice->GetEndTime();
407
408 if ((startT > 0. && time < startT) || (endT > 0. && time > endT)) {
409 LOG(warn) << "CbmCaMCModule: MC point with iExtId = " << iExtId << ", iEvent = " << iEvent
410 << ", iFile = " << iFile << " and det id " << int(DetID) << " fell out of the TS duration [" << startT
411 << ", " << endT << "] with measured time = " << time << " [ns]";
412 return std::nullopt;
413 }
414 }
415
416 // ----- Fill MC point
417 oPoint->SetExternalId(fpMcData->GetPointGlobExtIndex(DetID, iExtId));
418 oPoint->SetEventId(iEvent);
419 oPoint->SetFileId(iFile);
420 oPoint->SetTime(time);
421 oPoint->SetX(posMid.X());
422 oPoint->SetY(posMid.Y());
423 oPoint->SetZ(posMid.Z());
424 oPoint->SetXIn(posIn.X());
425 oPoint->SetYIn(posIn.Y());
426 oPoint->SetZIn(posIn.Z());
427 oPoint->SetXOut(posOut.X());
428 oPoint->SetYOut(posOut.Y());
429 oPoint->SetZOut(posOut.Z());
430 oPoint->SetPx(momMid.X());
431 oPoint->SetPy(momMid.Y());
432 oPoint->SetPz(momMid.Z());
433 oPoint->SetPxIn(momIn.X());
434 oPoint->SetPyIn(momIn.Y());
435 oPoint->SetPzIn(momIn.Z());
436 oPoint->SetPxOut(momOut.X());
437 oPoint->SetPyOut(momOut.Y());
438 oPoint->SetPzOut(momOut.Z());
439
440 // Select current number of points as a local id of points
441 oPoint->SetId(fpMcData->GetNofPoints());
442
443 // Match MC track and point to each other
444 int iTmcInt = fpMcData->FindInternalTrackIndex(iTmcExt, iEvent, iFile);
445
446 oPoint->SetTrackId(iTmcInt);
447 if (iTmcInt > -1) {
448 fpMcData->GetTrack(iTmcInt).AddPointIndex(oPoint->GetId());
449 }
450
451 oPoint->SetActiveStationId(stationID);
452 oPoint->SetDetectorId(DetID);
453 oPoint->SetLocalStationId(iStLoc);
454
455 auto* pExtTrk = dynamic_cast<CbmMCTrack*>(fpMCTracks->Get(iFile, iEvent, iTmcExt));
456 if (!pExtTrk) {
457 LOG(warn) << "CbmCaMCModule: MC track with iTmcExt = " << iTmcExt << ", iEvent = " << iEvent
458 << ", iFile = " << iFile << " MC track is undefined (nullptr)";
459 return std::nullopt;
460 }
461 oPoint->SetPdgCode(pExtTrk->GetPdgCode());
462 oPoint->SetMotherId(pExtTrk->GetMotherId());
463
464 auto* pPdgDB = TDatabasePDG::Instance()->GetParticle(oPoint->GetPdgCode());
465 oPoint->SetMass(pPdgDB ? pPdgDB->Mass() : 0.);
466 oPoint->SetCharge(pPdgDB ? pPdgDB->Charge() / 3. : 0.);
467
468 return oPoint;
469 }
470
471 // -------------------------------------------------------------------------------------------------------------------
472 //
473 template<ca::EDetectorID DetID>
475 {
476 if (!fvbUseDet[DetID]) {
477 return;
478 }
479
480 for (int iH = (*fpvFstHitId)[static_cast<int>(DetID)]; iH < (*fpvFstHitId)[static_cast<int>(DetID) + 1]; ++iH) {
481 auto& hit = (*fpvQaHits)[iH];
482 auto [iBestP, vAllP] = MatchHitWithMc<DetID>(hit.ExtIndex);
483 if (iBestP >= 0) {
484 hit.SetBestMcPointId(iBestP);
485 }
486 for (auto iP : vAllP) {
487 if (iP >= 0) {
488 hit.AddMcPointId(iP);
489 fpMcData->GetPoint(iP).AddHitID(iH);
490 }
491 }
492 }
493 }
494 // -------------------------------------------------------------------------------------------------------------------
495 // NOTE: template is used, because another template function FillMCPoint is used inside
496 template<ca::EDetectorID DetID>
498 {
499 if (!fvbUseDet[DetID]) {
500 return;
501 }
502 for (const auto& [iFile, iEvent] : fFileEventIDs) {
503 int nPointsEvent = fvpBrPoints[DetID]->Size(iFile, iEvent);
504 for (int iP = 0; iP < nPointsEvent; ++iP) {
505 std::optional<ca::McPoint> oPoint = FillMCPoint<DetID>(iP, iEvent, iFile);
506 if (oPoint) {
507 fpMcData->AddPoint(*oPoint);
508 }
509 } // iP: end
510 } // key: end
511 }
512} // namespace cbm::ca
513
514#endif // CbmCaMCModule_h
Data structure for internal tracking MC-information (header)
Internal class describing a MC point for CA tracking QA and performance (header)
CA Tracking monitor class.
Implementation of L1DetectorID enum class for CBM.
A manager for setup representation in CBM reconstruction.
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.
Access to a MC data branch for time-based analysis.
Container class for MC events with number, file and start time.
void SetMotherId(int32_t id)
Definition CbmMCTrack.h:109
Bookkeeping of time-slice content.
const algo::RecoSetup & GetSetup() const
Setup accessor.
static RecoSetupManager * Instance()
Instance access.
const RecoSetupUnit_t< ModuleId > * Get() const
Access to a reconstruction setup unit.
Definition RecoSetup.h:112
This class represents a package for tracking-related data.
Definition CaMcData.h:33
Monitor class for the CA tracking.
Definition CaMonitor.h:35
A container for all external parameters of the CA tracking algorithm.
std::tuple< int, std::vector< int > > MatchHitWithMc(int iHitExt)
Matches hit with MC point.
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.
void RegisterQaHitContainer(ca::Vector< cbm::algo::ca::McHitInfo > &vQaHits)
Registers debug hit container.
int fBestMcFile
Index of bestly matched MC file.
void ProcessEvent(CbmEvent *pEvent)
Processes event.
ca::Vector< algo::ca::McHitInfo > * fpvQaHits
Pointer to QA hit container.
ca::Vector< CbmL1HitId > * fpvHitIds
Pointer to hit index container.
void InitMcTrackInfo()
Initializes MC track.
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.
int GetVerbosity() const
Gets verbosity level.
DetIdArr_t< bool > fvbUseDet
Flag: is detector subsystem used.
std::unordered_map< uint32_t, double > fmTofRpcZpos
A map of TOF RPC reference z-position vs. address.
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.
algo::ca::McData * fpMcData
MC information (hits and tracks) instance.
int fVerbose
Verbosity level.
void RegisterParameters(std::shared_ptr< const ca::Parameters< double > > &pParameters)
Registers CA parameters object.
~MCModule()=default
Destructor.
DetIdArr_t< CbmMCDataArray * > fvpBrPoints
Array of points vs. detector.
void MatchRecoAndMCTracks()
Matches reconstructed tracks with MC tracks.
CbmMCDataObject * fpMCEventHeader
MC event header.
void ReadMCPointsForDetector()
Reads MC points in particular detector.
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.
std::optional< algo::ca::McPoint > FillMCPoint(int iExtId, int iEvent, int iFile)
Fills a single detector-specific MC point.
void MatchPointsAndHits()
Match sets of MC points and reconstructed hits for a given detector.
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.
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.
void RegisterMcData(cbm::algo::ca::McData &mcData)
Registers MC data object.
CbmMCDataArray * fpMCTracks
MC tracks input.
std::shared_ptr< const ca::Parameters< double > > fpParameters
Pointer to tracking parameters object.
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.
const cbm::algo::ca::McData * GetMcData() const
Gets a pointer to MC data object.
void MatchTracks()
Match reconstructed and MC data.
int fPerformanceMode
Mode of performance.
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:216
constexpr ECbmModuleId ToCbmModuleId(EDetectorID detID)
Conversion map from EDetectorID to ECbmModuleId.
Definition CbmDefs.h:228
constexpr DetIdArr_t< const char * > kDetName
Names of detector subsystems.
cbm::core::EnumArray< ca::EDetectorID, T > DetIdArr_t
Alias to array, indexed by L1DetectorID enum.
std::tuple_element_t< static_cast< std::size_t >(DetID), std::tuple< Types... > > at