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