]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - MUON/AliMUONPedestal.cxx
Fixing minor bug recognizing diffractive events in simulation
[u/mrichter/AliRoot.git] / MUON / AliMUONPedestal.cxx
index f76bccba67a5bc6219bbf2248b74931931ad5b55..5abbc0b7bc154349049ad6cdf2164fb9fa1f673a 100644 (file)
 #include "AliMUONVStore.h"
 #include "AliMUON2DMap.h"
 #include "AliMUONCalibParamND.h"
-
+#include "AliMpConstants.h"
 #include <TString.h>
-#include <THashTable.h>
 #include <TTimeStamp.h>
 #include <TMath.h>
 #include <TTree.h>
 #include <TFile.h>
 #include <TH1F.h>
+#include <THashList.h>
 #include <Riostream.h>
 
 #include <sstream>
@@ -50,43 +50,140 @@ ClassImp(AliMUONPedestal)
 //______________________________________________________________________________
 AliMUONPedestal::AliMUONPedestal()
 : TObject(),
+//fN(0),
+fNCurrentEvents(0),
+fNEvthreshold(0),
+fSorting(0),
 fNEvents(0),
 fRunNumber(0),
 fNChannel(0),
 fNManu(0),
-fErrorBuspatchTable(new THashTable(100,2)),
+fNManuConfig(0),
+fConfig(1),
+fStatusDA(0),
+fErrorBuspatchTable(new AliMUON2DMap(kFALSE)),
+fManuBuspatchTable(new AliMUON2DMap(kFALSE)),
+fManuBPoutofconfigTable(new AliMUON2DMap(kFALSE)),
 fDate(new TTimeStamp()),
 fFilcout(0),
-fPedestalStore(new AliMUON2DMap(kFALSE)),
-fIndex(-1)
+fHistoFileName(),
+fPedestalStore(new AliMUON2DMap(kTRUE)),
+fIndex(-1),
+fPrefixDA(),
+fPrefixLDC()
 {
 /// Default constructor
-  sprintf(fHistoFileName," ");
-  sprintf(fprefixDA," "); 
 }
-//  AliMUONPedestal& operator=(const AliMUONPedestal& other); Copy ctor
+//______________________________________________________________________________
+AliMUONPedestal::AliMUONPedestal(TRootIOCtor* /*dummy*/)
+: TObject(),
+//fN(0),
+fNCurrentEvents(0),
+fNEvthreshold(0),
+fSorting(0),
+fNEvents(0),
+fRunNumber(0),
+fNChannel(0),
+fNManu(0),
+fNManuConfig(0),
+fConfig(1),
+fStatusDA(0),
+fErrorBuspatchTable(0),
+fManuBuspatchTable(0),
+fManuBPoutofconfigTable(0),
+fDate(0),
+fFilcout(0),
+fHistoFileName(),
+fPedestalStore(0),
+fIndex(-1),
+fPrefixDA(),
+fPrefixLDC()
+{
+/// Root IO constructor
+}
 
 //______________________________________________________________________________
 AliMUONPedestal::~AliMUONPedestal()
 {
 /// Destructor
+  delete fErrorBuspatchTable;
+  delete fManuBuspatchTable;
+  delete fPedestalStore;
+  delete fManuBPoutofconfigTable;
+}
+
+//______________________________________________________________________________
+const char* 
+AliMUONPedestal::GetHistoFileName() const
+{
+  /// Return the name of file we use to store histograms
+  return fHistoFileName.Data();
 }
 
