BmnRoot
Loading...
Searching...
No Matches
CentralityClusterizer.cxx
Go to the documentation of this file.
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
11CentralityClusterizer::CentralityClusterizer(const std::string& rootFilePath)
12 : fRootFilePath(rootFilePath)
13 , fNumClusters(0)
14{}
15
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
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
71void 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
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
181void 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
191{
192 printf("Work time of CentralityClusterizer: %4.2f sec.\n", fworkTime);
193}
void SetCentrality(int cluster_id, float probability, BmnCentralityClass::Method method)
Class for Bmn FHCal data container in event.
Class for Bmn Hodo data container in event.
CentralityClusterizer(const std::string &rootFilePath)
virtual void OnlineWrite(const std::unique_ptr< TTree > &resultTree)
Write task resul to tree.
virtual void Exec(Option_t *opt)