Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TH1Merger.h
Go to the documentation of this file.
1 // @(#)root/cont:$Id$
2 // Author: Lorenzo Moneta 08/2016
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2003, Rene Brun, Fons Rademakers and al. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 // Helper clas implementing some of the TH1 functionality
13 
14 #include "TH1.h"
15 #include "TList.h"
16 
17 class TH1Merger {
18 
19 public:
20  enum EMergerType {
21  kNotCompatible = -1, // histogram arenot compatible and cannot be merged
22  kAllSameAxes = 0, // histogram have all some axes
23  kAllNoLimits = 1, // all histogram don't have limits (the buffer is used)
24  kHasNewLimits = 2, // all histogram don't have limits (the buffer is used)
25  kAllLabel = 3, // histogram have labels all axis
26  kAutoP2HaveLimits = 4, // P2 (power-of-2) algorithm: all histogram have limits
27  kAutoP2NeedLimits = 5 // P2 algorithm: some histogram still need projections
28  };
29 
30  static Bool_t AxesHaveLimits(const TH1 * h);
31 
32  static Int_t FindFixBinNumber(Int_t ibin, const TAxis & inAxis, const TAxis & outAxis) {
33  // should I ceck in case of underflow/overflow if underflow/overflow values of input axis
34  // outside output axis ?
35  if (ibin == 0 ) return 0; // return underflow
36  if (ibin == inAxis.GetNbins()+1 ) return outAxis.GetNbins()+1; // return overflow
37  return outAxis.FindFixBin(inAxis.GetBinCenter(ibin));
38  }
39 
40  // find bin number estending the axis
41  static Int_t FindBinNumber(Int_t ibin, const TAxis & inAxis, TAxis & outAxis) {
42  // should I ceck in case of underflow/overflow if underflow/overflow values of input axis
43  // outside output axis ?
44  if (ibin == 0 ) return 0; // return underflow
45  if (ibin == inAxis.GetNbins()+1 ) return outAxis.GetNbins()+1; // return overflow
46  return outAxis.FindBin(inAxis.GetBinCenter(ibin));
47  }
48 
49  // Function to find if axis label list has duplicates
50  static Bool_t HasDuplicateLabels(const THashList * labels);
51 
52  // check if histogram has duplicate labels
53  static Int_t CheckForDuplicateLabels(const TH1 * hist);
54 
55 
56  TH1Merger(TH1 & h, TCollection & l, Option_t * opt = "") :
57  fH0(&h),
58  fHClone(nullptr),
59  fNewAxisFlag(0)
60  {
61  fInputList.AddAll(&l);
62  TString option(opt);
63  if (!option.IsNull() ) {
64  option.ToUpper();
65  if (option.Contains("NOL") )
66  fNoLabelMerge = true;
67  if (option.Contains("NOCHECK") )
68  fNoCheck = true;
69  }
70  }
71 
72  ~TH1Merger() {
73  // The list contains fHClone, so let's clear it first to avoid
74  // accessing deleted memory later [we 'could' have just removed
75  // fHClone from the list]
76  fInputList.Clear();
77  if (fHClone) delete fHClone;
78  }
79 
80  // function douing the actual merge
81  Bool_t operator() ();
82 
83 private:
84  Bool_t AutoP2BuildAxes(TH1 *);
85 
86  EMergerType ExamineHistograms();
87 
88  void DefineNewAxes();
89 
90  void CopyBuffer(TH1 *hsrc, TH1 *hdes);
91 
92  Bool_t BufferMerge();
93 
94  Bool_t AutoP2BufferMerge();
95 
96  Bool_t AutoP2Merge();
97 
98  Bool_t SameAxesMerge();
99 
100  Bool_t DifferentAxesMerge();
101 
102  Bool_t LabelMerge();
103 
104 
105  Bool_t fNoLabelMerge = kFALSE; // force merger to not use labels and do bin center by bin center
106  Bool_t fNoCheck = kFALSE; // skip check on duplicate labels
107  TH1 * fH0; //! histogram on which the list is merged
108  TH1 * fHClone; //! copy of fH0 - managed by this class
109  TList fInputList; // input histogram List
110  TAxis fNewXAxis;
111  TAxis fNewYAxis;
112  TAxis fNewZAxis;
113  UInt_t fNewAxisFlag;
114 };