ndmspc  0.20250128.0
HnSparseBrowser.cxx
1 #include <sstream>
2 #include <TString.h>
3 #include <TApplication.h>
4 #include <TCanvas.h>
5 #include <TRootCanvas.h>
6 #include <TROOT.h>
7 #include <TH2.h>
8 #include <TH1D.h>
9 #include <TCanvas.h>
10 #include <TGrid.h>
11 #include <THnSparse.h>
12 #include "Utils.h"
13 #include "HnSparseBrowser.h"
14 
16 ClassImp(Ndmspc::HnSparseBrowser);
18 
19 namespace Ndmspc {
21 
22 {
26  fListOfHnSparses = new TList();
27 }
28 
30 {
34  SafeDelete(fListOfHnSparses);
35 }
36 
37 int HnSparseBrowser::Draw(std::string filename, std::string objects, std::string dirtoken)
38 {
42 
43  TApplication app("app", nullptr, nullptr);
44  TH1::AddDirectory(false);
45 
46  std::vector<std::string> objectList;
47  std::vector<std::string> labels;
48  std::stringstream ss(objects);
49  std::string label;
50  char delimiter = ',';
51 
52  if (filename.rfind("alien://", 0) == 0) {
53  TGrid::Connect("alien://");
54  }
55  while (std::getline(ss, label, delimiter)) {
56  objectList.push_back(label);
57  }
58 
59  // for (const auto & o : objects) {
60  // Printf("%s", o.c_str());
61  // }
62 
63  Printf("Opening file '%s' ...", filename.c_str());
64  TFile * inputFile = Ndmspc::Utils::OpenFile(filename.c_str());
65  if (!inputFile) {
66  Printf("Error: Cannot open filename %s!", filename.c_str());
67  return 2;
68  }
69 
70  for (int i = 0; i < objectList.size(); i++) {
71  std::string objTemp = objectList[i].c_str();
72  Printf("Object : %s", objTemp.c_str());
73  size_t pos = objTemp.find(dirtoken);
74  labels.push_back(objTemp.substr(pos + dirtoken.length()));
75  }
76 
77  fInputHnSparse = (THnSparse *)inputFile->Get(objectList[0].c_str());
78  if (!fInputHnSparse) {
79  Printf("Error: Cannot find objects %s!", objectList[0].c_str());
80  return 3;
81  }
82 
83  TObjArray * listOfAxes = fInputHnSparse->GetListOfAxes();
84  int nDim = listOfAxes->GetEntries();
85 
86  TH2 * hAxes = new TH2D("hAxes", "Sparse Axes", nDim, 0., nDim, objectList.size(), 0., objectList.size());
87  hAxes->SetMinimum(0);
88  hAxes->SetMaximum(1);
89 
90  for (int i = 0; i < nDim; i++) {
91  TAxis * a = (TAxis *)listOfAxes->At(i);
92  hAxes->GetXaxis()->SetBinLabel(i + 1, a->GetName());
93  for (int j = 0; j < objects.size(); j++) {
94  hAxes->SetBinContent(i + 1, j + 1, 1);
95  }
96  }
97 
98  for (int i = 0; i < objects.size(); i++) {
99  if (dirtoken.empty())
100  hAxes->GetYaxis()->SetBinLabel(i + 1, objectList[i].c_str());
101  else
102  hAxes->GetYaxis()->SetBinLabel(i + 1, labels[i].c_str());
103  }
104 
105  hAxes->GetYaxis()->SetLabelSize(0.08);
106  hAxes->GetXaxis()->SetLabelSize(0.08);
107  hAxes->SetStats(0);
108  THnSparse * sparseTmp = nullptr;
109  for (int i = 0; i < objects.size(); i++) {
110  sparseTmp = (THnSparse *)inputFile->Get(objectList[i].c_str());
111  if (!sparseTmp) {
112  Printf("Error: Cannot find objects %s!", objectList[i].c_str());
113  continue;
114  }
115  fListOfHnSparses->Add(sparseTmp);
116  }
117 
118  TCanvas * CanvasMap = new TCanvas("CanvasAxes", "CanvasAxes", 0, 0, 1000, 300);
119  // CanvasMap->HighlightConnect("HighlightMain(TVirtualPad*,TObject*,Int_t,Int_t)");
120  CanvasMap->Connect("Highlighted(TVirtualPad*,TObject*,Int_t,Int_t)", "Ndmspc::HnSparseBrowser", this,
121  "HighlightMain(TVirtualPad*,TObject*,Int_t,Int_t)");
122  hAxes->SetHighlight(true);
123  hAxes->Draw("colz");
124 
125  inputFile->Close();
126  delete inputFile;
127 
128  app.Run();
129  return 0;
130 }
131 
132 void HnSparseBrowser::HighlightMain(TVirtualPad * pad, TObject * obj, Int_t xBin, Int_t yBin)
133 {
134  // Printf("Obj = %p %d %d", (void *)obj, xBin, yBin);
135  auto hAxes = dynamic_cast<TH1D *>(obj);
136 
137  auto CanvasProj = (TCanvas *)gROOT->GetListOfCanvases()->FindObject("CanvasProj");
138  if (hAxes && !hAxes->IsHighlight()) { // after highlight disabled
139  if (CanvasProj) delete CanvasProj;
140  hAxes->SetTitle("Disable highlight");
141  return;
142  }
143 
144  if (!CanvasProj) {
145  CanvasProj = new TCanvas("CanvasProj", "CanvasProj", 1005, 0, 600, 600);
146  }
147  fInputHnSparse = dynamic_cast<THnSparse *>(fListOfHnSparses->At(yBin - 1));
148  if (!fInputHnSparse) {
149  Printf("Error: Cannot find THnSparse at index %d", yBin - 1);
150  return;
151  }
152  TH1 * proj = fInputHnSparse->Projection(xBin - 1);
153  if (!proj) {
154  Printf("Error: Cannot project THnSparse at bin %d", xBin - 1);
155  return;
156  }
157 
158  CanvasProj->cd();
159  proj->Draw();
160  CanvasProj->Modified();
161  CanvasProj->Update();
162 }
163 
164 } // namespace Ndmspc
HnSparseBrowser object.
int Draw(std::string filename="root://eos.ndmspc.io//eos/ndmspc/scratch/alice/hyperloop/PWGLF-376/222580/AnalysisResults.root", std::string objects="phianalysis-t-hn-sparse_default/unlikepm,phianalysis-t-hn-sparse_default/likepp", std::string dirtoken="/")
static TFile * OpenFile(std::string filename, std::string mode="READ", bool createLocalDir=true)
Definition: Utils.cxx:18