LCOV - code coverage report
Current view: top level - MUON/MUONmapping - AliMpManuStore.cxx (source / functions) Hit Total Coverage
Test: coverage.info Lines: 71 177 40.1 %
Date: 2016-06-14 17:26:59 Functions: 8 19 42.1 %

          Line data    Source code
       1             : /**************************************************************************
       2             :  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
       3             :  *                                                                        *
       4             :  * Author: The ALICE Off-line Project.                                    *
       5             :  * Contributors are mentioned in the code where appropriate.              *
       6             :  *                                                                        *
       7             :  * Permission to use, copy, modify and distribute this software and its   *
       8             :  * documentation strictly for non-commercial purposes is hereby granted   *
       9             :  * without fee, provided that the above copyright notice appears in all   *
      10             :  * copies and that both the copyright notice and this permission notice   *
      11             :  * appear in the supporting documentation. The authors make no claims     *
      12             :  * about the suitability of this software for any purpose. It is          *
      13             :  * provided "as is" without express or implied warranty.                  *
      14             :  **************************************************************************/
      15             : 
      16             : // $Id$
      17             : // Category: management
      18             : 
      19             : //-----------------------------------------------------------------------------
      20             : // Class AliMpManuStore
      21             : // --------------------
      22             : // The container class for manu serial numbersd
      23             : // Authors: Ivana Hrivnacova, IPN Orsay
      24             : //          Christian Finck, SUBATECH Nantes
      25             : //-----------------------------------------------------------------------------
      26             : 
      27             : #include "AliMpManuStore.h"
      28             : 
      29             : #include "AliMpDEStore.h"
      30             : #include "AliMpDEManager.h"
      31             : #include "AliMpDetElement.h"
      32             : #include "AliMpConstants.h"
      33             : #include "AliMpDataStreams.h"
      34             : #include "AliMpFiles.h"
      35             : #include "AliMpHelper.h"
      36             : #include "AliMpConstants.h"
      37             : #include "AliMpEncodePair.h"
      38             : 
      39             : #include "AliLog.h"
      40             : 
      41             : #include <Riostream.h>
      42             : #include <TClass.h>
      43             : #include <TSystem.h>
      44             : #include <TObjString.h>
      45             : #include <TObjArray.h>
      46             : #include <TMap.h>
      47             : 
      48             : #include <fstream>
      49             : #include <cstdlib>
      50             : 
      51             : /// \cond CLASSIMP
      52          18 : ClassImp(AliMpManuStore)
      53             : /// \endcond
      54             : 
      55             : AliMpManuStore* AliMpManuStore::fgInstance = 0;
      56             : Bool_t          AliMpManuStore::fgWarnIfDoublon = kFALSE;
      57             : 
      58             : //
      59             : // static methods
      60             : //
      61             : 
      62             : //______________________________________________________________________________
      63             : AliMpManuStore* AliMpManuStore::Instance(Bool_t warn) 
      64             : {
      65             :     /// Create the DDL store if it does not yet exist
      66             :     /// and return its instance
      67             : 
      68       67316 :     if ( ! fgInstance && warn  ) {
      69           0 :         AliWarningClass("Manu Store has not been loaded");
      70           0 :     }
      71             : 
      72       67314 :     return fgInstance;
      73             : }
      74             : 
      75             : //______________________________________________________________________________
      76             : AliMpManuStore* AliMpManuStore::ReadData(const AliMpDataStreams& dataStreams, 
      77             :                                          Bool_t warn) 
      78             : {
      79             :     /// Load the DDL store from ASCII data files
      80             :     /// and return its instance
      81             : 
      82           2 :     if ( fgInstance ) {
      83           0 :         if ( warn )
      84           0 :             AliWarningClass("Manu Store has been already loaded");
      85           0 :         return fgInstance;
      86             :     }
      87             : 
      88           2 :     if ( dataStreams.GetReadFromFiles() )
      89           0 :       AliInfoClass("Reading Manu Store from ASCII files.");
      90             : 
      91           4 :     fgInstance = new AliMpManuStore(dataStreams);
      92           2 :     return fgInstance;
      93           2 : }
      94             : 
      95             : //
      96             : // ctors, dtor
      97             : //
      98             : 
      99             : 
     100             : //______________________________________________________________________________
     101             : AliMpManuStore::AliMpManuStore(const AliMpDataStreams& dataStreams)
     102           2 : : TObject(),
     103           2 :   fManuToSerialNbs(),
     104           2 :   fSerialNbToManus(),
     105           2 :   fNofManusInDE(),
     106           2 :   fNofManus(0)
     107          10 : {  
     108             : /// Standard constructor
     109             : 
     110          10 :   AliDebug(1,"");
     111             : 
     112             :   // Check if DE store is loaded
     113           4 :   if ( ! AliMpDEStore::Instance(false) ) {
     114           0 :      AliErrorStream()
     115           0 :        << "Mapping segmentation has not be loaded. Cannont load Manu store"
     116           0 :        << endl;
     117             :      return;
     118             :   }      
     119             : 
     120           2 :   ReadManuSerial(dataStreams);
     121           4 : }
     122             : 
     123             : //______________________________________________________________________________
     124             : AliMpManuStore::AliMpManuStore(TRootIOCtor* /*ioCtor*/)
     125           0 : : TObject(),
     126           0 :   fManuToSerialNbs(),
     127           0 :   fSerialNbToManus(),
     128           0 :   fNofManusInDE(),
     129           0 :   fNofManus(0)
     130           0 : {  
     131             : /// Constructor for IO
     132             : 
     133           0 :   AliDebug(1,"");
     134           0 : }
     135             : 
     136             : 
     137             : //______________________________________________________________________________
     138             : AliMpManuStore::~AliMpManuStore()
     139           0 : {
     140             : /// Destructor
     141             : 
     142           0 :   AliDebug(1,"");
     143             :   
     144           0 :   fgInstance = 0;
     145           0 : }
     146             : 
     147             : //
     148             : // private methods
     149             : //
     150             : 
     151             : //______________________________________________________________________________
     152             : Bool_t AliMpManuStore::ReadData(const AliMpDataStreams& dataStreams,
     153             :                                 const AliMpDetElement* de, Int_t& nofManus)
     154             : {
     155             : /// Read manu serial numbers for the given detection element
     156             : 
     157         912 :   Int_t deId = de->GetId();
     158         456 :   TString deName = de->GetDEName();
     159             :   AliMp::StationType stationType 
     160         456 :     =  AliMpDEManager::GetStationType(de->GetId());
     161             :   AliMq::Station12Type station12Type 
     162         456 :     =  AliMpDEManager::GetStation12Type(de->GetId());
     163             : 
     164             :   // Nothing to be done for trigger
     165         456 :   if ( stationType == AliMp::kStationTrigger ) {
     166         144 :     nofManus = 0;
     167         144 :     return kTRUE;
     168             :   }  
     169             : 
     170         318 :   static Int_t manuMask = AliMpConstants::ManuMask(AliMp::kNonBendingPlane);
     171             : 
     172             :   istream& in 
     173         624 :     = dataStreams.
     174             :         CreateDataStream(
     175         312 :           AliMpFiles::ManuToSerialPath(deName, stationType, station12Type));
     176             : 
     177         312 :   char line[80];
     178             : 
     179         312 :   nofManus = 0;
     180      136886 :   while ( in.getline(line,80) ) {
     181             : 
     182       33994 :     if ( line[0] == '#' ) continue;
     183             : 
     184       33656 :     TString tmp(AliMpHelper::Normalize(line));
     185             : 
     186      100968 :     TObjArray* stringList = tmp.Tokenize(TString(" "));
     187             : 
     188      100968 :     Int_t manuId     = atoi( ((TObjString*)stringList->At(0))->GetName());
     189      100968 :     Int_t manuSerial = atoi( ((TObjString*)stringList->At(2))->GetName());
     190             :       
     191       67312 :     TString sPlane = ((TObjString*)stringList->At(1))->GetString();
     192             : 
     193             :     // filling manuId <> manuSerial
     194      100968 :     if (!sPlane.CompareTo(PlaneTypeName(AliMp::kBendingPlane)))
     195       18388 :         AddManu(deId, manuId, manuSerial);
     196             :     else 
     197       15268 :         AddManu(deId, manuId + manuMask, manuSerial);
     198             :         
     199       33656 :     ++nofManus;    
     200             : 
     201       67312 :     delete stringList;
     202       33656 :   }
     203             :   
     204         624 :   delete &in;
     205             :    
     206             :   return kTRUE;
     207         768 : }
     208             : 
     209             : //______________________________________________________________________________
     210             : Bool_t  AliMpManuStore::ReadManuSerial(const AliMpDataStreams& dataStreams)
     211             : {
     212             : /// Read data files for all detection elements.
     213             : /// Return true if reading was successful.
     214             : 
     215             :   Bool_t isOk = kTRUE;
     216             : 
     217             :   // Loop over DE
     218           4 :   AliMpDEIterator it;
     219        1374 :   for ( it.First(); ! it.IsDone(); it.Next() ) {
     220             : 
     221         456 :     AliMpDetElement* detElement = it.CurrentDE();
     222             : 
     223         456 :     Int_t nofManus;
     224         912 :     Bool_t result = ReadData(dataStreams, detElement, nofManus);
     225         456 :     fNofManusInDE.Add(detElement->GetId(), nofManus);
     226         456 :     fNofManus += nofManus;
     227             :     
     228         912 :     AliDebugStream(2) 
     229        1368 :       << "Adding " << nofManus << " manus for de " 
     230         912 :       << detElement->GetId() << endl;
     231             :     
     232         456 :     isOk = isOk && result;
     233         456 :   }
     234             :   
     235           2 :   return isOk;  
     236           2 : }   
     237             : 
     238             : //______________________________________________________________________________
     239             : void  AliMpManuStore::ReplaceManu(Int_t detElemId, Int_t manuId, Int_t serialNb) 
     240             : {
     241             : /// Replace manu in the map.
     242             : /// As TExMap has no replcae function, we have to rebuild map once again.
     243             : /// Not yet in use, declared private.
     244             : 
     245           0 :   Long_t index = AliMp::Pair(detElemId, manuId);
     246             : 
     247           0 :   TExMap newManuToSerialNbs;
     248             :   // Loop over map
     249           0 :   TExMapIter it(&fManuToSerialNbs);
     250             : 
     251             : #if (defined(ROOT_VERSION_CODE) && ROOT_VERSION_CODE >= 334082) || \
     252             :   (defined(ROOT_SVN_REVISION) && ROOT_SVN_REVISION >= 29598) 
     253           0 :   Long64_t key;
     254           0 :   Long64_t value;
     255             : #else
     256             :   Long_t key;
     257             :   Long_t value;
     258             : #endif
     259           0 :   while ( ( it.Next(key, value) ) ) {
     260             : 
     261           0 :     if ( key != index ) 
     262           0 :       newManuToSerialNbs.Add(key, value);
     263             :     else
     264           0 :       newManuToSerialNbs.Add(index, Long_t(serialNb));
     265             :   }
     266             :       
     267           0 :   TExMap newSerialNbToManus;
     268             :   // Loop over map
     269           0 :   TExMapIter it2(&fSerialNbToManus);
     270           0 :   while ( ( it2.Next(key, value) ) ) {
     271             : 
     272           0 :     if ( value != index ) 
     273           0 :       newSerialNbToManus.Add(key, value);
     274             :     else
     275           0 :       newSerialNbToManus.Add(Long_t(serialNb), index);
     276             :   }
     277             : 
     278             :   // And now replace the maps
     279           0 :   fManuToSerialNbs = newManuToSerialNbs;
     280           0 :   fSerialNbToManus = newManuToSerialNbs;
     281           0 : }     
     282             : 
     283             : 
     284             : //______________________________________________________________________________
     285             : Bool_t  AliMpManuStore::WriteData(const TString& outDir)
     286             : {
     287             : /// Write data files for all detection elements.
     288             : /// Return true if reading was successful.
     289             : /// Not yet in use, declared private.
     290             : 
     291           0 :   TString curDir = gSystem->pwd();
     292             : 
     293             :   // Create top directory
     294             :   //
     295           0 :   if ( gSystem->OpenDirectory(outDir.Data()) ) {
     296           0 :     AliErrorStream() 
     297           0 :       << "Directory " << outDir.Data() << " already exists" << endl;
     298           0 :     return kFALSE;
     299             :   }
     300             :   else {
     301           0 :     AliDebugStream(2) << "Making directory " <<  outDir.Data() << endl;
     302           0 :     gSystem->mkdir(outDir.Data());
     303             :   }  
     304             : 
     305             :   // Loop over DE
     306           0 :   AliMpDEIterator it;
     307           0 :   for ( it.First(); ! it.IsDone(); it.Next() ) {
     308             :   
     309           0 :     AliMpDetElement* detElement = it.CurrentDE();
     310           0 :     Int_t detElemId = detElement->GetId();
     311           0 :     TString deName = detElement->GetDEName();
     312             :     AliMp::StationType stationType 
     313           0 :       =  AliMpDEManager::GetStationType(detElemId);
     314             :     AliMq::Station12Type station12Type 
     315           0 :       =  AliMpDEManager::GetStation12Type(detElemId);
     316             :       
     317           0 :     if ( stationType == AliMp::kStationTrigger ) continue;  
     318             : 
     319             :     // Create directory if it does not yet exist
     320             :     //
     321           0 :     TString dirPath = outDir + AliMpFiles::StationDataDir(stationType, station12Type);
     322           0 :     if ( ! gSystem->OpenDirectory(dirPath.Data()) ) {
     323           0 :       AliDebugStream(2) << "Making directory " <<  dirPath.Data() << endl;
     324           0 :       gSystem->mkdir(dirPath.Data());
     325             :     }  
     326             : 
     327             :     // Compose output file path 
     328             :     //
     329           0 :     string dataPath = AliMpFiles::ManuToSerialPath(deName, stationType, station12Type).Data();
     330           0 :     string top = AliMpFiles::GetTop().Data();
     331           0 :     if ( dataPath.find(top) != string::npos ) dataPath.erase(0, top.size()+1);
     332           0 :     dataPath.erase(0,dataPath.find('/')+1);
     333           0 :     TString dataPath1 = dataPath;
     334           0 :     TString filePath = outDir + "/" + dataPath1; 
     335             : 
     336             :     // Open file
     337             :     //
     338           0 :     ofstream out(filePath.Data());
     339           0 :     if ( ! out.good() ) {
     340           0 :       AliErrorStream() 
     341           0 :         << "Cannot open output file  " << filePath.Data() << endl;
     342           0 :       return kFALSE;  
     343             :     }
     344             :     
     345             :     // Loop over map
     346           0 :     TExMapIter it2(&fManuToSerialNbs);
     347             : #if (defined(ROOT_SVN_REVISION) && ROOT_SVN_REVISION >= 29598) || \
     348             :   (defined(ROOT_VERSION_CODE) && ROOT_VERSION_CODE >= ROOT_VERSION(5,25,02))
     349           0 :     Long64_t key;
     350           0 :     Long64_t value;
     351             : #else
     352             :     Long_t key;
     353             :     Long_t value;
     354             : #endif
     355           0 :     while ( ( it2.Next(key, value) ) ) {
     356           0 :       Int_t pairFirst = AliMp::PairFirst(key);
     357             :       
     358           0 :       if ( pairFirst != detElemId ) continue;
     359             :       
     360           0 :       Int_t manuId = AliMp::PairSecond(key);
     361             : 
     362           0 :       AliDebugStream(3) 
     363           0 :         << "Go to write " << key << " " 
     364           0 :         << pairFirst << " " << manuId << " " << value << endl;
     365             : 
     366           0 :       static Int_t manuMask = AliMpConstants::ManuMask(AliMp::kNonBendingPlane);
     367             : 
     368           0 :       TString planeName = PlaneTypeName(AliMp::kBendingPlane);
     369           0 :       if ( manuId> manuMask ) {
     370           0 :         planeName = PlaneTypeName(AliMp::kNonBendingPlane);
     371           0 :         manuId -= manuMask;
     372           0 :       } 
     373           0 :       out << manuId << " " << planeName.Data() <<  " " << value << endl;
     374             :       
     375           0 :       AliDebugStream(3) 
     376           0 :         << manuId << " " << planeName.Data() <<  " " << value << endl;
     377           0 :     }  
     378           0 :     out.close();
     379           0 :   }   
     380           0 :   gSystem->cd(curDir); 
     381           0 :   return kTRUE;  
     382           0 : }   
     383             : 
     384             : //
     385             : // public methods
     386             : //
     387             : 
     388             : 
     389             : //______________________________________________________________________________
     390             : Int_t  AliMpManuStore::NofManus() const
     391             : { 
     392             : /// Return total number of manus in the store
     393             : 
     394           0 :   return fNofManus;
     395             : }  
     396             :   
     397             : 
     398             : //______________________________________________________________________________
     399             : Int_t  AliMpManuStore::NofManus(Int_t detElemId) const 
     400             : { 
     401             : /// Return number of manus in given detection element
     402             : 
     403           0 :    if ( ! AliMpDEManager::IsValidDetElemId(detElemId, kTRUE) ) return 0;   
     404             : 
     405           0 :    return fNofManusInDE.GetValue(detElemId); 
     406           0 : }
     407             : 
     408             : //______________________________________________________________________________
     409             : Bool_t  AliMpManuStore::AddManu(Int_t detElemId, Int_t manuId, Int_t serialNb) 
     410             : {
     411             : /// Add manu to the map
     412             : 
     413       67312 :   Long_t index = AliMp::Pair(detElemId, manuId);
     414             :   
     415       33656 :   AliDebugStream(2)
     416       33656 :     << "Adding (" << detElemId << "," <<  manuId 
     417       33656 :     << ") as index=" << index << " and serialNb=" << serialNb << endl;
     418             :   
     419       33656 :   fManuToSerialNbs.Add(index, Long_t(serialNb));
     420             :   
     421       33656 :   Long_t value = fSerialNbToManus.GetValue(Long_t(serialNb));
     422       33656 :   if ( value ) {
     423         190 :     if ( fgWarnIfDoublon ) {
     424           0 :       AliWarningStream() 
     425           0 :         << "Serial number " << serialNb 
     426           0 :         << " already present for (detElemId, manuId) = " ;
     427           0 :         AliMp::PairPut(AliWarningStream(), (MpPair_t) value) << endl;
     428           0 :      }
     429         190 :      return kFALSE;    
     430             :   }
     431             :   else {
     432       33466 :     fSerialNbToManus.Add(Long_t(serialNb), index);
     433       33466 :     return kTRUE;
     434             :   }  
     435       33656 : }     
     436             : 
     437             : 
     438             : //______________________________________________________________________________
     439             : Int_t AliMpManuStore::GetManuSerial(Int_t detElemId, Int_t manuId) const
     440             : {
     441             : /// Return manu serial number for given detElemId and manuId
     442             : 
     443           0 :   Long_t index = AliMp::Pair(detElemId, manuId);
     444             :   // cout << index << " " << fManuToSerialNbs.GetValue(index) << endl;
     445             :   
     446           0 :   return fManuToSerialNbs.GetValue(index);
     447             : }  
     448             : 
     449             : //______________________________________________________________________________
     450             : MpPair_t  AliMpManuStore::GetDetElemIdManu(Int_t manuSerial) const
     451             : {
     452             : /// Return detElemId and manuId for given manu serial number 
     453             : /// as encoded pair
     454             : 
     455           0 :   return fSerialNbToManus.GetValue(Long_t(manuSerial));
     456             : }  
     457             : 

Generated by: LCOV version 1.11