118831276ae0785d5b349bd973c4add0b4f462ea
[u/mrichter/AliRoot.git] / EMCAL / AliCaloCalibPedestal.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 //* $Id$ */
16
17 //________________________________________________________________________
18 //
19 // A help class for monitoring and calibration tools: MOOD, AMORE etc.,
20 // It can be created and used a la (ctor):
21 /*
22   //Create the object for making the histograms
23   fPedestals = new AliCaloCalibPedestal( fDetType );
24   // AliCaloCalibPedestal knows how many modules we have for PHOS or EMCAL
25   fNumModules = fPedestals->GetModules();
26 */
27 // fed an event:
28 //  fPedestals->ProcessEvent(fCaloRawStream);
29 // asked to draw histograms:
30 //  fPedestals->GetDeadMap(i)->Draw("col");
31 // or
32 //  fPedestals->GetPeakProfileHighGainRatio((i < fNumModules) ? i : fVisibleModule)->Draw("colz");
33 // etc.
34 // The pseudo-code examples above were from the first implementation in MOOD (summer 2007).
35 //________________________________________________________________________
36
37 //#include "TCanvas.h"
38 #include "TH1.h"
39 #include "TF1.h"
40 #include "TFile.h"
41 #include <fstream>
42 #include <sstream>
43 #include <iostream>
44 #include <stdexcept>
45 #include <cmath>
46
47 #include "AliRawReader.h"
48 #include "AliCaloRawStreamV3.h"
49
50 //The include file
51 #include "AliCaloCalibPedestal.h"
52
53 ClassImp(AliCaloCalibPedestal)
54
55 using namespace std;
56
57 // ctor; initialize everything in order to avoid compiler warnings
58 AliCaloCalibPedestal::AliCaloCalibPedestal(kDetType detectorType) :  
59   TObject(),
60   fPedestalLowGain(),
61   fPedestalHighGain(),
62   fPedestalLEDRefLowGain(),
63   fPedestalLEDRefHighGain(),
64   fPeakMinusPedLowGain(),
65   fPeakMinusPedHighGain(),
66   fPeakMinusPedHighGainHisto(),
67   fPedestalLowGainDiff(),
68   fPedestalHighGainDiff(),
69   fPedestalLEDRefLowGainDiff(),
70   fPedestalLEDRefHighGainDiff(),
71   fPeakMinusPedLowGainDiff(),
72   fPeakMinusPedHighGainDiff(),
73   fPedestalLowGainRatio(),
74   fPedestalHighGainRatio(),
75   fPedestalLEDRefLowGainRatio(),
76   fPedestalLEDRefHighGainRatio(),
77   fPeakMinusPedLowGainRatio(),
78   fPeakMinusPedHighGainRatio(),
79   fDeadMap(),
80   fNEvents(0),
81   fNChanFills(0),
82   fDeadTowers(0),
83   fNewDeadTowers(0),
84   fResurrectedTowers(0),
85   fReference(0),
86   fDetType(kNone),
87   fColumns(0),
88   fRows(0),
89   fLEDRefs(0),
90   fModules(0),
91   fRowMin(0),
92   fRowMax(0),
93   fRowMultiplier(0),
94   fCaloString(),
95   fMapping(NULL),
96   fRunNumber(-1),
97   fSelectPedestalSamples(kTRUE), 
98   fFirstPedestalSample(0),
99   fLastPedestalSample(15),
100   fDeadThreshold(5),
101   fWarningThreshold(50),
102   fWarningFraction(0.002),
103   fHotSigma(5)
104 {
105   //Default constructor. First we set the detector-type related constants.
106   if (detectorType == kPhos) {
107     fColumns = fgkPhosCols;
108     fRows = fgkPhosRows;
109     fLEDRefs = fgkPhosLEDRefs;
110     fModules = fgkPhosModules;
111     fCaloString = "PHOS";
112     fRowMin = -1*fRows;
113     fRowMax = 0;
114     fRowMultiplier = -1;
115   } 
116   else {
117     //We'll just trust the enum to keep everything in line, so that if detectorType
118     //isn't kPhos then it is kEmCal. Note, however, that this is not necessarily the
119     //case, if someone intentionally gives another number
120     fColumns = AliEMCALGeoParams::fgkEMCALCols;
121     fRows = AliEMCALGeoParams::fgkEMCALRows;
122     fLEDRefs = AliEMCALGeoParams::fgkEMCALLEDRefs;
123     fModules = AliEMCALGeoParams::fgkEMCALModules;
124     fCaloString = "EMCAL";
125     fRowMin = 0;
126     fRowMax = fRows;
127     fRowMultiplier = 1;
128   } 
129   fDetType = detectorType;
130
131   // ValidateProfiles(); // not to be done in ctor; info from Axel N. 
132 }
133
134 //_____________________________________________________________________
135 void AliCaloCalibPedestal::ValidateProfiles()
136 {
137   //Make sure the basic histos exist
138   if (!fPedestalLowGain.IsEmpty()) return; //The profiles already exist. We just check one, because they're all created at
139   //the same time
140
141   //Then, loop for the requested number of modules
142   TString title, name;
143   for (int i = 0; i < fModules; i++) {
144     //Pedestals, low gain
145     name = "hPedlowgain";
146     name += i;
147     title = "Pedestals, low gain, module ";
148     title += i; 
149     fPedestalLowGain.Add(new TProfile2D(name, title,
150                                         fColumns, 0.0, fColumns, 
151                                         fRows, fRowMin, fRowMax,"s"));
152   
153     //Pedestals, high gain
154     name = "hPedhighgain";
155     name += i;
156     title = "Pedestals, high gain, module ";
157     title += i; 
158     fPedestalHighGain.Add(new TProfile2D(name, title,
159                                          fColumns, 0.0, fColumns, 
160                                          fRows, fRowMin, fRowMax,"s"));
161
162     //LED Ref/Mon pedestals, low gain
163     name = "hPedestalLEDReflowgain";
164     name += i;
165     title = "Pedestal LEDRef, low gain, module ";
166     title += i; 
167     fPedestalLEDRefLowGain.Add(new TProfile(name, title,
168                                             fLEDRefs, 0.0, fLEDRefs, "s"));
169     
170     //LED Ref/Mon pedestals, high gain
171     name = "hPedestalLEDRefhighgain";
172     name += i;
173     title = "Pedestal LEDRef, high gain, module ";
174     title += i; 
175     fPedestalLEDRefHighGain.Add(new TProfile(name, title,
176                                              fLEDRefs, 0.0, fLEDRefs, "s"));
177   
178     //Peak-Pedestals, low gain
179     name = "hPeakMinusPedlowgain";
180     name += i;
181     title = "Peak-Pedestal, low gain, module ";
182     title += i; 
183     fPeakMinusPedLowGain.Add(new TProfile2D(name, title,
184                                             fColumns, 0.0, fColumns, 
185                                             fRows, fRowMin, fRowMax,"s"));
186   
187     //Peak-Pedestals, high gain
188     name = "hPeakMinusPedhighgain";
189     name += i;
190     title = "Peak-Pedestal, high gain, module ";
191     title += i; 
192     fPeakMinusPedHighGain.Add(new TProfile2D(name, title,
193                                              fColumns, 0.0, fColumns, 
194                                              fRows, fRowMin, fRowMax,"s"));
195
196     //Peak-Pedestals, high gain - TH2F histo
197     name = "hPeakMinusPedhighgainHisto";
198     name += i;
199     title = "Peak-Pedestal, high gain, module ";
200     title += i; 
201     fPeakMinusPedHighGainHisto.Add(new TH2F(name, title,
202                                             fColumns*fRows, 0.0, fColumns*fRows, 
203                                             100, 0, 1000));
204  
205     name = "hDeadMap";
206     name += i;
207     title = "Dead map, module ";
208     title += i;
209     fDeadMap.Add(new TH2D(name, title, fColumns, 0.0, fColumns, 
210                           fRows, fRowMin, fRowMax));
211   
212   }//end for nModules create the histograms
213
214   CompressAndSetOwner();
215 }
216
217 //_____________________________________________________________________
218 void AliCaloCalibPedestal::CompressAndSetOwner()
219
220   //Compress the arrays, in order to remove the empty objects (a 16 slot array is created by default)
221   fPedestalLowGain.Compress();
222   fPedestalHighGain.Compress();
223   fPedestalLEDRefLowGain.Compress();
224   fPedestalLEDRefHighGain.Compress();
225   fPeakMinusPedLowGain.Compress();
226   fPeakMinusPedHighGain.Compress();
227   fPeakMinusPedHighGainHisto.Compress();
228   fDeadMap.Compress();
229
230   // set owner ship for everyone
231   fPedestalLowGain.SetOwner(kTRUE);
232   fPedestalHighGain.SetOwner(kTRUE);
233   fPedestalLEDRefLowGain.SetOwner(kTRUE);
234   fPedestalLEDRefHighGain.SetOwner(kTRUE);
235   fPeakMinusPedLowGain.SetOwner(kTRUE);
236   fPeakMinusPedHighGain.SetOwner(kTRUE);
237   fPeakMinusPedHighGainHisto.SetOwner(kTRUE);
238   fPedestalLowGainDiff.SetOwner(kTRUE);
239   fPedestalHighGainDiff.SetOwner(kTRUE);
240   fPedestalLEDRefLowGainDiff.SetOwner(kTRUE);
241   fPedestalLEDRefHighGainDiff.SetOwner(kTRUE);
242   fPeakMinusPedLowGainDiff.SetOwner(kTRUE);
243   fPeakMinusPedHighGainDiff.SetOwner(kTRUE);
244   fPedestalLowGainRatio.SetOwner(kTRUE);
245   fPedestalHighGainRatio.SetOwner(kTRUE);
246   fPedestalLEDRefLowGainRatio.SetOwner(kTRUE);
247   fPedestalLEDRefHighGainRatio.SetOwner(kTRUE);
248   fPeakMinusPedLowGainRatio.SetOwner(kTRUE);
249   fPeakMinusPedHighGainRatio.SetOwner(kTRUE);
250   fDeadMap.SetOwner(kTRUE);
251 }
252
253 // dtor
254 //_____________________________________________________________________
255 AliCaloCalibPedestal::~AliCaloCalibPedestal()
256 {
257   if (fReference) delete fReference;//Delete the reference object, if it has been loaded
258
259   // delete also TObjArray's 
260   fPedestalLowGain.Delete();
261   fPedestalHighGain.Delete();
262   fPedestalLEDRefLowGain.Delete();
263   fPedestalLEDRefHighGain.Delete();
264   fPeakMinusPedLowGain.Delete();
265   fPeakMinusPedHighGain.Delete();
266   fPeakMinusPedHighGainHisto.Delete();
267   fPedestalLowGainDiff.Delete();
268   fPedestalHighGainDiff.Delete();
269   fPedestalLEDRefLowGainDiff.Delete();
270   fPedestalLEDRefHighGainDiff.Delete();
271   fPeakMinusPedLowGainDiff.Delete();
272   fPeakMinusPedHighGainDiff.Delete();
273   fPedestalLowGainRatio.Delete();
274   fPedestalHighGainRatio.Delete();
275   fPedestalLEDRefLowGainRatio.Delete();
276   fPedestalLEDRefHighGainRatio.Delete();
277   fPeakMinusPedLowGainRatio.Delete();
278   fPeakMinusPedHighGainRatio.Delete();
279   fDeadMap.Delete();
280
281 }
282
283 // copy ctor
284 //_____________________________________________________________________
285 AliCaloCalibPedestal::AliCaloCalibPedestal(AliCaloCalibPedestal &ped) :
286   TObject(ped),
287   fPedestalLowGain(),
288   fPedestalHighGain(),
289   fPedestalLEDRefLowGain(),
290   fPedestalLEDRefHighGain(),
291   fPeakMinusPedLowGain(),
292   fPeakMinusPedHighGain(),
293   fPeakMinusPedHighGainHisto(),
294   fPedestalLowGainDiff(),
295   fPedestalHighGainDiff(),
296   fPedestalLEDRefLowGainDiff(),
297   fPedestalLEDRefHighGainDiff(),
298   fPeakMinusPedLowGainDiff(),
299   fPeakMinusPedHighGainDiff(),
300   fPedestalLowGainRatio(),
301   fPedestalHighGainRatio(),
302   fPedestalLEDRefLowGainRatio(),
303   fPedestalLEDRefHighGainRatio(),
304   fPeakMinusPedLowGainRatio(),
305   fPeakMinusPedHighGainRatio(),
306   fDeadMap(),
307   fNEvents(ped.GetNEvents()),
308   fNChanFills(ped.GetNChanFills()),
309   fDeadTowers(ped.GetDeadTowerCount()),
310   fNewDeadTowers(ped.GetDeadTowerNew()),
311   fResurrectedTowers(ped.GetDeadTowerResurrected()),
312   fReference( 0 ), //! note that we do not try to copy the reference info here
313   fDetType(ped.GetDetectorType()),
314   fColumns(ped.GetColumns()),
315   fRows(ped.GetRows()),
316   fLEDRefs(ped.GetLEDRefs()),
317   fModules(ped.GetModules()),
318   fRowMin(ped.GetRowMin()),
319   fRowMax(ped.GetRowMax()),
320   fRowMultiplier(ped.GetRowMultiplier()),
321   fCaloString(ped.GetCaloString()),
322   fMapping(NULL), //! note that we are not copying the map info
323   fRunNumber(ped.GetRunNumber()),
324   fSelectPedestalSamples(ped.GetSelectPedestalSamples()),
325   fFirstPedestalSample(ped.GetFirstPedestalSample()),
326   fLastPedestalSample(ped.GetLastPedestalSample()),
327   fDeadThreshold(ped.GetDeadThreshold()),
328   fWarningThreshold(ped.GetWarningThreshold()),
329   fWarningFraction(ped.GetWarningFraction()),
330   fHotSigma(ped.GetHotSigma())
331 {
332   // Then the ObjArray ones; we add the histograms rather than trying TObjArray = assignment
333   //DS: this has not really been tested yet..
334   for (int i = 0; i < fModules; i++) {
335     fPedestalLowGain.Add( ped.GetPedProfileLowGain(i) );
336     fPedestalHighGain.Add( ped.GetPedProfileHighGain(i) );
337     fPedestalLEDRefLowGain.Add( ped.GetPedLEDRefProfileLowGain(i) );
338     fPedestalLEDRefHighGain.Add( ped.GetPedLEDRefProfileHighGain(i) );
339     fPeakMinusPedLowGain.Add( ped.GetPeakProfileLowGain(i) );
340     fPeakMinusPedHighGain.Add( ped.GetPeakProfileHighGain(i) );
341     fPeakMinusPedHighGainHisto.Add( ped.GetPeakHighGainHisto(i) );
342
343     fDeadMap.Add( ped.GetDeadMap(i) );  
344   }//end for nModules 
345  
346   CompressAndSetOwner();
347 }
348
349 // assignment operator; use copy ctor to make life easy..
350 //_____________________________________________________________________
351 AliCaloCalibPedestal& AliCaloCalibPedestal::operator = (AliCaloCalibPedestal &source)
352 {
353   // assignment operator; use copy ctor
354   if (&source == this) return *this;
355
356   new (this) AliCaloCalibPedestal(source);
357   return *this;
358 }
359
360 //_____________________________________________________________________
361 void AliCaloCalibPedestal::Reset()
362 {
363   ValidateProfiles(); // make sure histos/profiles exist
364   // Reset all arrays/histograms
365   for (int i = 0; i < fModules; i++) {
366     GetPedProfileLowGain(i)->Reset();
367     GetPedProfileHighGain(i)->Reset();
368     GetPedLEDRefProfileLowGain(i)->Reset();
369     GetPedLEDRefProfileHighGain(i)->Reset();
370     GetPeakProfileLowGain(i)->Reset();
371     GetPeakProfileHighGain(i)->Reset();
372     GetPeakHighGainHisto(i)->Reset();
373     GetDeadMap(i)->Reset();
374     
375     if (!fPedestalLowGainDiff.IsEmpty()) {
376       //This means that the comparison profiles have been created.
377   
378       GetPedProfileLowGainDiff(i)->Reset();
379       GetPedProfileHighGainDiff(i)->Reset();
380       GetPedLEDRefProfileLowGainDiff(i)->Reset();
381       GetPedLEDRefProfileHighGainDiff(i)->Reset();
382       GetPeakProfileLowGainDiff(i)->Reset();
383       GetPeakProfileHighGainDiff(i)->Reset();
384       
385       GetPedProfileLowGainRatio(i)->Reset();
386       GetPedProfileHighGainRatio(i)->Reset();
387       GetPedLEDRefProfileLowGainRatio(i)->Reset();
388       GetPedLEDRefProfileHighGainRatio(i)->Reset();
389       GetPeakProfileLowGainRatio(i)->Reset();
390       GetPeakProfileHighGainRatio(i)->Reset();
391     }
392   }
393   fNEvents = 0;
394   fNChanFills = 0;
395   fDeadTowers = 0;
396   fNewDeadTowers = 0;
397   fResurrectedTowers = 0;
398  
399   //To think about: should fReference be deleted too?... let's not do it this time, at least...
400 }
401
402 // Parameter/cut handling
403 //_____________________________________________________________________
404 void AliCaloCalibPedestal::SetParametersFromFile(const char *parameterFile)
405 {  
406   // Note: this method is a bit more complicated than it really has to be
407   // - allowing for multiple entries per line, arbitrary order of the
408   // different variables etc. But I wanted to try and do this in as
409   // correct a C++ way as I could (as an exercise).
410
411   static const string delimitor("::");
412         
413   // open, check input file
414   ifstream in( parameterFile );
415   if( !in ) {
416     printf("in AliCaloCalibPedestal::SetParametersFromFile - Using default/run_time parameters.\n");
417     return;
418   } 
419
420
421   // read in
422   char readline[1024];
423   while ((in.rdstate() & ios::failbit) == 0 ) {
424     
425     // Read into the raw char array and then construct a string
426     // to do the searching
427     in.getline(readline, 1024);
428     istringstream s(readline);          
429                 
430     while ( ( s.rdstate() & ios::failbit ) == 0 ) {
431                         
432       string keyValue; 
433       s >> keyValue;
434       
435       // check stream status
436       if( s.rdstate() & ios::failbit ) break;
437                         
438       // skip rest of line if comments found
439       if( keyValue.substr( 0, 2 ) == "//" ) break;
440                         
441       // look for "::" in keyValue pair
442       size_t position = keyValue.find( delimitor );
443       if( position == string::npos ) {
444         printf("wrong format for key::value pair: %s\n", keyValue.c_str());
445       }
446                                 
447       // split keyValue pair
448       string key( keyValue.substr( 0, position ) );
449       string value( keyValue.substr( position+delimitor.size(), 
450                                       keyValue.size()-delimitor.size() ) );
451                         
452       // check value does not contain a new delimitor
453       if( value.find( delimitor ) != string::npos ) {
454         printf("wrong format for key::value pair: %s\n", keyValue.c_str());
455       }
456       
457       // debug: check key value pair
458       // printf("AliCaloCalibPedestal::SetParametersFromFile - key %s value %s\n", key.c_str(), value.c_str());
459
460       // if the key matches with something we expect, we assign the new value
461       istringstream iss(value);
462       // the comparison strings defined at the beginning of this method
463       if ( (key == "fFirstPedestalSample") || (key == "fLastPedestalSample") || (key == "fDeadThreshold") || (key == "fWarningThreshold") || (key == "fWarningFraction") || (key == "fHotSigma") ) {
464         printf("AliCaloCalibPedestal::SetParametersFromFile - key %s value %s\n", key.c_str(), value.c_str());
465
466         if (key == "fFirstPedestalSample") { 
467           iss >> fFirstPedestalSample; 
468         }
469         else if (key == "fLastPedestalSample") { 
470           iss >> fLastPedestalSample; 
471         }
472         else if (key == "fDeadThreshold") { 
473           iss >> fDeadThreshold; 
474         }
475         else if (key == "fWarningThreshold") { 
476           iss >> fWarningThreshold; 
477         }
478         else if (key == "fWarningFraction") { 
479           iss >> fWarningFraction; 
480         }
481         else if (key == "fHotSigma") { 
482           iss >> fHotSigma; 
483         }
484
485       } // some match
486
487     }           
488   }
489
490   in.close();
491   return;
492         
493 }
494
495 //_____________________________________________________________________
496 void AliCaloCalibPedestal::WriteParametersToFile(const char *parameterFile)
497 {
498   //Write parameters in file.
499         
500   static const string delimitor("::");
501   ofstream out( parameterFile );
502   out << "// " << parameterFile << endl;
503   out << "fFirstPedestalSample" << "::" << fFirstPedestalSample << endl;
504   out << "fLastPedestalSample" << "::" << fLastPedestalSample << endl;
505   out << "fDeadThreshold" << "::" << fDeadThreshold << endl;
506   out << "fWarningThreshold" << "::" << fWarningThreshold << endl;
507   out << "fWarningFraction" << "::" << fWarningFraction << endl;
508   out << "fHotSigma" << "::" << fHotSigma << endl;
509
510   out.close();
511   return;
512 }
513
514 //_____________________________________________________________________
515 Bool_t AliCaloCalibPedestal::AddInfo(AliCaloCalibPedestal *ped)
516 {
517   ValidateProfiles(); // make sure histos/profiles exist
518   // just do this for the basic histograms/profiles that get filled in ProcessEvent
519   // may not have data for all modules, but let's just Add everything..
520   for (int i = 0; i < fModules; i++) {
521     GetPedProfileLowGain(i)->Add( ped->GetPedProfileLowGain(i) );
522     GetPedProfileHighGain(i)->Add( ped->GetPedProfileHighGain(i) );
523     GetPeakProfileLowGain(i)->Add( ped->GetPeakProfileLowGain(i) );
524     GetPeakProfileHighGain(i)->Add( ped->GetPeakProfileHighGain(i) );
525     GetPeakHighGainHisto(i)->Add( ped->GetPeakHighGainHisto(i) );
526
527   }//end for nModules 
528
529   // DeadMap; Diff profiles etc would need to be redone after this operation
530
531   return kTRUE;//We succesfully added info from the supplied object
532 }
533
534 //_____________________________________________________________________
535 Bool_t AliCaloCalibPedestal::ProcessEvent(AliRawReader *rawReader)
536
537   // if fMapping is NULL the rawstream will crate its own mapping
538   AliCaloRawStreamV3 rawStream(rawReader, fCaloString, (AliAltroMapping**)fMapping);
539   if (fDetType == kEmCal) {
540     rawReader->Select("EMCAL", 0, AliEMCALGeoParams::fgkLastAltroDDL) ; //select EMCAL DDL range 
541   }
542   return ProcessEvent(&rawStream);
543 }
544
545 //_____________________________________________________________________
546 Bool_t AliCaloCalibPedestal::ProcessEvent(AliCaloRawStreamV3 *in)
547
548   // Method to process=analyze one event in the data stream
549   if (!in) return kFALSE; //Return right away if there's a null pointer
550   fNEvents++; // one more event
551
552   if (fNEvents==1) ValidateProfiles(); // 1st event, make sure histos/profiles exist
553   
554   // indices for the reading
555   int sample = 0;
556   int time = 0;
557   int i = 0; // sample counter
558   int startBin = 0;
559
560   // start loop over input stream 
561   while (in->NextDDL()) {
562     while (in->NextChannel()) {
563
564       // counters
565       int max = AliEMCALGeoParams::fgkSampleMin, min = AliEMCALGeoParams::fgkSampleMax; // min and max sample values
566       int nsamples = 0;
567
568       // pedestal samples
569       int nPed = 0;
570       vector<int> pedSamples; 
571
572       while (in->NextBunch()) {
573         const UShort_t *sig = in->GetSignals();
574         startBin = in->GetStartTimeBin();
575         nsamples += in->GetBunchLength();
576         for (i = 0; i < in->GetBunchLength(); i++) {
577           sample = sig[i];
578           time = startBin--;
579
580           // check if it's a min or max value
581           if (sample < min) min = sample;
582           if (sample > max) max = sample;
583           
584           // should we add it for the pedestal calculation?
585           if ( (fFirstPedestalSample<=time && time<=fLastPedestalSample) || // sample time in range
586                !fSelectPedestalSamples ) { // or we don't restrict the sample range.. - then we'll take all 
587             pedSamples.push_back( sig[i] );
588             nPed++;
589           }
590           
591         } // loop over samples in bunch
592       } // loop over bunches
593
594       if (nsamples > 0) { // this check is needed for when we have zero-supp. on, but not sparse readout
595
596       // it should be enough to check the SuperModule info for each DDL really, but let's keep it here for now
597       int arrayPos = in->GetModule(); //The modules are numbered starting from 0
598       if (arrayPos >= fModules) {
599         //TODO: return an error message, if appopriate (perhaps if debug>0?)
600         return kFALSE;
601       }     
602       //Debug
603       if (arrayPos < 0 || arrayPos >= fModules) {
604         printf("Oh no: arrayPos = %i.\n", arrayPos); 
605       }
606       
607       fNChanFills++; // one more channel found, and profile to be filled
608       //NOTE: coordinates are (column, row) for the profiles
609       if ( in->IsLowGain() ) {
610         //fill the low gain histograms
611         ((TProfile2D*)fPeakMinusPedLowGain[arrayPos])->Fill(in->GetColumn(), fRowMultiplier*in->GetRow(), max - min);
612         if (nPed>0) { // only fill pedestal info in case it could be calculated
613           for ( i=0; i<nPed; i++) {
614             ((TProfile2D*)fPedestalLowGain[arrayPos])->Fill(in->GetColumn(), fRowMultiplier*in->GetRow(), pedSamples[i]); 
615           }
616         }
617       } 
618       else if ( in->IsHighGain() ) {    
619         //fill the high gain ones
620         ((TProfile2D*)fPeakMinusPedHighGain[arrayPos])->Fill(in->GetColumn(), fRowMultiplier*in->GetRow(), max - min);
621         if (nPed>0) { // only fill pedestal info in case it could be calculated
622           for ( i=0; i<nPed; i++) {
623             ((TProfile2D*)fPedestalHighGain[arrayPos])->Fill(in->GetColumn(), fRowMultiplier*in->GetRow(), pedSamples[i]); 
624           }       
625         }
626         // for warning checks
627         int idx = in->GetRow() + fRows * in->GetColumn();
628         ((TH2F*)fPeakMinusPedHighGainHisto[arrayPos])->Fill(idx, max - min);
629       } 
630       else if ( in->IsLEDMonData() ) {
631         // for LED Mon data, the mapping class holds the gain info in the Row variable
632         // and the Strip number in the Column..
633         int gain = in->GetRow(); 
634         int stripId = in->GetColumn();
635         if (nPed>0 && stripId<fLEDRefs) {
636           if (gain == 0) {
637             for ( i=0; i<nPed; i++) {
638               ((TProfile*)fPedestalLEDRefLowGain[arrayPos])->Fill(stripId, pedSamples[i]);
639             }
640           }
641           else {
642             for ( i=0; i<nPed; i++) {
643               ((TProfile*)fPedestalLEDRefHighGain[arrayPos])->Fill(stripId, pedSamples[i]);
644             }
645           }
646         }
647       }
648
649       } // nsamples>0 check, some data found for this channel; not only trailer/header
650     }// end while over channel   
651   }//end while over DDL's, of input stream 
652
653   in->Reset(); // just in case the next customer forgets to check if the stream was reset..
654  
655   return kTRUE;
656 }
657
658 //_____________________________________________________________________
659 Bool_t AliCaloCalibPedestal::SaveHistograms(TString fileName, Bool_t saveEmptyHistos)
660 {
661   //Saves all the histograms (or profiles, to be accurate) to the designated file
662   ValidateProfiles(); // make sure histos/profiles exist
663   TFile destFile(fileName, "recreate");
664   
665   if (destFile.IsZombie()) {
666     return kFALSE;
667   }
668   
669   destFile.cd();
670   
671   for (int i = 0; i < fModules; i++) {
672     if( ((TProfile2D *)fPeakMinusPedLowGain[i])->GetEntries() || saveEmptyHistos) {
673       fPeakMinusPedLowGain[i]->Write();
674     }
675     if( ((TProfile2D *)fPeakMinusPedHighGain[i])->GetEntries() || saveEmptyHistos) { 
676       fPeakMinusPedHighGain[i]->Write();
677     }
678     if( ((TProfile2D *)fPedestalLowGain[i])->GetEntries() || saveEmptyHistos) {
679       fPedestalLowGain[i]->Write();
680     }
681     if( ((TProfile2D *)fPedestalHighGain[i])->GetEntries() || saveEmptyHistos) {
682       fPedestalHighGain[i]->Write();
683     }
684     if( ((TProfile *)fPedestalLEDRefLowGain[i])->GetEntries() || saveEmptyHistos) {
685       fPedestalLEDRefLowGain[i]->Write();
686     }
687     if( ((TProfile *)fPedestalLEDRefHighGain[i])->GetEntries() || saveEmptyHistos) {
688       fPedestalLEDRefHighGain[i]->Write();
689     }
690     if( ((TH2F *)fPeakMinusPedHighGainHisto[i])->GetEntries() || saveEmptyHistos) { 
691       fPeakMinusPedHighGainHisto[i]->Write();
692     }
693
694   } 
695   
696   destFile.Close();
697   
698   return kTRUE;
699 }
700
701 //_____________________________________________________________________
702 Bool_t AliCaloCalibPedestal::LoadReferenceCalib(TString fileName, TString objectName)
703 {
704   
705   //Make sure that the histograms created when loading the object are not destroyed as the file object is destroyed
706   TH1::AddDirectory(kFALSE);
707   
708   TFile *sourceFile = new TFile(fileName);
709   if (sourceFile->IsZombie()) {
710     return kFALSE;//We couldn't load the reference
711   }
712
713   if (fReference) delete fReference;//Delete the reference object, if it already exists
714   fReference = 0;
715   
716   fReference = (AliCaloCalibPedestal*)sourceFile->Get(objectName);
717  
718   if (!fReference || !(fReference->InheritsFrom(AliCaloCalibPedestal::Class())) || (fReference->GetDetectorType() != fDetType)) {
719     if (fReference) delete fReference;//Delete the object, in case we had an object of the wrong type
720     fReference = 0;
721     return kFALSE;
722   }
723         
724   delete sourceFile;
725
726   //Reset the histogram ownership behaviour. NOTE: a better workaround would be good, since this may accidentally set AddDirectory to true, even
727   //if we are called by someone who has set it to false...
728   TH1::AddDirectory(kTRUE);
729  
730   return kTRUE;//We succesfully loaded the object
731 }
732
733
734 //_____________________________________________________________________
735 Bool_t AliCaloCalibPedestal::SetReference(AliCaloCalibPedestal *ref)
736 {
737   if (fReference) delete fReference;//Delete the reference object, if it already exists
738   fReference = 0;
739   
740   fReference = ref;
741  
742   if (!fReference || (fReference->GetDetectorType() != fDetType)) {
743     if (fReference) delete fReference;//Delete the object, in case we had an object of the wrong type
744     fReference = 0;
745     return kFALSE;
746   }
747
748   return kTRUE;//We succesfully loaded the object
749 }
750
751 //_____________________________________________________________________
752 void AliCaloCalibPedestal::ValidateComparisonProfiles()
753 {
754   //Make sure the comparison histos exist
755   if (!fPedestalLowGainDiff.IsEmpty()) return; //The profiles already exist. We just check one, because they're all created at
756   //the same time
757                                                 
758                                                 
759   //Then, loop for the requested number of modules
760   TString title, name;
761   for (int i = 0; i < fModules; i++) {
762     //Pedestals, low gain
763     name = "hPedlowgainDiff";
764     name += i;
765     title = "Pedestals difference, low gain, module ";
766     title += i; 
767     fPedestalLowGainDiff.Add(new TProfile2D(name, title,
768                                             fColumns, 0.0, fColumns, 
769                                             fRows, fRowMin, fRowMax,"s"));
770   
771     //Pedestals, high gain
772     name = "hPedhighgainDiff";
773     name += i;
774     title = "Pedestals difference, high gain, module ";
775     title += i; 
776     fPedestalHighGainDiff.Add(new TProfile2D(name, title,
777                                              fColumns, 0.0, fColumns, 
778                                              fRows, fRowMin, fRowMax,"s"));
779
780     //LED Ref/Mon pedestals, low gain
781     name = "hPedestalLEDReflowgainDiff";
782     name += i;
783     title = "Pedestal difference LEDRef, low gain, module ";
784     title += i; 
785     fPedestalLEDRefLowGainDiff.Add(new TProfile(name, title,
786                                                 fLEDRefs, 0.0, fLEDRefs, "s"));
787     
788     //LED Ref/Mon pedestals, high gain
789     name = "hPedestalLEDRefhighgainDiff";
790     name += i;
791     title = "Pedestal difference LEDRef, high gain, module ";
792     title += i; 
793     fPedestalLEDRefHighGainDiff.Add(new TProfile(name, title,
794                                                  fLEDRefs, 0.0, fLEDRefs, "s"));
795
796     //Peak-Pedestals, high gain
797     name = "hPeakMinusPedhighgainDiff";
798     name += i;
799     title = "Peak-Pedestal difference, high gain, module ";
800     title += i; 
801     fPeakMinusPedHighGainDiff.Add(new TProfile2D(name, title,
802                                                  fColumns, 0.0, fColumns, 
803                                                  fRows, fRowMin, fRowMax,"s"));
804
805     //Peak-Pedestals, low gain
806     name = "hPeakMinusPedlowgainDiff";
807     name += i;
808     title = "Peak-Pedestal difference, low gain, module ";
809     title += i; 
810     fPeakMinusPedLowGainDiff.Add(new TProfile2D(name, title,
811                                                 fColumns, 0.0, fColumns, 
812                                                 fRows, fRowMin, fRowMax,"s"));
813   
814     //Pedestals, low gain
815     name = "hPedlowgainRatio";
816     name += i;
817     title = "Pedestals ratio, low gain, module ";
818     title += i; 
819     fPedestalLowGainRatio.Add(new TProfile2D(name, title,
820                                              fColumns, 0.0, fColumns, 
821                                              fRows, fRowMin, fRowMax,"s"));
822   
823     //Pedestals, high gain
824     name = "hPedhighgainRatio";
825     name += i;
826     title = "Pedestals ratio, high gain, module ";
827     title += i; 
828     fPedestalHighGainRatio.Add(new TProfile2D(name, title,
829                                               fColumns, 0.0, fColumns, 
830                                               fRows, fRowMin, fRowMax,"s"));
831
832     //LED Ref/Mon pedestals, low gain
833     name = "hPedestalLEDReflowgainRatio";
834     name += i;
835     title = "Pedestal ratio LEDRef, low gain, module ";
836     title += i; 
837     fPedestalLEDRefLowGainRatio.Add(new TProfile(name, title,
838                                                  fLEDRefs, 0.0, fLEDRefs, "s"));
839     
840     //LED Ref/Mon pedestals, high gain
841     name = "hPedestalLEDRefhighgainRatio";
842     name += i;
843     title = "Pedestal ratio LEDRef, high gain, module ";
844     title += i; 
845     fPedestalLEDRefHighGainRatio.Add(new TProfile(name, title,
846                                                   fLEDRefs, 0.0, fLEDRefs, "s"));
847   
848     //Peak-Pedestals, low gain
849     name = "hPeakMinusPedlowgainRatio";
850     name += i;
851     title = "Peak-Pedestal ratio, low gain, module ";
852     title += i; 
853     fPeakMinusPedLowGainRatio.Add(new TProfile2D(name, title,
854                                                  fColumns, 0.0, fColumns, 
855                                                  fRows, fRowMin, fRowMax,"s"));
856   
857     //Peak-Pedestals, high gain
858     name = "hPeakMinusPedhighgainRatio";
859     name += i;
860     title = "Peak-Pedestal ratio, high gain, module ";
861     title += i; 
862     fPeakMinusPedHighGainRatio.Add(new TProfile2D(name, title,
863                                                   fColumns, 0.0, fColumns, 
864                                                   fRows, fRowMin, fRowMax,"s"));
865     
866   }//end for nModules create the histograms
867 }
868
869 //_____________________________________________________________________
870 void AliCaloCalibPedestal::ComputeDiffAndRatio()
871 {
872   // calculate differences and ratios relative to a reference
873   ValidateProfiles(); // make sure histos/profiles exist
874   ValidateComparisonProfiles();//Make sure the comparison histos exist
875  
876   if (!fReference) {
877     return;//Return if the reference object isn't loaded
878   }
879
880   int bin = 0;
881   double diff = 0;
882   double ratio = 1;
883   for (int i = 0; i < fModules; i++) {
884     //For computing the difference, we cannot simply do TProfile2D->Add(), because that subtracts the sum of all entries,
885     //which means that the mean of the new profile will not be the difference of the means. So do it by hand:
886     for (int j = 0; j < fColumns; j++) {
887       for (int k = 0; k < fRows; k++) {
888         bin = ((TProfile2D*)fPeakMinusPedHighGainDiff[i])->GetBin(j+1, k+1);//Note that we assume here that all histos have the same structure...
889
890         if (fReference->GetPeakProfileHighGain(i)->GetBinContent(bin) > 0) {
891           diff = GetPeakProfileHighGain(i)->GetBinContent(bin) - fReference->GetPeakProfileHighGain(i)->GetBinContent(bin);
892           ((TProfile2D*)fPeakMinusPedHighGainDiff[i])->SetBinContent(bin, diff);
893           ((TProfile2D*)fPeakMinusPedHighGainDiff[i])->SetBinEntries(bin, 1);
894           ratio = GetPeakProfileHighGain(i)->GetBinContent(bin) / fReference->GetPeakProfileHighGain(i)->GetBinContent(bin);  
895           ((TProfile2D*)fPeakMinusPedHighGainRatio[i])->SetBinContent(bin, ratio);
896           ((TProfile2D*)fPeakMinusPedHighGainRatio[i])->SetBinEntries(bin, 1);
897         }
898
899         if (fReference->GetPeakProfileLowGain(i)->GetBinContent(bin) > 0) {
900           diff = GetPeakProfileLowGain(i)->GetBinContent(bin) - fReference->GetPeakProfileLowGain(i)->GetBinContent(bin);
901           ((TProfile2D*)fPeakMinusPedLowGainDiff[i])->SetBinContent(bin, diff);
902           ((TProfile2D*)fPeakMinusPedLowGainDiff[i])->SetBinEntries(bin, 1);
903           ratio = GetPeakProfileLowGain(i)->GetBinContent(bin) / fReference->GetPeakProfileLowGain(i)->GetBinContent(bin);  
904           ((TProfile2D*)fPeakMinusPedLowGainRatio[i])->SetBinContent(bin, ratio);
905           ((TProfile2D*)fPeakMinusPedLowGainRatio[i])->SetBinEntries(bin, 1);
906         }
907
908         if (fReference->GetPedProfileHighGain(i)->GetBinContent(bin) > 0) {
909           diff = GetPedProfileHighGain(i)->GetBinContent(bin) - fReference->GetPedProfileHighGain(i)->GetBinContent(bin);
910           ((TProfile2D*)fPedestalHighGainDiff[i])->SetBinContent(bin, diff);
911           ((TProfile2D*)fPedestalHighGainDiff[i])->SetBinEntries(bin, 1);
912           ratio = GetPedProfileHighGain(i)->GetBinContent(bin) / fReference->GetPedProfileHighGain(i)->GetBinContent(bin);  
913           ((TProfile2D*)fPedestalHighGainRatio[i])->SetBinContent(bin, ratio);
914           ((TProfile2D*)fPedestalHighGainRatio[i])->SetBinEntries(bin, 1);
915         }
916
917         if (fReference->GetPedProfileLowGain(i)->GetBinContent(bin) > 0) {
918           diff = GetPedProfileLowGain(i)->GetBinContent(bin) - fReference->GetPedProfileLowGain(i)->GetBinContent(bin);
919           ((TProfile2D*)fPedestalLowGainDiff[i])->SetBinContent(bin, diff);
920           ((TProfile2D*)fPedestalLowGainDiff[i])->SetBinEntries(bin, 1);
921           ratio = GetPedProfileLowGain(i)->GetBinContent(bin) / fReference->GetPedProfileLowGain(i)->GetBinContent(bin);  
922           ((TProfile2D*)fPedestalLowGainRatio[i])->SetBinContent(bin, ratio);
923           ((TProfile2D*)fPedestalLowGainRatio[i])->SetBinEntries(bin, 1);
924         }
925
926       } // rows
927     } // columns
928
929     // same for LED Ref/Mon channels
930     for (int j = 0; j <= fLEDRefs; j++) {    
931       bin = j+1;//Note that we assume here that all histos have the same structure...
932
933       if (fReference->GetPedLEDRefProfileHighGain(i)->GetBinContent(bin) > 0) {
934         diff = GetPedLEDRefProfileHighGain(i)->GetBinContent(bin) - fReference->GetPedLEDRefProfileHighGain(i)->GetBinContent(bin);
935         ((TProfile*)fPedestalLEDRefHighGainDiff[i])->SetBinContent(bin, diff);
936         ((TProfile*)fPedestalLEDRefHighGainDiff[i])->SetBinEntries(bin, 1);
937         ratio = GetPedLEDRefProfileHighGain(i)->GetBinContent(bin) / fReference->GetPedLEDRefProfileHighGain(i)->GetBinContent(bin);  
938         ((TProfile*)fPedestalLEDRefHighGainRatio[i])->SetBinContent(bin, ratio);
939         ((TProfile*)fPedestalLEDRefHighGainRatio[i])->SetBinEntries(bin, 1);
940       }
941
942       if (fReference->GetPedLEDRefProfileLowGain(i)->GetBinContent(bin) > 0) {
943         diff = GetPedLEDRefProfileLowGain(i)->GetBinContent(bin) - fReference->GetPedLEDRefProfileLowGain(i)->GetBinContent(bin);
944         ((TProfile*)fPedestalLEDRefLowGainDiff[i])->SetBinContent(bin, diff);
945         ((TProfile*)fPedestalLEDRefLowGainDiff[i])->SetBinEntries(bin, 1);
946         ratio = GetPedLEDRefProfileLowGain(i)->GetBinContent(bin) / fReference->GetPedLEDRefProfileLowGain(i)->GetBinContent(bin);  
947         ((TProfile*)fPedestalLEDRefLowGainRatio[i])->SetBinContent(bin, ratio);
948         ((TProfile*)fPedestalLEDRefLowGainRatio[i])->SetBinEntries(bin, 1);
949       } 
950      
951     }
952
953   } // modules
954  
955 }
956
957 //_____________________________________________________________________
958 void AliCaloCalibPedestal::ComputeHotAndWarningTowers(const char * hotMapFile)
959 { // look for hot/noisy towers
960   ValidateProfiles(); // make sure histos/profiles exist
961   ofstream * fout = 0;
962   char name[512];//Quite a long temp buffer, just in case the filename includes a path
963
964   if (hotMapFile) {
965     snprintf(name, 512, "%s.txt", hotMapFile);
966     fout = new ofstream(name);
967     if (!fout->is_open()) {
968       delete fout;
969       fout = 0;//Set the pointer to empty if the file was not opened
970     }
971   }
972  
973   for(int i = 0; i < fModules; i++){
974                 
975     //first we compute the peak-pedestal distribution for each supermodule...
976     if( GetPeakHighGainHisto(i)->GetEntries() > 0 ) {
977       double min = GetPeakProfileHighGain(i)->GetBinContent(GetPeakProfileHighGain(i)->GetMinimumBin());
978       double max = GetPeakProfileHighGain(i)->GetBinContent(GetPeakProfileHighGain(i)->GetMaximumBin());
979       TH1D *hPeakFit = new TH1D(Form("hFit_%d", i), Form("hFit_%d", i), (int)((max-min)*10), min-1, max+1);
980
981       for (int j = 1; j <= fColumns; j++) {
982         for (int k = 1; k <= fRows; k++) {        
983           hPeakFit->Fill(GetPeakProfileHighGain(i)->GetBinContent(j, k));
984         }
985       }
986
987       //...and then we try to fit it
988       double mean  = hPeakFit->GetMean();
989       double sigma = hPeakFit->GetRMS();
990       try {
991         hPeakFit->Fit("gaus", "OQ", "",  mean - 3*sigma, mean + 3*sigma);
992         mean  = hPeakFit->GetFunction("gaus")->GetParameter(1);
993         sigma = hPeakFit->GetFunction("gaus")->GetParameter(2);
994       }
995       catch (const std::exception & e) {
996         printf("AliCaloCalibPedestal: TH1D PeakFit exception %s", e.what()); 
997       }      
998       //hPeakFit->Draw();
999
1000       delete hPeakFit;
1001
1002       //Then we look for warm/hot towers
1003       TH2F * hPeak2D = GetPeakHighGainHisto(i);
1004       hPeak2D->GetYaxis()->SetRangeUser( fWarningThreshold, hPeak2D->GetYaxis()->GetBinUpEdge(hPeak2D->GetNbinsY()) );
1005
1006       int idx = 0 ;
1007       int warnCounter = 0;
1008       for (int j = 1; j <= fColumns; j++) {
1009         for (int k = 1; k <= fRows; k++) {                              
1010           //we start looking for warm/warning towers...
1011           // histogram x-axis index
1012           idx = k-1 + fRows*(j-1); // this is what is used in the Fill call
1013           hPeak2D->GetXaxis()->SetRangeUser(idx, idx);
1014           warnCounter = (int) hPeak2D->Integral();
1015           if(warnCounter > fNEvents * fWarningFraction) {
1016             ((TH2D*)fDeadMap[i])->SetBinContent(j, k, kWarning);            
1017             /* printf("mod %d col %d row %d warnCounter %d - status %d\n", 
1018                i, j-1, k-1, warnCounter, (int) (kWarning)); */  
1019           }
1020           //...then we look for hot ones (towers whose values are greater than mean + X*sigma)  
1021           if(GetPeakProfileHighGain(i)->GetBinContent(j, k) > mean + fHotSigma*sigma ) {
1022             ((TH2D*)fDeadMap[i])->SetBinContent(j, k, kHot); 
1023             /* printf("mod %d col %d row %d  binc %d - status %d\n", 
1024                i, j-1, k-1, (int)(GetPeakProfileHighGain(i)->GetBinContent(j, k)), (int) (kHot)); */      
1025           }
1026
1027           //Write the status to the hot/warm map file, if the file is open.
1028           // module - column - row - status (1=dead, 2= warm/warning , 3 = hot, see .h file enum)
1029           if (fout && ((TH2D*)fDeadMap[i])->GetBinContent(j, k) > 1) {
1030             
1031             (*fout) << i << " " 
1032                     << (j - 1) << " " 
1033                     << (k - 1) << " " 
1034                     << ((TH2D*)fDeadMap[i])->GetBinContent(j, k) << endl;               }
1035           
1036         }
1037       }
1038
1039     } 
1040   }
1041   return;
1042 }
1043
1044 //_____________________________________________________________________
1045 void AliCaloCalibPedestal::ComputeDeadTowers(const char * deadMapFile)
1046 {
1047   ValidateProfiles(); // make sure histos/profiles exist
1048   //Computes the number of dead towers etc etc into memory, after this you can call the GetDead... -functions
1049   int countTot = 0;
1050   int countNew = 0;
1051   int countRes = 0;
1052   ofstream * fout = 0;
1053   ofstream * diff = 0;
1054   char name[512];//Quite a long temp buffer, just in case the filename includes a path
1055   
1056   if (deadMapFile) {
1057     snprintf(name, 512, "%s.txt", deadMapFile);
1058     fout = new ofstream(name);
1059     snprintf(name, 512, "%sdiff.txt", deadMapFile);
1060     diff = new ofstream(name);
1061     if (!fout->is_open()) {
1062       delete fout;
1063       fout = 0;//Set the pointer to empty if the file was not opened
1064     }
1065     if (!diff->is_open()) {
1066       delete diff;
1067       fout = 0;//Set the pointer to empty if the file was not opened
1068     }
1069   }
1070  
1071   for (int i = 0; i < fModules; i++) {
1072     if (GetPeakProfileHighGain(i)->GetEntries() > 0) { //don't care about empty histos
1073       for (int j = 1; j <= fColumns; j++) {
1074         for (int k = 1; k <= fRows; k++) {
1075
1076           if (GetPeakProfileHighGain(i)->GetBinContent(j, k) < fDeadThreshold) {//It's dead
1077             countTot++;//One more dead total
1078             if (fout) {
1079               (*fout) << i << " " 
1080                       << (j - 1) << " " 
1081                       << (k - 1) << " " 
1082                       << "1" << " " 
1083                       << "0" << endl;//Write the status to the deadmap file, if the file is open.
1084             }
1085             
1086             if (fReference && fReference->GetPeakProfileHighGain(i)->GetBinContent(j, k) >= fDeadThreshold) {
1087               ((TH2D*)fDeadMap[i])->SetBinContent(j, k, kRecentlyDeceased); 
1088               countNew++;//This tower wasn't dead before!
1089               if (diff) {
1090                 ( *diff) << i << " " 
1091                          << (j - 1) << " " 
1092                          << (k - 1) << " " 
1093                          << "1" << " " 
1094                          << "0" << endl;//Write the status to the deadmap difference file, if the file is open.
1095               }
1096             } 
1097             else {
1098               ((TH2D*)fDeadMap[i])->SetBinContent(j, k, kDead);//This has been dead before. Nothing new         
1099             }
1100           } 
1101           else { //It's ALIVE!!
1102             //Don't bother with writing the live ones.
1103             if (fReference && fReference->GetPeakProfileHighGain(i)->GetBinContent(j, k) < fDeadThreshold) {
1104               ((TH2D*)fDeadMap[i])->SetBinContent(j, k, kResurrected);
1105               countRes++; //This tower was dead before => it's a miracle! :P
1106               if (diff) {
1107                 (*diff) << i << " " 
1108                         << (j - 1) << " " 
1109                         << (k - 1) << " " 
1110                         << "1" << " " 
1111                         << "1" << endl;//Write the status to the deadmap difference file, if the file is open.
1112               }
1113             } 
1114             else {
1115               ((TH2D*)fDeadMap[i])->SetBinContent(j, k, kAlive);
1116             }
1117           }
1118             
1119         }//end for k/rows
1120       }//end for j/columns
1121     }//end if GetEntries >= 0
1122   
1123   }//end for modules
1124  
1125  if (fout) {
1126    fout->close();
1127    delete fout;
1128  }
1129  
1130  fDeadTowers = countTot;
1131  fNewDeadTowers = countNew;
1132  fResurrectedTowers = countRes;
1133 }
1134
1135 //_____________________________________________________________________
1136 Bool_t AliCaloCalibPedestal::IsBadChannel(int imod, int icol, int irow) const
1137 {
1138   // Check if status info histo exists
1139   if (!fDeadMap[imod]) { 
1140     return kFALSE;
1141   }
1142
1143   //Check if channel is dead or hot.  
1144   Int_t status =  (Int_t) ( ((TH2D*)fDeadMap[imod])->GetBinContent(icol,irow) );
1145   if(status == kAlive)
1146     return kFALSE;
1147   else 
1148     return kTRUE;
1149   
1150 }
1151
1152 //_____________________________________________________________________
1153 void AliCaloCalibPedestal::SetChannelStatus(int imod, int icol, int irow, int status)
1154 {
1155   ValidateProfiles(); // make sure histos/profiles exist
1156   //Set status of channel dead, hot, alive ...  
1157   ((TH2D*)fDeadMap[imod])->SetBinContent(icol, irow, status);   
1158 }