6 // Class that takes care of reading the sector data.
8 // Authors: David Guez, Ivana Hrivnacova; IPN Orsay
11 #if !defined(__HP_aCC) && !defined(__alpha)
15 #include <Riostream.h>
16 #include <Rstrstream.h>
21 #include "AliMpReader.h"
22 #include "AliMpSector.h"
23 #include "AliMpFiles.h"
24 #include "AliMpZone.h"
25 #include "AliMpSubZone.h"
27 #include "AliMpVRowSegment.h"
28 #include "AliMpRowSegment.h"
29 #include "AliMpRowSegmentLSpecial.h"
30 #include "AliMpRowSegmentRSpecial.h"
31 #include "AliMpPadRow.h"
32 #include "AliMpMotifMap.h"
33 #include "AliMpMotif.h"
34 #include "AliMpMotifSpecial.h"
35 #include "AliMpMotifType.h"
36 #include "AliMpConnection.h"
37 #include "AliMpIntPair.h"
38 #include "AliMpDirection.h"
43 const Int_t AliMpReader::fgkSeparator = 100;
46 const TString AliMpReader::fgkSectorKeyword = "SECTOR_DATA";
47 const TString AliMpReader::fgkZoneKeyword = "ZONE";
48 const TString AliMpReader::fgkSubZoneKeyword = "SUBZONE";
49 const TString AliMpReader::fgkRowKeyword = "ROW_SEGMENT";
50 const TString AliMpReader::fgkEofKeyword = "EOF";
51 const TString AliMpReader::fgkSectorSpecialKeyword = "SECTOR_SPECIAL_DATA";
52 const TString AliMpReader::fgkMotifKeyword = "MOTIF";
53 const TString AliMpReader::fgkRowSpecialKeyword = "ROW";
54 const TString AliMpReader::fgkPadRowsKeyword = "PAD_ROWS";
55 const TString AliMpReader::fgkPadRowSegmentKeyword = "PAD_ROW_SEGMENT";
57 //_____________________________________________________________________________
58 AliMpReader::AliMpReader(AliMpStationType station, AliMpPlaneType plane)
60 fStationType(station),
68 //_____________________________________________________________________________
69 AliMpReader::AliMpReader()
71 fStationType(kStation1),
72 fPlaneType(kBendingPlane),
79 //_____________________________________________________________________________
80 AliMpReader::~AliMpReader() {
89 //_____________________________________________________________________________
90 Int_t AliMpReader::GetIndex(const std::string& s) const
92 // Converts the TString to integer.
96 Fatal("GetIndex", "String too long.");
101 for (Int_t i=s.length(); i>=0; --i) index = index*100 + int(s[i]);
106 //______________________________________________________________________________
107 Int_t AliMpReader::GetIndex(const AliMpIntPair& pair) const
109 // Converts the pair of integers to integer.
112 if (pair.GetFirst() >= fgkSeparator || pair.GetSecond() >= fgkSeparator)
113 Fatal("GetIndex", "Index out of limit.");
115 return pair.GetFirst()*fgkSeparator + pair.GetSecond() + 1;
118 //_____________________________________________________________________________
119 std::string AliMpReader::GetString(Int_t index) const
121 // Converts the integer index to the string.
126 Char_t c = index%100;
135 //______________________________________________________________________________
136 AliMpIntPair AliMpReader::GetPair(Int_t index) const
138 // Converts the integer index to the pair of integers.
141 return AliMpIntPair((index-1)/fgkSeparator, (index-1)%fgkSeparator);
145 //_____________________________________________________________________________
146 void AliMpReader::ReadSectorData(ifstream& in)
148 // Reads sector input data;
149 // prepares zones and rows vectors to be filled in.
156 cout << keyword << endl;
158 if (keyword != fgkSectorKeyword) {
159 Fatal("ReadSectorData", "Wrong file format.");
163 Int_t nofZones, nofRows;
164 TString directionStr;
169 AliMpDirection direction;
170 direction = (directionStr == "Y") ? kY : kX;
172 cout << nofZones << " " << nofRows << endl;
174 fSector = new AliMpSector("Not defined", nofZones, nofRows,direction);
179 if (nextKeyword != fgkZoneKeyword) {
180 Fatal("ReadSectorData", "Wrong file format.");
187 //_____________________________________________________________________________
188 void AliMpReader::ReadZoneData(ifstream& in)
190 // Reads zone input data;
191 // creates zone and adds it to zones vector.
195 Double_t sizex, sizey;
200 cout << fgkZoneKeyword << " " << zoneID << " "
201 << sizex << " " << sizey << endl;
203 AliMpZone* zone = fSector->GetZone(zoneID);
204 zone->SetPadDimensions(TVector2(sizex/2.,sizey/2.));
209 if (nextKeyword != fgkSubZoneKeyword) {
210 Fatal("ReadZoneData", "Wrong file format.");
214 ReadSubZoneData(in, zone);
217 //_____________________________________________________________________________
218 void AliMpReader::ReadSubZoneData(ifstream& in, AliMpZone* zone)
220 // Reads subzone input data;
221 // creates subzone and its to the specified zone.
225 cout << fgkSubZoneKeyword << " ";
227 AliMpVMotif* motif = ReadMotifData(in, zone);
228 AliMpSubZone* subZone = new AliMpSubZone(motif);
229 zone->AddSubZone(subZone);
234 if (nextKeyword != fgkRowKeyword) {
235 Fatal("ReadSubZoneData", "Wrong file format.");
239 ReadRowSegmentsData(in, zone, subZone);
242 //_____________________________________________________________________________
243 AliMpVMotif* AliMpReader::ReadMotifData(ifstream& in, AliMpZone* zone)
245 // Reads the motif input data.
252 if (fVerboseLevel>0) {
253 cout << motifID << " "
254 << motifTypeID << endl;
257 AliMpMotifMap* motifMap = fSector->GetMotifMap();
259 AliMpMotifType* motifType = 0;
261 = motifMap->FindMotif(motifID, motifTypeID, zone->GetPadDimensions());
263 motifType = motifMap->FindMotifType(motifTypeID);
265 motifType = BuildMotifType(motifTypeID);
266 motifMap->AddMotifType(motifType);
269 if (zone->GetPadDimensions().X() != 0. && zone->GetPadDimensions().Y() != 0.)
270 motif = new AliMpMotif(motifID, motifType, zone->GetPadDimensions());
272 motif = BuildMotifSpecial(motifID, motifType);
275 motifMap->AddMotif(motif);
282 //_____________________________________________________________________________
283 void AliMpReader::ReadRowSegmentsData(ifstream& in,
284 AliMpZone* zone, AliMpSubZone* subZone)
286 // Reads row segments input data of a specified zone and subzone;
287 // creates row segment and adds it to the specified subzone
288 // and a corresponding row in the rows vector.
294 // Read data from file
296 Int_t offX, offY, inRow, nofMotifs, firstMotifPositionId, firstMotifPositionDId;
301 in >> firstMotifPositionId;
302 in >> firstMotifPositionDId;
304 cout << fgkRowKeyword << " "
305 << offX << " " << offY << " " << inRow << " " << nofMotifs << " "
306 << firstMotifPositionId << " " << firstMotifPositionDId
314 AliMpRow* row = fSector->GetRow(inRow);
315 AliMpVMotif* motif = subZone->GetMotif();
317 // Create row segment and add it to its zone, row
318 AliMpVRowSegment* rowSegment
319 = new AliMpRowSegment(row, motif, AliMpIntPair(offX, offY), nofMotifs,
320 firstMotifPositionId, firstMotifPositionDId);
322 subZone->AddRowSegment(rowSegment);
323 row->AddRowSegment(rowSegment);
325 while (!in.eof() && (nextKeyword == fgkRowKeyword));
327 if (in.eof()) return;
329 if (nextKeyword == fgkZoneKeyword) {
332 else if (nextKeyword == fgkSubZoneKeyword) {
333 ReadSubZoneData(in, zone);
336 Fatal("ReadRowSegmentsData", "Wrong file format.");
340 //_____________________________________________________________________________
341 void AliMpReader::ReadSectorSpecialData(ifstream& in, AliMpXDirection direction)
343 // Reads sector input data
344 // with a special (irregular) motifs.
350 cout << keyword << endl;
352 if (keyword != fgkSectorSpecialKeyword) {
353 Fatal("ReadSectorSpecialData", "Wrong file format.");
360 cout << keyword << endl;
362 if (nextKeyword != fgkMotifKeyword) {
363 Fatal("ReadSectorSpecialData", "Wrong file format.");
367 ReadMotifsSpecialData(in);
368 ReadRowSpecialData(in, direction);
371 //_____________________________________________________________________________
372 void AliMpReader::ReadMotifsSpecialData(ifstream& in)
374 // Reads the special (irregular) motifs input data.
378 cout << fgkMotifKeyword << " ";
384 AliMpVMotif* motif = ReadMotifData(in, fSector->GetZone(zone));
385 AliMpSubZone* subZone = new AliMpSubZone(motif);
386 fSector->GetZone(zone)->AddSubZone(subZone);
390 cout << nextKeyword << " ";
392 while (nextKeyword == fgkMotifKeyword);
394 if (nextKeyword != fgkRowSpecialKeyword) {
395 Fatal("ReadMotifSpecialData", "Wrong file format.");
400 //_____________________________________________________________________________
401 void AliMpReader::ReadRowSpecialData(ifstream& in, AliMpXDirection direction)
403 // Reads row input data
404 // with a special (irregular) motifs.
412 // Get the row and its border
413 AliMpRow* row = fSector->GetRow(id);
415 AliMpVRowSegmentSpecial* segment = 0;
416 if (direction == kLeft) {
417 AliMpVRowSegment* firstNormalSeg = row->GetRowSegment(0);
418 Double_t offsetX = firstNormalSeg->LeftBorderX();
420 // Create a special row segment
421 segment = new AliMpRowSegmentLSpecial(row, offsetX);
422 row->AddRowSegmentInFront(segment);
425 AliMpVRowSegment* precedentNormalSeg
426 = row->GetRowSegment(row->GetNofRowSegments()-1);
427 Double_t offsetX = precedentNormalSeg->RightBorderX();
429 // Create a special row segment
430 segment = new AliMpRowSegmentRSpecial(row, offsetX);
431 row->AddRowSegment(segment);
437 cout << nextKeyword << " ";
439 if (nextKeyword != fgkPadRowsKeyword) {
440 Fatal("ReadRowSpecialData", "Wrong file format.");
444 ReadRowSegmentSpecialData(in, segment, direction);
446 // Update row segment and set it to all subzones associated with
449 segment->UpdateMotifVector();
450 segment->UpdatePadsOffset();
452 for (Int_t i=0; i<segment->GetNofMotifs(); i++) {
453 AliMpSubZone* subZone = 0;
455 while (!subZone && j<fSector->GetNofZones())
456 subZone = fSector->GetZone(++j)->FindSubZone(segment->GetMotif(i));
458 subZone->AddRowSegment(segment);
462 //_____________________________________________________________________________
463 void AliMpReader::ReadRowSegmentSpecialData(ifstream& in,
464 AliMpVRowSegmentSpecial* segment,
465 AliMpXDirection direction)
467 // Reads row segment input data
468 // with a special (irregular) motifs.
474 cout << nofPadRows << endl;
479 cout << keyword << " ";
481 if (keyword != fgkPadRowSegmentKeyword) {
482 Fatal("ReadRowSegmentSpecialData", "Wrong file format.");
490 PadRowVector newPadRows;
491 for (Int_t i=0; i<nofPadRows; i++) {
494 AliMpPadRow* padRow = new AliMpPadRow(direction);
495 segment->AddPadRow(padRow);
497 // Keep the new rows in a temporary vector
499 newPadRows.push_back(padRow);
502 newPadRows.Add(padRow);
509 // Read data from file
511 Int_t nofPadsInRow, motifPositionId;
512 TString motifId, motifTypeId;
515 in >> motifPositionId;
518 cout << nofPadsInRow << " " << motifId << " " << motifPositionId << endl;
522 cout << nextKeyword << " ";
528 for (Int_t i=0; i<nofPadRows; i++) {
530 // Get pad row from the temporary vector
532 AliMpPadRow* padRow = newPadRows[i];
535 AliMpPadRow* padRow = (AliMpPadRow*)newPadRows[i];
539 AliMpVMotif* motif = fSector->GetMotifMap()->FindMotif(motifId);
542 Fatal("ReadRowSegmentSpecialData", "Unknown motif.");
546 // Create pad row segment
547 padRow->AddPadRowSegment(dynamic_cast<AliMpMotif *>(motif),
548 motifPositionId, nofPadsInRow);
551 while (!in.eof() && (nextKeyword == fgkPadRowSegmentKeyword));
553 if (in.eof()) return;
555 if (nextKeyword == fgkPadRowsKeyword) {
556 ReadRowSegmentSpecialData(in, segment, direction);
558 else if (nextKeyword == fgkRowSpecialKeyword) {
559 ReadRowSpecialData(in, direction);
562 Fatal("ReadRowSegmentSpecialData", "Wrong file format.");
570 //_____________________________________________________________________________
571 AliMpSector* AliMpReader::BuildSector()
573 // Reads the mapping data from ascii file
574 // $MINSTALL/data/fileName and creates the basic objects:
575 // zones, subzones, rows, row segments, motifs.
579 ifstream in(AliMpFiles::Instance()
580 ->SectorFilePath(fStationType, fPlaneType).Data(), ios::in);
582 cerr << AliMpFiles::Instance()
583 ->SectorFilePath(fStationType, fPlaneType) << endl;
584 Error("BuildSector", "File not found.");
589 fSector->SetRowSegmentOffsets();
591 // Open input file for special inner zone
592 TString sectorSpecialFileName
593 = AliMpFiles::Instance()->SectorSpecialFilePath(fStationType, fPlaneType);
594 if (!gSystem->AccessPathName(sectorSpecialFileName.Data())) {
595 ifstream in2(sectorSpecialFileName.Data(), ios::in);
597 cerr << AliMpFiles::Instance()
598 ->SectorSpecialFilePath(fStationType, fPlaneType) << endl;
599 Error("BuildSector", "File not found.");
603 ReadSectorSpecialData(in2, kLeft);
606 // Open input file for special outer zone
607 TString sectorSpecialFileName2
608 = AliMpFiles::Instance()->SectorSpecialFilePath2(fStationType, fPlaneType);
609 if (!gSystem->AccessPathName(sectorSpecialFileName2.Data())) {
610 ifstream in3(sectorSpecialFileName2.Data(), ios::in);
612 cerr << AliMpFiles::Instance()
613 ->SectorSpecialFilePath2(fStationType, fPlaneType) << endl;
614 Error("Build", "File not found.");
618 ReadSectorSpecialData(in3, kRight);
621 fSector->Initialize();
626 //_____________________________________________________________________________
627 AliMpMotifType* AliMpReader::BuildMotifType(const TString& motifTypeId)
630 // Read the files describing a motif in the "$MINSTALL/data" directory
631 // and fill the AliMpMotifType structure with.
632 // The files mentioned are are named padPos<maskName>.dat
633 // and connect<maskName>.dat
635 AliMpMotifType* motifType = new AliMpMotifType(motifTypeId);
638 = AliMpFiles::Instance()
639 ->PadPosFilePath(fStationType, fPlaneType, motifTypeId);
640 ifstream padPos(strPadPos.Data());
641 if (fVerboseLevel>0) cout<<"Opening file "<<strPadPos<<endl;
643 PadMapType positions;
647 padPos.getline(line,255);
650 #if defined (__HP_aCC) || (__alpha)
654 istringstream strline(line);
659 if ((key=="#") || (key=="") ) continue;
664 positions[key].first=i;
665 positions[key].second=j;
668 positions.Add(GetIndex(key), GetIndex(AliMpIntPair(i,j)));
670 } while (!padPos.eof());
675 cout << "Opening file "
676 << AliMpFiles::Instance()->BergToGCFilePath(fStationType)
679 ifstream bergToGCFile(AliMpFiles::Instance()->BergToGCFilePath(fStationType));
680 Int_t gassiChannel[80];
684 bergToGCFile>>bergNum>>gcStr;
685 if (!bergToGCFile.good()) break;
686 if (gcStr=="GND") continue;
688 Fatal("BuildMotifType","Berg number > 80 ...");
691 gassiChannel[bergNum-1]= atoi(gcStr);
693 bergToGCFile.close();
696 = AliMpFiles::Instance()
697 ->MotifFilePath(fStationType, fPlaneType, motifTypeId);
698 ifstream motif(strMotif);
699 if (fVerboseLevel>0) cout<<"Opening file "<<strMotif<<endl;
707 Int_t ix,iy,numBerg,numKapton,padNum,gassiNum;
709 TString lineStr,token;
710 lineStr.ReadLine(motif);
711 if (!motif.good()) break;
712 #if defined (__HP_aCC) || (__alpha)
714 tokenList << lineStr.Data();
716 istringstream tokenList(lineStr.Data());
719 token.ReadToken(tokenList);
720 if (!tokenList.good()) continue; // column is missing...
721 if ( (token.Length()>0) && (token[0]=='#') ) continue; // this is a comment line
723 numBerg = atoi(token.Data());
725 Warning("BuildMotifType","Berg number invalid");
729 token.ReadToken(tokenList);
730 if (!tokenList.good()) continue; // column is missing...
731 numKapton = atoi(token.Data());
732 if (numKapton==0) continue;
735 token.ReadToken(tokenList);
736 if (!tokenList.good()) continue; // column is missing...
737 if (token=="GND") continue;
738 string padName = token.Data();
739 padNum = motifType->PadNum(token);
741 token.ReadToken(tokenList);
742 if (token.IsNull() ) continue; // column is missing...
743 // if (token[0]!='E') {
744 // cerr<<"Problem : gassinumber isn't begining with E:"<<token<<endl;
747 // gassiNum = atoi(token.Data() +1 )-1;
749 if ( (numBerg<1) || (numBerg>80) ) {
750 Warning("BuildMotifType","Berg number outside range");
754 gassiNum = gassiChannel[numBerg-1];
757 PadMapTypeIterator iter = positions.find(padName);
758 if (iter==positions.end()) {
759 cerr<<"Problem: Pad number "<<padNum<<" found in the file "<<strMotif
760 <<" but not in the file"<<strPadPos<<endl;
764 ix= iter->second.first;
765 iy= iter->second.second;
769 Long_t value = positions.GetValue(GetIndex(padName));
771 cerr<<"Problem: Pad number "<<padNum<<" found in the file "<<strMotif
772 <<" but not in the file"<<strPadPos<<endl;
776 ix = GetPair(value).GetFirst();
777 iy = GetPair(value).GetSecond();
780 motifType->AddConnection(AliMpIntPair(ix,iy),
781 new AliMpConnection(padNum,numBerg,numKapton,gassiNum));
783 if (ix>=nofPadsX) nofPadsX=ix+1;
784 if (iy>=nofPadsY) nofPadsY=iy+1;
786 } while (!motif.eof());
789 motifType->SetNofPads(nofPadsX, nofPadsY);
797 //_____________________________________________________________________________
799 AliMpReader::BuildMotifSpecial(const TString& motifID,
800 AliMpMotifType* motifType)
802 // Build a special motif by reading the file motifSpecial<motifId>.dat
803 // in the data directory
806 // Open the input file
807 ifstream in(AliMpFiles::Instance()
808 ->MotifSpecialFilePath(fStationType, fPlaneType, motifID).Data(),
811 Error("BuildMotifSpecial", "File not found.");
815 AliMpMotifSpecial* res = new AliMpMotifSpecial(motifID,motifType);
821 res->SetPadDimensions(AliMpIntPair(i,j),TVector2(x/2.,y/2.));
830 //_____________________________________________________________________________
831 void AliMpReader::SetVerboseLevel(Int_t verboseLevel)
833 // Sets verbose level.
836 fVerboseLevel = verboseLevel;