6 // Class that takes care of reading the sector data.
8 // Authors: David Guez, Ivana Hrivnacova; IPN Orsay
10 #if !defined(__HP_aCC) && !defined(__alpha)
14 #include <Riostream.h>
15 #include <Rstrstream.h>
20 #include "AliMpReader.h"
21 #include "AliMpSector.h"
22 #include "AliMpFiles.h"
23 #include "AliMpZone.h"
24 #include "AliMpSubZone.h"
26 #include "AliMpVRowSegment.h"
27 #include "AliMpRowSegment.h"
28 #include "AliMpRowSegmentLSpecial.h"
29 #include "AliMpRowSegmentRSpecial.h"
30 #include "AliMpPadRow.h"
31 #include "AliMpMotifMap.h"
32 #include "AliMpMotif.h"
33 #include "AliMpMotifSpecial.h"
34 #include "AliMpMotifType.h"
35 #include "AliMpConnection.h"
36 #include "AliMpIntPair.h"
37 #include "AliMpDirection.h"
42 const Int_t AliMpReader::fgkSeparator = 100;
45 const TString AliMpReader::fgkSectorKeyword = "SECTOR_DATA";
46 const TString AliMpReader::fgkZoneKeyword = "ZONE";
47 const TString AliMpReader::fgkSubZoneKeyword = "SUBZONE";
48 const TString AliMpReader::fgkRowKeyword = "ROW_SEGMENT";
49 const TString AliMpReader::fgkEofKeyword = "EOF";
50 const TString AliMpReader::fgkSectorSpecialKeyword = "SECTOR_SPECIAL_DATA";
51 const TString AliMpReader::fgkMotifKeyword = "MOTIF";
52 const TString AliMpReader::fgkRowSpecialKeyword = "ROW";
53 const TString AliMpReader::fgkPadRowsKeyword = "PAD_ROWS";
54 const TString AliMpReader::fgkPadRowSegmentKeyword = "PAD_ROW_SEGMENT";
56 //_____________________________________________________________________________
57 AliMpReader::AliMpReader(AliMpStationType station, AliMpPlaneType plane)
59 fStationType(station),
67 //_____________________________________________________________________________
68 AliMpReader::AliMpReader()
70 fStationType(kStation1),
71 fPlaneType(kBendingPlane),
78 //_____________________________________________________________________________
79 AliMpReader::AliMpReader(const AliMpReader& right)
82 Fatal("AliMpReader", "Copy constructor not provided.");
85 //_____________________________________________________________________________
86 AliMpReader::~AliMpReader() {
94 //_____________________________________________________________________________
95 AliMpReader& AliMpReader::operator=(const AliMpReader& right)
97 // check assignement to self
98 if (this == &right) return *this;
100 Fatal("operator =", "Assignement operator not provided.");
110 //_____________________________________________________________________________
111 Int_t AliMpReader::GetIndex(const string& s) const
113 // Converts the TString to integer.
116 if (s.length() > 5) {
117 Fatal("GetIndex", "String too long.");
122 for (Int_t i=s.length(); i>=0; --i) index = index*100 + int(s[i]);
127 //______________________________________________________________________________
128 Int_t AliMpReader::GetIndex(const AliMpIntPair& pair) const
130 // Converts the pair of integers to integer.
133 if (pair.GetFirst() >= fgkSeparator || pair.GetSecond() >= fgkSeparator)
134 Fatal("GetIndex", "Index out of limit.");
136 return pair.GetFirst()*fgkSeparator + pair.GetSecond() + 1;
139 //_____________________________________________________________________________
140 string AliMpReader::GetString(Int_t index) const
142 // Converts the integer index to the string.
147 Char_t c = index%100;
156 //______________________________________________________________________________
157 AliMpIntPair AliMpReader::GetPair(Int_t index) const
159 // Converts the integer index to the pair of integers.
162 return AliMpIntPair((index-1)/fgkSeparator, (index-1)%fgkSeparator);
166 //_____________________________________________________________________________
167 void AliMpReader::ReadSectorData(ifstream& in)
169 // Reads sector input data;
170 // prepares zones and rows vectors to be filled in.
177 cout << keyword << endl;
179 if (keyword != fgkSectorKeyword) {
180 Fatal("ReadSectorData", "Wrong file format.");
184 Int_t nofZones, nofRows;
185 TString directionStr;
190 AliMpDirection direction;
191 direction = (directionStr == "Y") ? kY : kX;
193 cout << nofZones << " " << nofRows << endl;
195 fSector = new AliMpSector("Not defined", nofZones, nofRows,direction);
200 if (nextKeyword != fgkZoneKeyword) {
201 Fatal("ReadSectorData", "Wrong file format.");
208 //_____________________________________________________________________________
209 void AliMpReader::ReadZoneData(ifstream& in)
211 // Reads zone input data;
212 // creates zone and adds it to zones vector.
216 Double_t sizex, sizey;
221 cout << fgkZoneKeyword << " " << zoneID << " "
222 << sizex << " " << sizey << endl;
224 AliMpZone* zone = fSector->GetZone(zoneID);
225 zone->SetPadDimensions(TVector2(sizex/2.,sizey/2.));
230 if (nextKeyword != fgkSubZoneKeyword) {
231 Fatal("ReadZoneData", "Wrong file format.");
235 ReadSubZoneData(in, zone);
238 //_____________________________________________________________________________
239 void AliMpReader::ReadSubZoneData(ifstream& in, AliMpZone* zone)
241 // Reads subzone input data;
242 // creates subzone and its to the specified zone.
246 cout << fgkSubZoneKeyword << " ";
248 AliMpVMotif* motif = ReadMotifData(in, zone);
249 AliMpSubZone* subZone = new AliMpSubZone(motif);
250 zone->AddSubZone(subZone);
255 if (nextKeyword != fgkRowKeyword) {
256 Fatal("ReadSubZoneData", "Wrong file format.");
260 ReadRowSegmentsData(in, zone, subZone);
263 //_____________________________________________________________________________
264 AliMpVMotif* AliMpReader::ReadMotifData(ifstream& in, AliMpZone* zone)
266 // Reads the motif input data.
273 if (fVerboseLevel>0) {
274 cout << motifID << " "
275 << motifTypeID << endl;
278 AliMpMotifMap* motifMap = fSector->GetMotifMap();
280 AliMpMotifType* motifType = 0;
282 = motifMap->FindMotif(motifID, motifTypeID, zone->GetPadDimensions());
284 motifType = motifMap->FindMotifType(motifTypeID);
286 motifType = BuildMotifType(motifTypeID);
287 motifMap->AddMotifType(motifType);
290 if (zone->GetPadDimensions().X() != 0. && zone->GetPadDimensions().Y() != 0.)
291 motif = new AliMpMotif(motifID, motifType, zone->GetPadDimensions());
293 motif = BuildMotifSpecial(motifID, motifType);
296 motifMap->AddMotif(motif);
303 //_____________________________________________________________________________
304 void AliMpReader::ReadRowSegmentsData(ifstream& in,
305 AliMpZone* zone, AliMpSubZone* subZone)
307 // Reads row segments input data of a specified zone and subzone;
308 // creates row segment and adds it to the specified subzone
309 // and a corresponding row in the rows vector.
315 // Read data from file
317 Int_t offX, offY, inRow, nofMotifs, firstMotifPositionId, firstMotifPositionDId;
322 in >> firstMotifPositionId;
323 in >> firstMotifPositionDId;
325 cout << fgkRowKeyword << " "
326 << offX << " " << offY << " " << inRow << " " << nofMotifs << " "
327 << firstMotifPositionId << " " << firstMotifPositionDId
335 AliMpRow* row = fSector->GetRow(inRow);
336 AliMpVMotif* motif = subZone->GetMotif();
338 // Create row segment and add it to its zone, row
339 AliMpVRowSegment* rowSegment
340 = new AliMpRowSegment(row, motif, AliMpIntPair(offX, offY), nofMotifs,
341 firstMotifPositionId, firstMotifPositionDId);
343 subZone->AddRowSegment(rowSegment);
344 row->AddRowSegment(rowSegment);
346 while (!in.eof() && (nextKeyword == fgkRowKeyword));
348 if (in.eof()) return;
350 if (nextKeyword == fgkZoneKeyword) {
353 else if (nextKeyword == fgkSubZoneKeyword) {
354 ReadSubZoneData(in, zone);
357 Fatal("ReadRowSegmentsData", "Wrong file format.");
361 //_____________________________________________________________________________
362 void AliMpReader::ReadSectorSpecialData(ifstream& in, AliMpXDirection direction)
364 // Reads sector input data
365 // with a special (irregular) motifs.
371 cout << keyword << endl;
373 if (keyword != fgkSectorSpecialKeyword) {
374 Fatal("ReadSectorSpecialData", "Wrong file format.");
381 cout << keyword << endl;
383 if (nextKeyword != fgkMotifKeyword) {
384 Fatal("ReadSectorSpecialData", "Wrong file format.");
388 ReadMotifsSpecialData(in);
389 ReadRowSpecialData(in, direction);
392 //_____________________________________________________________________________
393 void AliMpReader::ReadMotifsSpecialData(ifstream& in)
395 // Reads the special (irregular) motifs input data.
399 cout << fgkMotifKeyword << " ";
405 AliMpVMotif* motif = ReadMotifData(in, fSector->GetZone(zone));
406 AliMpSubZone* subZone = new AliMpSubZone(motif);
407 fSector->GetZone(zone)->AddSubZone(subZone);
411 cout << nextKeyword << " ";
413 while (nextKeyword == fgkMotifKeyword);
415 if (nextKeyword != fgkRowSpecialKeyword) {
416 Fatal("ReadMotifSpecialData", "Wrong file format.");
421 //_____________________________________________________________________________
422 void AliMpReader::ReadRowSpecialData(ifstream& in, AliMpXDirection direction)
424 // Reads row input data
425 // with a special (irregular) motifs.
433 // Get the row and its border
434 AliMpRow* row = fSector->GetRow(id);
436 AliMpVRowSegmentSpecial* segment = 0;
437 if (direction == kLeft) {
438 AliMpVRowSegment* firstNormalSeg = row->GetRowSegment(0);
439 Double_t offsetX = firstNormalSeg->LeftBorderX();
441 // Create a special row segment
442 segment = new AliMpRowSegmentLSpecial(row, offsetX);
443 row->AddRowSegmentInFront(segment);
446 AliMpVRowSegment* precedentNormalSeg
447 = row->GetRowSegment(row->GetNofRowSegments()-1);
448 Double_t offsetX = precedentNormalSeg->RightBorderX();
450 // Create a special row segment
451 segment = new AliMpRowSegmentRSpecial(row, offsetX);
452 row->AddRowSegment(segment);
458 cout << nextKeyword << " ";
460 if (nextKeyword != fgkPadRowsKeyword) {
461 Fatal("ReadRowSpecialData", "Wrong file format.");
465 ReadRowSegmentSpecialData(in, segment, direction);
467 // Update row segment and set it to all subzones associated with
470 segment->UpdateMotifVector();
471 segment->UpdatePadsOffset();
473 for (Int_t i=0; i<segment->GetNofMotifs(); i++) {
474 AliMpSubZone* subZone = 0;
476 while (!subZone && j<fSector->GetNofZones())
477 subZone = fSector->GetZone(++j)->FindSubZone(segment->GetMotif(i));
479 subZone->AddRowSegment(segment);
483 //_____________________________________________________________________________
484 void AliMpReader::ReadRowSegmentSpecialData(ifstream& in,
485 AliMpVRowSegmentSpecial* segment,
486 AliMpXDirection direction)
488 // Reads row segment input data
489 // with a special (irregular) motifs.
495 cout << nofPadRows << endl;
500 cout << keyword << " ";
502 if (keyword != fgkPadRowSegmentKeyword) {
503 Fatal("ReadRowSegmentSpecialData", "Wrong file format.");
511 PadRowVector newPadRows;
512 for (Int_t i=0; i<nofPadRows; i++) {
515 AliMpPadRow* padRow = new AliMpPadRow(direction);
516 segment->AddPadRow(padRow);
518 // Keep the new rows in a temporary vector
520 newPadRows.push_back(padRow);
523 newPadRows.Add(padRow);
530 // Read data from file
532 Int_t nofPadsInRow, motifPositionId;
533 TString motifId, motifTypeId;
536 in >> motifPositionId;
539 cout << nofPadsInRow << " " << motifId << " " << motifPositionId << endl;
543 cout << nextKeyword << " ";
549 for (Int_t i=0; i<nofPadRows; i++) {
551 // Get pad row from the temporary vector
553 AliMpPadRow* padRow = newPadRows[i];
556 AliMpPadRow* padRow = (AliMpPadRow*)newPadRows[i];
560 AliMpVMotif* motif = fSector->GetMotifMap()->FindMotif(motifId);
563 Fatal("ReadRowSegmentSpecialData", "Unknown motif.");
567 // Create pad row segment
568 padRow->AddPadRowSegment(dynamic_cast<AliMpMotif *>(motif),
569 motifPositionId, nofPadsInRow);
572 while (!in.eof() && (nextKeyword == fgkPadRowSegmentKeyword));
574 if (in.eof()) return;
576 if (nextKeyword == fgkPadRowsKeyword) {
577 ReadRowSegmentSpecialData(in, segment, direction);
579 else if (nextKeyword == fgkRowSpecialKeyword) {
580 ReadRowSpecialData(in, direction);
583 Fatal("ReadRowSegmentSpecialData", "Wrong file format.");
591 //_____________________________________________________________________________
592 AliMpSector* AliMpReader::BuildSector()
594 // Reads the mapping data from ascii file
595 // $MINSTALL/data/fileName and creates the basic objects:
596 // zones, subzones, rows, row segments, motifs.
600 ifstream in(AliMpFiles::Instance()
601 ->SectorFilePath(fStationType, fPlaneType).Data(), ios::in);
603 cerr << AliMpFiles::Instance()
604 ->SectorFilePath(fStationType, fPlaneType) << endl;
605 Error("BuildSector", "File not found.");
610 fSector->SetRowSegmentOffsets();
612 // Open input file for special inner zone
613 TString sectorSpecialFileName
614 = AliMpFiles::Instance()->SectorSpecialFilePath(fStationType, fPlaneType);
615 if (!gSystem->AccessPathName(sectorSpecialFileName.Data())) {
616 ifstream in2(sectorSpecialFileName.Data(), ios::in);
618 cerr << AliMpFiles::Instance()
619 ->SectorSpecialFilePath(fStationType, fPlaneType) << endl;
620 Error("BuildSector", "File not found.");
624 ReadSectorSpecialData(in2, kLeft);
627 // Open input file for special outer zone
628 TString sectorSpecialFileName2
629 = AliMpFiles::Instance()->SectorSpecialFilePath2(fStationType, fPlaneType);
630 if (!gSystem->AccessPathName(sectorSpecialFileName2.Data())) {
631 ifstream in3(sectorSpecialFileName2.Data(), ios::in);
633 cerr << AliMpFiles::Instance()
634 ->SectorSpecialFilePath2(fStationType, fPlaneType) << endl;
635 Error("Build", "File not found.");
639 ReadSectorSpecialData(in3, kRight);
642 fSector->Initialize();
647 //_____________________________________________________________________________
648 AliMpMotifType* AliMpReader::BuildMotifType(const TString& motifTypeId)
651 // Read the files describing a motif in the "$MINSTALL/data" directory
652 // and fill the AliMpMotifType structure with.
653 // The files mentioned are are named padPos<maskName>.dat
654 // and connect<maskName>.dat
656 AliMpMotifType* motifType = new AliMpMotifType(motifTypeId);
659 = AliMpFiles::Instance()
660 ->PadPosFilePath(fStationType, fPlaneType, motifTypeId);
661 ifstream padPos(strPadPos.Data());
662 if (fVerboseLevel>0) cout<<"Opening file "<<strPadPos<<endl;
664 PadMapType positions;
668 padPos.getline(line,255);
671 #if defined (__HP_aCC) || (__alpha)
675 istringstream strline(line);
680 if ((key=="#") || (key=="") ) continue;
685 positions[key].first=i;
686 positions[key].second=j;
689 positions.Add(GetIndex(key), GetIndex(AliMpIntPair(i,j)));
691 } while (!padPos.eof());
696 cout << "Opening file "
697 << AliMpFiles::Instance()->BergToGCFilePath(fStationType)
700 ifstream bergToGCFile(AliMpFiles::Instance()->BergToGCFilePath(fStationType));
701 Int_t gassiChannel[80];
705 bergToGCFile>>bergNum>>gcStr;
706 if (!bergToGCFile.good()) break;
707 if (gcStr=="GND") continue;
709 Fatal("BuildMotifType","Berg number > 80 ...");
712 gassiChannel[bergNum-1]= atoi(gcStr);
714 bergToGCFile.close();
717 = AliMpFiles::Instance()
718 ->MotifFilePath(fStationType, fPlaneType, motifTypeId);
719 ifstream motif(strMotif);
720 if (fVerboseLevel>0) cout<<"Opening file "<<strMotif<<endl;
728 Int_t ix,iy,numBerg,numKapton,padNum,gassiNum;
730 TString lineStr,token;
731 lineStr.ReadLine(motif);
732 if (!motif.good()) break;
733 #if defined (__HP_aCC) || (__alpha)
735 tokenList << lineStr.Data();
737 istringstream tokenList(lineStr.Data());
740 token.ReadToken(tokenList);
741 if (!tokenList.good()) continue; // column is missing...
742 if ( (token.Length()>0) && (token[0]=='#') ) continue; // this is a comment line
744 numBerg = atoi(token.Data());
746 Warning("BuildMotifType","Berg number invalid");
750 token.ReadToken(tokenList);
751 if (!tokenList.good()) continue; // column is missing...
752 numKapton = atoi(token.Data());
753 if (numKapton==0) continue;
756 token.ReadToken(tokenList);
757 if (!tokenList.good()) continue; // column is missing...
758 if (token=="GND") continue;
759 string padName = token.Data();
760 padNum = motifType->PadNum(token);
762 token.ReadToken(tokenList);
763 if (token.IsNull() ) continue; // column is missing...
764 // if (token[0]!='E') {
765 // cerr<<"Problem : gassinumber isn't begining with E:"<<token<<endl;
768 // gassiNum = atoi(token.Data() +1 )-1;
770 if ( (numBerg<1) || (numBerg>80) ) {
771 Warning("BuildMotifType","Berg number outside range");
775 gassiNum = gassiChannel[numBerg-1];
778 PadMapTypeIterator iter = positions.find(padName);
779 if (iter==positions.end()) {
780 cerr<<"Problem: Pad number "<<padNum<<" found in the file "<<strMotif
781 <<" but not in the file"<<strPadPos<<endl;
785 ix= iter->second.first;
786 iy= iter->second.second;
790 Long_t value = positions.GetValue(GetIndex(padName));
792 cerr<<"Problem: Pad number "<<padNum<<" found in the file "<<strMotif
793 <<" but not in the file"<<strPadPos<<endl;
797 ix = GetPair(value).GetFirst();
798 iy = GetPair(value).GetSecond();
801 motifType->AddConnection(AliMpIntPair(ix,iy),
802 new AliMpConnection(padNum,numBerg,numKapton,gassiNum));
804 if (ix>=nofPadsX) nofPadsX=ix+1;
805 if (iy>=nofPadsY) nofPadsY=iy+1;
807 } while (!motif.eof());
810 motifType->SetNofPads(nofPadsX, nofPadsY);
818 //_____________________________________________________________________________
820 AliMpReader::BuildMotifSpecial(const TString& motifID,
821 AliMpMotifType* motifType)
823 // Build a special motif by reading the file motifSpecial<motifId>.dat
824 // in the data directory
827 // Open the input file
828 ifstream in(AliMpFiles::Instance()
829 ->MotifSpecialFilePath(fStationType, fPlaneType, motifID).Data(),
832 Error("BuildMotifSpecial", "File not found.");
836 AliMpMotifSpecial* res = new AliMpMotifSpecial(motifID,motifType);
842 res->SetPadDimensions(AliMpIntPair(i,j),TVector2(x/2.,y/2.));
851 //_____________________________________________________________________________
852 void AliMpReader::SetVerboseLevel(Int_t verboseLevel)
854 // Sets verbose level.
857 fVerboseLevel = verboseLevel;