]> git.uio.no Git - u/mrichter/AliRoot.git/blame - PWG/Tools/THistManager.cxx
Merge branch 'master' of https://git.cern.ch/reps/AliRoot
[u/mrichter/AliRoot.git] / PWG / Tools / THistManager.cxx
CommitLineData
c877a771 1/**************************************************************************
2 * Copyright(c) 1998-2007, ALICE Experiment at CERN, All rights reserved. *
3 * *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
6 * *
7 * Permission to use, copy, modify and distribute this software and its *
8 * documentation strictly for non-commercial purposes is hereby granted *
9 * without fee, provided that the above copyright notice appears in all *
10 * copies and that both the copyright notice and this permission notice *
11 * appear in the supporting documentation. The authors make no claims *
12 * about the suitability of this software for any purpose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
15
16/*
17 * Container class for histogram objects. Currenly can handle
18 * TH1
19 * TH2
20 * TH3
21 * THnSparse
22 * Histograms can be stored in groups. For this the parent group is
23 * included inside the histogram name, i.e. /base/inheriting/histogram.
24 * In case just the histogram name is given, it is assumed that the
25 * histogram is stored at the top level.
26 *
27 * Author: Markus Fasel
28 */
29
30#include <cstring>
1f65e0e2 31#include <string>
c877a771 32#include <vector>
33#include <TArrayD.h>
34#include <TAxis.h>
35#include <TH1.h>
36#include <TH2.h>
37#include <TH3.h>
38#include <THnSparse.h>
39#include <THashList.h>
40#include <TObjArray.h>
41#include <TObjString.h>
42#include <TString.h>
43
c877a771 44#include "THistManager.h"
45
46ClassImp(THistManager)
47
48//______________________________________________________________________________
49THistManager::THistManager():
50 TNamed(),
51 fHistos(NULL),
52 fIsOwner(true)
53{
54 /*
55 * Default constructor, only initialising pointers with 0
56 */
57}
58
59//______________________________________________________________________________
60THistManager::THistManager(const char *name):
61 TNamed(name, Form("Histogram container %s", name)),
62 fHistos(NULL),
63 fIsOwner(true)
64{
65 /*
66 * Main constructor, creating also a list for the histograms
67 *
68 * @param name: Name of the object (list named accordingly)
69 */
70 fHistos = new THashList();
71 fHistos->SetName(Form("histos%s", name));
72 fHistos->SetOwner();
73}
74
75//______________________________________________________________________________
76THistManager::~THistManager(){
77 /*
78 * Destructor, deletes the list of histograms if it is the owner
79 */
80 if(fHistos && fIsOwner) delete fHistos;
81}
82
83//______________________________________________________________________________
1f65e0e2 84void THistManager::CreateHistoGroup(const char *groupname, const char *parent) {
c877a771 85 /*
86 * Create a new group of histograms within a parent group. Groups are represented as list. The default parent is
87 * always the top list. List name structure accouding to unix paths (i.e. top list /, hirarchies separated by /).
88 *
89 * @param groupname: Name of the new group
90 * @param parent (@default "/"): Name of the parent group
91 * @throw HistManagerException
92 */
93 THashList *parentgroup = FindGroup(parent);
1f65e0e2 94 if(!parentgroup){
95 Fatal("THistManager::CreateHistoGroup", "Parent group %s does not exist", parentgroup->GetName());
495de23b 96 return;
1f65e0e2 97 }
c877a771 98 THashList *childgroup = new THashList();
99 childgroup->SetName(groupname);
100 parentgroup->Add(childgroup);
101}
102
103//______________________________________________________________________________
1f65e0e2 104void THistManager::CreateTH1(const char *name, const char *title, int nbins, double xmin, double xmax){
c877a771 105 /*
106 * Create a new TH1 within the container. The histogram name also contains the parent group(s) according to the common
107 * group notation.
108 *
109 * @param name: Name of the histogram
110 * @param title: Title of the histogram
111 * @param nbins: number of bins
112 * @param xmin: min. value of the range
113 * @param xmax: max. value of the range
1f65e0e2 114 * Raises fatals in case the parent group does not exist or the object is attempted to be duplicated within the group
c877a771 115 */
116 TString dirname(basename(name)), hname(histname(name));
117 THashList *parent(FindGroup(dirname.Data()));
495de23b 118 if(!parent){
1f65e0e2 119 Fatal("THistManager::CreateTH1", "Parent %s does not exist", dirname.Data());
495de23b 120 return;
121 }
122 if(parent->FindObject(hname.Data())){
1f65e0e2 123 Fatal("THistManager::CreateTH1", "Object %s already exists in group %s does not exist", hname.Data(), dirname.Data());
495de23b 124 return;
125 }
c877a771 126 parent->Add(new TH1D(hname.Data(), title, nbins, xmin, xmax));
127}
128
129//______________________________________________________________________________
1f65e0e2 130void THistManager::CreateTH1(const char *name, const char *title, int nbins, const double *xbins){
c877a771 131 /*
132 * Create a new TH1 within the container. The histogram name also contains the parent group(s) according to the common
133 * group notation.
134 *
135 * @param name: Name of the histogram
136 * @param title: Title of the histogram
137 * @param nbins: number of bins
138 * @param xbins: array of bin limits
1f65e0e2 139 * Raises fatals in case the parent group does not exist or the object is attempted to be duplicated within the group
c877a771 140 */
141 TString dirname(basename(name)), hname(histname(name));
142 THashList *parent(FindGroup(dirname));
495de23b 143 if(!parent){
1f65e0e2 144 Fatal("THistManager::CreateTH1", "Parent %s does not exist", dirname.Data());
495de23b 145 return;
146 }
147 if(parent->FindObject(hname.Data())){
1f65e0e2 148 Fatal("THistManager::CreateTH1", "Object %s already exists in group %s does not exist", hname.Data(), dirname.Data());
495de23b 149 return;
150 }
c877a771 151 parent->Add(new TH1D(hname.Data(), title, nbins, xbins));
152}
153
154//______________________________________________________________________________
1f65e0e2 155void THistManager::CreateTH1(const char *name, const char *title, const TArrayD &xbins){
c877a771 156 /*
157 * Create a new TH1 within the container. The histogram name also contains the parent group(s) according to the common
158 * group notation.
159 *
160 * @param name: Name of the histogram
161 * @param title: Title of the histogram
162 * @param xbins: array of bin limits (contains also number of bins)
1f65e0e2 163 * Raises fatals in case the parent group does not exist or the object is attempted to be duplicated within the group
c877a771 164 */
165 TString dirname(basename(name)), hname(histname(name));
166 THashList *parent(FindGroup(dirname));
495de23b 167 if(!parent){
1f65e0e2 168 Fatal("THistManager::CreateTH1", "Parent %s does not exist", dirname.Data());
495de23b 169 return;
170 }
171 if(parent->FindObject(hname.Data())){
1f65e0e2 172 Fatal("THistManager::CreateTH1", "Object %s already exists in group %s does not exist", hname.Data(), dirname.Data());
495de23b 173 return;
174 }
c877a771 175 parent->Add(new TH1D(hname.Data(), title, xbins.GetSize()-1, xbins.GetArray()));
176}
177
178//______________________________________________________________________________
1f65e0e2 179void THistManager::CreateTH2(const char *name, const char *title, int nbinsx, double xmin, double xmax, int nbinsy, double ymin, double ymax){
c877a771 180 /*
181 * Create a new TH2 within the container. The histogram name also contains the parent group(s) according to the common
182 * group notation.
183 *
184 * @param name: Name of the histogram
185 * @param title: Title of the histogram
186 * @param nbinsx: number of bins in x-direction
187 * @param xmin: min. value of the range in x-direction
188 * @param xmax: max. value of the range in x-direction
189 * @param nbinsy: number of bins in y-direction
190 * @param ymin: min. value of the range in y-direction
191 * @param ymax: max. value of the range in y-direction
1f65e0e2 192 * Raises fatals in case the parent group does not exist or the object is attempted to be duplicated within the group
c877a771 193 */
194 TString dirname(basename(name)), hname(histname(name));
195 THashList *parent(FindGroup(dirname.Data()));
495de23b 196 if(!parent){
1f65e0e2 197 Fatal("THistManager::CreateTH2", "Parent %s does not exist", dirname.Data());
495de23b 198 return;
199 }
200 if(parent->FindObject(hname.Data())){
1f65e0e2 201 Fatal("THistManager::CreateTH2", "Object %s already exists in group %s does not exist", hname.Data(), dirname.Data());
495de23b 202 return;
203 }
c877a771 204 parent->Add(new TH2D(hname.Data(), title, nbinsx, xmin, xmax, nbinsy, ymin, ymax));
205}
206
207//______________________________________________________________________________
1f65e0e2 208void THistManager::CreateTH2(const char *name, const char *title, int nbinsx, const double *xbins, int nbinsy, const double *ybins){
c877a771 209 /*
210 * Create a new TH2 within the container. The histogram name also contains the parent group(s) according to the common
211 * group notation.
212 *
213 * @param name: Name of the histogram
214 * @param title: Title of the histogram
215 * @param nbinsx: number of bins in x-direction
216 * @param xbins: array of bin limits in x-direction
217 * @param nbinsy: number of bins in y-direction
218 * @param ybins: array of bin limits in y-direction
1f65e0e2 219 * Raises fatals in case the parent group does not exist or the object is attempted to be duplicated within the group
c877a771 220 */
221 TString dirname(basename(name)), hname(histname(name));
222 THashList *parent(FindGroup(dirname.Data()));
495de23b 223 if(!parent){
1f65e0e2 224 Fatal("THistManager::CreateTH2", "Parent %s does not exist", dirname.Data());
495de23b 225 return;
226 }
227 if(parent->FindObject(hname.Data())){
1f65e0e2 228 Fatal("THistManager::CreateTH2", "Object %s already exists in group %s does not exist", hname.Data(), dirname.Data());
495de23b 229 return;
230 }
c877a771 231 parent->Add(new TH2D(hname.Data(), title, nbinsx, xbins, nbinsy, ybins));
232}
233
234//______________________________________________________________________________
1f65e0e2 235void THistManager::CreateTH2(const char *name, const char *title, const TArrayD &xbins, const TArrayD &ybins){
c877a771 236 /*
237 * Create a new TH2 within the container. The histogram name also contains the parent group(s) according to the common
238 * group notation.
239 *
240 * @param name: Name of the histogram
241 * @param title: Title of the histogram
242 * @param xbins: array of bin limits in x-direction (contains also the number of bins)
243 * @param ybins: array of bin limits in y-direction (contains also the number of bins)
1f65e0e2 244 * Raises fatals in case the parent group does not exist or the object is attempted to be duplicated within the group
c877a771 245 */
246 TString dirname(basename(name)), hname(histname(name));
247 THashList *parent(FindGroup(dirname.Data()));
495de23b 248 if(!parent){
1f65e0e2 249 Fatal("THistManager::CreateTH2", "Parent %s does not exist", dirname.Data());
495de23b 250 return;
251 }
252 if(parent->FindObject(hname.Data())){
1f65e0e2 253 Fatal("THistManager::CreateTH2", "Object %s already exists in group %s does not exist", hname.Data(), dirname.Data());
495de23b 254 return;
255 }
c877a771 256 parent->Add(new TH2D(hname.Data(), title, xbins.GetSize() - 1, xbins.GetArray(), ybins.GetSize() - 1, ybins.GetArray()));
257}
258
259//______________________________________________________________________________
1f65e0e2 260void THistManager::CreateTH3(const char* name, const char* title, int nbinsx, double xmin, double xmax, int nbinsy, double ymin, double ymax, int nbinsz, double zmin, double zmax) {
c877a771 261 /*
262 * Create a new TH3 within the container. The histogram name also contains the parent group(s) according to the common
263 * group notation.
264 *
265 * @param nbinsx: number of bins in x-direction
266 * @param xmin: min. value of the range in x-direction
267 * @param xmax: max. value of the range in x-direction
268 * @param nbinsy: number of bins in y-direction
269 * @param ymin: min. value of the range in y-direction
270 * @param ymax: max. value of the range in y-direction
271 * @param nbinsz: number of bins in z-direction
272 * @param zmin: min. value of the range in z-direction
273 * @param zmax: max. value of the range in z-direction
1f65e0e2 274 * Raises fatals in case the parent group does not exist or the object is attempted to be duplicated within the group
c877a771 275 */
276 TString dirname(basename(name)), hname(histname(name));
277 THashList *parent(FindGroup(dirname.Data()));
495de23b 278 if(!parent){
1f65e0e2 279 Fatal("THistManager::CreateTH3", "Parent %s does not exist", dirname.Data());
495de23b 280 return;
281 }
282 if(parent->FindObject(hname.Data())){
1f65e0e2 283 Fatal("THistManager::CreateTH3", "Object %s already exists in group %s does not exist", hname.Data(), dirname.Data());
495de23b 284 return;
285 }
c877a771 286 parent->Add(new TH3D(hname.Data(), title, nbinsx, xmin, xmax, nbinsy, ymin, ymax, nbinsz, zmin, zmax));
287}
288
289//______________________________________________________________________________
1f65e0e2 290void THistManager::CreateTH3(const char* name, const char* title, int nbinsx, const double* xbins, int nbinsy, const double* ybins, int nbinsz, const double* zbins) {
c877a771 291 /*
292 * Create a new TH3 within the container. The histogram name also contains the parent group(s) according to the common
293 * group notation.
294 *
295 * @param name: Name of the histogram
296 * @param title: Title of the histogram
297 * @param nbinsx: number of bins in x-direction
298 * @param xbins: array of bin limits in x-direction
299 * @param nbinsy: number of bins in y-direction
300 * @param ybins: array of bin limits in y-direction
301 * @param nbinsz: number of bins in z-direction
302 * @param zbins: array of bin limits in z-direction
1f65e0e2 303 * Raises fatals in case the parent group does not exist or the object is attempted to be duplicated within the group
c877a771 304 */
305 TString dirname(basename(name)), hname(histname(name));
306 THashList *parent(FindGroup(dirname.Data()));
495de23b 307 if(!parent){
1f65e0e2 308 Fatal("THistManager::CreateTH3", "Parent %s does not exist", dirname.Data());
495de23b 309 return;
310 }
311 if(parent->FindObject(hname.Data())){
1f65e0e2 312 Fatal("THistManager::CreateTH3", "Object %s already exists in group %s does not exist", hname.Data(), dirname.Data());
495de23b 313 }
c877a771 314 parent->Add(new TH3D(hname.Data(), title, nbinsx, xbins, nbinsy, ybins, nbinsz, zbins));
315}
316
317//______________________________________________________________________________
1f65e0e2 318void THistManager::CreateTH3(const char* name, const char* title, const TArrayD& xbins, const TArrayD& ybins, const TArrayD& zbins) {
c877a771 319 /*
320 * Create a new TH3 within the container. The histogram name also contains the parent group(s) according to the common
321 * group notation.
322 *
323 * @param name: Name of the histogram
324 * @param title: Title of the histogram
325 * @param xbins: array of bin limits in x-direction (contains also the number of bins)
326 * @param ybins: array of bin limits in y-direction (contains also the number of bins)
327 * @param zbins: array of bin limits in z-direction (contains also the number of bins)
1f65e0e2 328 * Raises fatals in case the parent group does not exist or the object is attempted to be duplicated within the group
c877a771 329 */
330 TString dirname(basename(name)), hname(histname(name));
331 THashList *parent(FindGroup(dirname.Data()));
495de23b 332 if(!parent){
1f65e0e2 333 Fatal("THistManager::CreateTH3", "Parent %s does not exist", dirname.Data());
495de23b 334 return;
335 }
336 if(parent->FindObject(hname.Data())){
1f65e0e2 337 Fatal("THistManager::CreateTH3", "Object %s already exists in group %s does not exist", hname.Data(), dirname.Data());
495de23b 338 return;
339 }
c877a771 340 parent->Add(new TH3D(hname.Data(), title, xbins.GetSize()-1, xbins.GetArray(), ybins.GetSize()-1, ybins.GetArray(), zbins.GetSize()-1, zbins.GetArray()));
341}
342
343//______________________________________________________________________________
1f65e0e2 344void THistManager::CreateTHnSparse(const char *name, const char *title, int ndim, const int *nbins, const double *min, const double *max) {
c877a771 345 /*
346 * Create a new THnSparse within the container. The histogram name also contains the parent group(s) according to the common
347 * group notation.
348 *
349 * @param name: Name of the histogram
350 * @param title: Title of the histogram
351 * @param ndim: Number of dimensions
352 * @param nbins: Number of bins per dimension
353 * @param min: min. value of the range for each dimension
354 * @param max: max. value of the range for each dimension
1f65e0e2 355 * Raises fatals in case the parent group does not exist or the object is attempted to be duplicated within the group
c877a771 356 */
357 TString dirname(basename(name)), hname(histname(name));
358 THashList *parent(FindGroup(dirname.Data()));
495de23b 359 if(!parent){
1f65e0e2 360 Fatal("THistManager::CreateTHnSparse", "Parent %s does not exist", dirname.Data());
495de23b 361 return;
362 }
363 if(parent->FindObject(hname.Data())){
1f65e0e2 364 Fatal("THistManager::CreateTHnSparse", "Object %s already exists in group %s does not exist", hname.Data(), dirname.Data());
495de23b 365 return;
366 }
c877a771 367 parent->Add(new THnSparseD(hname.Data(), title, ndim, nbins, min, max));
368}
369
370//______________________________________________________________________________
1f65e0e2 371void THistManager::CreateTHnSparse(const char *name, const char *title, int ndim, const TAxis **axes) {
c877a771 372 /*
373 * Create a new THnSparse within the container. The histogram name also contains the parent group(s) according to the common
374 * group notation.
375 *
376 * @param name: Name of the histogram
377 * @param title: Title of the histogram
378 * @param ndim: Number of dimensions
379 * @param axes: Array of pointers to TAxis for containing the axis definition for each dimension
1f65e0e2 380 * Raises fatals in case the parent group does not exist or the object is attempted to be duplicated within the group
c877a771 381 */
382 TString dirname(basename(name)), hname(histname(name));
383 THashList *parent(FindGroup(dirname.Data()));
495de23b 384 if(!parent){
1f65e0e2 385 Fatal("THistManager::CreateTHnSparse", "Parent %s does not exist", dirname.Data());
495de23b 386 return;
387 }
388 if(parent->FindObject(hname)){
1f65e0e2 389 Fatal("THistManager::CreateTHnSparse", "Object %s already exists in group %s does not exist", hname.Data(), dirname.Data());
495de23b 390 return;
391 }
c877a771 392 TArrayD xmin(ndim), xmax(ndim);
393 TArrayI nbins(ndim);
394 for(int idim = 0; idim < ndim; ++idim){
395 const TAxis &myaxis = *(axes[idim]);
396 nbins[idim] = myaxis.GetNbins();
397 xmin[idim] = myaxis.GetXmin();
398 xmax[idim] = myaxis.GetXmax();
399 }
400 THnSparseD *hsparse = new THnSparseD(hname.Data(), title, ndim, nbins.GetArray(), xmin.GetArray(), xmax.GetArray());
401 for(int id = 0; id < ndim; ++id)
402 *(hsparse->GetAxis(id)) = *(axes[id]);
403 parent->Add(hsparse);
404}
405
406//______________________________________________________________________________
1f65e0e2 407void THistManager::SetObject(TObject * const o, const char *group) {
c877a771 408 /*
409 * Set a new group into the container into the parent group
410 *
411 * @param o: the object ot be included
1f65e0e2 412 * Raises fatals in case the parent group is not found, the object is attempted to be duplicated in the group, or the type of
413 * the object is not a histogram type
c877a771 414 */
415 THashList *parent(FindGroup(group));
495de23b 416 if(!parent){
1f65e0e2 417 Fatal("THistManager::SetObject", "Parent %s does not exist", strcmp(group, "/") ? group : "");
495de23b 418 return;
419 }
420 if(parent->FindObject(o->GetName())){
1f65e0e2 421 Fatal("THistManager::SetObject", "Parent %s does not exist", strcmp(group, "/") ? group : "");
495de23b 422 return;
423 }
424 if(!(dynamic_cast<THnBase *>(o) || dynamic_cast<TH1 *>(o))){
1f65e0e2 425 Fatal("THistManager::SetObject", "Object %s is not of a histogram type",o->GetName());
495de23b 426 return;
427 }
c877a771 428 fHistos->Add(o);
429}
430
431//______________________________________________________________________________
1f65e0e2 432void THistManager::FillTH1(const char *name, double x, double weight) {
c877a771 433 /*
434 * Fill a 1D histogram within the container. The histogram name also contains the parent group(s) according to the common
435 * group notation.
436 *
437 * @param name: Name of the histogram
438 * @param x: x-coordinate
439 * @param weight (@default 1): optional weight of the entry
1f65e0e2 440 * Raises fatals in case the parent group is not found or the histogram is not found in the parent group
c877a771 441 */
442 TString dirname(basename(name)), hname(histname(name));
443 THashList *parent(FindGroup(dirname.Data()));
495de23b 444 if(!parent){
1f65e0e2 445 Fatal("THistManager::FillTH1", "Parnt group %s does not exist", dirname.Data());
495de23b 446 return;
447 }
c877a771 448 TH1 *hist = dynamic_cast<TH1 *>(parent->FindObject(hname.Data()));
495de23b 449 if(!hist){
1f65e0e2 450 Fatal("THistManager::FillTH1", "Histogram %s not found in parent group %s", hname.Data(), dirname.Data());
495de23b 451 return;
452 }
c877a771 453 hist->Fill(x, weight);
454}
455
456//______________________________________________________________________________
1f65e0e2 457void THistManager::FillTH2(const char *name, double x, double y, double weight) {
c877a771 458 /*
459 * Fill a 2D histogram within the container. The histogram name also contains the parent group(s) according to the common
460 * group notation.
461 *
462 * @param name: Name of the histogram
463 * @param x: x-coordinate
464 * @param y: y-coordinate
465 * @param weight (@default 1): optional weight of the entry
1f65e0e2 466 * Raises fatals in case the parent group is not found or the histogram is not found in the parent group
c877a771 467 */
468 TString dirname(basename(name)), hname(histname(name));
469 THashList *parent(FindGroup(dirname.Data()));
495de23b 470 if(!parent){
471 Fatal("THistManager::FillTH2", "Parent group %s does not exist", dirname.Data());
472 return;
473 }
c877a771 474 TH2 *hist = dynamic_cast<TH2 *>(parent->FindObject(hname.Data()));
495de23b 475 if(!hist){
1f65e0e2 476 Fatal("THistManager::FillTH2", "Histogram %s not found in parent group %s", hname.Data(), dirname.Data());
495de23b 477 return;
478 }
c877a771 479 hist->Fill(x, y, weight);
480}
481
482//______________________________________________________________________________
1f65e0e2 483void THistManager::FillTH2(const char *name, double *point, double weight) {
c877a771 484 /*
485 * Fill a 2D histogram within the container. The histogram name also contains the parent group(s) according to the common
486 * group notation.
487 *
488 * @param name: Name of the histogram
489 * @param point: coordinates of the data
490 * @param weight (@default 1): optional weight of the entry
1f65e0e2 491 * Raises fatals in case the parent group is not found or the histogram is not found in the parent group
c877a771 492 */
493 TString dirname(basename(name)), hname(histname(name));
494 THashList *parent(FindGroup(dirname.Data()));
495de23b 495 if(!parent){
496 Fatal("THistManager::FillTH2", "Parent group %s does not exist", dirname.Data());
497 return;
498 }
c877a771 499 TH2 *hist = dynamic_cast<TH2 *>(parent->FindObject(hname.Data()));
495de23b 500 if(!hist){
1f65e0e2 501 Fatal("THistManager::FillTH2", "Histogram %s not found in parent group %s", hname.Data(), dirname.Data());
495de23b 502 return;
503 }
c877a771 504 hist->Fill(point[0], point[1], weight);
505}
506
507//______________________________________________________________________________
1f65e0e2 508void THistManager::FillTH3(const char* name, double x, double y, double z, double weight) {
c877a771 509 /*
510 * Fill a 3D histogram within the container. The histogram name also contains the parent group(s) according to the common
511 * group notation.
512 *
513 * @param name: Name of the histogram
514 * @param x: x-coordinate
515 * @param y: y-coordinate
516 * @param z: z-coordinate
517 * @param weight (@default 1): optional weight of the entry
1f65e0e2 518 * Raises fatals in case the parent group is not found or the histogram is not found in the parent group
c877a771 519 */
520 TString dirname(basename(name)), hname(histname(name));
521 THashList *parent(FindGroup(dirname.Data()));
495de23b 522 if(!parent){
523 Fatal("THistManager::FillTH3", "Parent group %s does not exist", dirname.Data());
524 return;
525 }
c877a771 526 TH3 *hist = dynamic_cast<TH3 *>(parent->FindObject(hname.Data()));
495de23b 527 if(!hist){
1f65e0e2 528 Fatal("THistManager::FillTH3", "Histogram %s not found in parent group %s", hname.Data(), dirname.Data());
495de23b 529 return;
530 }
c877a771 531 hist->Fill(x, y, z, weight);
532}
533
534//______________________________________________________________________________
1f65e0e2 535void THistManager::FillTH3(const char* name, const double* point, double weight) {
536 /*
537 * Fill a 3D histogram within the container. The histogram name also contains the parent group(s) according to the common
538 * group notation.
539 *
540 * @param name: Name of the histogram
541 * @param point: 3D-coordinate of the point
542 * @param weight (@default 1): optional weight of the entry
543 * Raises fatals in case the parent group is not found or the histogram is not found in the parent group
544 */
c877a771 545 TString dirname(basename(name)), hname(histname(name));
546 THashList *parent(FindGroup(dirname.Data()));
495de23b 547 if(!parent){
548 Fatal("THistManager::FillTH3", "Parent group %s does not exist", dirname.Data());
549 return;
550 }
c877a771 551 TH3 *hist = dynamic_cast<TH3 *>(parent->FindObject(hname.Data()));
495de23b 552 if(!hist){
1f65e0e2 553 Fatal("THistManager::FillTH3", "Histogram %s not found in parent group %s", hname.Data(), dirname.Data());
495de23b 554 return;
555 }
c877a771 556 hist->Fill(point[0], point[1], point[2], weight);
557}
558
559
560//______________________________________________________________________________
1f65e0e2 561void THistManager::FillTHnSparse(const char *name, const double *x, double weight) {
c877a771 562 /*
563 * Fill a nD histogram within the container. The histogram name also contains the parent group(s) according to the common
564 * group notation.
565 *
566 * @param name: Name of the histogram
567 * @param x: coordinates of the data
568 * @param weight (@default 1): optional weight of the entry
1f65e0e2 569 * Raises fatals in case the parent group is not found or the histogram is not found in the parent group
c877a771 570 */
571 TString dirname(basename(name)), hname(histname(name));
572 THashList *parent(FindGroup(dirname.Data()));
495de23b 573 if(!parent){
574 Fatal("THistManager::FillTH3", "Parent group %s does not exist", dirname.Data());
575 return;
576 }
c877a771 577 THnSparseD *hist = dynamic_cast<THnSparseD *>(parent->FindObject(hname.Data()));
495de23b 578 if(!hist){
1f65e0e2 579 Fatal("THistManager::FillTH3", "Histogram %s not found in parent group %s", hname.Data(), dirname.Data());
495de23b 580 return;
581 }
c877a771 582 hist->Fill(x, weight);
583}
584
585//______________________________________________________________________________
586TObject *THistManager::FindObject(const char *name) const {
587 /*
588 * Find an object inside the container. The object can also be within a
589 * histogram group. For this the name has to follow the common notation
590 *
591 * @param name: Name of the object to find inside the container
592 * @return: pointer to the object (NULL if not found)
593 */
594 TString dirname(basename(name)), hname(histname(name));
595 THashList *parent(FindGroup(dirname.Data()));
596 if(!parent) return NULL;
597 return parent->FindObject(name);
598}
599
1f65e0e2 600//______________________________________________________________________________
601TObject* THistManager::FindObject(const TObject* obj) const {
602 /*
603 * Find and object inside the container. The object name is expected to contain the
604 * full path of the histogram object, including parent groups
605 *
606 * @param obj: the object to find
607 * @return: pointer to the object (NULL if not found)
608 */
609 TString dirname(basename(obj->GetName())), hname(histname(obj->GetName()));
610 THashList *parent(FindGroup(dirname.Data()));
611 if(!parent) return NULL;
612 return parent->FindObject(hname);
613}
614
c877a771 615//______________________________________________________________________________
616THashList *THistManager::FindGroup(const char *dirname) const {
617 /*
618 * Find histogram group. Name is using common notation
619 *
620 * @param dirname: Path of the group (treat empty path as top node
621 * @return: TList of objects (NULL if group does not exist)
622 */
623 if(!strlen(dirname) || !strcmp(dirname, "/")) return fHistos;
624 std::vector<std::string> tokens;
625 TokenizeFilename(dirname, "/", tokens);
626 THashList *currentdir(fHistos);
627 for(std::vector<std::string>::iterator it = tokens.begin(); it != tokens.end(); ++it){
628 currentdir = dynamic_cast<THashList *>(currentdir->FindObject(it->c_str()));
629 if(!currentdir) break;
630 }
631 return currentdir;
632}
633
634//______________________________________________________________________________
635void THistManager::TokenizeFilename(const char *name, const char *delim, std::vector<std::string> &listoftokens) const {
636 /*
637 * Tokenizes a string. Results are stored inside the vector listoftokens
638 *
639 * @ param name: string to be tokenised
640 * @ param delim: delimiter string
641 * @ param listoftokens: list of tokens (C++ strings)
642 */
643 TString s(name);
644 TObjArray *arr = s.Tokenize(delim);
645 TObjString *ostr(NULL);
646 TIter toks(arr);
647 while((ostr = dynamic_cast<TObjString *>(toks()))){
648 listoftokens.push_back(std::string(ostr->String().Data()));
649 }
650 delete arr;
651}
652
653//______________________________________________________________________________
654const char *THistManager::basename(const char *path) const {
655 /*
656 * Helper function extracting the basename from a given histogram path.
657 *
658 * @param path: histogram path
659 * @return: basename extracted
660 */
661 TString s(path);
662 int index = s.Last('/');
663 if(index < 0) return ""; // no directory structure
664 return TString(s(0, index)).Data();
665}
666
667//______________________________________________________________________________
668const char *THistManager::histname(const char *path) const {
669 /*
670 * Helper function extracting the histogram name from a given histogram path.
671 *
672 * @param path: histogram path
673 * @return: basename extracted
674 */
675 TString s(path);
676 int index = s.Last('/');
677 if(index < 0) return path; // no directory structure
678 return TString(s(index+1, s.Length() - (index+1))).Data();
679}