23#include "FairEventHeader.h"
24#include "FairMCEventHeader.h"
25#include "FairRootManager.h"
26#include "FairRunAna.h"
28#include "TDatabasePDG.h"
29#include "TLorentzVector.h"
32#include <boost/filesystem.hpp>
57 LOG(info) <<
"CA MC Module: initializing CA tracking Monte-Carlo module... ";
77 auto fairManager = FairRootManager::Instance();
80 auto mcManager =
dynamic_cast<CbmMCDataManager*
>(fairManager->GetObject(
"MCDataManager"));
99 auto InitMatchesBranch = [&](
const char* brName,
ca::EDetectorID detID) {
101 fvpBrHitMatches[detID] =
dynamic_cast<TClonesArray*
>(fairManager->GetObject(brName));
105 InitPointBranch(
"MvdPoint", ca::EDetectorID::kMvd);
106 InitPointBranch(
"StsPoint", ca::EDetectorID::kSts);
107 InitPointBranch(
"MuchPoint", ca::EDetectorID::kMuch);
108 InitPointBranch(
"TrdPoint", ca::EDetectorID::kTrd);
109 InitPointBranch(
"TofPoint", ca::EDetectorID::kTof);
111 InitMatchesBranch(
"MvdHitMatch", ca::EDetectorID::kMvd);
112 InitMatchesBranch(
"StsHitMatch", ca::EDetectorID::kSts);
113 InitMatchesBranch(
"MuchPixelHitMatch", ca::EDetectorID::kMuch);
114 InitMatchesBranch(
"TrdHitMatch", ca::EDetectorID::kTrd);
115 InitMatchesBranch(
"TofHitMatch", ca::EDetectorID::kTof);
135 LOG(info) <<
"CA MC Module: initializing CA tracking Monte-Carlo module... \033[1;32mDone!\033[0m";
139catch (
const std::logic_error& error) {
140 LOG(error) <<
"CA MC Module: initializing CA tracking Monte-Carlo module... \033[1;31mFailed\033[0m\nReason: "
158 for (
int iLink = 0; iLink < nLinks; ++iLink) {
159 const auto& link = pEvtMatch->
GetLink(iLink);
170 for (
int iE = 0; iE < nEvents; ++iE) {
186 aTrk.SortPointIndexes(
213 isRec &= aTrk.GetMaxNofPointsOnStation() <= 3;
227 default: LOG(fatal) <<
"CA MC Module: invalid performance mode " <<
fPerformanceMode;
230 if (aTrk.GetNofPoints() > 0) {
231 isAdd &= aTrk.GetNofConsStationsWithHit() == aTrk.GetTotNofStationsWithHit();
232 isAdd &= aTrk.GetNofConsStationsWithPoint() == aTrk.GetTotNofStationsWithHit();
233 isAdd &= aTrk.GetTotNofStationsWithHit() == aTrk.GetTotNofStationsWithPoint();
234 isAdd &= aTrk.GetNofConsStationsWithHit() >= 3;
241 aTrk.SetFlagReconstructable(isRec);
242 aTrk.SetFlagAdditional(isAdd);
274 if (trkMC.IsReconstructable()) {
285 for (
int iTre = 0; iTre < int(
fpvRecoTracks->size()); ++iTre) {
286 auto& trkRe = (*fpvRecoTracks)[iTre];
289 auto& mNofHitsVsMCTrkID = trkRe.hitMap;
290 mNofHitsVsMCTrkID.clear();
291 for (
int iH : trkRe.Hits) {
292 auto& vP = (*fpvQaHits)[iH].GetMcPointIds();
298 mNofHitsVsMCTrkID[iTmc]++;
307 int nHitsTrkRe = trkRe.Hits.size();
308 double maxPurity = 0.;
309 for (
auto& item : mNofHitsVsMCTrkID) {
310 int iTmc = item.first;
311 int nHitsTrkMc = item.second;
317 if (
double(nHitsTrkMc) >
double(nHitsTrkRe) * maxPurity) {
318 maxPurity = double(nHitsTrkMc) / double(nHitsTrkRe);
325 trkMc.AddRecoTrackIndex(iTre);
326 trkRe.AddMCTrackIndex(iTmc);
330 trkMc.AddTouchTrackIndex(iTre);
334 trkRe.SetMaxPurity(maxPurity);
349 throw std::logic_error(
"Tracking parameters object was not defined");
354 throw std::logic_error(
"MC data object was not registered");
357 throw std::logic_error(
"Reconstructed track container was not registered");
360 throw std::logic_error(
"Hit index container was not registered");
363 throw std::logic_error(
"QA hit container was not registered");
366 throw std::logic_error(
"Array of first hit indexes in each detector was not registered");
371 throw std::logic_error(
"MC event list was not found");
374 throw std::logic_error(
"Time slice was not found");
379 throw std::logic_error(
"MC tracks branch is unavailable");
384 throw std::logic_error(
"MC event header is unavailable");
388 for (
int iD = 0; iD < static_cast<int>(ca::EDetectorID::END); ++iD) {
391 throw std::logic_error(Form(
"MC points are unavailable for %s",
kDetName[iD]));
394 throw std::logic_error(Form(
"Hit matches are unavailable for %s",
kDetName[iD]));
403void MCModule::ReadMCPointsForDetector<ca::EDetectorID::kTof>()
405 if (!fvbUseDet[ca::EDetectorID::kTof]) {
409 auto* pBrHitMatches = fvpBrHitMatches[ca::EDetectorID::kTof];
410 auto* pBrPoints = fvpBrPoints[ca::EDetectorID::kTof];
413 constexpr int kNofBitsRpcAddress = 11;
415 for (
const auto& [iFile, iEvent] : fFileEventIDs) {
417 int nPoints = pBrPoints->Size(iFile, iEvent);
418 std::map<std::pair<int, int>,
int> mMatchedPointId;
419 for (
int iH = 0; iH < pBrHitMatches->GetEntriesFast(); ++iH) {
420 auto* pHitMatch =
dynamic_cast<CbmMatch*
>(pBrHitMatches->At(iH));
421 LOG_IF(fatal, !pHitMatch) <<
"CA MC Module: hit match was not found for TOF hit " << iH;
422 double bestWeight = 0;
423 for (
int iLink = 0; iLink < pHitMatch->GetNofLinks(); ++iLink) {
424 const auto& link = pHitMatch->GetLink(iLink);
425 if (link.GetFile() != iFile || link.GetEntry() != iEvent) {
428 int iPext = link.GetIndex();
432 auto* pExtPoint =
dynamic_cast<CbmTofPoint*
>(pBrPoints->Get(link));
434 LOG(error) <<
"ca::MCModule: MC Point with link=" << link.ToString() <<
" does not exist in branch";
438 int trkId = pExtPoint->GetTrackID();
439 int rpcAddr = pExtPoint->GetDetectorID() << kNofBitsRpcAddress;
440 auto key = std::make_pair(trkId, rpcAddr);
441 auto prev = mMatchedPointId.find(key);
442 if (prev == mMatchedPointId.end()) {
443 bestWeight = link.GetWeight();
444 mMatchedPointId[key] = iPext;
447 if (bestWeight < link.GetWeight()) {
448 mMatchedPointId[key] = iPext;
457 int iPointSelected = -1;
459 int rpcAddrCurr = -1;
460 bool bTrkHasHits =
false;
461 double zDist = std::numeric_limits<double>::max();
462 double zCell = std::numeric_limits<double>::signaling_NaN();
463 for (
int iP = 0; iP < nPoints; ++iP) {
464 auto* pExtPoint =
dynamic_cast<CbmTofPoint*
>(pBrPoints->Get(iFile, iEvent, iP));
465 LOG_IF(fatal, !pExtPoint) <<
"NO POINT: TOF: file, event, index = " << iFile <<
' ' << iEvent <<
' ' << iP;
467 int iTmc = pExtPoint->GetTrackID();
468 int rpcAddr = pExtPoint->GetDetectorID() << kNofBitsRpcAddress;
470 if (rpcAddrCurr != rpcAddr || iTmcCurr != iTmc) {
471 if (iPointSelected != -1) {
472 auto oPoint = FillMCPoint<ca::EDetectorID::kTof>(iPointSelected, iEvent, iFile);
474 fpMCData->AddPoint(*oPoint);
478 rpcAddrCurr = rpcAddr;
479 auto key = std::make_pair(iTmc, rpcAddr);
480 auto found = mMatchedPointId.find(key);
481 bTrkHasHits = found != mMatchedPointId.end();
483 iPointSelected = found->second;
487 zCell = fvpDetInterface[ca::EDetectorID::kTof]->GetZrefModule(pExtPoint->GetDetectorID());
488 zDist = std::fabs(zCell - pExtPoint->GetZ());
494 auto newDist = std::fabs(pExtPoint->GetZ() - zCell);
495 if (newDist < zDist) {
503 if (iPointSelected != -1) {
504 auto oPoint = FillMCPoint<ca::EDetectorID::kTof>(iPointSelected, iEvent, iFile);
506 fpMCData->AddPoint(*oPoint);
522 for (
int iD = 0; iD < static_cast<int>(vNofPointsDet.size()); ++iD) {
524 vNofPointsDet[iD] =
fvpBrPoints[iD]->Size(iFile, iEvent);
545 if (iFile < 0 || iEvent < 0) {
554 if (iFile < 0 || iEvent < 0) {
557 auto pEvtHeader =
dynamic_cast<FairMCEventHeader*
>(
fpMCEventHeader->
Get(iFile, iEvent));
559 LOG(fatal) <<
"cbm::ca::MCModule: event header is not found for file " << iFile <<
" and event " << iEvent;
565 for (
int iTrkExt = 0; iTrkExt < nTracks; ++iTrkExt) {
568 LOG(warn) <<
"cbm::ca::MCModule: track with (iF, iE, iT) = " << iFile <<
' ' << iEvent <<
' ' << iTrkExt
575 aTrk.SetExternalId(iTrkExt);
576 aTrk.SetEventId(iEvent);
577 aTrk.SetFileId(iFile);
579 aTrk.SetStartT(pExtMCTrk->
GetStartT() + eventTime);
584 aTrk.SetPx(pExtMCTrk->
GetPx());
585 aTrk.SetPy(pExtMCTrk->
GetPy());
586 aTrk.SetPz(pExtMCTrk->
GetPz());
588 aTrk.SetFlagSignal(aTrk.IsPrimary() && (aTrk.GetStartZ() > (pEvtHeader->GetZ() + 1.e-10)));
592 aTrk.SetMass(pExtMCTrk->
GetMass());
599 int extChainId = iTrkExt;
600 int extChainParent = extMotherId;
601 while (extChainParent >= 0) {
602 extChainId = extChainParent;
607 if (extMotherId < 0) {
610 aTrk.SetMotherId(extMotherId);
611 aTrk.SetChainId(extChainId);
617 if (motherId == -1) {
620 aTrk.SetMotherId(motherId);
621 aTrk.SetChainId(chainId);
void MCModule::ReadMCPointsForDetector< ca::EDetectorID::kTof >()
CA Tracking performance interface for CBM (header)
Implementation of L1DetectorID enum class for CBM.
Data class for a reconstructed hit in the STS.
Class characterising one event by a collection of links (indices) to data objects,...
CbmMatch * GetMatch() const
TObject * Get(const CbmLink *lnk)
Int_t Size(Int_t fileNumber, Int_t eventNumber)
Task class creating and managing CbmMCDataArray objects.
TObject * Get(const CbmLink *lnk)
Container class for MC events with number, file and start time.
int32_t GetFileIdByIndex(uint32_t index)
File number by index @value File number for event at given index in list.
int32_t GetEventIdByIndex(uint32_t index)
Event number by index @value Event number for event at given index in list.
std::size_t GetNofEvents() const
Number of events in the list @value Number of events.
double GetEventTime(uint32_t event, uint32_t file)
Event start time.
double GetCharge() const
Charge of the associated particle.
uint32_t GetGeantProcessId() const
int32_t GetMotherId() const
double GetMass() const
Mass of the associated particle.
int32_t GetPdgCode() const
const CbmLink & GetLink(int32_t i) const
int32_t GetNofLinks() const
const CbmLink & GetMatchedLink() const
static CbmMuchTrackingInterface * Instance()
Gets pointer to the instance of the CbmMuchTrackingInterface.
static CbmMvdTrackingInterface * Instance()
Gets pointer to the instance of the CbmMvdTrackingInterface.
static CbmStsTrackingInterface * Instance()
Gets pointer to the instance of the CbmStsTrackingInterface class.
Bookkeeping of time-slice content.
Geometric intersection of a MC track with a TOFb detector.
static CbmTofTrackingInterface * Instance()
Gets pointer to the instance of the CbmTofTrackingInterface.
static CbmTrdTrackingInterface * Instance()
Gets pointer to the instance of the CbmTrdTrackingInterface.
std::string ToString() const
Prints counters summary to string.
void IncrementCounter(ECounterKey key)
Increments key counter by 1.
void SetCounterName(ECounterKey key, std::string_view name)
Sets name of counter.
void SetRatioKeys(const std::vector< ECounterKey > &vKeys)
Sets keys of counters, which are used as denominators for ratios.
Class CbmCaPerformance is an interface to communicate between.
DetIdArr_t< CbmTrackingDetectorInterfaceBase * > fvpDetInterface
Tracking detector interface.
void ReadMCTracks()
Reads MC tracks from external trees and saves them to MCDataObject.
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.
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.
ca::Monitor< EMonitorKey > fMonitor
Monitor.
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.
int fVerbose
Verbosity level.
tools::MCData * fpMCData
MC information (hits and tracks) instance.
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.
@ 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.
ca::Vector< CbmL1Track > * fpvRecoTracks
Pointer to 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.
const CbmTimeSlice * fpTimeSlice
Current time slice.
void MatchTracks()
Match reconstructed and MC data.
int fPerformanceMode
Mode of performance.
void InitTrackInfo()
Initializes MC track.
std::shared_ptr< ca::Parameters< float > > fpParameters
Pointer to tracking parameters object.
const double MinRecoMom
Performance constants.
constexpr char RDb[]
bold red
EDetectorID
Enumeration for the tracking detector subsystems in CBM-CA.
constexpr DetIdArr_t< const char * > kDetName
Names of detector subsystems.