#include "AliMpManuStore.h"
#include "AliMpDEStore.h"
#include "AliMpDEManager.h"
#include "AliMpDetElement.h"
#include "AliMpConstants.h"
#include "AliMpDataStreams.h"
#include "AliMpFiles.h"
#include "AliMpHelper.h"
#include "AliMpConstants.h"
#include "AliMpEncodePair.h"
#include "AliLog.h"
#include <Riostream.h>
#include <TClass.h>
#include <TSystem.h>
#include <TObjString.h>
#include <TObjArray.h>
#include <TMap.h>
#include <fstream>
#include <cstdlib>
ClassImp(AliMpManuStore)
AliMpManuStore* AliMpManuStore::fgInstance = 0;
Bool_t AliMpManuStore::fgWarnIfDoublon = kFALSE;
AliMpManuStore* AliMpManuStore::Instance(Bool_t warn)
{
if ( ! fgInstance && warn ) {
AliWarningClass("Manu Store has not been loaded");
}
return fgInstance;
}
AliMpManuStore* AliMpManuStore::ReadData(const AliMpDataStreams& dataStreams,
Bool_t warn)
{
if ( fgInstance ) {
if ( warn )
AliWarningClass("Manu Store has been already loaded");
return fgInstance;
}
if ( dataStreams.GetReadFromFiles() )
AliInfoClass("Reading Manu Store from ASCII files.");
fgInstance = new AliMpManuStore(dataStreams);
return fgInstance;
}
AliMpManuStore::AliMpManuStore(const AliMpDataStreams& dataStreams)
: TObject(),
fManuToSerialNbs(),
fSerialNbToManus(),
fNofManusInDE(),
fNofManus(0)
{
AliDebug(1,"");
if ( ! AliMpDEStore::Instance(false) ) {
AliErrorStream()
<< "Mapping segmentation has not be loaded. Cannont load Manu store"
<< endl;
return;
}
ReadManuSerial(dataStreams);
}
AliMpManuStore::AliMpManuStore(TRootIOCtor* )
: TObject(),
fManuToSerialNbs(),
fSerialNbToManus(),
fNofManusInDE(),
fNofManus(0)
{
AliDebug(1,"");
}
AliMpManuStore::~AliMpManuStore()
{
AliDebug(1,"");
fgInstance = 0;
}
Bool_t AliMpManuStore::ReadData(const AliMpDataStreams& dataStreams,
const AliMpDetElement* de, Int_t& nofManus)
{
Int_t deId = de->GetId();
TString deName = de->GetDEName();
AliMp::StationType stationType
= AliMpDEManager::GetStationType(de->GetId());
AliMq::Station12Type station12Type
= AliMpDEManager::GetStation12Type(de->GetId());
if ( stationType == AliMp::kStationTrigger ) {
nofManus = 0;
return kTRUE;
}
static Int_t manuMask = AliMpConstants::ManuMask(AliMp::kNonBendingPlane);
istream& in
= dataStreams.
CreateDataStream(
AliMpFiles::ManuToSerialPath(deName, stationType, station12Type));
char line[80];
nofManus = 0;
while ( in.getline(line,80) ) {
if ( line[0] == '#' ) continue;
TString tmp(AliMpHelper::Normalize(line));
TObjArray* stringList = tmp.Tokenize(TString(" "));
Int_t manuId = atoi( ((TObjString*)stringList->At(0))->GetName());
Int_t manuSerial = atoi( ((TObjString*)stringList->At(2))->GetName());
TString sPlane = ((TObjString*)stringList->At(1))->GetString();
if (!sPlane.CompareTo(PlaneTypeName(AliMp::kBendingPlane)))
AddManu(deId, manuId, manuSerial);
else
AddManu(deId, manuId + manuMask, manuSerial);
++nofManus;
delete stringList;
}
delete ∈
return kTRUE;
}
Bool_t AliMpManuStore::ReadManuSerial(const AliMpDataStreams& dataStreams)
{
Bool_t isOk = kTRUE;
AliMpDEIterator it;
for ( it.First(); ! it.IsDone(); it.Next() ) {
AliMpDetElement* detElement = it.CurrentDE();
Int_t nofManus;
Bool_t result = ReadData(dataStreams, detElement, nofManus);
fNofManusInDE.Add(detElement->GetId(), nofManus);
fNofManus += nofManus;
AliDebugStream(2)
<< "Adding " << nofManus << " manus for de "
<< detElement->GetId() << endl;
isOk = isOk && result;
}
return isOk;
}
void AliMpManuStore::ReplaceManu(Int_t detElemId, Int_t manuId, Int_t serialNb)
{
Long_t index = AliMp::Pair(detElemId, manuId);
TExMap newManuToSerialNbs;
TExMapIter it(&fManuToSerialNbs);
#if (defined(ROOT_VERSION_CODE) && ROOT_VERSION_CODE >= 334082) || \
(defined(ROOT_SVN_REVISION) && ROOT_SVN_REVISION >= 29598)
Long64_t key;
Long64_t value;
#else
Long_t key;
Long_t value;
#endif
while ( ( it.Next(key, value) ) ) {
if ( key != index )
newManuToSerialNbs.Add(key, value);
else
newManuToSerialNbs.Add(index, Long_t(serialNb));
}
TExMap newSerialNbToManus;
TExMapIter it2(&fSerialNbToManus);
while ( ( it2.Next(key, value) ) ) {
if ( value != index )
newSerialNbToManus.Add(key, value);
else
newSerialNbToManus.Add(Long_t(serialNb), index);
}
fManuToSerialNbs = newManuToSerialNbs;
fSerialNbToManus = newManuToSerialNbs;
}
Bool_t AliMpManuStore::WriteData(const TString& outDir)
{
TString curDir = gSystem->pwd();
if ( gSystem->OpenDirectory(outDir.Data()) ) {
AliErrorStream()
<< "Directory " << outDir.Data() << " already exists" << endl;
return kFALSE;
}
else {
AliDebugStream(2) << "Making directory " << outDir.Data() << endl;
gSystem->mkdir(outDir.Data());
}
AliMpDEIterator it;
for ( it.First(); ! it.IsDone(); it.Next() ) {
AliMpDetElement* detElement = it.CurrentDE();
Int_t detElemId = detElement->GetId();
TString deName = detElement->GetDEName();
AliMp::StationType stationType
= AliMpDEManager::GetStationType(detElemId);
AliMq::Station12Type station12Type
= AliMpDEManager::GetStation12Type(detElemId);
if ( stationType == AliMp::kStationTrigger ) continue;
TString dirPath = outDir + AliMpFiles::StationDataDir(stationType, station12Type);
if ( ! gSystem->OpenDirectory(dirPath.Data()) ) {
AliDebugStream(2) << "Making directory " << dirPath.Data() << endl;
gSystem->mkdir(dirPath.Data());
}
string dataPath = AliMpFiles::ManuToSerialPath(deName, stationType, station12Type).Data();
string top = AliMpFiles::GetTop().Data();
if ( dataPath.find(top) != string::npos ) dataPath.erase(0, top.size()+1);
dataPath.erase(0,dataPath.find('/')+1);
TString dataPath1 = dataPath;
TString filePath = outDir + "/" + dataPath1;
ofstream out(filePath.Data());
if ( ! out.good() ) {
AliErrorStream()
<< "Cannot open output file " << filePath.Data() << endl;
return kFALSE;
}
TExMapIter it2(&fManuToSerialNbs);
#if (defined(ROOT_SVN_REVISION) && ROOT_SVN_REVISION >= 29598) || \
(defined(ROOT_VERSION_CODE) && ROOT_VERSION_CODE >= ROOT_VERSION(5,25,02))
Long64_t key;
Long64_t value;
#else
Long_t key;
Long_t value;
#endif
while ( ( it2.Next(key, value) ) ) {
Int_t pairFirst = AliMp::PairFirst(key);
if ( pairFirst != detElemId ) continue;
Int_t manuId = AliMp::PairSecond(key);
AliDebugStream(3)
<< "Go to write " << key << " "
<< pairFirst << " " << manuId << " " << value << endl;
static Int_t manuMask = AliMpConstants::ManuMask(AliMp::kNonBendingPlane);
TString planeName = PlaneTypeName(AliMp::kBendingPlane);
if ( manuId> manuMask ) {
planeName = PlaneTypeName(AliMp::kNonBendingPlane);
manuId -= manuMask;
}
out << manuId << " " << planeName.Data() << " " << value << endl;
AliDebugStream(3)
<< manuId << " " << planeName.Data() << " " << value << endl;
}
out.close();
}
gSystem->cd(curDir);
return kTRUE;
}
Int_t AliMpManuStore::NofManus() const
{
return fNofManus;
}
Int_t AliMpManuStore::NofManus(Int_t detElemId) const
{
if ( ! AliMpDEManager::IsValidDetElemId(detElemId, kTRUE) ) return 0;
return fNofManusInDE.GetValue(detElemId);
}
Bool_t AliMpManuStore::AddManu(Int_t detElemId, Int_t manuId, Int_t serialNb)
{
Long_t index = AliMp::Pair(detElemId, manuId);
AliDebugStream(2)
<< "Adding (" << detElemId << "," << manuId
<< ") as index=" << index << " and serialNb=" << serialNb << endl;
fManuToSerialNbs.Add(index, Long_t(serialNb));
Long_t value = fSerialNbToManus.GetValue(Long_t(serialNb));
if ( value ) {
if ( fgWarnIfDoublon ) {
AliWarningStream()
<< "Serial number " << serialNb
<< " already present for (detElemId, manuId) = " ;
AliMp::PairPut(AliWarningStream(), (MpPair_t) value) << endl;
}
return kFALSE;
}
else {
fSerialNbToManus.Add(Long_t(serialNb), index);
return kTRUE;
}
}
Int_t AliMpManuStore::GetManuSerial(Int_t detElemId, Int_t manuId) const
{
Long_t index = AliMp::Pair(detElemId, manuId);
return fManuToSerialNbs.GetValue(index);
}
MpPair_t AliMpManuStore::GetDetElemIdManu(Int_t manuSerial) const
{
return fSerialNbToManus.GetValue(Long_t(manuSerial));
}