]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - EMCAL/AliCaloCalibPedestal.cxx
All FMD corrections are now divided into the analysis + adding new corrections
[u/mrichter/AliRoot.git] / EMCAL / AliCaloCalibPedestal.cxx
index 14d516260a471469f9bf4cbfffdbcc90952b5ae3..264e427062c4971c8ecaeb98a80daaede660271d 100644 (file)
 
 #include "TH1.h"
 #include "TFile.h"
-
 #include <fstream>
 #include <iostream>
+#include <cmath>
 
-#include "AliCaloRawStream.h"
+#include "AliCaloRawStreamV3.h"
 
 //The include file
 #include "AliCaloCalibPedestal.h"
@@ -54,6 +54,8 @@ AliCaloCalibPedestal::AliCaloCalibPedestal(kDetType detectorType) :
   TObject(),
   fPedestalLowGain(),
   fPedestalHighGain(),
+  fPedestalRMSLowGain(),
+  fPedestalRMSHighGain(),
   fPeakMinusPedLowGain(),
   fPeakMinusPedHighGain(),
   fPedestalLowGainDiff(),
@@ -75,9 +77,15 @@ AliCaloCalibPedestal::AliCaloCalibPedestal(kDetType detectorType) :
   fColumns(0),
   fRows(0),
   fModules(0),
+  fRowMin(0),
+  fRowMax(0),
+  fRowMultiplier(0),
   fCaloString(),
   fMapping(NULL),
-  fRunNumber(-1)
+  fRunNumber(-1),
+  fSelectPedestalSamples(kTRUE), 
+  fFirstPedestalSample(0),
+  fLastPedestalSample(15)
 {
   //Default constructor. First we set the detector-type related constants.
   if (detectorType == kPhos) {
@@ -85,15 +93,21 @@ AliCaloCalibPedestal::AliCaloCalibPedestal(kDetType detectorType) :
     fRows = fgkPhosRows;
     fModules = fgkPhosModules;
     fCaloString = "PHOS";
+    fRowMin = -1*fRows;
+    fRowMax = 0;
+    fRowMultiplier = -1;
   } 
   else {
     //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;
+    fModules = AliEMCALGeoParams::fgkEMCALModules;
     fCaloString = "EMCAL";
+    fRowMin = 0;
+    fRowMax = fRows;
+    fRowMultiplier = 1;
   } 
   fDetType = detectorType;
  
@@ -107,7 +121,7 @@ AliCaloCalibPedestal::AliCaloCalibPedestal(kDetType detectorType) :
     title += i; 
     fPedestalLowGain.Add(new TProfile2D(name, title,
                                        fColumns, 0.0, fColumns, 
-                                       fRows, -fRows, 0.0));
+                                       fRows, fRowMin, fRowMax,"s"));
   
     //Pedestals, high gain
     name = "hPedhighgain";
@@ -116,7 +130,24 @@ AliCaloCalibPedestal::AliCaloCalibPedestal(kDetType detectorType) :
     title += i; 
     fPedestalHighGain.Add(new TProfile2D(name, title,
                                         fColumns, 0.0, fColumns, 
-                                        fRows, -fRows, 0.0));
+                                        fRows, fRowMin, fRowMax,"s"));
+    //All Samples, low gain
+    name = "hPedestalRMSlowgain";
+    name += i;
+    title = "Pedestal RMS, 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";
+    name += i;
+    title = "Pedestal RMS, high gain, module ";
+    title += i; 
+    fPedestalRMSHighGain.Add(new TProfile2D(name, title,
+                                        fColumns, 0.0, fColumns, 
+                                        fRows, fRowMin, fRowMax,"s"));
   
     //Peak-Pedestals, low gain
     name = "hPeakMinusPedlowgain";
@@ -125,7 +156,7 @@ AliCaloCalibPedestal::AliCaloCalibPedestal(kDetType detectorType) :
     title += i; 
     fPeakMinusPedLowGain.Add(new TProfile2D(name, title,
                                            fColumns, 0.0, fColumns, 
-                                           fRows, -fRows, 0.0));
+                                           fRows, fRowMin, fRowMax,"s"));
   
     //Peak-Pedestals, high gain
     name = "hPeakMinusPedhighgain";
@@ -134,20 +165,22 @@ AliCaloCalibPedestal::AliCaloCalibPedestal(kDetType detectorType) :
     title += i; 
     fPeakMinusPedHighGain.Add(new TProfile2D(name, title,
                                             fColumns, 0.0, fColumns, 
-                                            fRows, -fRows, 0.0));
+                                            fRows, fRowMin, fRowMax,"s"));
   
     name = "hDeadMap";
     name += i;
     title = "Dead map, module ";
     title += i;
     fDeadMap.Add(new TH2D(name, title, fColumns, 0.0, fColumns, 
-                         fRows, -fRows, 0.0));
+                         fRows, fRowMin, fRowMax));
   
   }//end for nModules create the histograms
  
   //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();
