BmnRoot 26.10.0-18-g4e3414f9
Software framework of the BM@N experiment
Loading...
Searching...
No Matches
read_digi_example.C
Go to the documentation of this file.
1// read_digi_example.C: loop over the events of a DIGI file and print the event header, the GEM digits per station,
2// the FHCal signal and, for simulated files, the MC particle of the first GEM digit
3// works with simulated and experimental DIGI files: the branch names are chosen by the type of the file
4void read_digi_example(TString digiFileName = "bmnsim.root", Int_t nPrint = 3)
5{
6 TFile* file = TFile::Open(digiFileName);
7 TTree* tree = (TTree*)file->Get("bmndata");
8 Bool_t isSimulation = (tree->GetBranch("MCTrack") != nullptr);
9
10 // run information is stored only in experimental files
11 DigiRunHeader* runHeader = (DigiRunHeader*)file->Get("DigiRunHeader");
12 if (runHeader)
13 cout << "Period " << runHeader->GetPeriodId() << ", run " << runHeader->GetRunId() << endl;
14
15 // the same detector data are stored in branches with different names in simulated and experimental files
16 TString gemName = isSimulation ? "BmnGemStripDigit" : "GEM";
17 TString fhcalName = isSimulation ? "FHCalDigit" : "FHCalDigi";
18 vector<TString> branches = {gemName, fhcalName};
19 if (isSimulation)
20 branches.insert(branches.end(), {"MCEventHeader.", "BmnGemStripDigitMatch", "StsPoint", "MCTrack"});
21 else
22 branches.push_back("BmnEventHeader.");
23 tree->SetBranchStatus("*", 0); // read only the needed branches
24 for (auto& name : branches)
25 tree->SetBranchStatus(name + "*", 1);
26
27 TClonesArray *gemDigits = nullptr, *fhcalDigits = nullptr;
28 tree->SetBranchAddress(gemName, &gemDigits);
29 tree->SetBranchAddress(fhcalName, &fhcalDigits);
30 BmnEventHeader* eventHeader = nullptr; // experimental files
31 FairMCEventHeader* mcHeader = nullptr; // simulated files
32 TClonesArray *gemMatches = nullptr, *gemPoints = nullptr, *mcTracks = nullptr;
33 if (isSimulation) {
34 tree->SetBranchAddress("MCEventHeader.", &mcHeader);
35 tree->SetBranchAddress("BmnGemStripDigitMatch", &gemMatches);
36 tree->SetBranchAddress("StsPoint", &gemPoints); // MC points of the GEM detectors
37 tree->SetBranchAddress("MCTrack", &mcTracks);
38 } else
39 tree->SetBranchAddress("BmnEventHeader.", &eventHeader);
40
41 for (Long64_t iEvent = 0; iEvent < TMath::Min(tree->GetEntries(), (Long64_t)nPrint); iEvent++) {
42 tree->GetEntry(iEvent);
43 if (isSimulation)
44 printf("Event %u (b = %.1f fm):", mcHeader->GetEventID(), mcHeader->GetB());
45 else
46 printf("Event %u (%s):", eventHeader->GetEventId(), eventHeader->GetEventTimeTS().AsString("s"));
47
48 // GEM: number of strip digits per station
49 map<Int_t, Int_t> nGemDigits;
50 for (Int_t i = 0; i < gemDigits->GetEntriesFast(); i++)
51 nGemDigits[((BmnStripDigit*)gemDigits->UncheckedAt(i))->GetStation()]++;
52 printf(" GEM digits per station:");
53 for (auto& [station, n] : nGemDigits)
54 printf(" %d", n);
55
56 // FHCal: the digit classes of both file types derive from BmnFHCalDigit
57 Double_t fhcalSignal = 0;
58 for (Int_t i = 0; i < fhcalDigits->GetEntriesFast(); i++)
59 fhcalSignal += ((BmnFHCalDigit*)fhcalDigits->UncheckedAt(i))->GetSignal();
60 printf("; FHCal: %d digits, total signal %.1f", fhcalDigits->GetEntriesFast(), fhcalSignal);
61
62 // simulated files: MC particle with the largest contribution to the first GEM digit
63 if (isSimulation && gemMatches->GetEntriesFast() > 0) {
64 BmnMatch* match = (BmnMatch*)gemMatches->UncheckedAt(0);
65 if (match->GetNofLinks() > 0) {
66 FairMCPoint* point = (FairMCPoint*)gemPoints->UncheckedAt(match->GetMatchedLink().GetIndex());
67 CbmMCTrack* track = (CbmMCTrack*)mcTracks->UncheckedAt(point->GetTrackID());
68 printf("; first GEM digit: PDG %d", track->GetPdgCode());
69 }
70 }
71 printf("\n");
72 }
73}
int i
Definition P4_F32vec4.h:22
bool Bool_t
int Int_t
double Double_t
long long Long64_t
UInt_t GetEventId()
Get the event ID for this event.
TTimeStamp GetEventTimeTS()
Get the time for this event.
Class BmnStripDigit Base class for strip detector digits.
CbmMCTrack.h.
Definition CbmMCTrack.h:26
Int_t GetPdgCode() const
Accessors.
Definition CbmMCTrack.h:56
uint64_t GetPeriodId()
Get the period ID for this run.
uint64_t GetRunId()
Get the run ID.
T * Get(const char *namecycle)
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Int_t GetEntriesFast() const
TObject * UncheckedAt(Int_t i) const
const char * AsString(const Option_t *option="") const
virtual void SetBranchStatus(const char *bname, bool status=true, UInt_t *found=nullptr)
virtual TBranch * GetBranch(const char *name)
virtual Int_t GetEntry(Long64_t entry, Int_t getall=0)
virtual Long64_t GetEntries() const
Int_t SetBranchAddress(const char *bname, T **add, TBranch **ptr=nullptr)
void read_digi_example(TString digiFileName="bmnsim.root", Int_t nPrint=3)
*brief user defined string literal for JSON values *sa std::size_t n
Definition json.hpp:20362
Double_t Min(Double_t a, Double_t b)