+//______________________________________________________________________________
+void AliMUONPedestal::LoadConfig(const char* dbfile)
+{
+  /// Load MUONTRK configuration from ascii file "dbfile" (in DetDB)
+
+  Int_t manuId;
+  Int_t busPatchId;
+
+  ifstream filein(dbfile,ios::in);
+  
+  while (!filein.eof())
+    { 
+      filein >> busPatchId >> manuId;
+
+      AliMUONVCalibParam* ped = 
+       static_cast<AliMUONVCalibParam*>(fPedestalStore ->FindObject(busPatchId, manuId));
+
+      if (!ped) {
+       fNManuConfig++;
+       fNChannel+=64;
+  ped = new AliMUONCalibParamND(2, AliMpConstants::ManuNofChannels(),busPatchId, manuId, -1.); // put default wise -1, not connected channel
+       fPedestalStore ->Add(ped);  
+
+        if ( ! fManuBuspatchTable->FindObject(busPatchId,manuId) )
+         {
+           // New (buspatch,manu)
+           AliMUONErrorCounter* manuCounter = new AliMUONErrorCounter(busPatchId,manuId);
+           fManuBuspatchTable->Add(manuCounter);
+         }
+      }
+    }
+} 
 //______________________________________________________________________________
 void AliMUONPedestal::MakePed(Int_t busPatchId, Int_t manuId, Int_t channelId, Int_t charge)
 {
+  static Int_t warn=0;
   /// Compute pedestals values
   AliMUONVCalibParam* ped = 
     static_cast<AliMUONVCalibParam*>(fPedestalStore ->FindObject(busPatchId, manuId));
 
-  if (!ped) {
-    fNManu++;
-    ped = new AliMUONCalibParamND(2, kNChannels,busPatchId, manuId, -1.); // put default wise -1, not connected channel
-    fPedestalStore ->Add(ped);  
-  }
+  if (!ped)   
+    {
+      if(fConfig) 
+       {  // Fill out_of_config (buspatch,manu) table
+         if (!(static_cast<AliMUONErrorCounter*>(fManuBPoutofconfigTable->FindObject(busPatchId,manuId))))
+           fManuBPoutofconfigTable->Add(new AliMUONErrorCounter(busPatchId,manuId));
+         if(warn<10) cout << fPrefixLDC.Data() << " : !!! WARNING  : busPatchId = " << busPatchId << " manuId = " << manuId << " not in the Detector configuration " << endl;
+         else if(warn==10) cout << fPrefixLDC.Data() << " : !!! see .log file for an exhaustive list of (busPatchId, manuId) out of Detector configuration \n" << endl; 
+          warn++;
+          (*fFilcout) <<" !!! WARNING  : busPatchId = " << busPatchId << " manuId = " << manuId << " not in the Detector configuration " << endl; 
+       }
+      else {fNManu++;}
+      fNChannel+=64;
+      // put default wise -1, not connected channel
+      ped = new AliMUONCalibParamND(2, AliMpConstants::ManuNofChannels(),busPatchId, manuId, -1.); 
+      fPedestalStore ->Add(ped);  
+    }
 
   // Initialization for the first value
-  if (ped->ValueAsDouble(channelId, 0) == -1) ped->SetValueAsDouble(channelId, 0, 0.);
+  if (ped->ValueAsDouble(channelId, 0) == -1)  
+    { 
+      if(fConfig && channelId == 0){fNManu++;}
+      ped->SetValueAsDouble(channelId, 0, 0.);
+    }
   if (ped->ValueAsDouble(channelId, 1) == -1) ped->SetValueAsDouble(channelId, 1, 0.);
 
   Double_t pedMean  = ped->ValueAsDouble(channelId, 0) + (Double_t) charge;
@@ -94,66 +191,265 @@ void AliMUONPedestal::MakePed(Int_t busPatchId, Int_t manuId, Int_t channelId, I
 
   ped->SetValueAsDouble(channelId, 0, pedMean);
   ped->SetValueAsDouble(channelId, 1, pedSigma);
-}
 
+  AliMUONErrorCounter* manuCounter;
+  if (!(manuCounter = static_cast<AliMUONErrorCounter*>(fManuBuspatchTable->FindObject(busPatchId,manuId))))
+    {
+      // New (buspatch,manu)
+      manuCounter = new AliMUONErrorCounter(busPatchId,manuId);
+      fManuBuspatchTable->Add(manuCounter);
+    }
+  else
+    {
+      // Existing buspatch
+      manuCounter->Increment();
+    }  
+}
 //______________________________________________________________________________
