#include "AliMpFiles.h"
#include "AliMpDataStreams.h"
#include "AliMpMotifReader.h"
#include "AliMpMotifMap.h"
#include "AliMpMotif.h"
#include "AliMpMotifSpecial.h"
#include "AliMpMotifType.h"
#include "AliMpConnection.h"
#include "AliMpEncodePair.h"
#include "AliLog.h"
#include <TSystem.h>
#include <TMath.h>
#include <Riostream.h>
#include <Rstrstream.h>
#if !defined(__HP_aCC) && !defined(__alpha)
#include <sstream>
#endif
ClassImp(AliMpMotifReader)
AliMpMotifReader::AliMpMotifReader(AliMp::StationType station,
AliMq::Station12Type station12,
AliMp::PlaneType plane)
: TObject(),
fStationType(station),
fStation12Type(station12),
fPlaneType(plane)
{
}
AliMpMotifReader::~AliMpMotifReader()
{
}
AliMpMotifType* AliMpMotifReader::BuildMotifType(
const AliMpDataStreams& dataStreams,
const TString& motifTypeId)
{
istream& padPosStream
= dataStreams.
CreateDataStream(AliMpFiles::PadPosFilePath(
fStationType, fStation12Type, fPlaneType, motifTypeId));
istream& bergToGCStream
= dataStreams.
CreateDataStream(AliMpFiles::BergToGCFilePath(fStationType, fStation12Type));
istream& motifTypeStream
= dataStreams.
CreateDataStream(AliMpFiles::MotifFilePath(
fStationType, fStation12Type, fPlaneType, motifTypeId));
AliMpMotifType* motifType = new AliMpMotifType(motifTypeId);
TExMap positions;
char line[256];
do {
padPosStream.getline(line,255);
if (!padPosStream) break;
#if defined (__HP_aCC) || (__alpha)
strstream strline;
strline << line;
#else
istringstream strline(line);
#endif
string key;
strline>>key;
if ((key=="#") || (key=="") ) continue;
int i,j;
strline>>i>>j;
positions.Add( AliMpExMap::GetIndex(key),
AliMp::Pair(i,j) + 1 );
} while (!padPosStream.eof());
const Int_t knbergpins =
(fStationType == AliMp::kStation12 ) ? 80 : 100;
Int_t gassiChannel[100];
for (Int_t i=0; i<100; ++i) gassiChannel[i] = 0;
while(1) {
Int_t bergNum;
TString gcStr;
bergToGCStream>>bergNum>>gcStr;
if (!bergToGCStream.good()) break;
if (gcStr=="GND") continue;
if (bergNum>knbergpins) {
Fatal("BuildMotifType","Berg number > 80 ...");
continue;
}
if ( bergNum <= 0 || bergNum >= 101 ) {
AliErrorStream() << "Wrong bergNum: " << bergNum << endl;
return 0;
}
gassiChannel[bergNum-1]= atoi(gcStr);
}
Int_t nofPadsX=0;
Int_t nofPadsY=0;
do {
Int_t ix,iy,numBerg,numKapton,padNum,gassiNum;
TString lineStr,token;
lineStr.ReadLine(motifTypeStream);
if (!motifTypeStream.good()) break;
#if defined (__HP_aCC) || (__alpha)
strstream tokenList;
tokenList << lineStr.Data();
#else
istringstream tokenList(lineStr.Data());
#endif
token.ReadToken(tokenList);
if (!tokenList.good()) continue;
if ( (token.Length()>0) && (token[0]=='#') ) continue;
numBerg = atoi(token.Data());
if (numBerg==0) {
AliWarning(Form("Berg number %s invalid",token.Data()));
continue;
}
token.ReadToken(tokenList);
if (!tokenList.good()) continue;
numKapton = atoi(token.Data());
if (numKapton==0) continue;
token.ReadToken(tokenList);
if (!tokenList.good()) continue;
if (token=="GND") continue;
string padName = token.Data();
padNum = motifType->PadNum(token);
token.ReadToken(tokenList);
if (token.IsNull() ) continue;
if ( (numBerg<1) || (numBerg>knbergpins) ) {
AliWarning(Form("Berg number %d outside range (1..%d)",numBerg,knbergpins));
continue;
}
gassiNum = gassiChannel[numBerg-1];
Long_t value = positions.GetValue(AliMpExMap::GetIndex(padName));
if (!value) {
AliWarningStream()
<< "Problem: Pad number " << padNum
<< " for motif type " << motifTypeId.Data()
<< " found in the motifType stream, but not in the padPos stream" << endl;
continue;
}
AliMpConnection* connection
= new AliMpConnection(padNum,numBerg,numKapton,gassiNum, --value);
Bool_t ok = motifType->AddConnection(connection);
if (!ok)
{
AliFatal("Could not add connection");
}
ix = AliMp::PairFirst(value);
iy = AliMp::PairSecond(value);
if (ix>=nofPadsX) nofPadsX=ix+1;
if (iy>=nofPadsY) nofPadsY=iy+1;
} while (!motifTypeStream.eof());
motifType->SetNofPads(nofPadsX, nofPadsY);
delete &padPosStream;
delete &bergToGCStream;
delete &motifTypeStream;
return motifType;
}
AliMpMotifSpecial*
AliMpMotifReader::BuildMotifSpecial(const AliMpDataStreams& dataStreams,
const TString& motifID,
AliMpMotifType* motifType,
Double_t scale)
{
istream& in
= dataStreams.
CreateDataStream(AliMpFiles::MotifSpecialFilePath(
fStationType, fStation12Type, fPlaneType, motifID));
TString id = MotifSpecialName(motifID,scale);
AliMpMotifSpecial* res = new AliMpMotifSpecial(id,motifType);
Int_t i,j;
Double_t x,y;
in >> i;
while (!in.eof()){
in >>j >>x >> y;
res->SetPadDimensions(i,j,x*scale/2.,y*scale/2.);
in >> i;
}
res->CalculateDimensions();
delete ∈
return res;
}
TString
AliMpMotifReader::MotifSpecialName(const TString& motifID, Double_t scale)
{
TString id;
if ( scale != 1.0 )
{
id = Form("%s-%e",motifID.Data(),scale);
}
else
{
id = motifID;
}
return id;
}