ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/CmsHi/HafHistogram/src/HHistogram.cc
Revision: 1.1
Committed: Wed Sep 29 15:44:21 2010 UTC (14 years, 7 months ago) by yjlee
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Error occurred while calculating annotation data.
Log Message:
HafHistogram tool

File Contents

# Content
1
2 /** \class HHistogram Ganzhur/HafHistogram/src/HHistogram.cc
3 *
4 * Description:
5 * Analysis code of the CMS experiment;
6 * Original version is a part of HAF package developed for CMS: UserCode/HAF
7 * Histogram class
8 *
9 * \author Marcel Kunze, Ruhr-University Bochum, Germany
10 * \author Serguei Ganjour, CEA-Saclay/IRFU, FR
11 *
12 * \version $Id: HHistogram.cc,v 1.5 2010/01/07 10:44:17 ganzhur Exp $
13 *
14 */
15
16 #include <iostream>
17
18 using namespace std;
19
20 #include "TH1.h"
21 #include "TH2.h"
22 #include "TProfile.h"
23 #include "TAxis.h"
24 #include "TDirectory.h"
25
26 #include "UserCode/HafHistogram/interface/HHistID.h"
27 #include "UserCode/HafHistogram/interface/HHistogram.h"
28
29 ClassImp(HHistogram)
30 ClassImp(HMassHistogram)
31 ClassImp(HEnergyHistogram)
32 ClassImp(HMomentumHistogram)
33 ClassImp(HEoverPHistogram)
34 ClassImp(HMoverPHistogram)
35 ClassImp(HDalitzPlot)
36
37 // Constructors:
38 // 1-D histo:
39 HHistogram::HHistogram( const char* hname, const char* htitle,
40 Int_t nbins, Axis_t lowX, Axis_t highX ) :
41 TNamed( hname,htitle )
42 {
43 // Create a 1-D ROOT histo:
44 histp= new TH1F( hname, htitle, nbins, lowX, highX );
45 histp->SetDirectory(gDirectory);
46 }
47
48 HHistogram::HHistogram( const char* hname, const char* htitle,
49 Int_t nbinsX, Axis_t lowX, Axis_t highX,
50 Int_t nbinsY, Axis_t lowY, Axis_t highY ) :
51 TNamed( hname,htitle )
52 {
53 // Create a 2-D ROOT histo:
54 histp= new TH2F( hname, htitle, nbinsX, lowX, highX, nbinsY, lowY, highY );
55 histp->SetDirectory(gDirectory);
56 }
57
58 HHistogram::HHistogram( const char* hname, const char* htitle,
59 Int_t nbins, Axis_t lowX, Axis_t highX,
60 Axis_t lowY, Axis_t highY ) :
61 TNamed( hname,htitle )
62 {
63 // Create a 1-D profile ROOT histo with default error calculation:
64 // error= sigma/sqrt(N)
65 histp= new TProfile( (char*)hname, (char*)htitle, nbins, lowX, highX,lowY, highY );
66 }
67
68 HHistogram::~HHistogram()
69 {
70 delete histp;
71 }
72
73 void HHistogram::Accumulate( Axis_t x, Stat_t weight )
74 {
75 if( histp->GetDimension() == 2 ||
76 ( histp->GetDimension() == 1 &&
77 histp->IsA() == TProfile::Class() ) ) {
78 Axis_t y = weight;
79 weight = 1.0;
80 ((TH2 *)histp)->Fill( x, y, weight );
81 }
82 else {
83 histp->Fill( x, weight );
84 }
85 return;
86 }
87
88 void HHistogram::Accumulate1( Axis_t x, Stat_t weight )
89 {
90 histp->Fill( x, weight );
91 }
92
93 void HHistogram::Accumulate( Axis_t x, Axis_t y, Stat_t weight )
94 {
95 ((TH2 *)histp)->Fill( x, y, weight );
96 }
97
98 void HHistogram::Accumulate2( Axis_t x, Axis_t y, Stat_t weight )
99 {
100 ((TH2 *)histp)->Fill( x, y, weight );
101 }
102
103
104 void HHistogram::Reset()
105 {
106 histp->Reset();
107 }
108
109
110 Float_t HHistogram::GetContents( Int_t nbinsX, Int_t nbinsY ) const
111 {
112 Float_t cont= 0;
113 if( nbinsY == 0 ) {
114 cont= (Float_t) histp->GetBinContent( nbinsX );
115 }
116 else {
117 cont= (Float_t) histp->GetCellContent( nbinsX, nbinsY );
118 }
119 return cont;
120 }
121
122 Float_t HHistogram::GetErrors( Int_t nbinsX, Int_t nbinsY ) const
123 {
124 Float_t error= 0;
125 if( nbinsY == 0 ) {
126 error = (Float_t) histp->GetBinError( nbinsX );
127 }
128 else {
129 error = (Float_t) histp->GetCellError( nbinsX, nbinsY );
130 }
131 return error;
132 }
133
134
135
136 Int_t HHistogram::GetEntries() const { return (Int_t) histp->GetEntries(); }
137
138 Float_t HHistogram::GetWtSum() const
139 {
140 return (Float_t) histp->GetSumOfWeights();
141 }
142
143
144
145 Int_t HHistogram::GetNbins( Int_t theDim ) const {
146
147 Int_t nbins= 0;
148 if( theDim == 0 ) {
149 nbins= histp->GetNbinsX();
150 }
151 else if( theDim == 1 ) {
152 if( histp->GetDimension() == 2 ) nbins= histp->GetNbinsY();
153 }
154 return nbins;
155
156 }
157
158 Float_t HHistogram::GetLow( Int_t theDim ) const {
159
160 Float_t low= 0;
161 if( theDim == 0 ) {
162 low= histp->GetXaxis()->GetBinLowEdge( 1 );
163 }
164 else if( theDim == 1 ) {
165 if( histp->GetDimension() == 2 ) {
166 low= histp->GetYaxis()->GetBinLowEdge( 1 );
167 }
168 }
169 return low;
170
171 }
172
173 Float_t HHistogram::GetHigh( Int_t theDim ) const {
174
175 TAxis* axisp= 0;
176 if( theDim == 0 ) {
177 axisp= histp->GetXaxis();
178 }
179 else if( theDim == 1 ) {
180 if( histp->GetDimension() == 2 ) axisp= histp->GetYaxis();
181 }
182 Float_t high= 0;
183 if( axisp != 0 ) {
184 Int_t n= axisp->GetNbins();
185 high= axisp->GetBinLowEdge( n ) + axisp->GetBinWidth( n );
186 }
187 return high;
188
189 }
190
191 Float_t HHistogram::GetAvg( Int_t theDim ) const {
192
193 Float_t mean= 0;
194 if( theDim == 0 ) {
195 mean= (Float_t) histp->GetMean( 1 );
196 }
197 else if( theDim == 1 ) {
198 if( histp->GetDimension() == 2 ) mean= (Float_t) histp->GetMean( 2 );
199 }
200 return mean;
201
202 }
203
204 Float_t HHistogram::GetCovar( Int_t dim1, Int_t dim2 ) const {
205
206 // Calculate weighted sums:
207 TAxis* xaxisp= histp->GetXaxis();
208 TAxis* yaxisp= histp->GetYaxis();
209 Int_t maxxbin= histp->GetNbinsX();
210 Int_t maxybin= histp->GetNbinsY();
211 Double_t meanx= 0;
212 Double_t meany= 0;
213 Double_t meanx2= 0;
214 Double_t meany2= 0;
215 Double_t meanxy= 0;
216 for( Int_t ybin= 1; ybin <= maxybin; ++ybin ) {
217 Double_t cony= 0;
218 for( Int_t xbin= 1; xbin <= maxxbin; ++xbin ) {
219 Double_t cellc= histp->GetCellContent( xbin, ybin );
220 cony+= cellc;
221 Double_t xbinc= xaxisp->GetBinCenter( xbin );
222 meanx+= cellc * xbinc;
223 meanx2+= cellc * xbinc*xbinc;
224 meanxy+= cellc * xbinc * yaxisp->GetBinCenter( ybin );
225 }
226 Double_t ybinc= yaxisp->GetBinCenter( ybin );
227 meany+= cony * ybinc;
228 meany2+= cony * ybinc*ybinc;
229 }
230 Double_t sumcon= histp->GetSumOfWeights();
231 if( sumcon ) {
232 meanx/= sumcon;
233 meany/= sumcon;
234 meanx2/= sumcon;
235 meany2/= sumcon;
236 meanxy/= sumcon;
237 }
238 else {
239 meanx= 0;
240 meany= 0;
241 meanx2= 0;
242 meany2= 0;
243 meanxy= 0;
244 }
245
246 // Return covariance between dim1 and dim2:
247 Float_t cov= 0;
248 if( dim1 == 0 && dim2 == 0 ) {
249 cov = (Float_t) (meanx2 - meanx*meanx);
250 }
251 else if( (dim1 == 0 && dim2 == 1) ||
252 (dim1 == 1 && dim2 == 0 ) ) {
253 cov = (Float_t) (meanxy - meanx*meany);
254 }
255 else if( dim1 == 1 && dim2 == 1 ) {
256 cov = (Float_t) (meany2 - meany*meany);
257 }
258 return cov;
259
260 }
261
262 Int_t HHistogram::GetType() const {
263
264 Int_t ht= 0;
265 if( histp->GetDimension() == 1 ) ht= 1;
266 else if( histp->GetDimension() == 2 ) ht= 2;
267 return ht;
268
269 }
270
271 HHistID HHistogram::GetHistID() const {
272
273 return HHistID( histp->GetName() );
274
275 }
276
277 Bool_t HHistogram::PtrIsEqual( TObject* ptr ) const {
278
279 Bool_t result;
280 if( (TObject*) histp == ptr ) result= kTRUE;
281 else result= kFALSE;
282 return result;
283
284 }
285
286 HHistogram& HHistogram::operator<<(Axis_t x)
287 {
288 Accumulate(x); return *this;
289 }
290
291
292 //----------------Spezialization-----------------
293
294 //----------------
295 // Constructors --
296 //----------------
297
298 HMassHistogram::HMassHistogram(const Text_t *name,const Text_t *title,Int_t nbins,Axis_t xlow,Axis_t xup)
299 : HHistogram(name,title,nbins,xlow,xup)
300 {
301 }
302
303 HEnergyHistogram::HEnergyHistogram(const Text_t *name,const Text_t *title,Int_t nbins,Axis_t xlow,Axis_t xup)
304 : HHistogram(name,title,nbins,xlow,xup)
305 {
306 }
307
308 HMomentumHistogram::HMomentumHistogram(const Text_t *name,const Text_t *title,Int_t nbins,Axis_t xlow,Axis_t xup)
309 : HHistogram(name,title,nbins,xlow,xup)
310 {
311 }
312
313 HEoverPHistogram::HEoverPHistogram(const Text_t *name,const Text_t *title,Int_t nbinsx,Axis_t xlow,Axis_t xup
314 ,Int_t nbinsy,Axis_t ylow,Axis_t yup)
315 : HHistogram(name,title,nbinsx,xlow,xup,nbinsy,ylow,yup)
316 {
317 }
318
319 HMoverPHistogram::HMoverPHistogram(const Text_t *name,const Text_t *title,Int_t nbinsx,Axis_t xlow,Axis_t xup
320 ,Int_t nbinsy,Axis_t ylow,Axis_t yup)
321 : HHistogram(name,title,nbinsx,xlow,xup,nbinsy,ylow,yup)
322 {
323 }
324
325 //--------------
326 // Destructor --
327 //--------------
328
329 HMassHistogram::~HMassHistogram( )
330 {
331 }
332
333 HEnergyHistogram::~HEnergyHistogram( )
334 {
335 }
336
337 HMomentumHistogram::~HMomentumHistogram( )
338 {
339 }
340
341 HEoverPHistogram::~HEoverPHistogram( )
342 {
343 }
344
345 HMoverPHistogram::~HMoverPHistogram( )
346 {
347 }
348
349 HDalitzPlot::~HDalitzPlot( )
350 {
351 }
352
353 //----------------------
354 //-- public Functions --
355 //----------------------
356
357 void HMassHistogram::Accumulate( Axis_t x, Stat_t weight )
358 {
359 Accumulate1( x, weight );
360 }
361
362
363 void HEnergyHistogram::Accumulate( Axis_t x, Stat_t weight )
364 {
365 Accumulate1( x, weight );
366 }
367
368 void HMomentumHistogram::Accumulate( Axis_t x, Stat_t weight )
369 {
370 Accumulate1( x, weight );
371 }
372
373
374 void HEoverPHistogram::Accumulate( Axis_t x, Axis_t y, Stat_t weight )
375 {
376 Accumulate2( x, y, weight );
377 }
378
379 void HMoverPHistogram::Accumulate( Axis_t x, Axis_t y, Stat_t weight )
380 {
381 Accumulate2( x, y, weight );
382 }
383
384
385 HDalitzPlot::HDalitzPlot(const Text_t *name,const Text_t *title,Int_t nbinsx,Axis_t xlow,Axis_t xup
386 ,Int_t nbinsy,Axis_t ylow,Axis_t yup)
387 : HHistogram(name,title,nbinsx,xlow,xup,nbinsy,ylow,yup)
388 {
389 }
390
391 /*
392 * This function fills a Dalitzplot for the particles name1,name2 and name3 from descriptor p.
393 * horizontal axis: (name1,name2), vertikal axis: (name2,name3)
394 * There are three cases:
395 * 1) Fill the plot for three identical particles (6 entries)
396 * 2) Fill the plot for two different particles (2 entries)
397 * 3) Fill the plot for three different particles (1 entry)
398 */
399