-TString AliMUONPedestal::WritePedHeader(void) 
+void AliMUONPedestal::Finalize()
 {
-///
+  /// final polishing of the store
+  
+  Double_t pedMean;
+  Double_t pedSigma;
+  Double_t pedSigmalimit=0.5;
+  Int_t busPatchId;
+  Int_t manuId;
+  Int_t channelId;
+  Int_t status=0;
 
-  ostringstream stream;
-  stream<<"//===========================================================================" << endl;
-  stream<<"//                       Pedestal file calculated by MUONTRKda"<<endl;
-  stream<<"//===========================================================================" << endl;
-  stream<<"//       * Run           : " << fRunNumber << endl; 
-  stream<<"//       * Date          : " << fDate->AsString("l") <<endl;
-  stream<<"//       * Statictics    : " << fNEvents << endl;
-  stream<<"//       * # of MANUS    : " << fNManu << endl;
-  stream<<"//       * # of channels : " << fNChannel << endl;
+  // print in logfile
   if (fErrorBuspatchTable->GetSize())
-  {
-    stream<<"//"<<endl;
-    stream<<"//       * Buspatches with less statistics (due to parity errors)"<<endl;
-    TIterator* iter = fErrorBuspatchTable->MakeIterator();
-    AliMUONErrorCounter* parityerror;
-    while((parityerror = (AliMUONErrorCounter*) iter->Next()))
     {
-      stream<<"//         bp "<<parityerror->BusPatch()<<" events used "<<fNEvents-parityerror->Events()<<endl;
+      cout<< "\n" << fPrefixLDC.Data() << " : See list of Buspatches with lower statistics (due to parity errors) in .log or .ped file "<<endl;
+      (*fFilcout)<<"\nWarning: Buspatches with less statistics (due to parity errors)"<<endl;
+      TIter nextParityError(fErrorBuspatchTable->CreateIterator());
+      AliMUONErrorCounter* parityerror;
+      while((parityerror = static_cast<AliMUONErrorCounter*>(nextParityError())))
+       {
+         //      cout<<"  bp "<<parityerror->BusPatch()<<": used events = "<<fNEvents-parityerror->Events()<<endl;
+         (*fFilcout)<<"  bp "<<parityerror->BusPatch()<<": used events = "<<fNEvents-parityerror->Events()<<endl;
+       }
     }
-  }  
-  stream<<"//"<<endl;
-  stream<<"//---------------------------------------------------------------------------" << endl;
-  stream<<"//---------------------------------------------------------------------------" << endl;
-  stream<<"//      BP     MANU     CH.      MEAN    SIGMA"<<endl;
-  stream<<"//---------------------------------------------------------------------------" << endl;
-
-  return TString(stream.str().c_str());
-}
+  
+  Int_t nADC4090=0;
+  Int_t nADCmax=0;
+  // iterator over pedestal
+  TIter next(fPedestalStore ->CreateIterator());
+  AliMUONVCalibParam* ped;
 
