star-travex
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Profile2D.cxx
Go to the documentation of this file.
1 #include "StiRootIO/Profile2D.h"
2 
3 #include <exception>
4 #include "TH1.h"
5 #include "TString.h"
6 #include "TError.h"
7 
8 class DifferentNumberOfBins: public std::exception {};
9 class DifferentAxisLimits: public std::exception {};
10 class DifferentBinLimits: public std::exception {};
11 class DifferentLabels: public std::exception {};
12 
13 
15 
16 
17 Profile2D::Profile2D() : TProfile2D()
18 {
19 }
20 
21 
22 Profile2D::Profile2D(const TH2D& h2D) : TProfile2D()
23 {
24  h2D.TH2D::Copy(*this);
25 
26  fBinEntries.Set(fNcells);
27  fBinSumw2.Set(fNcells);
28 
29  for (int i=0; i<fNcells; i++)
30  {
31  if (h2D.GetBinContent(i) && h2D.GetBinError(i))
32  {
33  fBinEntries.fArray[i] = 1;
34  fBinSumw2.fArray[i] = fSumw2.fArray[i];
35  } else {
36  fBinEntries.fArray[i] = 0;
37  fBinSumw2.fArray[i] = 0;
38  }
39  }
40 }
41 
42 
43 Profile2D::Profile2D(std::string name, std::string title, Int_t nbinsx, Double_t xlow, Double_t xup,
44  Int_t nbinsy, Double_t ylow, Double_t yup, std::string options) :
45  TProfile2D(name.c_str(), title.c_str(), nbinsx, xlow, xup, nbinsy, ylow, yup, options.c_str())
46 {
47  SetOption(options.c_str());
48 }
49 
50 
51 Profile2D::Profile2D(std::string name, std::string title, Int_t nbinsx, const Double_t* xbins,
52  Int_t nbinsy, const Double_t* ybins, std::string options) :
53  TProfile2D(name.c_str(), title.c_str(), nbinsx, xbins, nbinsy, ybins, options.c_str())
54 {
55  SetOption(options.c_str());
56 }
57 
58 
60 {
61  for (int iBin=0; iBin<fNcells; ++iBin)
62  {
63  double bc = GetBinContent(iBin);
64  double be = GetBinError(iBin);
65 
66  // Skip empty bins
67  if (!bc && !be) continue;
68 
69  fBinEntries.fArray[iBin] = val;
70  }
71 }
72 
73 
74 Bool_t Profile2D::Multiply(const Profile2D &prof2)
75 {
76  try {
77  CheckConsistency((TH1*) this, (TH1*) &prof2);
78  } catch(DifferentNumberOfBins&) {
79  Error("Multiply","Attempt to multiply histograms with different number of bins");
80  return kFALSE;
81  } catch(DifferentAxisLimits&) {
82  Warning("Multiply","Attempt to multiply histograms with different axis limits");
83  } catch(DifferentBinLimits&) {
84  Warning("Multiply","Attempt to multiply histograms with different bin limits");
85  } catch(DifferentLabels&) {
86  Warning("Multiply","Attempt to multiply histograms with different labels");
87  }
88 
89  // Loop on bins (including underflows/overflows)
90  for (Int_t i = 0; i < fNcells; ++i) {
91  Double_t c1 = prof2.GetBinContent(i);
92  fArray[i] *= c1;
93  }
94  return kTRUE;
95 }
96 
97 
98 TProfile *Profile2D::ProfileX(const char *name, Int_t firstybin, Int_t lastybin, Option_t *option) const
99 {
100  // *-*-*-*-*Project a 2-D histogram into a profile histogram along X*-*-*-*-*-*
101  // *-* ========================================================
102  //
103  // The projection is made from the channels along the Y axis
104  // ranging from firstybin to lastybin included.
105  // The result is a 1D profile which contains the combination of all the considered bins along Y
106  // By default, bins 1 to ny are included
107  // When all bins are included, the number of entries in the projection
108  // is set to the number of entries of the 2-D histogram, otherwise
109  // the number of entries is incremented by 1 for all non empty cells.
110  //
111  // The option can also be used to specify the projected profile error type.
112  // Values which can be used are 's', 'i', or 'g'. See TProfile::BuildOptions for details
113  //
114  //
115  return DoProfile(true, name, firstybin, lastybin, option);
116 }
117 
118 
119 TProfile *Profile2D::ProfileY(const char *name, Int_t firstxbin, Int_t lastxbin, Option_t *option) const
120 {
121  // *-*-*-*-*Project a 2-D histogram into a profile histogram along X*-*-*-*-*-*
122  // *-* ========================================================
123  //
124  // The projection is made from the channels along the X axis
125  // ranging from firstybin to lastybin included.
126  // The result is a 1D profile which contains the combination of all the considered bins along X
127  // By default, bins 1 to ny are included
128  // When all bins are included, the number of entries in the projection
129  // is set to the number of entries of the 2-D histogram, otherwise
130  // the number of entries is incremented by 1 for all non empty cells.
131  //
132  // The option can also be used to specify the projected profile error type.
133  // Values which can be used are 's', 'i', or 'g'. See TProfile::BuildOptions for details
134  //
135  //
136  //
137  return DoProfile(false, name, firstxbin, lastxbin, option);
138 }
139 
140 
141 TProfile * Profile2D::DoProfile(bool onX, const char *name, Int_t firstbin, Int_t lastbin, Option_t *option) const
142 {
143  // implementation of ProfileX or ProfileY for a TProfile2D
144  // Do correctly the combination of the bin averages when doing the projection
145 
146  TString opt = option;
147  opt.ToLower();
148  bool originalRange = opt.Contains("o");
149 
150  TString expectedName = ( onX ? "_pfx" : "_pfy" );
151 
152  TString pname(name);
153  if (pname.IsNull() || name == expectedName)
154  pname = TString(GetName() ) + expectedName;
155 
156  const TAxis& outAxis = ( onX ? fXaxis : fYaxis );
157  const TArrayD *bins = outAxis.GetXbins();
158  Int_t firstOutBin = outAxis.GetFirst();
159  Int_t lastOutBin = outAxis.GetLast();
160 
161  TProfile * p1 = 0;
162  // case of fixed bins
163  if (bins->fN == 0) {
164  if (originalRange)
165  p1 = new TProfile(pname,GetTitle(), outAxis.GetNbins(), outAxis.GetXmin(), outAxis.GetXmax(), opt );
166  else
167  p1 = new TProfile(pname,GetTitle(), lastOutBin-firstOutBin+1,
168  outAxis.GetBinLowEdge(firstOutBin),outAxis.GetBinUpEdge(lastOutBin), opt);
169  } else {
170  // case of variable bins
171  if (originalRange )
172  p1 = new TProfile(pname,GetTitle(),outAxis.GetNbins(),bins->fArray,opt);
173  else
174  p1 = new TProfile(pname,GetTitle(),lastOutBin-firstOutBin+1,&bins->fArray[firstOutBin-1],opt);
175 
176  }
177 
178  if (fBinSumw2.fN) p1->Sumw2();
179 
180  // make projection in a 2D first
181  TH2D * h2dW = ProjectionXY("h2temp-W","W");
182  TH2D * h2dN = ProjectionXY("h2temp-N","B");
183 
184  h2dW->SetDirectory(0); h2dN->SetDirectory(0);
185 
186 
187  TString opt1 = (originalRange) ? "o" : "";
188  TH1D * h1W = (onX) ? h2dW->ProjectionX("h1temp-W",firstbin,lastbin,opt1) : h2dW->ProjectionY("h1temp-W",firstbin,lastbin,opt1);
189  TH1D * h1N = (onX) ? h2dN->ProjectionX("h1temp-N",firstbin,lastbin,opt1) : h2dN->ProjectionY("h1temp-N",firstbin,lastbin,opt1);
190  h1W->SetDirectory(0); h1N->SetDirectory(0);
191 
192 
193  // fill the bin content
194  R__ASSERT( h1W->fN == p1->fN );
195  R__ASSERT( h1N->fN == p1->fN );
196  R__ASSERT( h1W->GetSumw2()->fN != 0); // h1W should always be a weighted histogram since h2dW is
197  for (int i = 0; i < p1->fN ; ++i) {
198  p1->fArray[i] = h1W->GetBinContent(i); // array of profile is sum of all values
199  p1->GetSumw2()->fArray[i] = h1W->GetSumw2()->fArray[i]; // array of content square of profile is weight square of the W projected histogram
200  p1->SetBinEntries(i, h1N->GetBinContent(i) );
201  if (fBinSumw2.fN) p1->GetBinSumw2()->fArray[i] = h1N->GetSumw2()->fArray[i]; // sum of weight squares are stored to compute errors in h1N histogram
202  }
203  // delete the created histograms
204  delete h2dW;
205  delete h2dN;
206  delete h1W;
207  delete h1N;
208 
209  // Also we need to set the entries since they have not been correctly calculated during the projection
210  // we can only set them to the effective entries
211  p1->SetEntries( p1->GetEffectiveEntries() );
212 
213  return p1;
214 }
virtual TProfile * DoProfile(bool onX, const char *name, Int_t firstbin, Int_t lastbin, Option_t *option) const
Definition: Profile2D.cxx:141
ClassImp(Profile2D) Profile2D
Definition: Profile2D.cxx:14
TProfile * ProfileY(const char *name="_pfy", Int_t firstxbin=0, Int_t lastxbin=-1, Option_t *option="") const
Definition: Profile2D.cxx:119
R__ASSERT(refX!=0 &&refY!=0 &&refZ!=0)
TProfile * ProfileX(const char *name="_pfx", Int_t firstybin=0, Int_t lastybin=-1, Option_t *option="") const
Definition: Profile2D.cxx:98
Bool_t Multiply(const Profile2D &prof2)
Definition: Profile2D.cxx:74
void ResetBinEntries(double val)
Definition: Profile2D.cxx:59