]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - MUON/AliMUONTrackerIO.cxx
o add Reset function to CalPad and CalROC o Add functionality to AliTPCdataQA - Reset...
[u/mrichter/AliRoot.git] / MUON / AliMUONTrackerIO.cxx
index c332e2c321f223b945716f7c510fc27b49ed3020..3e8b6fe75e10db3d984c0c0704f1e0973fa4a274 100644 (file)
@@ -16,6 +16,7 @@
 // $Id$
 
 #include <cstdlib>
+#include <iostream>
 #include "AliMUONTrackerIO.h"
 
 /// \class AliMUONTrackerIO
 ///
 /// \author Laurent Aphecetche, Subatech
 
+using std::ostringstream;
+using std::istringstream;
+using std::cout;
+using std::endl;
 /// \cond CLASSIMP
 ClassImp(AliMUONTrackerIO)
 /// \endcond
 
+#include "AliDCSValue.h"
 #include "AliLog.h"
+#include "AliMUONCalibParamND.h"
 #include "AliMUONCalibParamNF.h"
 #include "AliMUONVStore.h"
 #include "AliMpConstants.h"
 #include "AliMpDDLStore.h"
+#include "AliMpDEManager.h"
+#include "AliMpDetElement.h"
 #include <Riostream.h>
 #include <TClass.h>
 #include <TObjString.h>
@@ -53,6 +62,110 @@ AliMUONTrackerIO::~AliMUONTrackerIO()
   /// dtor
 }
 
+//_____________________________________________________________________________
+Int_t 
+AliMUONTrackerIO::ReadOccupancy(const char* filename,AliMUONVStore& occupancyMap)
+{
+  /// Read occupancy file created by online DA
+  /// and append values to the occupancyMap store.
+  /// Expected format of the file is :
+  /// busPatchId manuId sumofn nevt
+  
+  TString sFilename(gSystem->ExpandPathName(filename));
+  
+  std::ifstream in(sFilename.Data());
+  if (!in.good()) 
+  {
+    return kCannotOpenFile;
+  }
+  
+  TString datastring;
+  ostringstream stream;
+  char line[1024];
+  while ( in.getline(line,1024) )
+       stream << line << "\n";
+  
+  in.close();
+  
+  return DecodeOccupancy(stream.str().c_str(),occupancyMap);
+  
+}
+
+//_____________________________________________________________________________
+Int_t 
+AliMUONTrackerIO::DecodeOccupancy(const char* data, AliMUONVStore& occupancyMap)
+{
+  /// Decode occupancy string created append values to the occupancyMap store.
+  /// Expected format of the file is :
+  /// busPatchId manuId sumofn nevt
+  if ( ! AliMpDDLStore::Instance(kFALSE) )
+  {
+    AliErrorClass("Mapping not loaded. Cannot work");
+    return kNoMapping;
+  }
+  
+  char line[1024];
+  istringstream in(data);
+  
+  Int_t n(0);
+  
+  while ( in.getline(line,1024) )
+  {
+    AliDebugClass(3,Form("line=%s",line));
+    if ( line[0] == '/' && line[1] == '/' ) continue;
+    std::istringstream sin(line);
+    
+    Int_t busPatchId, manuId;
+    Int_t numberOfEvents;
+    Double_t sumn;
+
+    sin >> busPatchId >> manuId >> sumn >> numberOfEvents;
+    
+    if ( busPatchId == -1 && manuId == -1 && sumn == 0 && numberOfEvents == 0 )
+    {
+      /// DA could not produce information (because run failed somehow). 
+      /// That's OK, but let the world know about it
+      return kNoInfoFile;
+    }
+    
+    Int_t detElemId = AliMpDDLStore::Instance()->GetDEfromBus(busPatchId);
+
+    AliMpDetElement* de = AliMpDDLStore::Instance()->GetDetElement(detElemId);
+
+    Int_t numberOfChannelsInManu = -1;
+    
+    if (de) numberOfChannelsInManu = de->NofChannelsInManu(manuId);
+
+    if ( numberOfChannelsInManu <= 0 ) 
+    {
+      AliErrorClass(Form("BP %5d DE %5d MANU %5d nchannels=%d",busPatchId,detElemId,manuId,numberOfChannelsInManu));
+      continue;      
+    }
+    
+    AliMUONVCalibParam* occupancy = 
+    static_cast<AliMUONVCalibParam*>(occupancyMap.FindObject(detElemId,manuId));
+    if (occupancy) 
+    {
+      AliErrorClass(Form("DE %5d MANU %5d is already there ?!",detElemId,manuId));
+      continue;
+    }
+        
+    occupancy = new AliMUONCalibParamND(5,1,detElemId,manuId,0);
+
+    occupancyMap.Add(occupancy);
+    
+    occupancy->SetValueAsDouble(0,0,sumn);
+    occupancy->SetValueAsDouble(0,1,sumn); // with only 0 and 1s, sumw = sumw2 = sumn
+    occupancy->SetValueAsDouble(0,2,sumn);
+    occupancy->SetValueAsInt(0,3,numberOfChannelsInManu);
+    occupancy->SetValueAsInt(0,4,numberOfEvents);
+    ++n;
+  }
+  
+  return n;
+}
+
 //_____________________________________________________________________________
 Int_t 
 AliMUONTrackerIO::ReadPedestals(const char* filename, AliMUONVStore& pedStore)
