BmnRoot
26.10.0-210-gc52d34b0
Software framework of the BM@N experiment
Toggle main menu visibility
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
*/
41
int
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'
;
75
if
(!cl->
InheritsFrom
(
TClonesArray::Class
())) {
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
}
i
int i
Definition
P4_F32vec4.h:22
Int_t
int Int_t
kFALSE
constexpr Bool_t kFALSE
Long64_t
long long Long64_t
kTRUE
constexpr Bool_t kTRUE
TBranch.h
TClass.h
TClonesArray.h
TFile.h
TKey.h
TTree.h
TBranch
TBranch::GetClassName
virtual const char * GetClassName() const
TBranch::GetEntry
virtual Int_t GetEntry(Long64_t entry=0, Int_t getall=0)
TBranch::SetAddress
virtual void SetAddress(void *add)
TBranch::GetMother
TBranch * GetMother() const
TClass
TClass::GetClass
static TClass * GetClass(Bool_t load=kTRUE, Bool_t silent=kFALSE)
TClass::Dump
void Dump() const override
TClass::InheritsFrom
Bool_t InheritsFrom(const char *cl) const override
TClass::DynamicCast
const void * DynamicCast(const TClass *base, const void *obj, Bool_t up=kTRUE)
TClonesArray
TClonesArray::Class
static TClass * Class()
TDirectoryFile::Get
T * Get(const char *namecycle)
TDirectoryFile::GetKey
TKey * GetKey(const char *name, Short_t cycle=9999) const override
TFile
TKey
TNamed::GetName
const char * GetName() const override
TObjArray::GetEntriesFast
Int_t GetEntriesFast() const
TObjArray::At
TObject * At(Int_t idx) const override
TObject
TObject::ClassName
virtual const char * ClassName() const
TObject::Class
static TClass * Class()
TObject::IsZombie
R__ALWAYS_INLINE Bool_t IsZombie() const
TObject::IsA
virtual TClass * IsA() const
TTree
TTree::GetBranch
virtual TBranch * GetBranch(const char *name)
dump_object
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
align_new
macro
dump_object.C
Generated on
for BmnRoot by
1.17.0