]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - EMCAL/AliCaloCalibPedestal.cxx
new file to test SDigits
[u/mrichter/AliRoot.git] / EMCAL / AliCaloCalibPedestal.cxx
index d01e33246b160c964c75cd7670602b513d0678f9..fc1609c999d57e38472cbbc7cf2bc3b911f092f4 100644 (file)
@@ -12,7 +12,7 @@
  * about the suitability of this software for any purpose. It is          *
  * provided "as is" without express or implied warranty.                  *
  **************************************************************************/
-/* $Id$ */
+//* $Id$ */
 
 //________________________________________________________________________
 //
 // The pseudo-code examples above were from the first implementation in MOOD (summer 2007).
 //________________________________________________________________________
 
+//#include "TCanvas.h"
 #include "TH1.h"
+#include "TF1.h"
 #include "TFile.h"
 #include <fstream>
+#include <sstream>
 #include <iostream>
+#include <stdexcept>
 #include <cmath>
 
+#include "AliRawReader.h"
 #include "AliCaloRawStreamV3.h"
 
 //The include file
@@ -54,16 +59,21 @@ AliCaloCalibPedestal::AliCaloCalibPedestal(kDetType detectorType) :
   TObject(),
   fPedestalLowGain(),
   fPedestalHighGain(),
-  fPedestalRMSLowGain(),
-  fPedestalRMSHighGain(),
+  fPedestalLEDRefLowGain(),
+  fPedestalLEDRefHighGain(),
   fPeakMinusPedLowGain(),
   fPeakMinusPedHighGain(),
+  fPeakMinusPedHighGainHisto(),
   fPedestalLowGainDiff(),
   fPedestalHighGainDiff(),
+  fPedestalLEDRefLowGainDiff(),
+  fPedestalLEDRefHighGainDiff(),
   fPeakMinusPedLowGainDiff(),
   fPeakMinusPedHighGainDiff(),
   fPedestalLowGainRatio(),
   fPedestalHighGainRatio(),
+  fPedestalLEDRefLowGainRatio(),
+  fPedestalLEDRefHighGainRatio(),
   fPeakMinusPedLowGainRatio(),
   fPeakMinusPedHighGainRatio(),
   fDeadMap(),
@@ -76,6 +86,7 @@ AliCaloCalibPedestal::AliCaloCalibPedestal(kDetType detectorType) :
   fDetType(kNone),
   fColumns(0),
   fRows(0),
+  fLEDRefs(0),
   fModules(0),
   fRowMin(0),
   fRowMax(0),
@@ -85,12 +96,17 @@ AliCaloCalibPedestal::AliCaloCalibPedestal(kDetType detectorType) :
   fRunNumber(-1),
   fSelectPedestalSamples(kTRUE), 
   fFirstPedestalSample(0),
-  fLastPedestalSample(15)
+  fLastPedestalSample(15),
+  fDeadThreshold(5),
+  fWarningThreshold(50),
+  fWarningFraction(0.002),
+  fHotSigma(5)
 {
   //Default constructor. First we set the detector-type related constants.
   if (detectorType == kPhos) {
     fColumns = fgkPhosCols;
     fRows = fgkPhosRows;
+    fLEDRefs = fgkPhosLEDRefs;
     fModules = fgkPhosModules;
     fCaloString = "PHOS";
     fRowMin = -1*fRows;
@@ -101,16 +117,27 @@ AliCaloCalibPedestal::AliCaloCalibPedestal(kDetType detectorType) :
     //We'll just trust the enum to keep everything in line, so that if detectorType
     //isn't kPhos then it is kEmCal. Note, however, that this is not necessarily the
     //case, if someone intentionally gives another number
-    fColumns = fgkEmCalCols;
-    fRows = fgkEmCalRows;
-    fModules = fgkEmCalModules;
+    fColumns = AliEMCALGeoParams::fgkEMCALCols;
+    fRows = AliEMCALGeoParams::fgkEMCALRows;
+    fLEDRefs = AliEMCALGeoParams::fgkEMCALLEDRefs;
+    fModules = AliEMCALGeoParams::fgkEMCALModules;
     fCaloString = "EMCAL";
     fRowMin = 0;
     fRowMax = fRows;
     fRowMultiplier = 1;
   } 
   fDetType = detectorType;
+
+  // ValidateProfiles(); // not to be done in ctor; info from Axel N. 
+}
+
+//_____________________________________________________________________
+void AliCaloCalibPedestal::ValidateProfiles()
+{
+  //Make sure the basic histos exist
+  if (!fPedestalLowGain.IsEmpty()) return; //The profiles already exist. We just check one, because they're all created at
+  //the same time
+
   //Then, loop for the requested number of modules
   TString title, name;
   for (int i = 0; i < fModules; i++) {
@@ -131,23 +158,22 @@ AliCaloCalibPedestal::AliCaloCalibPedestal(kDetType detectorType) :
     fPedestalHighGain.Add(new TProfile2D(name, title,
                                         fColumns, 0.0, fColumns, 
                                         fRows, fRowMin, fRowMax,"s"));
-    //All Samples, low gain
-    name = "hPedestalRMSlowgain";
+
+    //LED Ref/Mon pedestals, low gain
+    name = "hPedestalLEDReflowgain";
     name += i;
-    title = "Pedestal RMS, low gain, module ";
+    title = "Pedestal LEDRef, low gain, module ";
     title += i; 
-    fPedestalRMSLowGain.Add(new TProfile2D(name, title,
-                                       fColumns, 0.0, fColumns, 
-                                       fRows, fRowMin, fRowMax,"s"));
-  
-    //All Samples, high gain
-    name = "hPedestalRMShighgain";
+    fPedestalLEDRefLowGain.Add(new TProfile(name, title,
+                                           fLEDRefs, 0.0, fLEDRefs, "s"));
+    
+    //LED Ref/Mon pedestals, high gain
+    name = "hPedestalLEDRefhighgain";
     name += i;
-    title = "Pedestal RMS, high gain, module ";
+    title = "Pedestal LEDRef, high gain, module ";
     title += i; 
-    fPedestalRMSHighGain.Add(new TProfile2D(name, title,
-                                        fColumns, 0.0, fColumns, 
-                                        fRows, fRowMin, fRowMax,"s"));
+    fPedestalLEDRefHighGain.Add(new TProfile(name, title,
+                                            fLEDRefs, 0.0, fLEDRefs, "s"));
   
     //Peak-Pedestals, low gain
     name = "hPeakMinusPedlowgain";
@@ -166,7 +192,16 @@ AliCaloCalibPedestal::AliCaloCalibPedestal(kDetType detectorType) :
     fPeakMinusPedHighGain.Add(new TProfile2D(name, title,
                                             fColumns, 0.0, fColumns, 
                                             fRows, fRowMin, fRowMax,"s"));
-  
+
+    //Peak-Pedestals, high gain - TH2F histo
+    name = "hPeakMinusPedhighgainHisto";
+    name += i;
+    title = "Peak-Pedestal, high gain, module ";
+    title += i; 
+    fPeakMinusPedHighGainHisto.Add(new TH2F(name, title,
+                                           fColumns*fRows, 0.0, fColumns*fRows, 
+                                           100, 0, 1000));
     name = "hDeadMap";
     name += i;
     title = "Dead map, module ";
@@ -175,47 +210,99 @@ AliCaloCalibPedestal::AliCaloCalibPedestal(kDetType detectorType) :
                          fRows, fRowMin, fRowMax));
   
   }//end for nModules create the histograms
