]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - MUON/AliMUONPedestalSubprocessor.cxx
new functionality and new class added
[u/mrichter/AliRoot.git] / MUON / AliMUONPedestalSubprocessor.cxx
index ac2440b23148a947b7280d6894da8c5827826347..a6f7de1750395396625b18f4556aefac838ff175 100644 (file)
 #include "AliMUONPedestalSubprocessor.h"
 
 #include "AliCDBMetaData.h"
+#include "AliCDBEntry.h"
+#include "AliDAQ.h"
 #include "AliLog.h"
 #include "AliMUON2DMap.h"
 #include "AliMUON2DStoreValidator.h"
-#include "AliMUONCalibParam2F.h"
-#include "Riostream.h"
+#include "AliMUONCalibParamNF.h"
+#include "AliMUONPreprocessor.h"
+#include "AliMUONTrackerIO.h"
+#include "AliMpConstants.h"
+#include "AliMpDDLStore.h"
 #include "TObjString.h"
-#include "TSystem.h"
+#include <Riostream.h>
+#include <TList.h>
+#include <TObjString.h>
+#include <TSystem.h>
 #include <sstream>
-#include "AliMUONVDataIterator.h"
-#include "AliMUONConstants.h"
-#include "AliMUONObjectPair.h"
-#include "AliMpDDLStore.h"
-#include "AliMUONPreprocessor.h"
 
-///
+//-----------------------------------------------------------------------------
 /// \class AliMUONPedestalSubprocessor
 ///
 /// Implementation of AliMUONVSubprocessor class to deal with MUON TRK pedestals.
@@ -43,7 +46,7 @@
 ///---------------------------------------------------------------------------\n
 ///
 /// \author L. Aphecetche
-///
+//-----------------------------------------------------------------------------
 
 /// \cond CLASSIMP
 ClassImp(AliMUONPedestalSubprocessor)
