BmnRoot 26.10.0-210-gc52d34b0
Software framework of the BM@N experiment
Loading...
Searching...
No Matches
dump_object.C
Go to the documentation of this file.
1/*
2 BM@N alignment routine
3 BM@N experiment at NICA complex, JINR, 2026
4
5 Department: Math & Soft Group of HEP lab
6 Author: Claude (Opus 5)
7 Supervisor: Igor Polev, polev@jinr.int
8
9 Object dump macro: prints every data member of an object stored in a .root
10 file, as the class dictionary describes it
11*/
12
13// headers below are for IDE syntax to work and they will be
14// turned off during execution in ROOT macro mode
15#ifndef __CLING__
16#include "TBranch.h"
17#include "TClass.h"
18#include "TClonesArray.h"
19#include "TFile.h"
20#include "TKey.h"
21#include "TTree.h"
22
23#include <iostream>
24#endif
25
26/**
27 * @brief Print all data members of an object read from a .root file.
28 *
29 * The name is looked up as a key of the file first - run-level objects such as
30 * DstRunHeader - and then as a top-level class branch of the tree, read at the
31 * given entry - per-event objects such as DstEventHeader. or StsHit. A
32 * TClonesArray is dumped element by element.
33 *
34 * @param filePath .root file to read
35 * @param objectName key or top-level branch name
36 * @param entry tree entry to read the branch at, ignored for a key
37 * @param treeName tree holding the branches
38 *
39 * @return 0 on success, -1 if the object is not found or cannot be read
40 */
41int dump_object(const char* filePath = "/home/igor/DATA/bmn/26.02.0/dst/rc_noa_rlerr.run_Top_9555_ev0_p0.root",
42 const char* objectName = "DstRunHeader",
43 Long64_t entry = 0,
44 const char* treeName = "bmndata")
45{
46 TFile file{filePath, "READ"};
47 if (file.IsZombie())
48 return -1;
49
50 TClass* cl{nullptr};
51 void* obj{nullptr};
52 if (TKey * key{file.GetKey(objectName)}) {
53 cl = TClass::GetClass(key->GetClassName());
54 obj = key->ReadObjectAny(cl);
55 } else {
56 TTree* tree{file.Get<TTree>(treeName)};
57 TBranch* branch{tree ? tree->GetBranch(objectName) : nullptr};
58 // SetAddress(&obj) is right for a top-level class branch only: a leaf
59 // branch or a split sub-branch would write its data over the pointer
60 if (branch && branch->GetMother() == branch && entry >= 0 && entry < tree->GetEntries())
61 cl = TClass::GetClass(branch->GetClassName());
62 if (cl) {
63 branch->SetAddress(&obj); // a pointer to null makes the branch allocate the object
64 if (branch->GetEntry(entry) <= 0)
65 obj = nullptr;
66 }
67 }
68 if (!cl || !obj) {
69 std::cout << "dump_object: no readable object '" << objectName << "' in " << filePath
70 << " - neither a key nor a top-level class branch of '" << treeName << "' at entry " << entry << '\n';
71 return -1;
72 }
73
74 std::cout << objectName << " : " << cl->GetName() << '\n';
76 cl->Dump(obj, kTRUE);
77 return 0;
78 }
79 const TClonesArray* array{static_cast<TClonesArray*>(obj)};
80 for (Int_t i{0}; i < array->GetEntriesFast(); i++) {
81 const TObject* element{array->At(i)};
82 std::cout << "\n--- [" << i << "] " << element->ClassName() << '\n';
83 // TClass::Dump wants the start of the object, which is not the TObject
84 // part when TObject is not the first base - the cast TObject::Dump does too
85 element->IsA()->Dump(element->IsA()->DynamicCast(TObject::Class(), element, kFALSE), kTRUE);
86 }
87 return 0;
88}
int i
Definition P4_F32vec4.h:22
int Int_t
constexpr Bool_t kFALSE
long long Long64_t
constexpr Bool_t kTRUE
virtual const char * GetClassName() const
virtual Int_t GetEntry(Long64_t entry=0, Int_t getall=0)
virtual void SetAddress(void *add)
TBranch * GetMother() const
static TClass * GetClass(Bool_t load=kTRUE, Bool_t silent=kFALSE)
void Dump() const override
Bool_t InheritsFrom(const char *cl) const override
const void * DynamicCast(const TClass *base, const void *obj, Bool_t up=kTRUE)
static TClass * Class()
T * Get(const char *namecycle)
TKey * GetKey(const char *name, Short_t cycle=9999) const override
const char * GetName() const override
Int_t GetEntriesFast() const
TObject * At(Int_t idx) const override
virtual const char * ClassName() const
static TClass * Class()
R__ALWAYS_INLINE Bool_t IsZombie() const
virtual TClass * IsA() const
virtual TBranch * GetBranch(const char *name)
int dump_object(const char *filePath="/home/igor/DATA/bmn/26.02.0/dst/rc_noa_rlerr.run_Top_9555_ev0_p0.root", const char *objectName="DstRunHeader", Long64_t entry=0, const char *treeName="bmndata")
Print all data members of an object read from a .root file.
Definition dump_object.C:41