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