+
+  CompressAndSetOwner();
+}
+
+//_____________________________________________________________________
+void AliCaloCalibPedestal::CompressAndSetOwner()
+{ 
   //Compress the arrays, in order to remove the empty objects (a 16 slot array is created by default)
   fPedestalLowGain.Compress();
   fPedestalHighGain.Compress();
-  fPedestalRMSLowGain.Compress();
-  fPedestalRMSHighGain.Compress();
+  fPedestalLEDRefLowGain.Compress();
+  fPedestalLEDRefHighGain.Compress();
   fPeakMinusPedLowGain.Compress();
   fPeakMinusPedHighGain.Compress();
+  fPeakMinusPedHighGainHisto.Compress();
   fDeadMap.Compress();
-  //Make them the owners of the profiles, so we don't need to care about deleting them
-  //fPedestalLowGain.SetOwner();
-  //fPedestalHighGain.SetOwner();
-  //fPeakMinusPedLowGain.SetOwner();
-  //fPeakMinusPedHighGain.SetOwner();
-  
+
+  // set owner ship for everyone
+  fPedestalLowGain.SetOwner(kTRUE);
+  fPedestalHighGain.SetOwner(kTRUE);
+  fPedestalLEDRefLowGain.SetOwner(kTRUE);
+  fPedestalLEDRefHighGain.SetOwner(kTRUE);
+  fPeakMinusPedLowGain.SetOwner(kTRUE);
+  fPeakMinusPedHighGain.SetOwner(kTRUE);
+  fPeakMinusPedHighGainHisto.SetOwner(kTRUE);
+  fPedestalLowGainDiff.SetOwner(kTRUE);
+  fPedestalHighGainDiff.SetOwner(kTRUE);
+  fPedestalLEDRefLowGainDiff.SetOwner(kTRUE);
+  fPedestalLEDRefHighGainDiff.SetOwner(kTRUE);
+  fPeakMinusPedLowGainDiff.SetOwner(kTRUE);
+  fPeakMinusPedHighGainDiff.SetOwner(kTRUE);
+  fPedestalLowGainRatio.SetOwner(kTRUE);
+  fPedestalHighGainRatio.SetOwner(kTRUE);
+  fPedestalLEDRefLowGainRatio.SetOwner(kTRUE);
+  fPedestalLEDRefHighGainRatio.SetOwner(kTRUE);
+  fPeakMinusPedLowGainRatio.SetOwner(kTRUE);
+  fPeakMinusPedHighGainRatio.SetOwner(kTRUE);
+  fDeadMap.SetOwner(kTRUE);
 }
 
 // dtor
 //_____________________________________________________________________
 AliCaloCalibPedestal::~AliCaloCalibPedestal()
 {
+  //dtor
+  
   if (fReference) delete fReference;//Delete the reference object, if it has been loaded
-  //TObjArray will delete the histos/profiles when it is deleted.
+  
+  // delete also TObjArray's 
+  fPedestalLowGain.Delete(); 
+  fPedestalHighGain.Delete();
+  fPedestalLEDRefLowGain.Delete();
+  fPedestalLEDRefHighGain.Delete();
+  fPeakMinusPedLowGain.Delete();
+  fPeakMinusPedHighGain.Delete();
+  fPeakMinusPedHighGainHisto.Delete();
+  fPedestalLowGainDiff.Delete();
+  fPedestalHighGainDiff.Delete();
+  fPedestalLEDRefLowGainDiff.Delete();
+  fPedestalLEDRefHighGainDiff.Delete();
+  fPeakMinusPedLowGainDiff.Delete();
+  fPeakMinusPedHighGainDiff.Delete();
+  fPedestalLowGainRatio.Delete();
+  fPedestalHighGainRatio.Delete();
+  fPedestalLEDRefLowGainRatio.Delete();
+  fPedestalLEDRefHighGainRatio.Delete();
+  fPeakMinusPedLowGainRatio.Delete();
+  fPeakMinusPedHighGainRatio.Delete();
+  fDeadMap.Delete();
+  
 }
 
 // copy ctor
 //_____________________________________________________________________
-AliCaloCalibPedestal::AliCaloCalibPedestal(const AliCaloCalibPedestal &ped) :
+AliCaloCalibPedestal::AliCaloCalibPedestal(AliCaloCalibPedestal &ped) :
   TObject(ped),
   fPedestalLowGain(),
   fPedestalHighGain(),
-  fPedestalRMSLowGain(),
-  fPedestalRMSHighGain(),
+  fPedestalLEDRefLowGain(),
+  fPedestalLEDRefHighGain(),
   fPeakMinusPedLowGain(),
   fPeakMinusPedHighGain(),
+  fPeakMinusPedHighGainHisto(),
   fPedestalLowGainDiff(),
   fPedestalHighGainDiff(),
+  fPedestalLEDRefLowGainDiff(),
+  fPedestalLEDRefHighGainDiff(),
   fPeakMinusPedLowGainDiff(),
   fPeakMinusPedHighGainDiff(),
   fPedestalLowGainRatio(),
   fPedestalHighGainRatio(),
+  fPedestalLEDRefLowGainRatio(),
+  fPedestalLEDRefHighGainRatio(),
   fPeakMinusPedLowGainRatio(),
   fPeakMinusPedHighGainRatio(),
   fDeadMap(),
@@ -228,6 +315,7 @@ AliCaloCalibPedestal::AliCaloCalibPedestal(const AliCaloCalibPedestal &ped) :
   fDetType(ped.GetDetectorType()),
   fColumns(ped.GetColumns()),
   fRows(ped.GetRows()),
+  fLEDRefs(ped.GetLEDRefs()),
   fModules(ped.GetModules()),
   fRowMin(ped.GetRowMin()),
   fRowMax(ped.GetRowMax()),
@@ -237,34 +325,32 @@ AliCaloCalibPedestal::AliCaloCalibPedestal(const AliCaloCalibPedestal &ped) :
   fRunNumber(ped.GetRunNumber()),
   fSelectPedestalSamples(ped.GetSelectPedestalSamples()),
   fFirstPedestalSample(ped.GetFirstPedestalSample()),