+  while ( ( ped = dynamic_cast<AliMUONVCalibParam*>(next() ) ) )
+    {
+      busPatchId              = ped->ID0();
+      manuId                  = ped->ID1();
+      if(manuId==0)
+       {
+         cout << fPrefixLDC.Data() << " : Warning: ManuId = " << manuId << " !!! in  BP = " << busPatchId << endl;
+         (*fFilcout) << "Warning: ManuId = " << manuId << " !!! in  BP = " << busPatchId << endl;
+       }
+      Int_t eventCounter;
+      // Correct the number of events for buspatch with errors
+      AliMUONErrorCounter* errorCounter;
+      if ((errorCounter = (AliMUONErrorCounter*)fErrorBuspatchTable->FindObject(busPatchId)))
+       {
+         eventCounter = fNEvents - errorCounter->Events();
+       }
+      else
+       {
+         eventCounter = fNEvents;
+       }
+      Int_t occupancy=0; // channel missing in raw data or read but rejected (case of parity error)
+      // value of (buspatch, manu) occupancy
+      AliMUONErrorCounter* manuCounter;
+      manuCounter = static_cast<AliMUONErrorCounter*>(fManuBuspatchTable->FindObject(busPatchId,manuId));
+      if(eventCounter>0)occupancy = manuCounter->Events()/64/eventCounter;
+      if(occupancy>1)
+       {
+         cout << fPrefixLDC.Data() << " : Warning: ManuId = " << manuId << " !!! in  BP = " << busPatchId << " occupancy (>1) = " << occupancy << endl;
+         (*fFilcout) << "Warning: ManuId = " << manuId << " !!! in  BP = " << busPatchId << " occupancy (>1) = " << occupancy <<endl;
+       }
+
+      for (channelId = 0; channelId < ped->Size() ; ++channelId) 
+       {
+         pedMean  = ped->ValueAsDouble(channelId, 0);
+
+         if (pedMean >= 0) // connected channels
+           {
+             ped->SetValueAsDouble(channelId, 0, pedMean/(Double_t)eventCounter);
+             pedMean  = ped->ValueAsDouble(channelId, 0);
+             pedSigma = ped->ValueAsDouble(channelId, 1);
+             ped->SetValueAsDouble(channelId, 1, TMath::Sqrt(TMath::Abs(pedSigma/(Double_t)eventCounter - pedMean*pedMean)));
+
+             if(eventCounter < fNEvthreshold )
+               { nADCmax++; ped->SetValueAsDouble(channelId, 0, ADCMax());
+                 ped->SetValueAsDouble(channelId, 1, ADCMax());}
+             if( ped->ValueAsDouble(channelId, 1) < pedSigmalimit )
+               { nADC4090++; ped->SetValueAsDouble(channelId, 0, ADCMax()-5);
+                 ped->SetValueAsDouble(channelId, 1, ADCMax()-5);}
+             if(manuId == 0 || occupancy>1)
+               { nADCmax++; ped->SetValueAsDouble(channelId, 0, ADCMax());
+                 ped->SetValueAsDouble(channelId, 1, ADCMax());
+                 if(occupancy>1 && channelId==0)ped->SetValueAsDouble(channelId, 0, ADCMax()+occupancy);}
+           }
+         else
+           { nADCmax++; ped->SetValueAsDouble(channelId, 0, ADCMax());
+             ped->SetValueAsDouble(channelId, 1, ADCMax());}
+       }
+    }
+
+  float frac1=0. , frac2=0. ;
+  float frac_badped = 0.25 ; // maximal acceptable ratio of bad pedestals
+  char* detail;
+  if(fNChannel)
+    {
+      if(nADCmax>0)
+       { frac1 = float(nADCmax)/float(fNChannel); 
+         detail=Form("%s : Warning: Nb of Channels with bad Pedestal (Ped=4095) = %d over %d (%6.4f)",fPrefixLDC.Data(),nADCmax,fNChannel,frac1);
+         printf("%s\n",detail);
+         (*fFilcout) <<  detail << endl;}
+
+      if(nADC4090>0)
+       { frac2 = 1.*nADC4090/fNChannel; 
+         detail=Form("%s : Warning: Nb of Channels with PedSigma<0.5 (Ped=4090) = %d over %d (%6.4f)",fPrefixLDC.Data(),nADC4090,fNChannel,frac2);
+         printf("%s\n",detail);
+         (*fFilcout) <<  detail << endl; }
+
+      if (frac1+frac2 > frac_badped) { status=-1 ;
+       detail=Form("\n%s !!! ERROR : fraction of Channels with Pedestal>=4090 = %6.4f (> %5.3f)  (status= %d) \n",fPrefixLDC.Data(),frac1+frac2,frac_badped,status);
+       (*fFilcout) <<  detail << endl; printf("%s",detail) ;}
+    }
+  else { status= -1;
+    detail=Form("\n%s !!! ERROR : Nb good channel = 0 (all pedestals are forced to 4095) !!! (status= %d)\n",fPrefixLDC.Data(),status); 
+    cout << detail; (*fFilcout) << detail ;}
+   
+  SetStatusDA(status);
+}
 //______________________________________________________________________________
