]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MONITOR/AliMonitorHisto.cxx
Bug fix in AliITSgeomMatrix::AngleFromMatrix (B. Nilsen)
[u/mrichter/AliRoot.git] / MONITOR / AliMonitorHisto.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, 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 /* $Id$ */
17
18 ///////////////////////////////////////////////////////////////////////////////
19 //                                                                           //
20 //  This class maintains a histogram that is used to monitor the quality     //
21 //  of the recorded data.                                                    //
22 //  The histogram is created and filled by a sub class of AliMonitor.        //
23 //  It can be compared to a reference histogram. For the comparison a        //
24 //  maximal deviation (in standard deviations) can be specified.             //
25 //  The bins where the maximal deviation is exceeded are drawn in red.       //
26 //                                                                           //
27 ///////////////////////////////////////////////////////////////////////////////
28
29
30 #include <TProfile.h>
31 #include <TH2.h>
32 #include <TVirtualPad.h>
33
34 #include "AliLog.h"
35
36 #include "AliMonitorHisto.h"
37
38
39 ClassImp(AliMonitorHisto) 
40
41
42 Int_t   AliMonitorHisto::fgNHistosMax = 10;
43
44
45 //_____________________________________________________________________________
46 AliMonitorHisto::AliMonitorHisto() :
47   AliMonitorPlot(),
48   fHisto(NULL),
49   fHistoList(),
50   fNHistos(0),
51   fHistoRun(NULL),
52   fHistoDraw(NULL),
53   fHistoRef(NULL),
54   fHistoCompare(NULL),
55   fNorm(kNormNone)
56 {
57 // default contructor
58
59 }
60
61 //_____________________________________________________________________________
62 AliMonitorHisto::AliMonitorHisto(const AliMonitorHisto& histo) :
63   AliMonitorPlot(histo),
64   fHisto(NULL),
65   fHistoList(),
66   fNHistos(histo.fNHistos),
67   fHistoRun(NULL),
68   fHistoDraw(NULL),
69   fHistoRef(NULL),
70   fHistoCompare(NULL),
71   fNorm(histo.fNorm)
72 {
73 // copy constructor
74
75   Bool_t addStatus = TH1::AddDirectoryStatus();
76   TH1::AddDirectory(kFALSE);
77   if (histo.fHisto) fHisto = (TH1*) histo.fHisto->Clone();
78   TObjLink* link = histo.fHistoList.FirstLink();
79   for (Int_t i = 0; i < fNHistos; i++) {
80     fHistoList.Add(link->GetObject()->Clone());
81     link = link->Next();
82   }
83   if (histo.fHistoRun) fHistoRun = (TH1*) histo.fHistoRun->Clone();
84   if (histo.fHistoRef) fHistoRef = (TH1*) histo.fHistoRef->Clone();
85   TH1::AddDirectory(addStatus);
86 }
87
88 //_____________________________________________________________________________
89 AliMonitorHisto& AliMonitorHisto::operator =(const AliMonitorHisto& histo)
90 {
91 // assignment operator
92
93   AliMonitorPlot::operator =(histo);
94
95   Bool_t addStatus = TH1::AddDirectoryStatus();
96   TH1::AddDirectory(kFALSE);
97   fHisto = NULL;
98   if (histo.fHisto) fHisto = (TH1*) histo.fHisto->Clone();
99   fNHistos = histo.fNHistos;
100   TObjLink* link = histo.fHistoList.FirstLink();
101   for (Int_t i = 0; i < fNHistos; i++) {
102     fHistoList.Add(link->GetObject()->Clone());
103     link = link->Next();
104   }
105   fHistoRun = NULL;
106   if (histo.fHistoRun) fHistoRun = (TH1*) histo.fHistoRun->Clone();
107   fHistoDraw = NULL;
108   fHistoRef = NULL;
109   if (histo.fHistoRef) fHistoRef = (TH1*) histo.fHistoRef->Clone();
110   fHistoCompare = NULL;
111   fNorm = histo.fNorm;
112   TH1::AddDirectory(addStatus);
113
114   return *this;
115 }
116
117 //_____________________________________________________________________________
118 AliMonitorHisto::AliMonitorHisto(TH1* histo, ENorm norm) :
119   AliMonitorPlot(histo->GetName(), histo->GetTitle()),
120   fHisto(histo),
121   fHistoList(),
122   fNHistos(0),
123   fHistoRun(NULL),
124   fHistoDraw(NULL),
125   fHistoRef(NULL),
126   fHistoCompare(NULL),
127   fNorm(norm)
128 {
129 // create a monitor histogram from the given histogram
130
131   if (histo->GetDimension() > 2) {
132     AliFatal("3 dimensional histograms are not supported");
133   }
134
135   histo->SetDirectory(NULL);
136   histo->Reset();
137   fHisto->Sumw2();
138   Bool_t addStatus = TH1::AddDirectoryStatus();
139   TH1::AddDirectory(kFALSE);
140   fHistoRun = (TH1*) histo->Clone();
141   TH1::AddDirectory(addStatus);
142 }
143
144 //_____________________________________________________________________________
145 AliMonitorHisto::~AliMonitorHisto()
146 {
147 // delete all histograms
148
149   if (fHisto) delete fHisto;
150   fHistoList.Delete();
151   if (fHistoRun) delete fHistoRun;
152   if (fHistoDraw) delete fHistoDraw;
153   if (fHistoCompare) delete fHistoCompare;
154 }
155
156
157 //_____________________________________________________________________________
158 void AliMonitorHisto::SetReference(TH1* ref)
159 {
160 // set the reference histogram for comparison
161
162   if (fHistoRef) delete fHistoRef;
163   Bool_t addStatus = TH1::AddDirectoryStatus();
164   TH1::AddDirectory(kFALSE);
165   TH1::AddDirectory(addStatus);
166   fHistoRef = (TH1*) ref->Clone();
167   if (fHistoCompare) fHistoCompare->Reset();
168 }
169
170 //_____________________________________________________________________________
171 void AliMonitorHisto::SetReference(AliMonitorPlot* ref)
172 {
173 // set the reference histogram for comparison
174
175   if (!ref->InheritsFrom(AliMonitorHisto::Class())) return;
176   ((AliMonitorHisto*)ref)->GetRun();
177   SetReference(((AliMonitorHisto*)ref)->fHistoDraw);
178 }
179
180
181 //_____________________________________________________________________________
182 void AliMonitorHisto::Fill(Axis_t x)
183 {
184 // fill the monitor histogram
185
186   fHisto->Fill(x);
187 }
188
189 //_____________________________________________________________________________
190 void AliMonitorHisto::Fill(Axis_t x, Axis_t y)
191 {
192 // fill the monitor histogram
193
194   fHisto->Fill(x, y);
195 }
196
197 //_____________________________________________________________________________
198 void AliMonitorHisto::Fill(Axis_t x, Axis_t y, Stat_t w)
199 {
200 // fill the monitor histogram
201
202   if (fHisto->InheritsFrom(TH2::Class())) {
203     ((TH2*)fHisto)->Fill(x, y, w);
204   } else if (fHisto->InheritsFrom(TProfile::Class())) {
205     ((TProfile*)fHisto)->Fill(x, y, w);
206   } else {
207     AliError("trying to fill x and y of a 1 dimensinal histogram");
208     return;
209   }
210 }
211
212 //_____________________________________________________________________________
213 void AliMonitorHisto::ScaleErrorBy(Double_t factor)
214 {
215 // multiply the error of each bin by the given factor
216
217   Int_t yMax = 1; 
218   if (fHisto->GetDimension() > 1) 
219     yMax = fHisto->GetYaxis()->GetNbins();
220   Int_t xMax = fHisto->GetXaxis()->GetNbins();
221   for (Int_t iY = 1; iY <= yMax; iY++) {
222     for (Int_t iX = 1; iX <= xMax; iX++) {
223       Int_t iBin = fHisto->GetBin(iX, iY);
224       fHisto->SetBinError(iBin, factor * fHisto->GetBinError(iBin));
225     }
226   }
227 }
228
229
230 //_____________________________________________________________________________
231 void AliMonitorHisto::Update()
232 {
233 // update the normalized data histogram
234
235   Update(fHisto);
236   fHisto->Reset();
237 }
238
239 //_____________________________________________________________________________
240 void AliMonitorHisto::Update(TH1* histo)
241 {
242 // update the normalized data histogram using the given histo instead of fHisto
243
244   fNumberOfEvents++;
245   while (fNHistos >= fgNHistosMax) {
246     fHistoList.Remove(fHistoList.LastLink());
247     fNHistos--;
248   }
249   Bool_t addStatus = TH1::AddDirectoryStatus();
250   TH1::AddDirectory(kFALSE);
251   fHistoList.AddFirst(histo->Clone());
252   TH1::AddDirectory(addStatus);
253   fNHistos++;
254   fHistoRun->Add(histo);
255   fHisto->Reset();
256 }
257
258 //_____________________________________________________________________________
259 void AliMonitorHisto::Add(AliMonitorPlot* plot)
260 {
261 // merge the given histo to this one
262
263   if (!plot->InheritsFrom(AliMonitorHisto::Class())) return;
264   AliMonitorHisto* histo = (AliMonitorHisto*) plot;
265
266   fNumberOfEvents += histo->fNumberOfEvents;
267   Bool_t addStatus = TH1::AddDirectoryStatus();
268   TH1::AddDirectory(kFALSE);
269   TObjLink* link = histo->fHistoList.LastLink();
270   while (link) {
271     fHistoList.AddFirst(link->GetObject()->Clone());
272     link = link->Prev();
273   }
274   TH1::AddDirectory(addStatus);
275   fNHistos += histo->fNHistos;
276   while (fNHistos > fgNHistosMax) {
277     fHistoList.Remove(fHistoList.LastLink());
278     fNHistos--;
279   }
280   fHistoRun->Add(histo->fHistoRun);
281 }
282
283 //_____________________________________________________________________________
284 void AliMonitorHisto::Reset()
285 {
286 // reset the monitor histogram for a new run
287
288   fHisto->Reset();
289   fHistoList.Delete();
290   fNHistos = 0;
291   fHistoRun->Reset();
292   if (fHistoDraw) delete fHistoDraw;
293   fHistoDraw = NULL;
294   if (fHistoCompare) delete fHistoCompare;
295   fHistoCompare = NULL;
296   fNumberOfEvents = 0;
297 }
298
299 //_____________________________________________________________________________
300 void AliMonitorHisto::ResetList()
301 {
302 // reset the the list of monitor histograms
303
304   fHistoList.Delete();
305   fNHistos = 0;
306 }
307
308
309 //_____________________________________________________________________________
310 void AliMonitorHisto::Scale(Int_t nEvents)
311 {
312 // scale the histogram to the correct normalization
313
314   Double_t scale = 1.;
315   switch (fNorm) {
316   case kNormNone    : scale = 1.; break;
317   case kNormEvents  : scale = 1./nEvents; break;
318   case kNormEntries : scale = ((fHistoDraw->GetEntries() > 0) ? 
319                                1./fHistoDraw->GetEntries() : 1.); break;
320   case kNormIntegral: scale = ((fHistoDraw->Integral() > 0) ? 
321                                1./fHistoDraw->Integral() : 1.); break;
322   }
323   fHistoDraw->Scale(scale);
324 }
325
326 //_____________________________________________________________________________
327 Bool_t AliMonitorHisto::ComparePlot()
328 {
329 // compare the data histogram to the reference histogram
330 // if they deviate by more than fgThreshold standard deviations in a bin,
331 // this bin is set in fHistoCompare and kFALSE is returned
332   
333   if (!fHistoRef) return kTRUE;
334   if (fgThreshold <= 0) return kTRUE;
335   if (!fHistoDraw) {
336     AliWarning("no data histogram available for comparison\ncall DrawEvent, DrawSum or DrawRaw before calling Compare");
337     return kTRUE;
338   }
339   if (fHistoCompare) delete fHistoCompare;
340   Bool_t addStatus = TH1::AddDirectoryStatus();
341   TH1::AddDirectory(kFALSE);
342   fHistoCompare = (TH1*) fHistoDraw->Clone();
343   TH1::AddDirectory(addStatus);
344   fHistoCompare->Reset();
345   Bool_t result = kTRUE;
346
347   Int_t yMax = 1; 
348   if (fHistoDraw->GetDimension() > 1) 
349     yMax = fHistoDraw->GetYaxis()->GetNbins();
350   Int_t xMax = fHistoDraw->GetXaxis()->GetNbins();
351   for (Int_t iY = 1; iY <= yMax; iY++) {
352     for (Int_t iX = 1; iX <= xMax; iX++) {
353       Int_t iBin = fHistoDraw->GetBin(iX, iY);
354       Double_t delta = TMath::Abs(fHistoDraw->GetBinContent(iBin) -
355                                   fHistoRef->GetBinContent(iBin));
356       Double_t errorData = fHistoDraw->GetBinError(iBin);
357       Double_t errorRef = fHistoRef->GetBinError(iBin);
358       Double_t sigma = TMath::Sqrt(errorData*errorData + errorRef*errorRef);
359       if (delta > fgThreshold*sigma) {
360         fHistoCompare->SetBinContent(iBin, fHistoDraw->GetBinContent(iBin));
361         fHistoCompare->SetBinError(iBin, errorData);
362         result = kFALSE;
363       }
364     }
365   }
366
367   return result;
368 }
369
370 //_____________________________________________________________________________
371 Bool_t AliMonitorHisto::GetEvent(Int_t number)
372 {
373 // get the normalized monitor histogram for the "number"th last event
374
375   if (fNHistos == 0) {
376     AliWarning("there are no histograms for single events available");
377     return kFALSE;
378   }
379   if (number > fNHistos) {
380     AliError(Form("requested event number (%d) exceeds range of available events (%d)", 
381                   number, fNHistos));
382     return kFALSE;
383   }
384   if (number <= 0) return kFALSE;
385
386   if (fHistoDraw) delete fHistoDraw;
387   if (fHistoCompare) delete fHistoCompare;
388   fHistoCompare = NULL;
389
390   TObjLink* link = fHistoList.FirstLink();
391   for (Int_t i = 1; i < number; i++) link = link->Next();
392   Bool_t addStatus = TH1::AddDirectoryStatus();
393   TH1::AddDirectory(kFALSE);
394   fHistoDraw = (TH1*) link->GetObject()->Clone();
395   TH1::AddDirectory(addStatus);
396
397   Scale(1);
398   return kTRUE;
399 }
400
401 //_____________________________________________________________________________
402 Bool_t AliMonitorHisto::GetSum(Int_t number)
403 {
404 // get the normalized monitor histogram for the sum of the last 
405 // "number" events
406
407   if (fNHistos == 0) {
408     AliWarning("there are no histograms for single events available");
409     return kFALSE;
410   }
411   if (number > fNHistos) {
412     AliError(Form("requested number of events (%d) exceeds range of available events (%d)\nusing last %d event(s)", 
413                   number, fNHistos, fNHistos));
414     number = fNHistos;
415   }
416   if (number <= 0) return kFALSE;
417
418   if (fHistoDraw) delete fHistoDraw;
419   if (fHistoCompare) delete fHistoCompare;
420   fHistoCompare = NULL;
421
422   TObjLink* link = fHistoList.FirstLink();
423   Bool_t addStatus = TH1::AddDirectoryStatus();
424   TH1::AddDirectory(kFALSE);
425   fHistoDraw = (TH1*) link->GetObject()->Clone();
426   TH1::AddDirectory(addStatus);
427   for (Int_t i = 1; i < number; i++) {
428     link = link->Next();
429     fHistoDraw->Add((TH1*) link->GetObject());
430   }
431
432   Scale(number);
433   return kTRUE;
434 }
435
436 //_____________________________________________________________________________
437 Bool_t AliMonitorHisto::GetRun()
438 {
439 // get the normalized monitor histogram for all monitored events 
440 // of the current run
441
442   if (fHistoDraw) delete fHistoDraw;
443   if (fHistoCompare) delete fHistoCompare;
444   fHistoCompare = NULL;
445
446   Bool_t addStatus = TH1::AddDirectoryStatus();
447   TH1::AddDirectory(kFALSE);
448   fHistoDraw = (TH1*) fHistoRun->Clone();
449   TH1::AddDirectory(addStatus);
450
451   Scale(fNumberOfEvents);
452   return kTRUE;
453 }
454
455 //_____________________________________________________________________________
456 void AliMonitorHisto::DrawPlot()
457 {
458 // draw the histograms
459
460   fHistoDraw->SetMarkerColor(fgColorData);
461   fHistoDraw->SetLineColor(fgColorData);
462   fHistoDraw->SetFillColor(fgColorData);
463   fHistoDraw->DrawCopy();
464
465   if (fHistoRef && fgDrawRef) {
466     char option[256];
467     sprintf(option, "%sSAME", fHistoDraw->GetOption());
468
469     if (fHistoRef->GetMaximum() > fHistoDraw->GetMaximum()) {
470       fHistoDraw->SetMaximum(fHistoRef->GetMaximum() * 1.1);
471     }
472
473     fHistoRef->SetMarkerColor(fgColorRef);
474     fHistoRef->SetLineColor(fgColorRef);
475     fHistoRef->SetFillColor(fgColorRef);
476     fHistoRef->DrawCopy(option);
477
478     fHistoDraw->DrawCopy(option);
479
480     if (fHistoCompare && (fgThreshold > 0)) {
481       fHistoCompare->SetMarkerColor(fgColorCompare);
482       fHistoCompare->SetLineColor(fgColorCompare);
483       fHistoCompare->SetFillColor(fgColorCompare);
484       fHistoCompare->DrawCopy(option);
485     }
486   }
487
488   gPad->Update();
489 }