-  fLastPedestalSample(ped.GetLastPedestalSample())
+  fLastPedestalSample(ped.GetLastPedestalSample()),
+  fDeadThreshold(ped.GetDeadThreshold()),
+  fWarningThreshold(ped.GetWarningThreshold()),
+  fWarningFraction(ped.GetWarningFraction()),
+  fHotSigma(ped.GetHotSigma())
 {
   // Then the ObjArray ones; we add the histograms rather than trying TObjArray = assignment
   //DS: this has not really been tested yet..
   for (int i = 0; i < fModules; i++) {
     fPedestalLowGain.Add( ped.GetPedProfileLowGain(i) );
     fPedestalHighGain.Add( ped.GetPedProfileHighGain(i) );
-    fPedestalRMSLowGain.Add( ped.GetPedRMSProfileLowGain(i) );
-    fPedestalRMSHighGain.Add( ped.GetPedRMSProfileHighGain(i) );
+    fPedestalLEDRefLowGain.Add( ped.GetPedLEDRefProfileLowGain(i) );
+    fPedestalLEDRefHighGain.Add( ped.GetPedLEDRefProfileHighGain(i) );
     fPeakMinusPedLowGain.Add( ped.GetPeakProfileLowGain(i) );
     fPeakMinusPedHighGain.Add( ped.GetPeakProfileHighGain(i) );
+    fPeakMinusPedHighGainHisto.Add( ped.GetPeakHighGainHisto(i) );
 
     fDeadMap.Add( ped.GetDeadMap(i) );  
   }//end for nModules 
  
-  //Compress the arrays, in order to remove the empty objects (a 16 slot array is created by default)
-  fPedestalLowGain.Compress();
-  fPedestalHighGain.Compress();
-  fPedestalRMSLowGain.Compress();
-  fPedestalRMSHighGain.Compress();
-  fPeakMinusPedLowGain.Compress();
-  fPeakMinusPedHighGain.Compress();
-  fDeadMap.Compress();
+  CompressAndSetOwner();
 }
 
 // assignment operator; use copy ctor to make life easy..
 //_____________________________________________________________________