-TString AliMUONPedestal::WritePedData(Int_t BP, Int_t Manu, Int_t ch, Double_t pedMean, Double_t pedSigma) 
+void AliMUONPedestal::MakeASCIIoutput(ostream& out) const
 {
-///
+/// put pedestal store in the output stream
+
+  out<<"//===========================================================================" << endl;
+  out<<"//                 Pedestal file calculated by "<< fPrefixDA.Data() << endl;
+  out<<"//===========================================================================" << endl;
+  out<<"//     * Run           : " << fRunNumber << endl; 
+  out<<"//     * Date          : " << fDate->AsString("l") <<endl;
+  out<<"//     * Statictics    : " << fNEvents << endl;
+  if(fConfig)
+    out<<"//     * Nb of MANUS   : " << fNManuConfig << " read in the Det. config. " << endl;
+  out<<"//     * Nb of MANUS   : " << fNManu << " read in raw data " << endl;
+  out<<"//     * Nb of MANUS   : " << fNChannel/64 << " written in pedestal file " << endl;
+  out<<"//     * Nb of channels: " << fNChannel << endl;
+  out<<"//"<<endl;
+  out<<"//     * Below " << fNEvthreshold << " events=> Ped.&sig.=4095" << endl;
+  out<<"//     * PedSigma < 0.5 => Ped.&sig.=4090" << endl;
+
+  if (fErrorBuspatchTable->GetSize())
+    {
+      out<<"//"<<endl;
+      out<<"//    * Buspatches with less statistics (due to parity errors)"<<endl;
+      TIter next(fErrorBuspatchTable->CreateIterator());
+      AliMUONErrorCounter* parityerror;
+      while((parityerror = static_cast<AliMUONErrorCounter*>(next())))
+       {
+         if(fNEvents-parityerror->Events()>fNEvthreshold)
+           { out<<"//      BusPatch = "<<parityerror->BusPatch()<<"\t Nevents used = "<<fNEvents-parityerror->Events()<<endl; }
+         else
+           { out<<"//      BusPatch = "<<parityerror->BusPatch()<<"\t Nevents used = "<<fNEvents-parityerror->Events()<< " (Ped.&sig.=4095)" << endl; }
+       }
+    }  
+  Int_t writitle=0;
+  Int_t occupancy=1;
+  if(occupancy)
+    {
+      TIter next(fPedestalStore ->CreateIterator());
+      AliMUONVCalibParam* ped;
+      while ( ( ped = dynamic_cast<AliMUONVCalibParam*>(next() ) ) )
+       {
+         Int_t busPatchId = ped->ID0();
+         Int_t manuId = ped->ID1();
+         Double_t pedMean  = ped->ValueAsDouble(0, 0); // check pedestal value for channelId=0
+
+         if(pedMean>ADCMax()) 
+           {
+             writitle++;
+             if(writitle==1){ 
+               out<<"//"<<endl;
+               out<<"//    * Puzzling (Buspatch,Manu) read in raw data ? (Ped.&sig.=4095)"<<endl;}
+             occupancy=TMath::Nint(pedMean-ADCMax());
+             ped->SetValueAsDouble(0, 0, ADCMax());
+             out<<"//      BusPatch = "<< busPatchId <<"\t ManuId =  "<< manuId << "\t occupancy = " << occupancy  << endl;
+           }
+
+         if (manuId==0 || (fConfig && static_cast<AliMUONErrorCounter*>(fManuBPoutofconfigTable->FindObject(busPatchId,manuId))))
+           {
+             writitle++;
+             if(writitle==1){ 
+               out<<"//"<<endl;
+               out<<"//    * Puzzling (Buspatch,Manu) read in raw data ? (Ped.&sig.=4095)"<<endl;}
+             out<<"//      BusPatch = "<< busPatchId <<"\t ManuId =  "<< manuId << "\t missing in the mapping" << endl;
+           }
+       }
+    }
+  out<<"//"<<endl;
+  out<<"//---------------------------------------------------------------------------" << endl;
+  out<<"//---------------------------------------------------------------------------" << endl;
+  out<<"//      BP     MANU     CH.      MEAN    SIGMA"<<endl;
+  out<<"//---------------------------------------------------------------------------" << endl;
 
-  ostringstream stream("");
-  stream << "\t" << BP << "\t" << Manu <<"\t"<< ch << "\t"
-         << pedMean <<"\t"<< pedSigma << endl;
-  return TString(stream.str().c_str());
+  TIter next(fPedestalStore->CreateIterator());
+  AliMUONVCalibParam* ped;  
 
+  // Sorting 
+  if  (fSorting)
+    {
+      printf("%s : ..... sorting pedestal values .....\n",fPrefixLDC.Data());
+      THashList pedtable(100,2);
+      while ( ( ped = dynamic_cast<AliMUONVCalibParam*>(next() ) ) )
+      {
+        pedtable.Add(ped);
+      }
+      pedtable.Sort();
+      //      iterator over sorted pedestal
+      TIter nextSorted(&pedtable);
+      while ( (ped = (AliMUONVCalibParam*)(nextSorted()) ) )
+      {
+        Int_t busPatchId = ped->ID0();
+        Int_t manuId = ped->ID1();
+        for ( Int_t channelId = 0; channelId < ped->Size(); ++channelId ) 
+        {
+          Double_t pedMean  = ped->ValueAsDouble(channelId, 0);
+          Double_t pedSigma = ped->ValueAsDouble(channelId, 1);
+          out << "\t" << busPatchId << "\t" << manuId <<"\t"<< channelId << "\t" << pedMean <<"\t"<< pedSigma << endl;
+        }
+      }
+    }
+  else
+    {
+      while ( ( ped = dynamic_cast<AliMUONVCalibParam*>(next() ) ) )
+       {
+         Int_t busPatchId = ped->ID0();
+         Int_t manuId = ped->ID1();
+         for ( Int_t channelId = 0; channelId < ped->Size(); ++channelId ) 
+           {
+             Double_t pedMean  = ped->ValueAsDouble(channelId, 0);
+             Double_t pedSigma = ped->ValueAsDouble(channelId, 1);
+             out << "\t" << busPatchId << "\t" << manuId <<"\t"<< channelId << "\t" << pedMean <<"\t"<< pedSigma << endl;
+           }
+       }
+    }
 }
 
 //______________________________________________________________________________
