BmnRoot
26.10.0-18-g4e3414f9
Software framework of the BM@N experiment
Toggle main menu visibility
Loading...
Searching...
No Matches
CentralityClusterizer.cxx
Go to the documentation of this file.
1
#include "
CentralityClusterizer.h
"
2
3
#include <BmnFHCalEvent.h>
4
#include <BmnHodoEvent.h>
5
#include <
TBranch.h
>
6
#include <
TClass.h
>
7
#include <
TKey.h
>
8
#include <
TROOT.h
>
9
#include <
TVector2.h
>
10
11
CentralityClusterizer::CentralityClusterizer
(
const
std::string& rootFilePath)
12
:
fRootFilePath
(rootFilePath)
13
,
fNumClusters
(0)
14
{}
15
16
CentralityClusterizer::~CentralityClusterizer
()
17
{
18
for
(
auto
& entry :
fPDF
) {
19
delete
entry.second;
// Delete the TH1F object
20
}
21
fPDF
.clear();
// Clear the map after deleting all entries
22
if
(
fPdfFile
)
23
fPdfFile
->Close();
24
if
(
fBmnEventCentrality
)
25
delete
fBmnEventCentrality
;
26
}
27
28
InitStatus
CentralityClusterizer::Init
()
29
{
30
fworkTime
= 0.;
31
fpFairRootMgr
= FairRootManager::Instance();
32
LoadHistograms
();
33
34
for
(
auto
& pdf :
fPDF
) {
35
auto
key = pdf.first;
36
key = key;
37
38
// TODO
39
// It would be much better to do something abstract and take a float like
40
// fpFairRootMgr->SetBranchAddress(feature.c_str(), fFeature[feature]);
41
// But for now I do not know how to do that
42
43
{
44
std::string feature =
"FHCalEvent"
;
45
BmnFHCalEvent
* branch = (
BmnFHCalEvent
*)
fpFairRootMgr
->GetObject(feature.c_str());
46
if
(!branch) {
47
LOG(error) <<
"CentralityClusterizer::Init(): Failed to initialize Branch "
<< feature
48
<<
" ! Task will be deactivated."
;
49
SetActive(
kFALSE
);
50
return
kERROR;
51
}
52
}
53
{
54
std::string feature =
"HodoEvent"
;
55
BmnHodoEvent
* branch = (
BmnHodoEvent
*)
fpFairRootMgr
->GetObject(feature.c_str());
56
if
(!branch) {
57
LOG(error) <<
"CentralityClusterizer::Init(): Failed to initialize Branch "
<< feature
58
<<
" ! Task will be deactivated."
;
59
SetActive(
kFALSE
);
60
return
kERROR;
61
}
62
}
63
}
64
65
fBmnEventCentrality
=
new
BmnEventCentrality
();
66
fpFairRootMgr
->RegisterAny(
"EventCentrality"
,
fBmnEventCentrality
,
kTRUE
);
67
LOG(debug) <<
"CentralityClusterizer Reconstructor ready"
;
68
return
kSUCCESS;
69
}
70
71
void
CentralityClusterizer::LoadHistograms
()
72
{
73
74
std::string dir = getenv(
"VMCWORKDIR"
);
75
std::string path = dir +
"/parameters/centrality/"
;
76
fNumClusters
= 0;
77
fPdfFile
=
new
TFile
((path +
fRootFilePath
).c_str(),
"READONLY"
);
78
TIter
nextkey(
fPdfFile
->GetListOfKeys());
79
TKey
* key;
80
while
((key = (
TKey
*)nextkey())) {
81
82
TClass
* cl =
gROOT
->GetClass(key->GetClassName());
83
if
(!cl->
InheritsFrom
(
"TH1"
))
84
continue
;
85
86
std::string keyName(key->GetName());
87
std::smatch matches;
88
if
(std::regex_search(keyName, matches,
fPattern
) && matches.size() == 2) {
89
int
cluster_id = std::stoi(matches[1]);
90
91
// Retrieve the histogram from the file and add it to the map
92
TH2F
* hist = (
TH2F
*)
fPdfFile
->Get(keyName.c_str());
93
if
(hist) {
94
fPDF
[cluster_id] = hist;
95
LOG(debug) <<
Form
(
"CentralityClusterizer::LoadHistograms(): %s cluster %d entries %.0f"
,
96
keyName.c_str(), cluster_id,
fPDF
[cluster_id]->GetEntries());
97
98
// Update the number of clusters if necessary
99
if
(cluster_id + 1 >
fNumClusters
)
100
fNumClusters
= cluster_id + 1;
101
}
else
{
102
LOG(error) <<
"CentralityClusterizer::LoadHistograms(): Failed to retrieve histogram "
<< keyName;
103
}
104
105
}
else
{
106
// Log an error if the key name does not match the pattern
107
LOG(error) <<
"CentralityClusterizer::LoadHistograms(): "
<< keyName <<
" No match found."
;
108
}
109
}
110
}
111
112
void
CentralityClusterizer::Exec
(
Option_t
* opt)
113
{
114
if
(!IsActive())
115
return
;
116
LOG(debug2) <<
"Exec of CentralityClusterizer"
;
117
118
TStopwatch
sw;
119
sw.
Start
();
120
fBmnEventCentrality
->Reset();
121
122
float
x = ((
BmnFHCalEvent
*)
fpFairRootMgr
->GetObject(
"FHCalEvent"
))->GetTotalEnergy();
123
float
y = ((
BmnHodoEvent
*)
fpFairRootMgr
->GetObject(
"HodoEvent"
))->GetTotalSignal();
124
125
std::vector<float> prob(
fNumClusters
);
126
for
(
auto
& pdf :
fPDF
) {
127
auto
cluster_id = pdf.first;
128
TH2F
* hist = pdf.second;
129
if
(!hist) {
130
LOG(error) <<
"CentralityClusterizer::Exec(): Failed to get histogram "
<< cluster_id;
131
continue
;
132
}
133
134
// Handle the case where x or y are outside the histogram's range
135
if
(x < hist->GetXaxis()->GetXmin() || x > hist->
GetXaxis
()->
GetXmax
() || y < hist->GetYaxis()->GetXmin()
136
|| y > hist->
GetYaxis
()->
GetXmax
())
137
{
138
continue
;
139
}
140
141
float
probability = hist->
Interpolate
(x, y);
142
LOG(debug2) <<
Form
(
"CentralityClusterizer::Exec(): value fhcal %.3f value_hodo %.3f cluster %d prob %.3f"
, x,
143
y, cluster_id, probability);
144
prob.at(cluster_id) = probability;
145
}
146
147
for
(
int
cluster_id = 0; cluster_id <
fNumClusters
; ++cluster_id) {
148
fBmnEventCentrality
->SetCentrality(cluster_id, prob.at(cluster_id),
BmnCentralityClass::Method::FHCalHodo
);
149
}
150
LOG(debug2) <<
Form
(
"CentralityClusterizer::Exec(): class %d prob %.3f"
,
fBmnEventCentrality
->GetClass(),
151
fBmnEventCentrality
->GetProbability());
152
153
// handle case of empty bin. Set class by closest centroid
154
if
(
fBmnEventCentrality
->GetProbability() < std::numeric_limits<float>::epsilon()) {
155
fBmnEventCentrality
->Reset();
156
TVector2
this_event(x, y);
157
float
min_distance = std::numeric_limits<float>::max();
158
int
candida = -1;
159
for
(
auto
& pdf :
fPDF
) {
160
auto
cluster_id = pdf.first;
161
TH2F
* hist = pdf.second;
162
if
(!hist) {
163
LOG(error) <<
"CentralityClusterizer::Exec(): Failed to get histogram "
<< cluster_id;
164
continue
;
165
}
166
167
TVector2
centroid(hist->
GetMean
(1), hist->
GetMean
(2));
168
float
distance = (this_event - centroid).Mod();
169
if
(distance < min_distance) {
170
min_distance = distance;
171
candida = cluster_id;
172
}
173
}
174
fBmnEventCentrality
->SetCentrality(candida, 0.0,
BmnCentralityClass::Method::FHCalHodo
);
175
}
176
177
sw.
Stop
();
178
fworkTime
+= sw.
RealTime
();
179
}
180
181
void
CentralityClusterizer::OnlineWrite
(
const
std::unique_ptr<TTree>& resultTree)
182
{
183
if
(!IsActive())
184
return
;
185
186
resultTree->Branch(
"EventCentrality"
, &
fBmnEventCentrality
);
187
resultTree->Fill();
188
}
189
190
void
CentralityClusterizer::Finish
()
191
{
192
printf(
"Work time of CentralityClusterizer: %4.2f sec.\n"
,
fworkTime
);
193
}
kFALSE
constexpr Bool_t kFALSE
kTRUE
constexpr Bool_t kTRUE
Option_t
const char Option_t
TBranch.h
TClass.h
TKey.h
TROOT.h
gROOT
#define gROOT
Form
char * Form(const char *fmt,...)
TVector2.h
BmnCentralityClass::Method::FHCalHodo
@ FHCalHodo
Definition
BmnCentralityClass.h:18
BmnEventCentrality
Definition
BmnEventCentrality.h:10
BmnFHCalEvent
Class for Bmn FHCal data container in event.
Definition
BmnFHCalEvent.h:21
BmnHodoEvent
Definition
BmnHodoEvent.h:10
CentralityClusterizer::fPattern
static const std::regex fPattern
Definition
CentralityClusterizer.h:48
CentralityClusterizer::CentralityClusterizer
CentralityClusterizer(const std::string &rootFilePath)
Definition
CentralityClusterizer.cxx:11
CentralityClusterizer::fBmnEventCentrality
BmnEventCentrality * fBmnEventCentrality
Definition
CentralityClusterizer.h:49
CentralityClusterizer::fpFairRootMgr
FairRootManager * fpFairRootMgr
Definition
CentralityClusterizer.h:42
CentralityClusterizer::fPDF
std::map< int, TH2F * > fPDF
Definition
CentralityClusterizer.h:47
CentralityClusterizer::~CentralityClusterizer
virtual ~CentralityClusterizer()
Definition
CentralityClusterizer.cxx:16
CentralityClusterizer::OnlineWrite
virtual void OnlineWrite(const std::unique_ptr< TTree > &resultTree)
Write task resul to tree.
Definition
CentralityClusterizer.cxx:181
CentralityClusterizer::fPdfFile
TFile * fPdfFile
Definition
CentralityClusterizer.h:43
CentralityClusterizer::fworkTime
float fworkTime
Definition
CentralityClusterizer.h:52
CentralityClusterizer::Exec
virtual void Exec(Option_t *opt)
Definition
CentralityClusterizer.cxx:112
CentralityClusterizer::Init
virtual InitStatus Init()
Definition
CentralityClusterizer.cxx:28
CentralityClusterizer::Finish
virtual void Finish()
Definition
CentralityClusterizer.cxx:190
CentralityClusterizer::LoadHistograms
void LoadHistograms()
Definition
CentralityClusterizer.cxx:71
CentralityClusterizer::fRootFilePath
std::string fRootFilePath
Definition
CentralityClusterizer.h:44
CentralityClusterizer::fNumClusters
int fNumClusters
Definition
CentralityClusterizer.h:46
TAxis::GetXmax
Double_t GetXmax() const
TClass
TClass::InheritsFrom
Bool_t InheritsFrom(const char *cl) const override
TFile
TH1::GetMean
virtual Double_t GetMean(Int_t axis=1) const
TH1::GetXaxis
TAxis * GetXaxis()
TH1::GetYaxis
TAxis * GetYaxis()
TH2F
TH2::Interpolate
Double_t Interpolate(Double_t x, Double_t y) const override
TIter
TKey
TStopwatch
TStopwatch::RealTime
Double_t RealTime()
TStopwatch::Start
void Start(Bool_t reset=kTRUE)
TStopwatch::Stop
void Stop()
TVector2
CentralityClusterizer.h
reconstruction
centrality
CentralityClusterizer.cxx
Generated on
for BmnRoot by
1.17.0