]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/mapping/AliMpReader.cxx
Code for MUON Station1 (I.Hrivnacova)
[u/mrichter/AliRoot.git] / MUON / mapping / AliMpReader.cxx
1 // $Id$
2 // Category: sector
3 //
4 // Class AliMpReader
5 // -------------------
6 // Class that takes care of reading the sector data.
7 //
8 // Authors: David Guez, Ivana Hrivnacova; IPN Orsay
9
10 #include <string>
11 #if !defined(__HP_aCC) && !defined(__alpha)
12   #include <sstream>
13 #endif
14
15 #include <Riostream.h>
16 #include <Rstrstream.h>
17 #include <TSystem.h>
18 #include <TError.h>
19 #include <TMath.h>
20
21 #include "AliMpReader.h"
22 #include "AliMpSector.h"
23 #include "AliMpFiles.h"
24 #include "AliMpZone.h"
25 #include "AliMpSubZone.h"
26 #include "AliMpRow.h"
27 #include "AliMpVRowSegment.h"
28 #include "AliMpRowSegment.h"
29 #include "AliMpRowSegmentSpecial.h"
30 #include "AliMpPadRow.h"
31 #include "AliMpPadRowSegment.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"
39
40 ClassImp(AliMpReader)
41
42 const TString  AliMpReader::fgkSectorKeyword  = "SECTOR_DATA";
43 const TString  AliMpReader::fgkZoneKeyword    = "ZONE";
44 const TString  AliMpReader::fgkSubZoneKeyword = "SUBZONE";
45 const TString  AliMpReader::fgkRowKeyword     = "ROW_SEGMENT";
46 const TString  AliMpReader::fgkEofKeyword     = "EOF";
47 const TString  AliMpReader::fgkSectorSpecialKeyword  = "SECTOR_SPECIAL_DATA";
48 const TString  AliMpReader::fgkMotifKeyword          = "MOTIF";
49 const TString  AliMpReader::fgkRowSpecialKeyword     = "ROW";
50 const TString  AliMpReader::fgkPadRowsKeyword        = "PAD_ROWS";
51 const TString  AliMpReader::fgkPadRowSegmentKeyword  = "PAD_ROW_SEGMENT";
52
53 //_____________________________________________________________________________
54 AliMpReader::AliMpReader(AliMpPlaneType plane) 
55   : TObject(),
56     fPlaneType(plane),
57     fSector(0),
58     fVerboseLevel(0)
59 {
60 //
61 }
62
63 //_____________________________________________________________________________
64 AliMpReader::AliMpReader() 
65   : TObject(),
66     fPlaneType(kBendingPlane),
67     fSector(0),
68     fVerboseLevel(0)
69 {
70 //
71 }
72
73 //_____________________________________________________________________________
74 AliMpReader::~AliMpReader() {
75 //  
76 }
77
78 //
79 // private methods
80 //
81
82 //_____________________________________________________________________________
83 void  AliMpReader::ReadSectorData(ifstream& in)
84 {
85 // Reads sector input data;
86 // prepares zones and rows vectors to be filled in.
87 // ---
88
89   TString keyword;
90   in >> keyword;
91   
92   if (fVerboseLevel>0) 
93     cout << keyword << endl;
94
95   if (keyword != fgkSectorKeyword) {
96      Fatal("ReadSectorData", "Wrong file format.");
97      return;
98   }   
99     
100   Int_t nofZones, nofRows;
101   TString directionStr;
102   in >> nofZones;
103   in >> nofRows;
104   in >> directionStr;
105   
106   AliMpDirection direction;
107   direction = (directionStr == "Y") ? kY  :  kX;
108   if (fVerboseLevel>0) 
109      cout << nofZones << " " <<  nofRows << endl;
110
111   fSector = new AliMpSector("Not defined", nofZones, nofRows,direction);
112   
113   TString nextKeyword;
114   in >> nextKeyword;
115     
116   if (nextKeyword != fgkZoneKeyword) {
117      Fatal("ReadSectorData", "Wrong file format.");
118      return;
119   }      
120   
121   ReadZoneData(in);
122 }  
123
124 //_____________________________________________________________________________
125 void AliMpReader::ReadZoneData(ifstream& in)
126 {
127 // Reads zone input data;
128 // creates zone and adds it to zones vector.
129 // ---
130
131   Int_t zoneID;
132   Double_t  sizex, sizey; 
133   in >> zoneID;    
134   in >> sizex;
135   in >> sizey;
136   if (fVerboseLevel>0) 
137      cout << fgkZoneKeyword << " " <<  zoneID << "  "        
138           << sizex << " " << sizey << endl;
139   
140   AliMpZone* zone =  fSector->GetZone(zoneID);
141   zone->SetPadDimensions(TVector2(sizex/2.,sizey/2.)); 
142       
143   TString nextKeyword;
144   in >> nextKeyword;
145     
146   if (nextKeyword != fgkSubZoneKeyword) {
147      Fatal("ReadZoneData", "Wrong file format.");
148      return;
149   }  
150     
151   ReadSubZoneData(in, zone);
152 }
153
154 //_____________________________________________________________________________
155 void AliMpReader::ReadSubZoneData(ifstream& in, AliMpZone* zone)
156 {
157 // Reads subzone input data;
158 // creates subzone and its to the specified zone.
159 // ---
160
161   if (fVerboseLevel>0) 
162     cout << fgkSubZoneKeyword << " ";
163
164   AliMpVMotif* motif = ReadMotifData(in, zone);
165   AliMpSubZone* subZone = new AliMpSubZone(motif); 
166   zone->AddSubZone(subZone); 
167
168   TString nextKeyword;
169   in >> nextKeyword;
170     
171   if (nextKeyword != fgkRowKeyword) {
172      Fatal("ReadSubZoneData", "Wrong file format.");
173      return;
174   }  
175     
176   ReadRowSegmentsData(in, zone, subZone);
177 }   
178
179 //_____________________________________________________________________________
180 AliMpVMotif*  AliMpReader::ReadMotifData(ifstream& in, AliMpZone* zone)
181 {
182 // Reads the motif input data.
183 // ---
184
185   TString  motifID;
186   TString  motifTypeID;
187   in >> motifID;
188   in >> motifTypeID;
189   if (fVerboseLevel>0) {
190     cout << motifID << " " 
191          << motifTypeID << endl;
192   }      
193   
194   AliMpMotifMap* motifMap = fSector->GetMotifMap();
195
196   AliMpMotifType* motifType = 0;
197   AliMpVMotif* motif 
198     = motifMap->FindMotif(motifID, motifTypeID, zone->GetPadDimensions());
199   if (!motif) {    
200     motifType = motifMap->FindMotifType(motifTypeID);
201     if (!motifType) {
202       motifType = BuildMotifType(motifTypeID);     
203       motifMap->AddMotifType(motifType);
204     }
205     
206     if (zone->GetPadDimensions().X() != 0. && zone->GetPadDimensions().Y() != 0.) 
207       motif = new AliMpMotif(motifID, motifType, zone->GetPadDimensions());
208     else 
209       motif = BuildMotifSpecial(motifID, motifType);
210       
211     if (motif) 
212       motifMap->AddMotif(motif);
213
214   }  
215
216   return motif;   
217 }  
218
219 //_____________________________________________________________________________
220 void AliMpReader::ReadRowSegmentsData(ifstream& in, 
221                                       AliMpZone* zone, AliMpSubZone* subZone)
222 {
223 // Reads row segments input data of a specified zone and subzone;
224 // creates row segment and adds it to the specified subzone
225 // and a corresponding row in the rows vector.
226 // ---
227
228   TString nextKeyword;
229   do {
230     // 
231     // Read data from file
232     //
233     Int_t offX, offY, inRow, nofMotifs, firstMotifPositionId, firstMotifPositionDId;
234     in >> offX;
235     in >> offY;
236     in >> inRow;
237     in >> nofMotifs;
238     in >> firstMotifPositionId;
239     in >> firstMotifPositionDId;
240     if (fVerboseLevel>0) 
241        cout << fgkRowKeyword << " " 
242             << offX << " " << offY << " " << inRow << " " << nofMotifs << " " 
243             << firstMotifPositionId << " " << firstMotifPositionDId
244             << endl;
245
246     in >> nextKeyword;
247
248     //
249     // Process data
250     //
251     AliMpRow* row = fSector->GetRow(inRow);
252     AliMpVMotif* motif = subZone->GetMotif();
253     
254     // Create row segment and add it to its zone, row   
255     AliMpVRowSegment* rowSegment
256       = new AliMpRowSegment(row, motif, AliMpIntPair(offX, offY), nofMotifs, 
257                         firstMotifPositionId, firstMotifPositionDId);
258                         
259     subZone->AddRowSegment(rowSegment);
260     row->AddRowSegment(rowSegment);
261   }
262   while (!in.eof() && (nextKeyword == fgkRowKeyword));
263
264   if (in.eof()) return;
265
266   if (nextKeyword == fgkZoneKeyword) {
267     ReadZoneData(in);
268   }
269   else if (nextKeyword == fgkSubZoneKeyword) {
270     ReadSubZoneData(in, zone);
271   }   
272   else {
273     Fatal("ReadRowSegmentsData", "Wrong file format.");
274   } 
275 }   
276
277 //_____________________________________________________________________________
278 void AliMpReader::ReadSectorSpecialData(ifstream& in)
279 {
280 // Reads sector input data
281 // with a special (irregular) motifs.
282 // ---
283
284   TString keyword;
285   in >> keyword;
286   if (fVerboseLevel>0) 
287     cout << keyword << endl;
288
289   if (keyword != fgkSectorSpecialKeyword) {
290      Fatal("ReadSectorSpecialData", "Wrong file format.");
291      return;
292   }   
293
294   TString nextKeyword;
295   in >> nextKeyword;
296   if (fVerboseLevel>0) 
297     cout << keyword << endl;
298     
299   if (nextKeyword != fgkMotifKeyword) {
300     Fatal("ReadSectorSpecialData", "Wrong file format.");
301     return;
302   }  
303
304   ReadMotifsSpecialData(in);     
305   ReadRowSpecialData(in); 
306 }  
307
308 //_____________________________________________________________________________
309 void AliMpReader::ReadMotifsSpecialData(ifstream& in)
310 {
311 // Reads the special (irregular) motifs input data.
312 // ---
313
314   if (fVerboseLevel>0) 
315     cout << fgkMotifKeyword << " ";
316
317   TString nextKeyword;
318   do {
319
320     AliMpVMotif* motif =  ReadMotifData(in, fSector->GetZone(1));
321     AliMpSubZone* subZone = new AliMpSubZone(motif); 
322     fSector->GetZone(1)->AddSubZone(subZone); 
323              // special row segments are always in zone 1
324   
325     in >> nextKeyword;
326     if (fVerboseLevel>0) 
327       cout << nextKeyword << " ";      
328   }
329   while (nextKeyword == fgkMotifKeyword);
330     
331   if (nextKeyword != fgkRowSpecialKeyword) {
332      Fatal("ReadMotifSpecialData", "Wrong file format.");
333      return;
334   }      
335 }  
336
337 //_____________________________________________________________________________
338 void AliMpReader::ReadRowSpecialData(ifstream& in)
339 {
340 // Reads row input data
341 // with a special (irregular) motifs.
342 // ---
343
344   Int_t id;
345   in >> id;
346   if (fVerboseLevel>0) 
347       cout << id << endl;      
348   
349   // Get the row and its border
350   AliMpRow* row = fSector->GetRow(id);
351   AliMpVRowSegment* firstNormalSeg = row->GetRowSegment(0);
352   Double_t offsetX = firstNormalSeg->LeftBorderX();
353   
354   // Create a special row segment
355   AliMpRowSegmentSpecial* segment = new AliMpRowSegmentSpecial(row, offsetX);
356   row->AddRowSegmentInFront(segment);
357   
358   TString nextKeyword;
359   in >> nextKeyword;
360   if (fVerboseLevel>0) 
361     cout << nextKeyword << " ";
362     
363   if (nextKeyword != fgkPadRowsKeyword) {
364      Fatal("ReadRowSpecialData", "Wrong file format.");
365      return;
366   }  
367     
368   ReadRowSegmentSpecialData(in, segment); 
369   
370   // Update row segment and set it to all subzones associated with
371   // contained motifs
372   
373   segment->UpdateMotifVector();
374   segment->UpdatePadsOffset();
375   
376   for (Int_t i=0; i<segment->GetNofMotifs(); i++)
377     fSector->GetZone(1)->FindSubZone(segment->GetMotif(i))->AddRowSegment(segment);
378 }  
379
380 //_____________________________________________________________________________
381 void AliMpReader::ReadRowSegmentSpecialData(ifstream& in, 
382                                             AliMpRowSegmentSpecial* segment)
383 {
384 // Reads row segment input data
385 // with a special (irregular) motifs.
386 // ---
387
388   Int_t nofPadRows;
389   in >> nofPadRows;
390   if (fVerboseLevel>0) 
391     cout << nofPadRows << endl;
392   
393   TString keyword;
394   in >> keyword;
395   if (fVerboseLevel>0) 
396     cout << keyword << " ";
397     
398   if (keyword != fgkPadRowSegmentKeyword) {
399      Fatal("ReadRowSegmentSpecialData", "Wrong file format.");
400      return;
401   }  
402   
403   //
404   // Process data
405   //
406     
407   PadRowVector  newPadRows;
408   for (Int_t i=0; i<nofPadRows; i++) {
409     
410      // Create pad row
411      AliMpPadRow* padRow = new AliMpPadRow();
412      segment->AddPadRow(padRow);
413      
414      // Keep the new rows in a temporary vector
415      newPadRows.push_back(padRow);
416   }   
417       
418   TString nextKeyword;
419   do {
420     // 
421     // Read data from file
422     //
423     Int_t    nofPadsInRow, motifPositionId;
424     TString   motifId, motifTypeId;
425     in >> nofPadsInRow;
426     in >> motifId;
427     in >> motifPositionId; 
428   
429     if (fVerboseLevel>0) 
430        cout << nofPadsInRow << " " << motifId << " " << motifPositionId << endl;
431
432     in >> nextKeyword;
433     if (fVerboseLevel>0) 
434       cout << nextKeyword << " ";
435
436     //
437     // Process data
438     //
439     
440     for (Int_t i=0; i<nofPadRows; i++) {
441     
442       // Get pad row from the temporary vector
443       AliMpPadRow* padRow = newPadRows[i];
444       
445       // Find motif
446       AliMpVMotif* motif = fSector->GetMotifMap()->FindMotif(motifId);
447       
448       if (!motif) {
449         Fatal("ReadRowSegmentSpecialData", "Unknown motif.");
450         return;
451       }
452
453       // Create pad row segment
454       AliMpPadRowSegment* padRowSegment 
455         = new AliMpPadRowSegment(padRow, dynamic_cast<AliMpMotif *>(motif), 
456                              motifPositionId, nofPadsInRow);
457       padRow->AddPadRowSegment(padRowSegment);   
458     }  
459   }
460   while (!in.eof() && (nextKeyword == fgkPadRowSegmentKeyword));
461   
462   if (in.eof()) return;
463
464   if (nextKeyword == fgkPadRowsKeyword) {
465     ReadRowSegmentSpecialData(in, segment);
466   }
467   else if (nextKeyword == fgkRowSpecialKeyword) {
468     ReadRowSpecialData(in);
469   }   
470   else {
471     Fatal("ReadRowSegmentSpecialData", "Wrong file format.");
472   } 
473 }  
474
475 //
476 // public methods
477 //
478
479 //_____________________________________________________________________________
480 AliMpSector* AliMpReader::BuildSector()
481 {
482 // Reads the mapping data from ascii file
483 // $MINSTALL/data/fileName and creates the basic objects:
484 // zones, subzones, rows, row segments, motifs.
485 // ---
486
487   // Open input file
488   ifstream in(AliMpFiles::Instance()->SectorFilePath(fPlaneType).Data(), ios::in);
489   if (!in) {    
490      Error("Read", "File not found.");
491      return 0;
492   }
493   
494   ReadSectorData(in);
495   fSector->SetRowSegmentOffsets();
496
497   // Open second input file
498   TString sectorSpecialFileName 
499     = AliMpFiles::Instance()->SectorSpecialFilePath(fPlaneType);
500   if (!gSystem->AccessPathName(sectorSpecialFileName.Data())) {
501     ifstream in2(sectorSpecialFileName.Data(), ios::in);
502     if (!in2) { 
503        Error("Read", "File not found.");
504        return 0;
505     }
506     
507     ReadSectorSpecialData(in2);
508   }   
509
510   fSector->Initialize();
511   
512   return fSector;
513 }  
514
515 //_____________________________________________________________________________
516 AliMpMotifType* AliMpReader::BuildMotifType(TString motifTypeId)
517 {
518
519   // Read the files describing a motif in the "$MINSTALL/data" directory
520   // and fill the AliMpMotifType structure with.
521   // The files mentioned are are named padPos<maskName>.dat
522   // and connect<maskName>.dat
523
524   AliMpMotifType*  motifType = new AliMpMotifType(motifTypeId); 
525
526   TString strPadPos 
527     = AliMpFiles::Instance()->PadPosFilePath(fPlaneType,motifTypeId);
528   ifstream padPos(strPadPos.Data());
529   if (fVerboseLevel>0) cout<<"Opening file "<<strPadPos<<endl;
530
531   PadMapType positions;
532
533   char line[256];
534   do {
535     padPos.getline(line,255);
536     if (!padPos) break;
537
538 #if defined (__HP_aCC) || (__alpha)
539     strstream strline;
540     strline << line;
541 #else
542     istringstream strline(line);
543 #endif    
544     string key;
545
546     strline>>key;
547     if ((key=="#") || (key=="") ) continue;
548
549     int i,j;
550     strline>>i>>j;
551     positions[key].first=i;
552     positions[key].second=j;
553   } while (!padPos.eof());
554
555   padPos.close();
556
557   if (fVerboseLevel>0) cout <<"Opening file "
558                             <<AliMpFiles::Instance()->BergToGCFilePath()
559                             <<endl;
560
561   ifstream bergToGCFile(AliMpFiles::Instance()->BergToGCFilePath());
562   Int_t GassiChannel[80];
563   while(1) {
564     Int_t bergNum;
565     TString GCStr;
566     bergToGCFile>>bergNum>>GCStr;
567     if (!bergToGCFile.good()) break;
568     if (GCStr=="GND") continue;
569     if (bergNum>80) {
570         Fatal("BuildMotifType","Berg number > 80 ...");
571         continue;
572     }
573     GassiChannel[bergNum-1]= atoi(GCStr);
574   }
575   bergToGCFile.close();
576   
577   TString strMotif 
578     = AliMpFiles::Instance()->MotifFilePath(fPlaneType,motifTypeId);
579   ifstream motif(strMotif);
580   if (fVerboseLevel>0) cout<<"Opening file "<<strMotif<<endl;
581
582
583   Int_t nofPadsX=0;
584   Int_t nofPadsY=0;
585
586   do {
587   
588     Int_t ix,iy,numBerg,numKapton,padNum,gassiNum;
589
590     TString lineStr,token;
591     lineStr.ReadLine(motif);
592     if (!motif.good()) break;
593 #if defined (__HP_aCC) || (__alpha)
594     strstream tokenList;
595     tokenList << lineStr.Data();
596 #else
597     istringstream tokenList(lineStr.Data());
598 #endif    
599     
600     token.ReadToken(tokenList);
601     if (!tokenList.good()) continue; // column is missing...
602     if ( (token.Length()>0) && (token[0]=='#') ) continue; // this is a comment line
603     
604     numBerg = atoi(token.Data());
605     if (numBerg==0) {
606       Warning("BuildMotifType","Berg number invalid");
607       continue;
608     }
609     
610     token.ReadToken(tokenList);
611     if (!tokenList.good()) continue; // column is missing...
612     numKapton = atoi(token.Data());
613     if (numKapton==0) continue;
614
615     
616     token.ReadToken(tokenList);
617     if (!tokenList.good()) continue; // column is missing...
618     if (token=="GND") continue;
619     string padName = token.Data();
620     padNum = motifType->PadNum(token);
621     
622      token.ReadToken(tokenList);
623      if (token.IsNull() ) continue; // column is missing...
624 //     if (token[0]!='E') {
625 //       cerr<<"Problem : gassinumber isn't begining with E:"<<token<<endl;
626 //       continue;
627 //     }  else {
628 //        gassiNum = atoi(token.Data() +1 )-1;
629 //     }
630     if ( (numBerg<1) || (numBerg>80) ) {
631         Warning("BuildMotifType","Berg number outside range");
632         continue;
633     }
634     
635     gassiNum  = GassiChannel[numBerg-1];
636
637     PadMapTypeIterator iter = positions.find(padName);
638     if (iter==positions.end()) {
639       cerr<<"Problem: Pad number "<<padNum<<" found in the file "<<strMotif
640           <<" but not in the file"<<strPadPos<<endl;
641       continue;
642     }
643
644     ix= iter->second.first;
645     iy= iter->second.second;
646
647     motifType->AddConnection(AliMpIntPair(ix,iy),
648                   new AliMpConnection(padNum,numBerg,numKapton,gassiNum));
649
650     if (ix>=nofPadsX) nofPadsX=ix+1;
651     if (iy>=nofPadsY) nofPadsY=iy+1;
652
653   } while (!motif.eof());    
654
655
656   motifType->SetNofPads(nofPadsX, nofPadsY);
657
658   motif.close();
659
660   return motifType;
661 }
662
663
664 //_____________________________________________________________________________
665 AliMpMotifSpecial*  
666 AliMpReader::BuildMotifSpecial(TString motifID,AliMpMotifType* motifType)
667 {
668 // Build a special motif by reading the file motifSpecial<motifId>.dat
669 // in the data directory
670 // ---
671
672   // Open the input file
673   ifstream in(AliMpFiles::Instance()
674                 ->MotifSpecialFilePath(fPlaneType, motifID).Data(), ios::in);
675   if (!in) {    
676      Error("BuildMotifSpecial", "File not found.");
677      return 0;
678   }
679
680   AliMpMotifSpecial* res = new AliMpMotifSpecial(motifID,motifType);
681   Int_t i,j;
682   Double_t x,y;
683   in >> i;
684   while (!in.eof()){
685     in >>j >>x >> y;
686     res->SetPadDimensions(AliMpIntPair(i,j),TVector2(x/2.,y/2.));
687     in >> i;
688   }
689   
690   in.close();
691   return res;
692 }
693
694
695 //_____________________________________________________________________________
696 void AliMpReader::SetVerboseLevel(Int_t verboseLevel)
697 {
698 // Sets verbose level.
699 // ---
700
701   fVerboseLevel = verboseLevel;
702 }
703