@@ -70,18 +183,20 @@ AliMUONTrackerIO::ReadPedestals(const char* filename, AliMUONVStore& pedStore)
     return kCannotOpenFile;
   }
   
-  TString datastring;
-  datastring.ReadFile(in);
-    
+  ostringstream stream;
+  char line[1024];
+  while ( in.getline(line,1024) )
+       stream << line << "\n";
+  
   in.close();
 
-  return DecodePedestals(datastring,pedStore);
+  return DecodePedestals(stream.str().c_str(),pedStore);
   
 }
 
 //_____________________________________________________________________________
 Int_t 
-AliMUONTrackerIO::DecodePedestals(TString data, AliMUONVStore& pedStore)
+AliMUONTrackerIO::DecodePedestals(const char* data, AliMUONVStore& pedStore)
 {
   /// Read pedestal Data (produced by the MUONTRKda.exe program for instance)
   /// and append the read values into the given VStore
@@ -92,7 +207,7 @@ AliMUONTrackerIO::DecodePedestals(TString data, AliMUONVStore& pedStore)
   Int_t busPatchID, manuID, manuChannel;
   Float_t pedMean, pedSigma;
   Int_t n(0);
-  istringstream in(data.Data());
+  istringstream in(data);
   
   while ( in.getline(line,1024) )
   {
@@ -101,6 +216,14 @@ AliMUONTrackerIO::DecodePedestals(TString data, AliMUONVStore& pedStore)
     std::istringstream sin(line);
     sin >> busPatchID >> manuID >> manuChannel >> pedMean >> pedSigma;
     Int_t detElemID = AliMpDDLStore::Instance()->GetDEfromBus(busPatchID);
+    
+    if ( !AliMpDEManager::IsValidDetElemId(detElemID) )
+    {
+      AliErrorClass(Form("Got an invalid DE = %d from busPatchId=%d manuId=%d",
+                         detElemID,busPatchID,manuID));
+      continue;
+    }
+    
     AliDebugClass(3,Form("BUSPATCH %3d DETELEMID %4d MANU %3d CH %3d MEAN %7.2f SIGMA %7.2f",
                     busPatchID,detElemID,manuID,manuChannel,pedMean,pedSigma));
                    
@@ -141,31 +264,29 @@ AliMUONTrackerIO::ReadGains(const char* filename, AliMUONVStore& gainStore,
     return kCannotOpenFile;
   }
   
-  TString datastring;
   ostringstream stream;
   char line[1024];
   while ( in.getline(line,1024) )
        stream << line << "\n";
-  datastring = TString(stream.str().c_str());
   
   in.close();
   
-  return DecodeGains(datastring,gainStore,comment);
+  return DecodeGains(stream.str().c_str(),gainStore,comment);
 
 }
 
 //_____________________________________________________________________________
 Int_t 
-AliMUONTrackerIO::DecodeGains(TString data, AliMUONVStore& gainStore,
-                            TString& comment)
+AliMUONTrackerIO::DecodeGains(const char* data, AliMUONVStore& gainStore,
+                              TString& comment)
 {
   /// Read gain file (produced by the MUONTRKda.exe program for instance)
   /// and append the read values into the given VStore
-  /// To be used when the input is a TString (for instance when getting data 
+  /// To be used when the input is a string (for instance when getting data 
   /// from AMORE DB).
   
   char line[1024];
-  istringstream in(data.Data());
+  istringstream in(data);
   Int_t busPatchID, manuID, manuChannel;
   Float_t a0, a1;
   Int_t thres;
@@ -176,7 +297,7 @@ AliMUONTrackerIO::DecodeGains(TString data, AliMUONVStore& gainStore,
   Int_t* runs(0x0);
   Int_t* dac(0x0);
   Int_t nDAC(0);
-  Int_t iDAC(0);
+  Int_t nInit(0),f1nbp(0),f2nbp(0);
   
   while ( in.getline(line,1024) )
   {
@@ -187,6 +308,8 @@ AliMUONTrackerIO::DecodeGains(TString data, AliMUONVStore& gainStore,
       if ( sline.Contains("DUMMY") )
       {
         AliDebugClass(1,"Got a dummy file here");
+        delete [] runs;
+        delete [] dac;
         return kDummyFile;
       }
       if ( sline.Contains("* Run") )
@@ -198,6 +321,7 @@ AliMUONTrackerIO::DecodeGains(TString data, AliMUONVStore& gainStore,
           runNumber = s.Atoi();
           AliDebugClass(1,Form("runNumber is %d",runNumber));
         }            
+       delete a;
       }
       if ( sline.Contains("DAC values") )
       {
@@ -207,12 +331,28 @@ AliMUONTrackerIO::DecodeGains(TString data, AliMUONVStore& gainStore,
         {
           if ( nDAC < 100 ) 
           {
+            delete[] runs;
+            delete[] dac;
             runs = new Int_t[nDAC];
             dac = new Int_t[nDAC];
             // skip two lines
             in.getline(line,1024);
+            sline = line;
+            if (!sline.Contains("*  nInit ="))
+            {
+              AliErrorClass("Improper format : was expecting nInit= line...");              
+            }
+            else
+            {
+              sscanf(line,"//   *  nInit = %d  *  f1nbp = %d  *  f2nbp = %d",&nInit,&f1nbp,&f2nbp);
+              AliDebugClass(1,Form("nInit = %d",nInit));
+              AliDebugClass(1,Form("f1nbp = %d",f1nbp));
+              AliDebugClass(1,Form("f2nbp = %d",f2nbp));
+            }
+            in.getline(line,1024);
             in.getline(line,1024);
             // then get run and dac values
+            Int_t iDAC(0);
             for ( Int_t i = 0; i < nDAC; ++i ) 
             {
               in.getline(line,1024);
@@ -228,6 +368,8 @@ AliMUONTrackerIO::DecodeGains(TString data, AliMUONVStore& gainStore,
           {
             AliErrorClass(Form("Something went wrong, as I get too big nDAC = %d",nDAC));
             nDAC = 0;
+            delete [] runs;
+            delete [] dac;
             return kFormatError;
           }
         }
@@ -239,6 +381,14 @@ AliMUONTrackerIO::DecodeGains(TString data, AliMUONVStore& gainStore,
            &a0,&a1,&thres,&qual); 
     AliDebugClass(3,Form("line=%s",line));
     Int_t detElemID = AliMpDDLStore::Instance()->GetDEfromBus(busPatchID);
+    
+    if ( !AliMpDEManager::IsValidDetElemId(detElemID) )
+    {
+      AliErrorClass(Form("Got an invalid DE = %d from busPatchId=%d manuId=%d",
+                         detElemID,busPatchID,manuID));
+      continue;
+    }
+    
     AliDebugClass(3,Form("BUSPATCH %3d DETELEMID %4d MANU %3d CH %3d A0 %7.2f "
                     "A1 %e THRES %5d QUAL %x",
                     busPatchID,detElemID,manuID,manuChannel,a0,a1,thres,qual));
@@ -271,6 +421,10 @@ AliMUONTrackerIO::DecodeGains(TString data, AliMUONVStore& gainStore,
   {
     comment += Form(";(RUN %d = DAC %d)",runs[i],dac[i]);
   }
+  comment += Form(";(nDAC = %d)",nDAC);
+  comment += Form(";(nInit = %d)",nInit);
+  comment += Form(";(f1nbp = %d)",f1nbp);
+  comment += Form(";(f2nbp = %d)",f2nbp);
   
   delete[] runs;
   delete[] dac;
@@ -280,20 +434,44 @@ AliMUONTrackerIO::DecodeGains(TString data, AliMUONVStore& gainStore,
 
 //_____________________________________________________________________________
 Int_t
-AliMUONTrackerIO::ReadCapacitances(const char* file, AliMUONVStore& capaStore)
+AliMUONTrackerIO::ReadCapacitances(const char* filename, AliMUONVStore& capaStore)
 {
   /// Read capacitance file
   /// and append the read values into the given VStore
   
-  ifstream in(gSystem->ExpandPathName(file));
-  if (in.bad()) return kCannotOpenFile;
+  TString sFilename(gSystem->ExpandPathName(filename));
+  
+  std::ifstream in(sFilename.Data());
+  if (!in.good()) 
+  {
+    return kCannotOpenFile;
+  }
+  
+  ostringstream stream;
+  char line[1024];
+  while ( in.getline(line,1024) )
+       stream << line << "\n";
+  
+  in.close();
+  
+  return DecodeCapacitances(stream.str().c_str(),capaStore);
+}
+
+//_____________________________________________________________________________
+Int_t
+AliMUONTrackerIO::DecodeCapacitances(const char* data, AliMUONVStore& capaStore)
+{
+  /// Read capacitances and append the read values into the given VStore
+  /// To be used when the input is a string (for instance when getting data 
+  /// from AMORE DB).
   
   Int_t ngenerated(0);
   
   char line[1024];
   Int_t serialNumber(-1);
   AliMUONVCalibParam* param(0x0);
-  
+  istringstream in(data);
+
   while ( in.getline(line,1024,'\n') )
   {
     if ( isdigit(line[0]) ) 
@@ -314,6 +492,9 @@ AliMUONTrackerIO::ReadCapacitances(const char* file, AliMUONVStore& capaStore)
       }      
       continue;
     }
+    
+    if (!param) continue;
+    
     Int_t channel;
     Float_t capaValue;
     Float_t injectionGain;
@@ -325,7 +506,110 @@ AliMUONTrackerIO::ReadCapacitances(const char* file, AliMUONVStore& capaStore)
     ++ngenerated;
   }
   
+  return ngenerated;
+}
+
+//_____________________________________________________________________________
+Int_t 
+AliMUONTrackerIO::ReadConfig(const char* filename, AliMUONVStore& confStore)
+{
+  /// Read config file (produced by the MUONTRKda.exe program for instance)
+  /// and append the read values into the given VStore
+  /// To be used when the input is a file (for instance when reading data 
+  /// from the OCDB).
+  
+  TString sFilename(gSystem->ExpandPathName(filename));
+  
+  std::ifstream in(sFilename.Data());
+  if (!in.good()) 
+  {
+    return kCannotOpenFile;
+  }
+  
+  ostringstream stream;
+  char line[1024];
+  while ( in.getline(line,1024) )
+       stream << line << "\n";
+  
   in.close();
   
-  return ngenerated;
+  return DecodeConfig(stream.str().c_str(),confStore);
 }
+
+//_____________________________________________________________________________
+Int_t 
+AliMUONTrackerIO::DecodeConfig(const char* data, AliMUONVStore& confStore)
+{
+  /// Read config data (produced by the MUONTRKda.exe program for instance)
+  /// and append the read values into the given VStore
+  /// To be used when the input is a TString (for instance when getting data 
+  /// from AMORE DB).
+
+  char line[1024];
+  Int_t busPatchID, manuID;
+  Int_t n(0);
+  istringstream in(data);
+  
+  while ( in.getline(line,1024) )
+  {
+    AliDebugClass(3,Form("line=%s",line));
+    if ( line[0] == '#' ) 
+    {
+      continue;
+    }
+    std::istringstream sin(line);
+    sin >> busPatchID >> manuID;
+
+    Int_t detElemID = AliMpDDLStore::Instance()->GetDEfromBus(busPatchID);
+
+    if ( !AliMpDEManager::IsValidDetElemId(detElemID) )
+    {
+      AliErrorClass(Form("Got an invalid DE = %d from busPatchId=%d manuId=%d",
+                         detElemID,busPatchID,manuID));
+      continue;
+    }
+    
+    AliMUONVCalibParam* conf = 
+    static_cast<AliMUONVCalibParam*>(confStore.FindObject(detElemID,manuID));
+    if (!conf) 
+    {
+      conf = new AliMUONCalibParamNF(1,1,detElemID,manuID,1);
+      confStore.Add(conf);
+    }
+    ++n;
+  }
+  
+  return n;
+}
+
+//_____________________________________________________________________________
+Int_t 
+AliMUONTrackerIO::WriteConfig(ofstream& out, const AliMUONVStore& confStore)
+{
+  /// Write the conf store as an ASCII file
+  /// Note that we are converting (back) the detElemId into a busPatchId
+  /// Return the number of lines written
+  
+  if ( !AliMpDDLStore::Instance() ) 
+  {
+    cout << "ERROR: mapping not loaded. Cannot work" << endl;
+    return 0;
+  }
+  
+  TIter next(confStore.CreateIterator());
+  AliMUONVCalibParam* param;
+  Int_t n(0);
+  
+  while ( (param=static_cast<AliMUONVCalibParam*>(next())) )
+  {
+    Int_t detElemId = param->ID0();
+    Int_t manuId = param->ID1();
+    
+    Int_t busPatchId = AliMpDDLStore::Instance()->GetBusPatchId(detElemId,manuId);
+    ++n;
+    
+    out << busPatchId << " " << manuId << endl;
+  }
+  return n;
+}
+