-void AliMUONPedestal::MakePedStore(TString shuttleFile_1 = "") 
+void AliMUONPedestal::MakeControlHistos()
 {
+  /// Create control histograms
+  if (fIndex>=0) return; // Pedestal run (fIndex=-1)
 
-  /// Store pedestals in ASCII files
   Double_t pedMean;
   Double_t pedSigma;
-  ofstream fileout;
-#ifdef ALI_AMORE
-  ostringstream stringout; // String to be sent to AMORE_DB
-#endif
-  TString tempstring;  
+  Double_t evt;
   Int_t busPatchId;
   Int_t manuId;
   Int_t channelId;
@@ -163,144 +459,54 @@ void AliMUONPedestal::MakePedStore(TString shuttleFile_1 = "")
   TTree* tree = 0;
   TH1F* pedMeanHisto = 0;
   TH1F* pedSigmaHisto = 0;
-
-  if (fIndex<0) // Pedestal run (fIndex=-1)
-  {
-    sprintf(fHistoFileName,"%s_%d.root",fprefixDA,fRunNumber);
-    histoFile = new TFile(fHistoFileName,"RECREATE","MUON Tracking pedestals");
-
-    Char_t name[255];
-    Char_t title[255];
-    sprintf(name,"pedmean_allch");
-    sprintf(title,"Pedestal mean all channels");
-    Int_t nx = 4096;
-    Int_t xmin = 0;
-    Int_t xmax = 4095; 
-    pedMeanHisto = new TH1F(name,title,nx,xmin,xmax);
-    pedMeanHisto->SetDirectory(histoFile);
-
-    sprintf(name,"pedsigma_allch");
-    sprintf(title,"Pedestal sigma all channels");
-    nx = 201;
-    xmin = 0;
-    xmax = 200; 
-    pedSigmaHisto = new TH1F(name,title,nx,xmin,xmax);
-    pedSigmaHisto->SetDirectory(histoFile);
-
-    tree = new TTree("t","Pedestal tree");
-    tree->Branch("bp",&busPatchId,"bp/I");
-    tree->Branch("manu",&manuId,",manu/I");
-    tree->Branch("channel",&channelId,",channel/I");
-    tree->Branch("pedMean",&pedMean,",pedMean/D");
-    tree->Branch("pedSigma",&pedSigma,",pedSigma/D");
-  }
-
-  if (!shuttleFile_1.IsNull()) {
-    fileout.open(shuttleFile_1.Data());
-    tempstring = WritePedHeader();
-    fileout << tempstring;
-#ifdef ALI_AMORE
-    stringout << tempstring;
-#endif
-  }
-  // print in logfile
-  if (fErrorBuspatchTable->GetSize())
-  {
-    cout<<"\n* Buspatches with less statistics (due to parity errors)"<<endl;
-    (*fFilcout)<<"\n* Buspatches with less statistics (due to parity errors)"<<endl;
-    TIterator* iter = fErrorBuspatchTable->MakeIterator();
-    AliMUONErrorCounter* parityerror;
-    while((parityerror = (AliMUONErrorCounter*) iter->Next()))
-    {
-      cout<<"  bp "<<parityerror->BusPatch()<<": events used = "<<fNEvents-parityerror->Events()<<endl;
-      (*fFilcout)<<"  bp "<<parityerror->BusPatch()<<": events used = "<<fNEvents-parityerror->Events()<<endl;
-    }
-
-  }
-
-
-// iterator over pedestal
+    
+  fHistoFileName=Form("%s.root",fPrefixDA.Data());
+  histoFile = new TFile(fHistoFileName,"RECREATE","MUON Tracking pedestals");
+
+  Int_t nx = ADCMax()+1;
+  Int_t xmin = 0;
+  Int_t xmax = ADCMax(); 
+  pedMeanHisto = new TH1F("pedmean_allch","Pedestal mean all channels",nx,xmin,xmax);
+  pedMeanHisto->SetDirectory(histoFile);
+
+  nx = 201;
+  xmin = 0;
+  xmax = 200; 
+  pedSigmaHisto = new TH1F("pedsigma_allch","Pedestal sigma all channels",nx,xmin,xmax);
+  pedSigmaHisto->SetDirectory(histoFile);
+
+  tree = new TTree("t","Pedestal tree");
+  tree->Branch("bp",&busPatchId,"bp/I");
+  tree->Branch("manu",&manuId,",manu/I");
+  tree->Branch("channel",&channelId,",channel/I");
+  tree->Branch("pedMean",&pedMean,",pedMean/D");
+  tree->Branch("pedSigma",&pedSigma,",pedSigma/D");
+  tree->Branch("nevt",&evt,",evt/D");
+
+  // iterator over pedestal
   TIter next(fPedestalStore ->CreateIterator());
   AliMUONVCalibParam* ped;
-
+  AliMUONErrorCounter* manuCounter;
+  
   while ( ( ped = dynamic_cast<AliMUONVCalibParam*>(next() ) ) )
   {
-    busPatchId              = ped->ID0();
-    manuId                  = ped->ID1();
-    Int_t eventCounter;
-
-    // Correct the number of events for buspatch with errors
-    char bpname[256];
-    AliMUONErrorCounter* errorCounter;
-    sprintf(bpname,"bp%d",busPatchId);
-    if ((errorCounter = (AliMUONErrorCounter*)fErrorBuspatchTable->FindObject(bpname)))
+    busPatchId = ped->ID0();
+    manuId = ped->ID1();
+    
+    for ( channelId = 0; channelId < ped->Size(); ++channelId ) 
     {
-      eventCounter = fNEvents - errorCounter->Events();
-    }
-    else
-    {
-      eventCounter = fNEvents;
-    }
-
-    for (channelId = 0; channelId < ped->Size() ; ++channelId) {
       pedMean  = ped->ValueAsDouble(channelId, 0);
-
-      if (pedMean > 0) { // connected channels
-
-        ped->SetValueAsDouble(channelId, 0, pedMean/(Double_t)eventCounter);
-
-        pedMean  = ped->ValueAsDouble(channelId, 0);
-        pedSigma = ped->ValueAsDouble(channelId, 1);
-
-        ped->SetValueAsDouble(channelId, 1, TMath::Sqrt(TMath::Abs(pedSigma/(Double_t)eventCounter - pedMean*pedMean)));
-
-        pedMean  = ped->ValueAsDouble(channelId, 0);
-        pedSigma = ped->ValueAsDouble(channelId, 1);
-
-
-        if (!shuttleFile_1.IsNull()) {
-          tempstring = WritePedData(busPatchId,manuId,channelId,pedMean,pedSigma);
-          fileout << tempstring;
-#ifdef ALI_AMORE
-          stringout << tempstring;
-#endif
-        }
-        if(fIndex<0) // Pedestal Run
-        {
-          pedMeanHisto->Fill(pedMean);
-          pedSigmaHisto->Fill(pedSigma);
-          tree->Fill();
-        }
-      }
+      pedSigma = ped->ValueAsDouble(channelId, 1);
+      manuCounter = static_cast<AliMUONErrorCounter*>(fManuBuspatchTable->FindObject(busPatchId,manuId));
+      evt = manuCounter->Events()/64;
+          
+      pedMeanHisto->Fill(pedMean);
+      pedSigmaHisto->Fill(pedSigma);
+      tree->Fill();  
     }
   }
+    
+  histoFile->Write();  
+  histoFile->Close(); 
 
-// file outputs
-  if (!shuttleFile_1.IsNull())  fileout.close();
-
-// Outputs to root file and AMORE DB
-#ifdef ALI_AMORE
-  //
-  //Send objects to the AMORE DB
-  //
-  const char *role=gSystem->Getenv("AMORE_DA_NAME");
-  if ( role ){
-    amore::da::AmoreDA amoreDA(amore::da::AmoreDA::kSender);
-//  TObjString peddata(stringout.str().c_str());
-    TObjString peddata(stringout.str().c_str());
-    Int_t status =0;
-    status = amoreDA.Send("Pedestals",&peddata);
-    if ( status )
-      cout << "Warning: Failed to write Pedestals in the AMORE database : " << status << endl;
-  } 
-  else {
-    cout << "Warning: environment variable 'AMORE_DA_NAME' not set. Cannot write to the AMORE database" << endl;
-  }
-#endif
-  if(fIndex<0) // Pedestal Run
-  { 
-    histoFile->Write();  
-    histoFile->Close(); 
-    delete fPedestalStore ;
-  }
 }