@@ -173,6 +206,8 @@ AliCaloCalibPedestal::AliCaloCalibPedestal(const AliCaloCalibPedestal &ped) :
   TObject(ped),
   fPedestalLowGain(),
   fPedestalHighGain(),
+  fPedestalRMSLowGain(),
+  fPedestalRMSHighGain(),
   fPeakMinusPedLowGain(),
   fPeakMinusPedHighGain(),
   fPedestalLowGainDiff(),
@@ -194,15 +229,23 @@ AliCaloCalibPedestal::AliCaloCalibPedestal(const AliCaloCalibPedestal &ped) :
   fColumns(ped.GetColumns()),
   fRows(ped.GetRows()),
   fModules(ped.GetModules()),
+  fRowMin(ped.GetRowMin()),
+  fRowMax(ped.GetRowMax()),
+  fRowMultiplier(ped.GetRowMultiplier()),
   fCaloString(ped.GetCaloString()),
   fMapping(NULL), //! note that we are not copying the map info
-  fRunNumber(ped.GetRunNumber())
+  fRunNumber(ped.GetRunNumber()),
+  fSelectPedestalSamples(ped.GetSelectPedestalSamples()),
+  fFirstPedestalSample(ped.GetFirstPedestalSample()),
+  fLastPedestalSample(ped.GetLastPedestalSample())
 {
   // 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) );
     fPeakMinusPedLowGain.Add( ped.GetPeakProfileLowGain(i) );
     fPeakMinusPedHighGain.Add( ped.GetPeakProfileHighGain(i) );
 
@@ -212,6 +255,8 @@ AliCaloCalibPedestal::AliCaloCalibPedestal(const AliCaloCalibPedestal &ped) :
   //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();
@@ -283,60 +328,118 @@ Bool_t AliCaloCalibPedestal::AddInfo(const AliCaloCalibPedestal *ped)
 Bool_t AliCaloCalibPedestal::ProcessEvent(AliRawReader *rawReader)
 { 
   // if fMapping is NULL the rawstream will crate its own mapping
-  AliCaloRawStream rawStream(rawReader, fCaloString, (AliAltroMapping**)fMapping);
+  AliCaloRawStreamV3 rawStream(rawReader, fCaloString, (AliAltroMapping**)fMapping);
   return ProcessEvent(&rawStream);
 }
 
 //_____________________________________________________________________
-Bool_t AliCaloCalibPedestal::ProcessEvent(AliCaloRawStream *in)
+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
-  
   fNEvents++; // one more event
-  int sample, i = 0; //The sample temp, and the sample number in current event.
-  int max = fgkSampleMin, min = fgkSampleMax;//Use these for picking the pedestal
-  int gain = 0;
   
-  while (in->Next()) {
-    sample = in->GetSignal(); //Get the adc signal
-    if (sample < min) min = sample;
-    if (sample > max) max = sample;
-    i++;
-    if ( i >= in->GetTimeLength()) {
-      //If we're here then we're done with this tower
-      gain = 1 - in->IsLowGain();
+  // indices for the reading
+  int sample = 0;
+  int gain = 0;
+  int time = 0;
+  int i = 0; // sample counter
+  int startBin = 0;
+
+  // start loop over input stream 
+  while (in->NextDDL()) {
+    while (in->NextChannel()) {
+
+      // counters
+      int max = AliEMCALGeoParams::fgkSampleMin, min = AliEMCALGeoParams::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;
+      
+      while (in->NextBunch()) {
+       const UShort_t *sig = in->GetSignals();
+       startBin = in->GetStartTimeBin();
+       for (i = 0; i < in->GetBunchLength(); i++) {
+         sample = sig[i];
+         time = startBin--;
+
+         // check if it's a min or max value
+         if (sample < min) min = sample;
+         if (sample > max) max = sample;
+         
+         // 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++;
+         }
+         
+       } // 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;
+      }
+      
+      // 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) {
        //TODO: return an error message, if appopriate (perhaps if debug>0?)
        return kFALSE;
-      } 
-    
+      }     
       //Debug
       if (arrayPos < 0 || arrayPos >= fModules) {
        printf("Oh no: arrayPos = %i.\n", arrayPos); 
       }
-
+      
       fNChanFills++; // one more channel found, and profile to be filled
       //NOTE: coordinates are (column, row) for the profiles
       if (gain == 0) {
        //fill the low gain histograms
-       ((TProfile2D*)fPedestalLowGain[arrayPos])->Fill(in->GetColumn(), -in->GetRow() - 1, min);
-       ((TProfile2D*)fPeakMinusPedLowGain[arrayPos])->Fill(in->GetColumn(), -in->GetRow() - 1, max - min);
+       ((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);
+       }
       } 
-      else {//fill the high gain ones
-       ((TProfile2D*)fPedestalHighGain[arrayPos])->Fill(in->GetColumn(), -in->GetRow() - 1, min);
-       ((TProfile2D*)fPeakMinusPedHighGain[arrayPos])->Fill(in->GetColumn(), -in->GetRow() - 1, max - min);
-      }//end if gain
+      else if (gain == 1) {
+       //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);
+       }
+      }//end if valid gain
 
-      max = fgkSampleMin; min = fgkSampleMax;
-      i = 0;
     
-    }//End if end of tower
-   
-  }//end while, of stream
-  
+    }// 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;
 }
 