@@ -54,20 +57,68 @@ AliMUONPedestalSubprocessor::AliMUONPedestalSubprocessor(AliMUONPreprocessor* ma
 : AliMUONVSubprocessor(master,
                        "Pedestals",
                        "Upload MUON Tracker pedestals to OCDB"),
-fPedestals(0x0)
+fPedestals(0x0),
+fConfig(0x0),
+fConfigChanged(kFALSE)
 {
-  // default ctor
+  /// default ctor
 }
 
 //_____________________________________________________________________________
 AliMUONPedestalSubprocessor::~AliMUONPedestalSubprocessor()
 {
-  // dtor
+  /// dtor
   delete fPedestals;
+  delete fConfig;
 }
 
 //_____________________________________________________________________________
-void 
+Bool_t
+AliMUONPedestalSubprocessor::HasConfigChanged(const AliMUONVStore& newConfig) const
+{
+  /// Check whether the config changed. 
+  /// Any error will return kTRUE to trig a config upload (safer way).
+  
+  AliCDBEntry* entry = Master()->GetFromOCDB("Calib","Config");
+  if (!entry)
+  {
+    AliError("Could not get MUON/Calib/Config entry for current run ! That's not OK !");
+    return kTRUE;
+  }
+  AliMUONVStore* oldConfig = dynamic_cast<AliMUONVStore*>(entry->GetObject());
+  if (!oldConfig)
+  {
+    AliError("Could not get MUON/Calib/Config object for current run (wrong type ?) ! That's not OK !");
+    return kTRUE;
+  }
+  
+  if ( oldConfig->GetSize() != newConfig.GetSize() ) 
+  {
+    return kTRUE;
+  }
+  
+  TIter next(oldConfig->CreateIterator());
+  AliMUONVCalibParam* old;
+  
+  while ( ( old = static_cast<AliMUONVCalibParam*>(next()) ) )
+  {
+    Int_t detElemId = old->ID0();
+    Int_t manuId = old->ID1();
+    
+    if ( ! newConfig.FindObject(detElemId,manuId) )
+    {
+      // not found in new. Configurations are different. Return right now.
+      return kTRUE;
+    }
+  }
+  
+  // all tests OK. Configuration has not changed.
+  return kFALSE;
+}
+
+
+//_____________________________________________________________________________
+Bool_t 
 AliMUONPedestalSubprocessor::Initialize(Int_t run, UInt_t startTime, UInt_t endTime)
 {
   /// When starting a new run, reads in the pedestals ASCII files.
@@ -78,102 +129,188 @@ AliMUONPedestalSubprocessor::Initialize(Int_t run, UInt_t startTime, UInt_t endT
   delete fPedestals;
   fPedestals = new AliMUON2DMap(kTRUE);
   
-  AliInfo(Form("Reading pedestal files for Run %d startTime %ld endTime %ld",
-               run,startTime,endTime));
+  delete fConfig;
+  fConfig = new AliMUON2DMap(kTRUE);
+  
+  Master()->Log(Form("Reading pedestal files for Run %d startTime %ld endTime %ld",
+                     run,startTime,endTime));
   
   TList* sources = Master()->GetFileSources(kSystem,kId);
   TIter next(sources);
   TObjString* o(0x0);
+  Int_t n(0);
+  Int_t npedFiles(0);
+  
   while ( ( o = static_cast<TObjString*>(next()) ) )
   {
     TString fileName(Master()->GetFile(kSystem,kId,o->GetName()));
-    Bool_t ok = ReadFile(fileName.Data());
-    if (!ok)
+    Int_t ok = ReadPedestalFile(fileName.Data());
+    if (ok>0)
+    {
+      n += ok;
+      ++npedFiles;
+    }
+  }
+
+  delete sources;
+  
+  if (!n)
+  {
+    Master()->Log("Failed to read any pedestals");
+    delete fPedestals;
+    fPedestals = 0;
+    delete fConfig;
+    fConfig = 0;
+    return kFALSE;
+  }
+  
+  const char* kIdConf = "CONFIG";
+
+  sources = Master()->GetFileSources(kSystem,kIdConf);
+  TIter nextConf(sources);
+  Int_t nconf(0);
+  Int_t nconfFiles(0);
+  
+  while ( ( o = static_cast<TObjString*>(nextConf()) ) )
+  {
+    TString fileName(Master()->GetFile(kSystem,kIdConf,o->GetName()));
+    Int_t ok = ReadConfigFile(fileName.Data());
+    if (ok>0)
     {
-      AliError(Form("Could not read file %s",fileName.Data()));
+      nconf += ok;
+      ++nconfFiles;
     }
   }
+  
   delete sources;
+  
+  if ( npedFiles != nconfFiles )
+  {
+    Master()->Log(Form("ERROR : Number of config files (%d) different from number of pedestal files (%d)",nconfFiles,npedFiles));
+    delete fPedestals;
+    fPedestals = 0;
+    delete fConfig;
+    fConfig = 0;
+    return kFALSE;
+  }
+  
+  fConfigChanged = HasConfigChanged(*fConfig);
+  
+  return kTRUE;
 }
 
 //_____________________________________________________________________________
 UInt_t 
 AliMUONPedestalSubprocessor::Process(TMap* /*dcsAliasMap*/)
 {
-  // Store the pedestals into the CDB
-  
-  if (!fPedestals) return 0;
+  /// Store the pedestals into the CDB
   
-  AliInfo("Validating pedestals");
+  if (!fPedestals || !fConfig) 
+  {
+    // this is the only reason to fail for the moment : getting no pedestal or no config
+    // at all.
+    return 1;
+  }
+    
   AliMUON2DStoreValidator validator;
-  TObjArray* missing =
-    validator.Validate(*fPedestals,AliMUONCalibParam2F::InvalidFloatValue());  
+
+  Master()->Log("Validating");
+
+  TObjArray* chambers = validator.Validate(*fPedestals,fConfig);
   
-  if (missing)  
+  if (chambers)
   {
-    validator.Report(*missing);
-//    AliError("Will not write into CDB as some pieces are missing...");
-//    return 0;
+    // we hereby report what's missing, but this is not a reason to fail ;-)
+    // the only reason to fail would be if we got no pedestal at all
+    TList lines;
+    lines.SetOwner(kTRUE);
+  
+    validator.Report(lines,*chambers);
+  
+    TIter next(&lines);
+    TObjString* line;
+  
+    while ( ( line = static_cast<TObjString*>(next()) ) )
+    {
+      Master()->Log(line->String().Data());
+    }
   }
   
-  AliInfo("Storing pedestals");
+  Master()->Log("Storing pedestals...");
+  if ( fConfigChanged ) 
+  {
+    Master()->Log("...and configuration, as it has changed");
+  }
   
   AliCDBMetaData metaData;
        metaData.SetBeamPeriod(0);
        metaData.SetResponsible("MUON TRK");
-       metaData.SetComment("Computed by AliMUONPedestalSubprocessor $Id$");
+  TString comment("Computed by AliMUONPedestalSubprocessor $Id$");
+  comment.ReplaceAll("$","");
+       metaData.SetComment(comment.Data());
+  
+  Bool_t validToInfinity = kTRUE;
+       Bool_t result = Master()->Store("Calib", "Pedestals", fPedestals, &metaData, 0, validToInfinity);
+  if ( fConfigChanged ) 
+  {
+    result = result && Master()->Store("Calib", "Config", fConfig, &metaData, 0, validToInfinity);
+  }
+  return ( result != kTRUE ); // return 0 if everything is ok.  
+}
+
+//_____________________________________________________________________________
+Int_t
+AliMUONPedestalSubprocessor::ReadPedestalFile(const char* filename)
+{
+  /// Read the pedestals from an ASCII file.                                  \n
+  /// Format of that file is one line per channel :                           \n
+  ///-------------------------------------------------------------------------\n
+  /// BUS_PATCH MANU_ADDR CHANNEL      MEAN       SIGMA                       \n
+  ///-------------------------------------------------------------------------\n
+  ///                                                                         \n
+  /// Return kFALSE if reading was not successfull.                           \n
+  ///
+  
+  TString sFilename(gSystem->ExpandPathName(filename));
   
-       UInt_t result = Master()->Store("Calib", "Pedestals", fPedestals, &metaData, 0, 0);
+  Master()->Log(Form("Reading %s",sFilename.Data()));
   
-  return result;  
+  Int_t n = AliMUONTrackerIO::ReadPedestals(sFilename.Data(),*fPedestals);
+  
+  switch (n)
+  {
+    case -1:
+      Master()->Log(Form("Could not open %s",sFilename.Data()));
+      break;
+  }
+  
+  return n;
 }
 
 //_____________________________________________________________________________
-Bool_t
-AliMUONPedestalSubprocessor::ReadFile(const char* filename)
+Int_t
+AliMUONPedestalSubprocessor::ReadConfigFile(const char* filename)
 {
-  // Read the pedestals from an ASCII file.
-  // Format of that file is one line per channel :
-  //---------------------------------------------------------------------------
-  // BUS_PATCH MANU_ADDR CHANNEL      MEAN       SIGMA
-  //---------------------------------------------------------------------------
-  //
-  // Return kFALSE if reading was not successfull.
-  //
-  
-  AliInfo(Form("Reading %s",filename));
-  
-  std::ifstream in(gSystem->ExpandPathName(filename));
-  if (!in.good()) return kFALSE;
-  
-  char line[80];
-  Int_t busPatchID, manuID, manuChannel;
-  Float_t pedMean, pedSigma;
-  static const Int_t kNchannels(64);
-  static Bool_t replace(kFALSE);
-  
-  while ( in.getline(line,80) )
+  /// Read the configuration from an ASCII file.                              
+  /// Format of that file is one line per manu :                              
+  /// BUS_PATCH MANU_ADDR
+  /// Return kFALSE if reading was not successfull.                           
+  ///
+  
+  TString sFilename(gSystem->ExpandPathName(filename));
+  
+  Master()->Log(Form("Reading %s",sFilename.Data()));
+  
+  Int_t n = AliMUONTrackerIO::ReadConfig(sFilename.Data(),*fConfig);
+  
+  switch (n)
   {
-    if ( line[0] == '/' && line[1] == '/' ) continue;
-    std::istringstream sin(line);
-    sin >> busPatchID >> manuID >> manuChannel >> pedMean >> pedSigma;
-    Int_t detElemID = AliMpDDLStore::Instance()->GetDEfromBus(busPatchID);
-    AliDebug(3,Form("BUSPATCH %3d DETELEMID %4d MANU %3d CH %3d MEAN %7.2f SIGMA %7.2f",
-             busPatchID,detElemID,manuID,manuChannel,pedMean,pedSigma));
-    
-    AliMUONVCalibParam* ped = 
-      static_cast<AliMUONVCalibParam*>(fPedestals->Get(detElemID,manuID));
-    
-    if (!ped) 
-    {
-      ped = new AliMUONCalibParam2F(kNchannels,AliMUONCalibParam2F::InvalidFloatValue());  
-      fPedestals->Set(detElemID,manuID,ped,replace);
-    }
-    ped->SetValueAsFloat(manuChannel,0,pedMean);
-    ped->SetValueAsFloat(manuChannel,1,pedSigma);
+    case -1:
+      Master()->Log(Form("Could not open %s",sFilename.Data()));
+      break;
   }
-  in.close();
-  return kTRUE;
+  
+  return n;
 }
 
 
@@ -182,12 +319,7 @@ void
 AliMUONPedestalSubprocessor::Print(Option_t* opt) const
 {
   /// ouput to screen
-  AliMUONVDataIterator* it = fPedestals->Iterator();
-  AliMUONObjectPair* p;
-
-  while ( ( p = static_cast<AliMUONObjectPair*>(it->Next() ) ) )
-  {
-    AliMUONVCalibParam* value = static_cast<AliMUONVCalibParam*>(p->Value());
-    value->Print(opt);
-  }
+  if (fPedestals) fPedestals->Print("",opt);
+  if (fConfig) fConfig->Print("",opt);
 }
+