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() {
88 //_____________________________________________________________________________
89 Int_t AliMpReader::GetIndex(const string& s) const
91 // Converts the TString to integer.
95 Fatal("GetIndex", "String too long.");
100 for (Int_t i=s.length(); i>=0; --i) index = index*100 + int(s[i]);
105 //______________________________________________________________________________
106 Int_t AliMpReader::GetIndex(const AliMpIntPair& pair) const
108 // Converts the pair of integers to integer.
111 if (pair.GetFirst() >= fgkSeparator || pair.GetSecond() >= fgkSeparator)
112 Fatal("GetIndex", "Index out of limit.");
114 return pair.GetFirst()*fgkSeparator + pair.GetSecond() + 1;
117 //_____________________________________________________________________________
118 string AliMpReader::GetString(Int_t index) const
120 // Converts the integer index to the string.
125 Char_t c = index%100;
134 //______________________________________________________________________________
135 AliMpIntPair AliMpReader::GetPair(Int_t index) const
137 // Converts the integer index to the pair of integers.
140 return AliMpIntPair((index-1)/fgkSeparator, (index-1)%fgkSeparator);
144 //_____________________________________________________________________________
145 void AliMpReader::ReadSectorData(ifstream& in)
147 // Reads sector input data;
148 // prepares zones and rows vectors to be filled in.
155 cout << keyword << endl;
157 if (keyword != fgkSectorKeyword) {
158 Fatal("ReadSectorData", "Wrong file format.");
162 Int_t nofZones, nofRows;
163 TString directionStr;
168 AliMpDirection direction;
169 direction = (directionStr == "Y") ? kY : kX;
171 cout << nofZones << " " << nofRows << endl;
173 fSector = new AliMpSector("Not defined", nofZones, nofRows,direction);
178 if (nextKeyword != fgkZoneKeyword) {
179 Fatal("ReadSectorData", "Wrong file format.");
186 //_____________________________________________________________________________
187 void AliMpReader::ReadZoneData(ifstream& in)
189 // Reads zone input data;
190 // creates zone and adds it to zones vector.
194 Double_t sizex, sizey;
199 cout << fgkZoneKeyword << " " << zoneID << " "
200 << sizex << " " << sizey << endl;
202 AliMpZone* zone = fSector->GetZone(zoneID);
203 zone->SetPadDimensions(TVector2(sizex/2.,sizey/2.));
208 if (nextKeyword != fgkSubZoneKeyword) {
209 Fatal("ReadZoneData", "Wrong file format.");
213 ReadSubZoneData(in, zone);
216 //_____________________________________________________________________________
217 void AliMpReader::ReadSubZoneData(ifstream& in, AliMpZone* zone)
219 // Reads subzone input data;
220 // creates subzone and its to the specified zone.
224 cout << fgkSubZoneKeyword << " ";
226 AliMpVMotif* motif = ReadMotifData(in, zone);
227 AliMpSubZone* subZone = new AliMpSubZone(motif);
228 zone->AddSubZone(subZone);
233 if (nextKeyword != fgkRowKeyword) {
234 Fatal("ReadSubZoneData", "Wrong file format.");
238 ReadRowSegmentsData(in, zone, subZone);
241 //_____________________________________________________________________________
242 AliMpVMotif* AliMpReader::ReadMotifData(ifstream& in, AliMpZone* zone)
244 // Reads the motif input data.
251 if (fVerboseLevel>0) {
252 cout << motifID << " "
253 << motifTypeID << endl;
256 AliMpMotifMap* motifMap = fSector->GetMotifMap();
258 AliMpMotifType* motifType = 0;
260 = motifMap->FindMotif(motifID, motifTypeID, zone->GetPadDimensions());
262 motifType = motifMap->FindMotifType(motifTypeID);
264 motifType = BuildMotifType(motifTypeID);
265 motifMap->AddMotifType(motifType);
268 if (zone->GetPadDimensions().X() != 0. && zone->GetPadDimensions().Y() != 0.)
269 motif = new AliMpMotif(motifID, motifType, zone->GetPadDimensions());
271 motif = BuildMotifSpecial(motifID, motifType);
274 motifMap->AddMotif(motif);
281 //_____________________________________________________________________________
282 void AliMpReader::ReadRowSegmentsData(ifstream& in,
283 AliMpZone* zone, AliMpSubZone* subZone)
285 // Reads row segments input data of a specified zone and subzone;
286 // creates row segment and adds it to the specified subzone
287 // and a corresponding row in the rows vector.
293 // Read data from file
295 Int_t offX, offY, inRow, nofMotifs, firstMotifPositionId, firstMotifPositionDId;
300 in >> firstMotifPositionId;
301 in >> firstMotifPositionDId;
303 cout << fgkRowKeyword << " "
304 << offX << " " << offY << " " << inRow << " " << nofMotifs << " "
305 << firstMotifPositionId << " " << firstMotifPositionDId
313 AliMpRow* row = fSector->GetRow(inRow);
314 AliMpVMotif* motif = subZone->GetMotif();
316 // Create row segment and add it to its zone, row
317 AliMpVRowSegment* rowSegment
318 = new AliMpRowSegment(row, motif, AliMpIntPair(offX, offY), nofMotifs,
319 firstMotifPositionId, firstMotifPositionDId);
321 subZone->AddRowSegment(rowSegment);
322 row->AddRowSegment(rowSegment);
324 while (!in.eof() && (nextKeyword == fgkRowKeyword));
326 if (in.eof()) return;
328 if (nextKeyword == fgkZoneKeyword) {
331 else if (nextKeyword == fgkSubZoneKeyword) {
332 ReadSubZoneData(in, zone);
335 Fatal("ReadRowSegmentsData", "Wrong file format.");
339 //_____________________________________________________________________________
340 void AliMpReader::ReadSectorSpecialData(ifstream& in, AliMpXDirection direction)
342 // Reads sector input data
343 // with a special (irregular) motifs.
349 cout << keyword << endl;
351 if (keyword != fgkSectorSpecialKeyword) {
352 Fatal("ReadSectorSpecialData", "Wrong file format.");
359 cout << keyword << endl;
361 if (nextKeyword != fgkMotifKeyword) {
362 Fatal("ReadSectorSpecialData", "Wrong file format.");
366 ReadMotifsSpecialData(in);
367 ReadRowSpecialData(in, direction);
370 //_____________________________________________________________________________
371 void AliMpReader::ReadMotifsSpecialData(ifstream& in)
373 // Reads the special (irregular) motifs input data.
377 cout << fgkMotifKeyword << " ";
383 AliMpVMotif* motif = ReadMotifData(in, fSector->GetZone(zone));
384 AliMpSubZone* subZone = new AliMpSubZone(motif);
385 fSector->GetZone(zone)->AddSubZone(subZone);
389 cout << nextKeyword << " ";
391 while (nextKeyword == fgkMotifKeyword);
393 if (nextKeyword != fgkRowSpecialKeyword) {
394 Fatal("ReadMotifSpecialData", "Wrong file format.");
399 //_____________________________________________________________________________
400 void AliMpReader::ReadRowSpecialData(ifstream& in, AliMpXDirection direction)
402 // Reads row input data
403 // with a special (irregular) motifs.
411 // Get the row and its border
412 AliMpRow* row = fSector->GetRow(id);
414 AliMpVRowSegmentSpecial* segment = 0;
415 if (direction == kLeft) {
416 AliMpVRowSegment* firstNormalSeg = row->GetRowSegment(0);
417 Double_t offsetX = firstNormalSeg->LeftBorderX();
419 // Create a special row segment
420 segment = new AliMpRowSegmentLSpecial(row, offsetX);
421 row->AddRowSegmentInFront(segment);
424 AliMpVRowSegment* precedentNormalSeg
425 = row->GetRowSegment(row->GetNofRowSegments()-1);
426 Double_t offsetX = precedentNormalSeg->RightBorderX();
428 // Create a special row segment
429 segment = new AliMpRowSegmentRSpecial(row, offsetX);
430 row->AddRowSegment(segment);
436 cout << nextKeyword << " ";
438 if (nextKeyword != fgkPadRowsKeyword) {
439 Fatal("ReadRowSpecialData", "Wrong file format.");
443 ReadRowSegmentSpecialData(in, segment, direction);
445 // Update row segment and set it to all subzones associated with
448 segment->UpdateMotifVector();
449 segment->UpdatePadsOffset();
451 for (Int_t i=0; i<segment->GetNofMotifs(); i++) {
452 AliMpSubZone* subZone = 0;
454 while (!subZone && j<fSector->GetNofZones())
455 subZone = fSector->GetZone(++j)->FindSubZone(segment->GetMotif(i));
457 subZone->AddRowSegment(segment);
461 //_____________________________________________________________________________
462 void AliMpReader::ReadRowSegmentSpecialData(ifstream& in,
463 AliMpVRowSegmentSpecial* segment,
464 AliMpXDirection direction)
466 // Reads row segment input data
467 // with a special (irregular) motifs.
473 cout << nofPadRows << endl;
478 cout << keyword << " ";
480 if (keyword != fgkPadRowSegmentKeyword) {
481 Fatal("ReadRowSegmentSpecialData", "Wrong file format.");
489 PadRowVector newPadRows;
490 for (Int_t i=0; i<nofPadRows; i++) {
493 AliMpPadRow* padRow = new AliMpPadRow(direction);
494 segment->AddPadRow(padRow);
496 // Keep the new rows in a temporary vector
498 newPadRows.push_back(padRow);
501 newPadRows.Add(padRow);
508 // Read data from file
510 Int_t nofPadsInRow, motifPositionId;
511 TString motifId, motifTypeId;
514 in >> motifPositionId;
517 cout << nofPadsInRow << " " << motifId << " " << motifPositionId << endl;
521 cout << nextKeyword << " ";
527 for (Int_t i=0; i<nofPadRows; i++) {
529 // Get pad row from the temporary vector
531 AliMpPadRow* padRow = newPadRows[i];
534 AliMpPadRow* padRow = (AliMpPadRow*)newPadRows[i];
538 AliMpVMotif* motif = fSector->GetMotifMap()->FindMotif(motifId);
541 Fatal("ReadRowSegmentSpecialData", "Unknown motif.");
545 // Create pad row segment
546 padRow->AddPadRowSegment(dynamic_cast<AliMpMotif *>(motif),
547 motifPositionId, nofPadsInRow);
550 while (!in.eof() && (nextKeyword == fgkPadRowSegmentKeyword));
552 if (in.eof()) return;
554 if (nextKeyword == fgkPadRowsKeyword) {
555 ReadRowSegmentSpecialData(in, segment, direction);
557 else if (nextKeyword == fgkRowSpecialKeyword) {
558 ReadRowSpecialData(in, direction);
561 Fatal("ReadRowSegmentSpecialData", "Wrong file format.");
569 //_____________________________________________________________________________
570 AliMpSector* AliMpReader::BuildSector()
572 // Reads the mapping data from ascii file
573 // $MINSTALL/data/fileName and creates the basic objects:
574 // zones, subzones, rows, row segments, motifs.
578 ifstream in(AliMpFiles::Instance()
579 ->SectorFilePath(fStationType, fPlaneType).Data(), ios::in);
581 cerr << AliMpFiles::Instance()
582 ->SectorFilePath(fStationType, fPlaneType) << endl;
583 Error("BuildSector", "File not found.");
588 fSector->SetRowSegmentOffsets();
590 // Open input file for special inner zone
591 TString sectorSpecialFileName
592 = AliMpFiles::Instance()->SectorSpecialFilePath(fStationType, fPlaneType);
593 if (!gSystem->AccessPathName(sectorSpecialFileName.Data())) {
594 ifstream in2(sectorSpecialFileName.Data(), ios::in);
596 cerr << AliMpFiles::Instance()
597 ->SectorSpecialFilePath(fStationType, fPlaneType) << endl;
598 Error("BuildSector", "File not found.");
602 ReadSectorSpecialData(in2, kLeft);
605 // Open input file for special outer zone
606 TString sectorSpecialFileName2
607 = AliMpFiles::Instance()->SectorSpecialFilePath2(fStationType, fPlaneType);
608 if (!gSystem->AccessPathName(sectorSpecialFileName2.Data())) {
609 ifstream in3(sectorSpecialFileName2.Data(), ios::in);
611 cerr << AliMpFiles::Instance()
612 ->SectorSpecialFilePath2(fStationType, fPlaneType) << endl;
613 Error("Build", "File not found.");
617 ReadSectorSpecialData(in3, kRight);
620 fSector->Initialize();
625 //_____________________________________________________________________________
626 AliMpMotifType* AliMpReader::BuildMotifType(const TString& motifTypeId)
629 // Read the files describing a motif in the "$MINSTALL/data" directory
630 // and fill the AliMpMotifType structure with.
631 // The files mentioned are are named padPos<maskName>.dat
632 // and connect<maskName>.dat
634 AliMpMotifType* motifType = new AliMpMotifType(motifTypeId);
637 = AliMpFiles::Instance()
638 ->PadPosFilePath(fStationType, fPlaneType, motifTypeId);
639 ifstream padPos(strPadPos.Data());
640 if (fVerboseLevel>0) cout<<"Opening file "<<strPadPos<<endl;
642 PadMapType positions;
646 padPos.getline(line,255);
649 #if defined (__HP_aCC) || (__alpha)
653 istringstream strline(line);
658 if ((key=="#") || (key=="") ) continue;
663 positions[key].first=i;
664 positions[key].second=j;
667 positions.Add(GetIndex(key), GetIndex(AliMpIntPair(i,j)));
669 } while (!padPos.eof());
674 cout << "Opening file "
675 << AliMpFiles::Instance()->BergToGCFilePath(fStationType)
678 ifstream bergToGCFile(AliMpFiles::Instance()->BergToGCFilePath(fStationType));
679 Int_t gassiChannel[80];
683 bergToGCFile>>bergNum>>gcStr;
684 if (!bergToGCFile.good()) break;
685 if (gcStr=="GND") continue;
687 Fatal("BuildMotifType","Berg number > 80 ...");
690 gassiChannel[bergNum-1]= atoi(gcStr);
692 bergToGCFile.close();
695 = AliMpFiles::Instance()
696 ->MotifFilePath(fStationType, fPlaneType, motifTypeId);
697 ifstream motif(strMotif);
698 if (fVerboseLevel>0) cout<<"Opening file "<<strMotif<<endl;
706 Int_t ix,iy,numBerg,numKapton,padNum,gassiNum;
708 TString lineStr,token;
709 lineStr.ReadLine(motif);
710 if (!motif.good()) break;
711 #if defined (__HP_aCC) || (__alpha)
713 tokenList << lineStr.Data();
715 istringstream tokenList(lineStr.Data());
718 token.ReadToken(tokenList);
719 if (!tokenList.good()) continue; // column is missing...
720 if ( (token.Length()>0) && (token[0]=='#') ) continue; // this is a comment line
722 numBerg = atoi(token.Data());
724 Warning("BuildMotifType","Berg number invalid");
728 token.ReadToken(tokenList);
729 if (!tokenList.good()) continue; // column is missing...
730 numKapton = atoi(token.Data());
731 if (numKapton==0) continue;
734 token.ReadToken(tokenList);
735 if (!tokenList.good()) continue; // column is missing...
736 if (token=="GND") continue;
737 string padName = token.Data();
738 padNum = motifType->PadNum(token);
740 token.ReadToken(tokenList);
741 if (token.IsNull() ) continue; // column is missing...
742 // if (token[0]!='E') {
743 // cerr<<"Problem : gassinumber isn't begining with E:"<<token<<endl;
746 // gassiNum = atoi(token.Data() +1 )-1;
748 if ( (numBerg<1) || (numBerg>80) ) {
749 Warning("BuildMotifType","Berg number outside range");
753 gassiNum = gassiChannel[numBerg-1];
756 PadMapTypeIterator iter = positions.find(padName);
757 if (iter==positions.end()) {
758 cerr<<"Problem: Pad number "<<padNum<<" found in the file "<<strMotif
759 <<" but not in the file"<<strPadPos<<endl;
763 ix= iter->second.first;
764 iy= iter->second.second;
768 Long_t value = positions.GetValue(GetIndex(padName));
770 cerr<<"Problem: Pad number "<<padNum<<" found in the file "<<strMotif
771 <<" but not in the file"<<strPadPos<<endl;
775 ix = GetPair(value).GetFirst();
776 iy = GetPair(value).GetSecond();
779 motifType->AddConnection(AliMpIntPair(ix,iy),
780 new AliMpConnection(padNum,numBerg,numKapton,gassiNum));
782 if (ix>=nofPadsX) nofPadsX=ix+1;
783 if (iy>=nofPadsY) nofPadsY=iy+1;
785 } while (!motif.eof());
788 motifType->SetNofPads(nofPadsX, nofPadsY);
796 //_____________________________________________________________________________
798 AliMpReader::BuildMotifSpecial(const TString& motifID,
799 AliMpMotifType* motifType)
801 // Build a special motif by reading the file motifSpecial<motifId>.dat
802 // in the data directory
805 // Open the input file
806 ifstream in(AliMpFiles::Instance()
807 ->MotifSpecialFilePath(fStationType, fPlaneType, motifID).Data(),
810 Error("BuildMotifSpecial", "File not found.");
814 AliMpMotifSpecial* res = new AliMpMotifSpecial(motifID,motifType);
820 res->SetPadDimensions(AliMpIntPair(i,j),TVector2(x/2.,y/2.));
829 //_____________________________________________________________________________
830 void AliMpReader::SetVerboseLevel(Int_t verboseLevel)
832 // Sets verbose level.
835 fVerboseLevel = verboseLevel;