@@ -365,6 +468,13 @@ 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( ((TProfile2D *)fPedestalRMSHighGain[i])->GetEntries() || saveEmptyHistos) {
+      fPedestalRMSHighGain[i]->Write();
     }
   } 
   
@@ -423,7 +533,7 @@ void AliCaloCalibPedestal::ValidateComparisonProfiles()
     title += i; 
     fPedestalLowGainDiff.Add(new TProfile2D(name, title,
                                            fColumns, 0.0, fColumns, 
-                                           fRows, -fRows, 0.0));
+                                           fRows, fRowMin, fRowMax,"s"));
   
     //Pedestals, high gain
     name = "hPedhighgainDiff";
@@ -432,17 +542,8 @@ void AliCaloCalibPedestal::ValidateComparisonProfiles()
     title += i; 
     fPedestalHighGainDiff.Add(new TProfile2D(name, title,
                                             fColumns, 0.0, fColumns, 
-                                            fRows, -fRows, 0.0));
-  
-    //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, -fRows, 0.0));
-  
+                                            fRows, fRowMin, fRowMax,"s"));
+
     //Peak-Pedestals, high gain
     name = "hPeakMinusPedhighgainDiff";
     name += i;
@@ -450,7 +551,7 @@ void AliCaloCalibPedestal::ValidateComparisonProfiles()
     title += i; 
     fPeakMinusPedHighGainDiff.Add(new TProfile2D(name, title,
                                                 fColumns, 0.0, fColumns, 
-                                                fRows, -fRows, 0.0));
+                                                fRows, fRowMin, fRowMax,"s"));
   
     //Pedestals, low gain
     name = "hPedlowgainRatio";
@@ -459,7 +560,7 @@ void AliCaloCalibPedestal::ValidateComparisonProfiles()
     title += i; 
     fPedestalLowGainRatio.Add(new TProfile2D(name, title,
                                             fColumns, 0.0, fColumns, 
-                                            fRows, -fRows, 0.0));
+                                            fRows, fRowMin, fRowMax,"s"));
   
     //Pedestals, high gain
     name = "hPedhighgainRatio";
@@ -468,7 +569,7 @@ void AliCaloCalibPedestal::ValidateComparisonProfiles()
     title += i; 
     fPedestalHighGainRatio.Add(new TProfile2D(name, title,
                                              fColumns, 0.0, fColumns, 
-                                             fRows, -fRows, 0.0));
+                                             fRows, fRowMin, fRowMax,"s"));
   
     //Peak-Pedestals, low gain
     name = "hPeakMinusPedlowgainRatio";
@@ -477,7 +578,7 @@ void AliCaloCalibPedestal::ValidateComparisonProfiles()
     title += i; 
     fPeakMinusPedLowGainRatio.Add(new TProfile2D(name, title,
                                                 fColumns, 0.0, fColumns, 
-                                                fRows, -fRows, 0.0));
+                                                fRows, fRowMin, fRowMax,"s"));
   
     //Peak-Pedestals, high gain
     name = "hPeakMinusPedhighgainRatio";
@@ -486,7 +587,7 @@ void AliCaloCalibPedestal::ValidateComparisonProfiles()
     title += i; 
     fPeakMinusPedHighGainRatio.Add(new TProfile2D(name, title,
                                                  fColumns, 0.0, fColumns, 
-                                                 fRows, -fRows, 0.0));
+                                                 fRows, fRowMin, fRowMax,"s"));
     
   }//end for nModules create the histograms
 }