-AliCaloCalibPedestal& AliCaloCalibPedestal::operator = (const AliCaloCalibPedestal &source)
+AliCaloCalibPedestal& AliCaloCalibPedestal::operator = (AliCaloCalibPedestal &source)
 {
   // assignment operator; use copy ctor
   if (&source == this) return *this;
@@ -275,13 +361,16 @@ AliCaloCalibPedestal& AliCaloCalibPedestal::operator = (const AliCaloCalibPedest
 
 //_____________________________________________________________________
 void AliCaloCalibPedestal::Reset()
-{
-  // Reset all arrays/histograms
+{   // Reset all arrays/histograms
+  ValidateProfiles(); // make sure histos/profiles exist
   for (int i = 0; i < fModules; i++) {
     GetPedProfileLowGain(i)->Reset();
     GetPedProfileHighGain(i)->Reset();
+    GetPedLEDRefProfileLowGain(i)->Reset();
+    GetPedLEDRefProfileHighGain(i)->Reset();
     GetPeakProfileLowGain(i)->Reset();
     GetPeakProfileHighGain(i)->Reset();
+    GetPeakHighGainHisto(i)->Reset();
     GetDeadMap(i)->Reset();
     
     if (!fPedestalLowGainDiff.IsEmpty()) {
@@ -289,11 +378,15 @@ void AliCaloCalibPedestal::Reset()
   
       GetPedProfileLowGainDiff(i)->Reset();
       GetPedProfileHighGainDiff(i)->Reset();
+      GetPedLEDRefProfileLowGainDiff(i)->Reset();
+      GetPedLEDRefProfileHighGainDiff(i)->Reset();
       GetPeakProfileLowGainDiff(i)->Reset();
       GetPeakProfileHighGainDiff(i)->Reset();
       
       GetPedProfileLowGainRatio(i)->Reset();
       GetPedProfileHighGainRatio(i)->Reset();
+      GetPedLEDRefProfileLowGainRatio(i)->Reset();
+      GetPedLEDRefProfileHighGainRatio(i)->Reset();
       GetPeakProfileLowGainRatio(i)->Reset();
       GetPeakProfileHighGainRatio(i)->Reset();
     }
@@ -307,18 +400,153 @@ void AliCaloCalibPedestal::Reset()
   //To think about: should fReference be deleted too?... let's not do it this time, at least...
 }
 
+// Parameter/cut handling
 //_____________________________________________________________________
-Bool_t AliCaloCalibPedestal::AddInfo(const AliCaloCalibPedestal *ped)
+void AliCaloCalibPedestal::SetParametersFromFile(const char *parameterFile)
+{  
+  // Note: this method is a bit more complicated than it really has to be
+  // - allowing for multiple entries per line, arbitrary order of the
+  // different variables etc. But I wanted to try and do this in as
+  // correct a C++ way as I could (as an exercise).
+
+  static const string delimitor("::");
+       
+  // open, check input file
+  ifstream in( parameterFile );
+  if( !in ) {
+    printf("in AliCaloCalibPedestal::SetParametersFromFile - Using default/run_time parameters.\n");
+    return;
+  } 
+
+
+  // read in
+  char readline[1024];
+  while ((in.rdstate() & ios::failbit) == 0 ) {
+    
+    // Read into the raw char array and then construct a string
+    // to do the searching
+    in.getline(readline, 1024);
+    istringstream s(readline);         
+               
+    while ( ( s.rdstate() & ios::failbit ) == 0 ) {
+                       
+      string keyValue; 
+      s >> keyValue;
+      
+      // check stream status
+      if( ( s.rdstate() & ios::failbit ) == ios::failbit) break;
+                       
+      // skip rest of line if comments found
+      if( keyValue.substr( 0, 2 ) == "//" ) break;
+                       
+      // look for "::" in keyValue pair
+      size_t position = keyValue.find( delimitor );
+      if( position == string::npos ) {
+       printf("wrong format for key::value pair: %s\n", keyValue.c_str());
+      }
+                               
+      // split keyValue pair
+      string key( keyValue.substr( 0, position ) );
+      string value( keyValue.substr( position+delimitor.size(), 
+                                     keyValue.size()-delimitor.size() ) );
+                       
+      // check value does not contain a new delimitor
+      if( value.find( delimitor ) != string::npos ) {
+       printf("wrong format for key::value pair: %s\n", keyValue.c_str());
+      }
+      
+      // debug: check key value pair
+      // printf("AliCaloCalibPedestal::SetParametersFromFile - key %s value %s\n", key.c_str(), value.c_str());
+
+      // if the key matches with something we expect, we assign the new value
+      istringstream iss(value);
+      // the comparison strings defined at the beginning of this method
+      if ( (key == "fFirstPedestalSample") || (key == "fLastPedestalSample") || (key == "fDeadThreshold") || (key == "fWarningThreshold") || (key == "fWarningFraction") || (key == "fHotSigma") ) {
+       printf("AliCaloCalibPedestal::SetParametersFromFile - key %s value %s\n", key.c_str(), value.c_str());
+
+       if (key == "fFirstPedestalSample") { 
+         iss >> fFirstPedestalSample; 
+       }
+       else if (key == "fLastPedestalSample") { 
+         iss >> fLastPedestalSample; 
+       }
+       else if (key == "fDeadThreshold") { 
+         iss >> fDeadThreshold; 
+       }
+       else if (key == "fWarningThreshold") { 
+         iss >> fWarningThreshold; 
+       }
+       else if (key == "fWarningFraction") { 
+         iss >> fWarningFraction; 
+       }
+       else if (key == "fHotSigma") { 
+         iss >> fHotSigma; 
+       }
+
+      } // some match
+
+    }          
+  }
+
+  in.close();
+  return;
+       
+}
+
+//_____________________________________________________________________
+void AliCaloCalibPedestal::WriteParametersToFile(const char *parameterFile)
+{
+  //Write parameters in file.
+       
+  static const string delimitor("::");
+  ofstream out( parameterFile );
+  out << "// " << parameterFile << endl;
+  out << "fFirstPedestalSample" << "::" << fFirstPedestalSample << endl;
+  out << "fLastPedestalSample" << "::" << fLastPedestalSample << endl;
+  out << "fDeadThreshold" << "::" << fDeadThreshold << endl;
+  out << "fWarningThreshold" << "::" << fWarningThreshold << endl;
+  out << "fWarningFraction" << "::" << fWarningFraction << endl;
+  out << "fHotSigma" << "::" << fHotSigma << endl;
+
+  out.close();
+  return;
+}
+
+//_____________________________________________________________________
+Bool_t AliCaloCalibPedestal::AddInfo(AliCaloCalibPedestal *ped)
 {
   // just do this for the basic histograms/profiles that get filled in ProcessEvent
   // may not have data for all modules, but let's just Add everything..
+  ValidateProfiles(); // make sure histos/profiles exist
+
   for (int i = 0; i < fModules; i++) {
     GetPedProfileLowGain(i)->Add( ped->GetPedProfileLowGain(i) );
     GetPedProfileHighGain(i)->Add( ped->GetPedProfileHighGain(i) );
+    GetPedLEDRefProfileLowGain(i)->Add( ped->GetPedLEDRefProfileLowGain(i) );
+    GetPedLEDRefProfileHighGain(i)->Add( ped->GetPedLEDRefProfileHighGain(i) );
     GetPeakProfileLowGain(i)->Add( ped->GetPeakProfileLowGain(i) );
     GetPeakProfileHighGain(i)->Add( ped->GetPeakProfileHighGain(i) );
+    GetPeakHighGainHisto(i)->Add( ped->GetPeakHighGainHisto(i) );
   }//end for nModules 
 
+  // We should also copy other pieces of info: counters and parameters 
+  // (not number of columns and rows etc which should be the same)
+  // note that I just assign them here rather than Add them, but we
+  // normally just Add (e.g. in Preprocessor) one object so this should be fine.
+  fNEvents = ped->GetNEvents();
+  fNChanFills = ped->GetNChanFills();
+  fDeadTowers = ped->GetDeadTowerCount();
+  fNewDeadTowers = ped->GetDeadTowerNew();
+  fResurrectedTowers = ped->GetDeadTowerResurrected();
+  fRunNumber = ped->GetRunNumber();
+  fSelectPedestalSamples = ped->GetSelectPedestalSamples();
+  fFirstPedestalSample = ped->GetFirstPedestalSample();
+  fLastPedestalSample = ped->GetLastPedestalSample();
+  fDeadThreshold = ped->GetDeadThreshold();
+  fWarningThreshold = ped->GetWarningThreshold();
+  fWarningFraction = ped->GetWarningFraction();
+  fHotSigma = ped->GetHotSigma();
+
   // DeadMap; Diff profiles etc would need to be redone after this operation
 
   return kTRUE;//We succesfully added info from the supplied object
@@ -329,6 +557,9 @@ Bool_t AliCaloCalibPedestal::ProcessEvent(AliRawReader *rawReader)
 { 
   // if fMapping is NULL the rawstream will crate its own mapping
   AliCaloRawStreamV3 rawStream(rawReader, fCaloString, (AliAltroMapping**)fMapping);
+  if (fDetType == kEmCal) {
+    rawReader->Select("EMCAL", 0, AliEMCALGeoParams::fgkLastAltroDDL) ; //select EMCAL DDL range 
+  }
   return ProcessEvent(&rawStream);
 }
 
@@ -337,11 +568,14 @@ Bool_t AliCaloCalibPedestal::ProcessEvent(AliCaloRawStreamV3 *in)
 { 
   // Method to process=analyze one event in the data stream
   if (!in) return kFALSE; //Return right away if there's a null pointer
+  in->Reset(); // just in case the next customer forgets to check if the stream was reset..
+
   fNEvents++; // one more event
+
+  if (fNEvents==1) ValidateProfiles(); // 1st event, make sure histos/profiles exist
   
   // indices for the reading
   int sample = 0;
-  int gain = 0;
   int time = 0;
   int i = 0; // sample counter
   int startBin = 0;
@@ -351,18 +585,17 @@ Bool_t AliCaloCalibPedestal::ProcessEvent(AliCaloRawStreamV3 *in)
     while (in->NextChannel()) {
 
       // counters
-      int max = fgkSampleMin, min = fgkSampleMax; // min and max sample values
-      
-      // for the pedestal calculation
-      int sampleSum = 0; // sum of samples
-      int squaredSampleSum = 0; // sum of samples squared
-      int nSum = 0; // number of samples in sum
-      // calc. quantities
-      double mean = 0, squaredMean = 0, rms = 0;
-      
+      int max = AliEMCALGeoParams::fgkSampleMin, min = AliEMCALGeoParams::fgkSampleMax; // min and max sample values
+      int nsamples = 0;
+
+      // pedestal samples
+      int nPed = 0;
+      vector<int> pedSamples; 
+
       while (in->NextBunch()) {
        const UShort_t *sig = in->GetSignals();
        startBin = in->GetStartTimeBin();
+       nsamples += in->GetBunchLength();
        for (i = 0; i < in->GetBunchLength(); i++) {
          sample = sig[i];
          time = startBin--;
@@ -374,36 +607,15 @@ Bool_t AliCaloCalibPedestal::ProcessEvent(AliCaloRawStreamV3 *in)
          // should we add it for the pedestal calculation?
          if ( (fFirstPedestalSample<=time && time<=fLastPedestalSample) || // sample time in range
               !fSelectPedestalSamples ) { // or we don't restrict the sample range.. - then we'll take all 
-           sampleSum += sample;
-           squaredSampleSum += sample*sample;
-           nSum++;
+           pedSamples.push_back( sig[i] );
+           nPed++;
          }
          
        } // loop over samples in bunch
       } // loop over bunches
 
-      // calculate pedesstal estimate: mean of possibly selected samples
-      if (nSum > 0) {
-       mean = sampleSum / (1.0 * nSum);
-       squaredMean = squaredSampleSum / (1.0 * nSum);
-       // The variance (rms squared) is equal to the mean of the squares minus the square of the mean..
-       rms = sqrt(squaredMean - mean*mean); 
-      }
-      else {
-       mean = 0;
-       squaredMean = 0;
-       rms  = 0;
-      }
-      
-      // we're done with the calc. for this channel; let's prepare to fill histo
-      gain = -1; // init to not valid value
-      if ( in->IsLowGain() ) {
-       gain = 0;
-      }
-      else if ( in->IsHighGain() ) {
-       gain = 1;
-      }
-      
+      if (nsamples > 0) { // this check is needed for when we have zero-supp. on, but not sparse readout
+
       // it should be enough to check the SuperModule info for each DDL really, but let's keep it here for now
       int arrayPos = in->GetModule(); //The modules are numbered starting from 0
       if (arrayPos >= fModules) {
@@ -417,28 +629,50 @@ Bool_t AliCaloCalibPedestal::ProcessEvent(AliCaloRawStreamV3 *in)
       
       fNChanFills++; // one more channel found, and profile to be filled
       //NOTE: coordinates are (column, row) for the profiles
-      if (gain == 0) {
+      if ( in->IsLowGain() ) {
        //fill the low gain histograms
        ((TProfile2D*)fPeakMinusPedLowGain[arrayPos])->Fill(in->GetColumn(), fRowMultiplier*in->GetRow(), max - min);
-       if (nSum>0) { // only fill pedestal info in case it could be calculated
-         ((TProfile2D*)fPedestalLowGain[arrayPos])->Fill(in->GetColumn(), fRowMultiplier*in->GetRow(), mean); 
-         ((TProfile2D*)fPedestalRMSLowGain[arrayPos])->Fill(in->GetColumn(), fRowMultiplier*in->GetRow(), rms);
+       if (nPed>0) { // only fill pedestal info in case it could be calculated
+         for ( i=0; i<nPed; i++) {
+           ((TProfile2D*)fPedestalLowGain[arrayPos])->Fill(in->GetColumn(), fRowMultiplier*in->GetRow(), pedSamples[i]); 
+         }
        }
       } 
-      else if (gain == 1) {
+      else if ( in->IsHighGain() ) {   
        //fill the high gain ones
        ((TProfile2D*)fPeakMinusPedHighGain[arrayPos])->Fill(in->GetColumn(), fRowMultiplier*in->GetRow(), max - min);
-       if (nSum>0) { // only fill pedestal info in case it could be calculated
-         ((TProfile2D*)fPedestalHighGain[arrayPos])->Fill(in->GetColumn(), fRowMultiplier*in->GetRow(), mean); 
-         ((TProfile2D*)fPedestalRMSHighGain[arrayPos])->Fill(in->GetColumn(), fRowMultiplier*in->GetRow(), rms);
+       if (nPed>0) { // only fill pedestal info in case it could be calculated
+         for ( i=0; i<nPed; i++) {
+           ((TProfile2D*)fPedestalHighGain[arrayPos])->Fill(in->GetColumn(), fRowMultiplier*in->GetRow(), pedSamples[i]); 
+         }       
+       }
+       // for warning checks
+       int idx = in->GetRow() + fRows * in->GetColumn();
+       ((TH2F*)fPeakMinusPedHighGainHisto[arrayPos])->Fill(idx, max - min);
+      } 
+      else if ( in->IsLEDMonData() ) {
+       // for LED Mon data, the mapping class holds the gain info in the Row variable
+       // and the Strip number in the Column..
+       int gain = in->GetRow(); 
+       int stripId = in->GetColumn();
+       if (nPed>0 && stripId<fLEDRefs) {
+         if (gain == 0) {
+           for ( i=0; i<nPed; i++) {
+             ((TProfile*)fPedestalLEDRefLowGain[arrayPos])->Fill(stripId, pedSamples[i]);
+           }
+         }
+         else {
+           for ( i=0; i<nPed; i++) {
+             ((TProfile*)fPedestalLEDRefHighGain[arrayPos])->Fill(stripId, pedSamples[i]);
+           }
+         }
        }
-      }//end if valid gain
+      }
 
-    
+      } // nsamples>0 check, some data found for this channel; not only trailer/header
     }// end while over channel   
   }//end while over DDL's, of input stream 
 
-  in->Reset(); // just in case the next customer forgets to check if the stream was reset..
  
   return kTRUE;
 }
@@ -447,7 +681,7 @@ Bool_t AliCaloCalibPedestal::ProcessEvent(AliCaloRawStreamV3 *in)
 Bool_t AliCaloCalibPedestal::SaveHistograms(TString fileName, Bool_t saveEmptyHistos)
 {
   //Saves all the histograms (or profiles, to be accurate) to the designated file
-  
+  ValidateProfiles(); // make sure histos/profiles exist
   TFile destFile(fileName, "recreate");
   
   if (destFile.IsZombie()) {
@@ -468,14 +702,17 @@ Bool_t AliCaloCalibPedestal::SaveHistograms(TString fileName, Bool_t saveEmptyHi
     }
     if( ((TProfile2D *)fPedestalHighGain[i])->GetEntries() || saveEmptyHistos) {
       fPedestalHighGain[i]->Write();
-      Printf("save %d", i);
     }
-    if( ((TProfile2D *)fPedestalRMSLowGain[i])->GetEntries() || saveEmptyHistos) {
-      fPedestalRMSLowGain[i]->Write();
+    if( ((TProfile *)fPedestalLEDRefLowGain[i])->GetEntries() || saveEmptyHistos) {
+      fPedestalLEDRefLowGain[i]->Write();
     }
-    if( ((TProfile2D *)fPedestalRMSHighGain[i])->GetEntries() || saveEmptyHistos) {
-      fPedestalRMSHighGain[i]->Write();
+    if( ((TProfile *)fPedestalLEDRefHighGain[i])->GetEntries() || saveEmptyHistos) {
+      fPedestalLEDRefHighGain[i]->Write();
     }
+    if( ((TH2F *)fPeakMinusPedHighGainHisto[i])->GetEntries() || saveEmptyHistos) { 
+      fPeakMinusPedHighGainHisto[i]->Write();
+    }
+
   } 
   
   destFile.Close();
@@ -507,7 +744,7 @@ Bool_t AliCaloCalibPedestal::LoadReferenceCalib(TString fileName, TString object
   }
        
   delete sourceFile;
+
   //Reset the histogram ownership behaviour. NOTE: a better workaround would be good, since this may accidentally set AddDirectory to true, even
   //if we are called by someone who has set it to false...
   TH1::AddDirectory(kTRUE);
@@ -515,6 +752,24 @@ Bool_t AliCaloCalibPedestal::LoadReferenceCalib(TString fileName, TString object
   return kTRUE;//We succesfully loaded the object
 }
 
+
+//_____________________________________________________________________
+Bool_t AliCaloCalibPedestal::SetReference(AliCaloCalibPedestal *ref)
+{ // set reference object
+  if (fReference) delete fReference;//Delete the reference object, if it already exists
+  fReference = 0;
+  
+  fReference = ref;
+  if (!fReference || (fReference->GetDetectorType() != fDetType)) {
+    if (fReference) delete fReference;//Delete the object, in case we had an object of the wrong type
+    fReference = 0;
+    return kFALSE;
+  }
+
+  return kTRUE;//We succesfully loaded the object
+}
+
 //_____________________________________________________________________
 void AliCaloCalibPedestal::ValidateComparisonProfiles()
 {
@@ -544,6 +799,22 @@ void AliCaloCalibPedestal::ValidateComparisonProfiles()
                                             fColumns, 0.0, fColumns, 
                                             fRows, fRowMin, fRowMax,"s"));
 
+    //LED Ref/Mon pedestals, low gain
+    name = "hPedestalLEDReflowgainDiff";
+    name += i;
+    title = "Pedestal difference LEDRef, low gain, module ";
+    title += i; 
+    fPedestalLEDRefLowGainDiff.Add(new TProfile(name, title,
+                                               fLEDRefs, 0.0, fLEDRefs, "s"));
+    
+    //LED Ref/Mon pedestals, high gain
+    name = "hPedestalLEDRefhighgainDiff";
+    name += i;
+    title = "Pedestal difference LEDRef, high gain, module ";
+    title += i; 
+    fPedestalLEDRefHighGainDiff.Add(new TProfile(name, title,
+                                                fLEDRefs, 0.0, fLEDRefs, "s"));
+
     //Peak-Pedestals, high gain
     name = "hPeakMinusPedhighgainDiff";
     name += i;
@@ -552,6 +823,15 @@ void AliCaloCalibPedestal::ValidateComparisonProfiles()
     fPeakMinusPedHighGainDiff.Add(new TProfile2D(name, title,
                                                 fColumns, 0.0, fColumns, 
                                                 fRows, fRowMin, fRowMax,"s"));
+
+    //Peak-Pedestals, low gain
+    name = "hPeakMinusPedlowgainDiff";
+    name += i;
+    title = "Peak-Pedestal difference, low gain, module ";
+    title += i; 
+    fPeakMinusPedLowGainDiff.Add(new TProfile2D(name, title,
+                                               fColumns, 0.0, fColumns, 
+                                               fRows, fRowMin, fRowMax,"s"));
   
     //Pedestals, low gain
     name = "hPedlowgainRatio";
@@ -570,6 +850,22 @@ void AliCaloCalibPedestal::ValidateComparisonProfiles()
     fPedestalHighGainRatio.Add(new TProfile2D(name, title,
                                              fColumns, 0.0, fColumns, 
                                              fRows, fRowMin, fRowMax,"s"));
+
+    //LED Ref/Mon pedestals, low gain
+    name = "hPedestalLEDReflowgainRatio";
+    name += i;
+    title = "Pedestal ratio LEDRef, low gain, module ";
+    title += i; 
+    fPedestalLEDRefLowGainRatio.Add(new TProfile(name, title,
+                                                fLEDRefs, 0.0, fLEDRefs, "s"));
+    
+    //LED Ref/Mon pedestals, high gain
+    name = "hPedestalLEDRefhighgainRatio";
+    name += i;
+    title = "Pedestal ratio LEDRef, high gain, module ";
+    title += i; 
+    fPedestalLEDRefHighGainRatio.Add(new TProfile(name, title,
+                                                 fLEDRefs, 0.0, fLEDRefs, "s"));
   
     //Peak-Pedestals, low gain
     name = "hPeakMinusPedlowgainRatio";
@@ -594,53 +890,182 @@ void AliCaloCalibPedestal::ValidateComparisonProfiles()
 
 //_____________________________________________________________________
 void AliCaloCalibPedestal::ComputeDiffAndRatio()
-{
-  // calculate differences and ratios relative to a reference
+{ // calculate differences and ratios relative to a reference
+  ValidateProfiles(); // make sure histos/profiles exist
   ValidateComparisonProfiles();//Make sure the comparison histos exist
  
   if (!fReference) {
     return;//Return if the reference object isn't loaded
   }
 
+  int bin = 0;
+  double diff = 0;
+  double ratio = 1;
   for (int i = 0; i < fModules; i++) {
-    //Compute the ratio of the histograms
-    
-    ((TProfile2D*)fPedestalLowGainRatio[i])->Divide(GetPedProfileLowGain(i), fReference->GetPedProfileLowGain(i), 1.0, 1.0);
-    ((TProfile2D*)fPedestalHighGainRatio[i])->Divide(GetPedProfileHighGain(i), fReference->GetPedProfileHighGain(i), 1.0, 1.0);
-    ((TProfile2D*)fPeakMinusPedLowGainRatio[i])->Divide(GetPeakProfileLowGain(i), fReference->GetPeakProfileLowGain(i), 1.0, 1.0);
-    ((TProfile2D*)fPeakMinusPedHighGainRatio[i])->Divide(GetPeakProfileHighGain(i), fReference->GetPeakProfileHighGain(i), 1.0, 1.0);
-  
     //For computing the difference, we cannot simply do TProfile2D->Add(), because that subtracts the sum of all entries,
     //which means that the mean of the new profile will not be the difference of the means. So do it by hand:
-    for (int j = 0; j <= fColumns; j++) {
-      for (int k = 0; k <= fRows; k++) {
-       int bin = ((TProfile2D*)fPeakMinusPedHighGainDiff[i])->GetBin(j+1, k+1);//Note that we assume here that all histos have the same structure...
-       double diff = fReference->GetPeakProfileHighGain(i)->GetBinContent(bin) - GetPeakProfileHighGain(i)->GetBinContent(bin);
-       ((TProfile2D*)fPeakMinusPedHighGainDiff[i])->SetBinContent(j+1, k+1, diff);
-       ((TProfile2D*)fPeakMinusPedHighGainDiff[i])->SetBinEntries(bin, 1);
-
-       diff = fReference->GetPeakProfileLowGain(i)->GetBinContent(bin) - GetPeakProfileLowGain(i)->GetBinContent(bin);
-       ((TProfile2D*)fPeakMinusPedLowGainDiff[i])->SetBinContent(j+1, k+1, diff);
-       ((TProfile2D*)fPeakMinusPedLowGainDiff[i])->SetBinEntries(bin, 1);
-    
-       diff = fReference->GetPedProfileHighGain(i)->GetBinContent(bin) - GetPedProfileHighGain(i)->GetBinContent(bin);
-       ((TProfile2D*)fPedestalHighGainDiff[i])->SetBinContent(j+1, k+1, diff);
-       ((TProfile2D*)fPedestalHighGainDiff[i])->SetBinEntries(bin, 1);
-
-       diff = fReference->GetPedProfileLowGain(i)->GetBinContent(bin) - GetPedProfileLowGain(i)->GetBinContent(bin);
-       ((TProfile2D*)fPedestalLowGainDiff[i])->SetBinContent(j+1, k+1, diff);
-       ((TProfile2D*)fPedestalLowGainDiff[i])->SetBinEntries(bin, 1);
-       
+    for (int j = 0; j < fColumns; j++) {
+      for (int k = 0; k < fRows; k++) {
+       bin = ((TProfile2D*)fPeakMinusPedHighGainDiff[i])->GetBin(j+1, k+1);//Note that we assume here that all histos have the same structure...
+
+       if (fReference->GetPeakProfileHighGain(i)->GetBinContent(bin) > 0) {
+         diff = GetPeakProfileHighGain(i)->GetBinContent(bin) - fReference->GetPeakProfileHighGain(i)->GetBinContent(bin);
+         ((TProfile2D*)fPeakMinusPedHighGainDiff[i])->SetBinContent(bin, diff);
+         ((TProfile2D*)fPeakMinusPedHighGainDiff[i])->SetBinEntries(bin, 1);
+         ratio = GetPeakProfileHighGain(i)->GetBinContent(bin) / fReference->GetPeakProfileHighGain(i)->GetBinContent(bin);  
+         ((TProfile2D*)fPeakMinusPedHighGainRatio[i])->SetBinContent(bin, ratio);
+         ((TProfile2D*)fPeakMinusPedHighGainRatio[i])->SetBinEntries(bin, 1);
+       }
+
+       if (fReference->GetPeakProfileLowGain(i)->GetBinContent(bin) > 0) {
+         diff = GetPeakProfileLowGain(i)->GetBinContent(bin) - fReference->GetPeakProfileLowGain(i)->GetBinContent(bin);
+         ((TProfile2D*)fPeakMinusPedLowGainDiff[i])->SetBinContent(bin, diff);
+         ((TProfile2D*)fPeakMinusPedLowGainDiff[i])->SetBinEntries(bin, 1);
+         ratio = GetPeakProfileLowGain(i)->GetBinContent(bin) / fReference->GetPeakProfileLowGain(i)->GetBinContent(bin);  
+         ((TProfile2D*)fPeakMinusPedLowGainRatio[i])->SetBinContent(bin, ratio);
+         ((TProfile2D*)fPeakMinusPedLowGainRatio[i])->SetBinEntries(bin, 1);
+       }
+
+       if (fReference->GetPedProfileHighGain(i)->GetBinContent(bin) > 0) {
+         diff = GetPedProfileHighGain(i)->GetBinContent(bin) - fReference->GetPedProfileHighGain(i)->GetBinContent(bin);
+         ((TProfile2D*)fPedestalHighGainDiff[i])->SetBinContent(bin, diff);
+         ((TProfile2D*)fPedestalHighGainDiff[i])->SetBinEntries(bin, 1);
+         ratio = GetPedProfileHighGain(i)->GetBinContent(bin) / fReference->GetPedProfileHighGain(i)->GetBinContent(bin);  
+         ((TProfile2D*)fPedestalHighGainRatio[i])->SetBinContent(bin, ratio);
+         ((TProfile2D*)fPedestalHighGainRatio[i])->SetBinEntries(bin, 1);
+       }
+
+       if (fReference->GetPedProfileLowGain(i)->GetBinContent(bin) > 0) {
+         diff = GetPedProfileLowGain(i)->GetBinContent(bin) - fReference->GetPedProfileLowGain(i)->GetBinContent(bin);
+         ((TProfile2D*)fPedestalLowGainDiff[i])->SetBinContent(bin, diff);
+         ((TProfile2D*)fPedestalLowGainDiff[i])->SetBinEntries(bin, 1);
+         ratio = GetPedProfileLowGain(i)->GetBinContent(bin) / fReference->GetPedProfileLowGain(i)->GetBinContent(bin);  
+         ((TProfile2D*)fPedestalLowGainRatio[i])->SetBinContent(bin, ratio);
+         ((TProfile2D*)fPedestalLowGainRatio[i])->SetBinEntries(bin, 1);
+       }
+
       } // rows
     } // columns
-    
+
+    // same for LED Ref/Mon channels
+    for (int j = 0; j <= fLEDRefs; j++) {    
+      bin = j+1;//Note that we assume here that all histos have the same structure...
+
+      if (fReference->GetPedLEDRefProfileHighGain(i)->GetBinContent(bin) > 0) {
+       diff = GetPedLEDRefProfileHighGain(i)->GetBinContent(bin) - fReference->GetPedLEDRefProfileHighGain(i)->GetBinContent(bin);
+       ((TProfile*)fPedestalLEDRefHighGainDiff[i])->SetBinContent(bin, diff);
+       ((TProfile*)fPedestalLEDRefHighGainDiff[i])->SetBinEntries(bin, 1);
+       ratio = GetPedLEDRefProfileHighGain(i)->GetBinContent(bin) / fReference->GetPedLEDRefProfileHighGain(i)->GetBinContent(bin);  
+       ((TProfile*)fPedestalLEDRefHighGainRatio[i])->SetBinContent(bin, ratio);
+       ((TProfile*)fPedestalLEDRefHighGainRatio[i])->SetBinEntries(bin, 1);
+      }
+
+      if (fReference->GetPedLEDRefProfileLowGain(i)->GetBinContent(bin) > 0) {
+       diff = GetPedLEDRefProfileLowGain(i)->GetBinContent(bin) - fReference->GetPedLEDRefProfileLowGain(i)->GetBinContent(bin);
+       ((TProfile*)fPedestalLEDRefLowGainDiff[i])->SetBinContent(bin, diff);
+       ((TProfile*)fPedestalLEDRefLowGainDiff[i])->SetBinEntries(bin, 1);
+       ratio = GetPedLEDRefProfileLowGain(i)->GetBinContent(bin) / fReference->GetPedLEDRefProfileLowGain(i)->GetBinContent(bin);  
+       ((TProfile*)fPedestalLEDRefLowGainRatio[i])->SetBinContent(bin, ratio);
+       ((TProfile*)fPedestalLEDRefLowGainRatio[i])->SetBinEntries(bin, 1);
+      } 
+     
+    }
+
   } // modules
  
 }
 
 //_____________________________________________________________________
-void AliCaloCalibPedestal::ComputeDeadTowers(int threshold, const char * deadMapFile)
+void AliCaloCalibPedestal::ComputeHotAndWarningTowers(const char * hotMapFile)
+{ // look for hot/noisy towers
+  ValidateProfiles(); // make sure histos/profiles exist
+  ofstream * fout = 0;
+  char name[512];//Quite a long temp buffer, just in case the filename includes a path
+
+  if (hotMapFile) {
+    snprintf(name, 512, "%s.txt", hotMapFile);
+    fout = new ofstream(name);
+    if (!fout->is_open()) {
+      delete fout;
+      fout = 0;//Set the pointer to empty if the file was not opened
+    }
+  }
+  for(int i = 0; i < fModules; i++){
+               
+    //first we compute the peak-pedestal distribution for each supermodule...
+    if( GetPeakHighGainHisto(i)->GetEntries() > 0 ) {
+      double min = GetPeakProfileHighGain(i)->GetBinContent(GetPeakProfileHighGain(i)->GetMinimumBin());
+      double max = GetPeakProfileHighGain(i)->GetBinContent(GetPeakProfileHighGain(i)->GetMaximumBin());
+      TH1D *hPeakFit = new TH1D(Form("hFit_%d", i), Form("hFit_%d", i), (int)((max-min)*10), min-1, max+1);
+
+      for (int j = 1; j <= fColumns; j++) {
+       for (int k = 1; k <= fRows; k++) {        
+         hPeakFit->Fill(GetPeakProfileHighGain(i)->GetBinContent(j, k));
+       }
+      }
+
+      //...and then we try to fit it
+      double mean  = hPeakFit->GetMean();
+      double sigma = hPeakFit->GetRMS();
+      try {
+       hPeakFit->Fit("gaus", "OQ", "",  mean - 3*sigma, mean + 3*sigma);
+       mean  = hPeakFit->GetFunction("gaus")->GetParameter(1);
+       sigma = hPeakFit->GetFunction("gaus")->GetParameter(2);
+      }
+      catch (const std::exception & e) {
+       printf("AliCaloCalibPedestal: TH1D PeakFit exception %s", e.what()); 
+      }      
+      //hPeakFit->Draw();
+
+      delete hPeakFit;
+
+      //Then we look for warm/hot towers
+      TH2F * hPeak2D = GetPeakHighGainHisto(i);
+      hPeak2D->GetYaxis()->SetRangeUser( fWarningThreshold, hPeak2D->GetYaxis()->GetBinUpEdge(hPeak2D->GetNbinsY()) );
+
+      int idx = 0 ;
+      int warnCounter = 0;
+      for (int j = 1; j <= fColumns; j++) {
+       for (int k = 1; k <= fRows; k++) {                              
+         //we start looking for warm/warning towers...
+         // histogram x-axis index
+         idx = k-1 + fRows*(j-1); // this is what is used in the Fill call
+         hPeak2D->GetXaxis()->SetRangeUser(idx, idx);
+         warnCounter = (int) hPeak2D->Integral();
+         if(warnCounter > fNEvents * fWarningFraction) {
+           ((TH2D*)fDeadMap[i])->SetBinContent(j, k, kWarning);            
+           /* printf("mod %d col %d row %d warnCounter %d - status %d\n", 
+              i, j-1, k-1, warnCounter, (int) (kWarning)); */  
+         }
+         //...then we look for hot ones (towers whose values are greater than mean + X*sigma)  
+         if(GetPeakProfileHighGain(i)->GetBinContent(j, k) > mean + fHotSigma*sigma ) {
+           ((TH2D*)fDeadMap[i])->SetBinContent(j, k, kHot); 
+           /* printf("mod %d col %d row %d  binc %d - status %d\n", 
+              i, j-1, k-1, (int)(GetPeakProfileHighGain(i)->GetBinContent(j, k)), (int) (kHot)); */      
+         }
+
+         //Write the status to the hot/warm map file, if the file is open.
+         // module - column - row - status (1=dead, 2= warm/warning , 3 = hot, see .h file enum)
+         if (fout && ((TH2D*)fDeadMap[i])->GetBinContent(j, k) > 1) {
+           
+           (*fout) << i << " " 
+                   << (j - 1) << " " 
+                   << (k - 1) << " " 
+                   << ((TH2D*)fDeadMap[i])->GetBinContent(j, k) << endl;               }
+         
+       }
+      }
+
+    } 
+  }
+  return;
+}
+
+//_____________________________________________________________________
+void AliCaloCalibPedestal::ComputeDeadTowers(const char * deadMapFile)
 {
+  ValidateProfiles(); // make sure histos/profiles exist
   //Computes the number of dead towers etc etc into memory, after this you can call the GetDead... -functions
   int countTot = 0;
   int countNew = 0;
@@ -660,7 +1085,7 @@ void AliCaloCalibPedestal::ComputeDeadTowers(int threshold, const char * deadMap
     }
     if (!diff->is_open()) {
       delete diff;
-      fout = 0;//Set the pointer to empty if the file was not opened
+      diff = 0;//Set the pointer to empty if the file was not opened
     }
   }
  
@@ -669,23 +1094,23 @@ void AliCaloCalibPedestal::ComputeDeadTowers(int threshold, const char * deadMap
       for (int j = 1; j <= fColumns; j++) {
        for (int k = 1; k <= fRows; k++) {
 
-         if (GetPeakProfileHighGain(i)->GetBinContent(j, k) < threshold) {//It's dead
+         if (GetPeakProfileHighGain(i)->GetBinContent(j, k) < fDeadThreshold) {//It's dead
            countTot++;//One more dead total
            if (fout) {
              (*fout) << i << " " 
-                     << (fRows - k) << " " 
-                     << (j-1) << " " 
+                     << (j - 1) << " " 
+                     << (k - 1) << " " 
                      << "1" << " " 
                      << "0" << endl;//Write the status to the deadmap file, if the file is open.
            }
            
-           if (fReference && fReference->GetPeakProfileHighGain(i)->GetBinContent(j, k) >= threshold) {
+           if (fReference && fReference->GetPeakProfileHighGain(i)->GetBinContent(j, k) >= fDeadThreshold) {
              ((TH2D*)fDeadMap[i])->SetBinContent(j, k, kRecentlyDeceased); 
              countNew++;//This tower wasn't dead before!
              if (diff) {
                ( *diff) << i << " " 
-                        << (fRows - k) << " " 
                         << (j - 1) << " " 
+                        << (k - 1) << " " 
                         << "1" << " " 
                         << "0" << endl;//Write the status to the deadmap difference file, if the file is open.
              }
@@ -696,19 +1121,13 @@ void AliCaloCalibPedestal::ComputeDeadTowers(int threshold, const char * deadMap
          } 
          else { //It's ALIVE!!
            //Don't bother with writing the live ones.
-           //if (fout)
-           //  (*fout) << i << " " 
-           //     << (fRows - k) << " " 
-           //     << (j - 1) << " " 
-           //     << "1" << " " 
-           //     << "1" << endl;//Write the status to the deadmap file, if the file is open.
-           if (fReference && fReference->GetPeakProfileHighGain(i)->GetBinContent(j, k) < threshold) {
+           if (fReference && fReference->GetPeakProfileHighGain(i)->GetBinContent(j, k) < fDeadThreshold) {
              ((TH2D*)fDeadMap[i])->SetBinContent(j, k, kResurrected);
              countRes++; //This tower was dead before => it's a miracle! :P
              if (diff) {
                (*diff) << i << " " 
-                       << (fRows - k) << " " 
                        << (j - 1) << " " 
+                       << (k - 1) << " " 
                        << "1" << " " 
                        << "1" << endl;//Write the status to the deadmap difference file, if the file is open.
              }
@@ -734,3 +1153,27 @@ void AliCaloCalibPedestal::ComputeDeadTowers(int threshold, const char * deadMap
  fResurrectedTowers = countRes;
 }
 
+//_____________________________________________________________________
+Bool_t AliCaloCalibPedestal::IsBadChannel(int imod, int icol, int irow) const
+{
+  // Check if status info histo exists
+  if (!fDeadMap[imod]) { 
+    return kFALSE;
+  }
+
+  //Check if channel is dead or hot.  
+  Int_t status =  (Int_t) ( ((TH2D*)fDeadMap[imod])->GetBinContent(icol,irow) );
+  if(status == kAlive)
+    return kFALSE;
+  else 
+    return kTRUE;
+  
+}
+
+//_____________________________________________________________________
+void AliCaloCalibPedestal::SetChannelStatus(int imod, int icol, int irow, int status)
+{
+  ValidateProfiles(); // make sure histos/profiles exist
+  //Set status of channel dead, hot, alive ...  
+  ((TH2D*)fDeadMap[imod])->SetBinContent(icol, irow, status);  
+}