BmnRoot 26.10.0-210-gc52d34b0
Software framework of the BM@N experiment
Loading...
Searching...
No Matches
BmnHitDumper.cxx
Go to the documentation of this file.
1/*
2 BM@N alignment routine
3 BM@N experiment at NICA complex, JINR, 2025
4
5 Department: Math & Soft Group of HEP lab
6 Author: Claude 4.7 (Opus)
7 Supervisor: Igor Polev, polev@jinr.int
8
9 BmnHitDumper class implementation
10*/
11
12#include "BmnHitDumper.h"
13
14#include "BmnEventHeader.h"
15#include "CbmStsHit.h"
16#include "FairMCEventHeader.h"
17#include "FairRootManager.h"
18#include "TSystem.h"
19#include "fairlogger/Logger.h"
20
22 : fCsvFilePath(dstFileName)
23{
24 // expand $VAR and ~ if present
25 char* expanded = gSystem->ExpandPathName(fCsvFilePath.Data());
26 fCsvFilePath = expanded;
27 delete[] expanded;
28 // replace extension with .csv (or append if none)
29 Int_t dotIdx{fCsvFilePath.Last('.')};
30 Int_t sepIdx{fCsvFilePath.Last('/')};
31 if (dotIdx > sepIdx)
32 fCsvFilePath.Replace(dotIdx, fCsvFilePath.Length() - dotIdx, ".csv");
33 else
34 fCsvFilePath.Append(".csv");
35}
36
38{
39 if (fDetModel.IgnoresSome()) {
40 LOG(error) << " <!> BmnHitDumper::Init() : detector model is incomplete (ignores some hits)"
41 " - cannot be used to dump hits consistently. Task disabled.";
42 SetActive(kFALSE);
43 return kERROR;
44 }
45 FairRootManager* ioman = FairRootManager::Instance();
46 if (!ioman) {
47 LOG(error) << " <!> BmnHitDumper::Init() : FairRootManager not found";
48 SetActive(kFALSE);
49 return kERROR;
50 }
51 fpStsHitsArray = (TClonesArray*)ioman->GetObject("StsHit");
52 if (!fpStsHitsArray) {
53 LOG(error) << " <!> BmnHitDumper::Init() : Failed to get StsHit array";
54 SetActive(kFALSE);
55 return kERROR;
56 }
57 // The "DstEventHeader." branch cannot be queried via ioman->GetObject(): the
58 // output DstEventHeader (registered via FairRunAna::SetEventHeader) shadows
59 // the input one in FairRootManager's folder lookup, and its BMN-specific
60 // fEventId is never populated by FairFileSource/BmnFileSource (which only
61 // touch FairEventHeader base fields). Read the per-event ID from the input
62 // header instead: BmnEventHeader. for experimental data, MCEventHeader. for
63 // simulation. Both are auto-updated each event via FairFileSource::ReadEvent.
64 fpBmnHeader = (BmnEventHeader*)ioman->GetObject("BmnEventHeader.");
65 fpMcHeader = (FairMCEventHeader*)ioman->GetObject("MCEventHeader.");
66 if (!fpBmnHeader && !fpMcHeader) {
67 LOG(error) << " <!> BmnHitDumper::Init() : "
68 "no BmnEventHeader. or MCEventHeader. branch in input";
69 SetActive(kFALSE);
70 return kERROR;
71 }
72 if (!fDetModel.Init()) {
73 LOG(error) << " <!> BmnHitDumper::Init() : Failed to initialize detector model";
74 SetActive(kFALSE);
75 return kERROR;
76 }
77 fCsvFile.open(fCsvFilePath.Data(), std::ofstream::out);
78 if (!fCsvFile.is_open()) {
79 LOG(error) << " <!> BmnHitDumper::Init() : Failed to open output file " << fCsvFilePath.Data();
80 SetActive(kFALSE);
81 return kERROR;
82 }
83 fCsvFile << "eventID,detectorID,x,y,z,dx,dy\n";
84 LOG(info) << "BmnHitDumper: writing hits to " << fCsvFilePath.Data();
85 SetActive(kTRUE);
86 fTimer.Start();
87 return kSUCCESS;
88}
89
91{
92 UInt_t eventID{fpBmnHeader ? fpBmnHeader->GetEventId() : (UInt_t)fpMcHeader->GetEventID()};
93 Int_t n{fpStsHitsArray->GetEntriesFast()};
94 for (Int_t i = 0; i < n; i++) {
95 CbmStsHit* hit = static_cast<CbmStsHit*>(fpStsHitsArray->At(i));
96 fCsvFile << eventID << "," << fDetModel.IDfromHit(hit->GetDetectorID()) << "," << hit->GetX() << ","
97 << hit->GetY() << "," << hit->GetZ() << "," << hit->GetDx() << "," << hit->GetDy() << "\n";
98 }
99 LOG(info) << "BmnHitDumper: " << n << " hits written from event " << eventID;
100}
101
103{
104 if (fCsvFile.is_open())
105 fCsvFile.close();
106 fTimer.Stop();
107 Int_t secs{static_cast<Int_t>(fTimer.RealTime())};
108 LOG(info) << Form("BmnHitDumper: total time spent: %dh %02dm %02ds", secs / 3600, (secs % 3600) / 60, secs % 60);
109}
int i
Definition P4_F32vec4.h:22
int Int_t
unsigned int UInt_t
constexpr Bool_t kFALSE
constexpr Bool_t kTRUE
const char Option_t
char * Form(const char *fmt,...)
R__EXTERN TSystem * gSystem
FairMCEventHeader * fpMcHeader
virtual void Finish()
virtual InitStatus Init()
BmnEventHeader * fpBmnHeader
TString fCsvFilePath
TStopwatch fTimer
std::ofstream fCsvFile
BmnRun8DM fDetModel
TClonesArray * fpStsHitsArray
virtual void Exec(Option_t *opt)
BmnHitDumper()=delete