]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/mapping/AliMpSectorReader.cxx
Adding new libraries
[u/mrichter/AliRoot.git] / MUON / mapping / AliMpSectorReader.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 // $Id$
17 // $MpId: AliMpSectorReader.cxx,v 1.9 2006/05/24 13:58:46 ivana Exp $
18 // Category: sector
19 //
20 // Class AliMpSectorReader
21 // -----------------------
22 // Class that takes care of reading the sector data.
23 // Included in AliRoot: 2003/05/02
24 // Authors: David Guez, Ivana Hrivnacova; IPN Orsay
25
26 #include "AliMpSectorReader.h"
27 #include "AliMpSector.h"
28 #include "AliMpFiles.h"
29 #include "AliMpZone.h"
30 #include "AliMpSubZone.h"
31 #include "AliMpRow.h"
32 #include "AliMpVRowSegment.h"
33 #include "AliMpRowSegment.h"
34 #include "AliMpRowSegmentLSpecial.h"
35 #include "AliMpRowSegmentRSpecial.h"
36 #include "AliMpPadRow.h"
37 #include "AliMpMotifReader.h"
38 #include "AliMpMotifMap.h"
39 #include "AliMpMotif.h"
40 #include "AliMpMotifSpecial.h"
41 #include "AliMpMotifType.h"
42 #include "AliMpConnection.h"
43 #include "AliMpIntPair.h"
44 #include "AliMpDirection.h"
45 #include "AliMpConstants.h"
46
47 #include "AliLog.h"
48
49 #include <Riostream.h>
50 #include <Rstrstream.h>
51 #include <TSystem.h>
52 #include <TMath.h>
53
54 #if !defined(__HP_aCC) && !defined(__alpha)
55   #include <sstream>
56 #endif
57
58 /// \cond CLASSIMP
59 ClassImp(AliMpSectorReader)
60 /// \endcond
61
62 const TString  AliMpSectorReader::fgkSectorKeyword  = "SECTOR_DATA";
63 const TString  AliMpSectorReader::fgkZoneKeyword    = "ZONE";
64 const TString  AliMpSectorReader::fgkSubZoneKeyword = "SUBZONE";
65 const TString  AliMpSectorReader::fgkRowKeyword     = "ROW_SEGMENT";
66 const TString  AliMpSectorReader::fgkEofKeyword     = "EOF";
67 const TString  AliMpSectorReader::fgkSectorSpecialKeyword  = "SECTOR_SPECIAL_DATA";
68 const TString  AliMpSectorReader::fgkMotifKeyword          = "MOTIF";
69 const TString  AliMpSectorReader::fgkRowSpecialKeyword     = "ROW";
70 const TString  AliMpSectorReader::fgkPadRowsKeyword        = "PAD_ROWS";
71 const TString  AliMpSectorReader::fgkPadRowSegmentKeyword  = "PAD_ROW_SEGMENT";
72
73 //_____________________________________________________________________________
74 AliMpSectorReader::AliMpSectorReader(AliMp::StationType station, 
75                                      AliMp::PlaneType plane) 
76   : TObject(),
77     fStationType(station),
78     fPlaneType(plane),
79     fSector(0),
80     fMotifReader(new AliMpMotifReader(station, plane))
81 {
82 /// Standard constructor
83 }
84
85 //_____________________________________________________________________________
86 AliMpSectorReader::AliMpSectorReader() 
87   : TObject(),
88     fStationType(AliMp::kStation1),
89     fPlaneType(AliMp::kBendingPlane),
90     fSector(0),
91     fMotifReader(0)
92 {
93 /// Default constructor
94 }
95
96 //_____________________________________________________________________________
97 AliMpSectorReader::~AliMpSectorReader() 
98 {
99 /// Destructor  
100
101   delete fMotifReader;
102 }
103
104 //
105 // private methods
106 //
107
108 //_____________________________________________________________________________
109 void  AliMpSectorReader::ReadSectorData(ifstream& in)
110 {
111 /// Read sector input data;
112 /// prepare zones and rows vectors to be filled in.
113
114   TString keyword;
115   in >> keyword;
116   
117   AliDebugStream(2) << keyword << endl;
118
119   if (keyword != fgkSectorKeyword) {
120      Fatal("ReadSectorData", "Wrong file format.");
121      return;
122   }   
123     
124   Int_t nofZones, nofRows;
125   TString directionStr;
126   Double_t offsetX, offsetY;
127   in >> nofZones;
128   in >> nofRows;
129   in >> directionStr;
130   in >> offsetX;
131   in >> offsetY;
132   
133   AliMp::Direction direction;
134   direction = (directionStr == "Y") ? AliMp::kY  :  AliMp::kX;
135
136   AliDebugStream(2) << nofZones << " " <<  nofRows << endl;
137
138   fSector = new AliMpSector("Not defined", nofZones, nofRows,direction,
139                             TVector2(offsetX, offsetY));
140   
141   TString nextKeyword;
142   in >> nextKeyword;
143     
144   if (nextKeyword != fgkZoneKeyword) {
145      Fatal("ReadSectorData", "Wrong file format.");
146      return;
147   }      
148   
149   ReadZoneData(in);
150 }  
151
152 //_____________________________________________________________________________
153 void AliMpSectorReader::ReadZoneData(ifstream& in)
154 {
155 /// Read zone input data;
156 /// create zone and adds it to zones vector.
157
158   Int_t zoneID;
159   Double_t  sizex, sizey; 
160   in >> zoneID;    
161   in >> sizex;
162   in >> sizey;
163   AliDebugStream(2)
164      << fgkZoneKeyword << " " <<  zoneID << "  " 
165      << sizex << " " << sizey << endl;
166   
167   AliMpZone* zone =  fSector->GetZone(zoneID);
168   zone->SetPadDimensions(TVector2(sizex/2.,sizey/2.)); 
169       
170   TString nextKeyword;
171   in >> nextKeyword;
172     
173   if (nextKeyword != fgkSubZoneKeyword) {
174      Fatal("ReadZoneData", "Wrong file format.");
175      return;
176   }  
177     
178   ReadSubZoneData(in, zone);
179 }
180
181 //_____________________________________________________________________________
182 void AliMpSectorReader::ReadSubZoneData(ifstream& in, AliMpZone* zone)
183 {
184 /// Read subzone input data;
185 /// create subzone and its to the specified zone.
186
187   AliDebugStream(2) << fgkSubZoneKeyword << endl;
188
189   AliMpVMotif* motif = ReadMotifData(in, zone);
190   AliMpSubZone* subZone = new AliMpSubZone(motif); 
191   zone->AddSubZone(subZone); 
192
193   TString nextKeyword;
194   in >> nextKeyword;
195     
196   if (nextKeyword != fgkRowKeyword) {
197      Fatal("ReadSubZoneData", "Wrong file format.");
198      return;
199   }  
200     
201   ReadRowSegmentsData(in, zone, subZone);
202 }   
203
204 //_____________________________________________________________________________
205 AliMpVMotif*  AliMpSectorReader::ReadMotifData(ifstream& in, AliMpZone* zone)
206 {
207 /// Read the motif input data.
208
209   TString  motifID;
210   TString  motifTypeID;
211   in >> motifID;
212   in >> motifTypeID;
213
214   AliDebugStream(2) << motifID << " " << motifTypeID << endl;
215   
216   AliMpMotifMap* motifMap = fSector->GetMotifMap();
217
218   AliMpMotifType* motifType = 0;
219   AliMpVMotif* motif 
220     = motifMap->FindMotif(motifID, motifTypeID, zone->GetPadDimensions());
221   if (!motif) {    
222     motifType = motifMap->FindMotifType(motifTypeID);
223     if (!motifType) {
224       motifType = fMotifReader->BuildMotifType(motifTypeID);     
225       motifMap->AddMotifType(motifType);
226     }
227     
228     if (zone->GetPadDimensions().X() != 0. && zone->GetPadDimensions().Y() != 0.) 
229       motif = new AliMpMotif(motifID, motifType, zone->GetPadDimensions());
230     else 
231       motif = fMotifReader->BuildMotifSpecial(motifID, motifType);
232       
233     if (motif) 
234       motifMap->AddMotif(motif);
235
236   }  
237
238   return motif;   
239 }  
240
241 //_____________________________________________________________________________
242 void AliMpSectorReader::ReadRowSegmentsData(ifstream& in, 
243                                       AliMpZone* zone, AliMpSubZone* subZone)
244 {
245 /// Read row segments input data of a specified zone and subzone;
246 /// creates row segment and add it to the specified subzone
247 /// and a corresponding row in the rows vector.
248
249   TString nextKeyword;
250   do {
251     // 
252     // Read data from file
253     //
254     Int_t offX, offY, inRow, nofMotifs, firstMotifPositionId, firstMotifPositionDId;
255     in >> offX;
256     in >> offY;
257     in >> inRow;
258     in >> nofMotifs;
259     in >> firstMotifPositionId;
260     in >> firstMotifPositionDId;
261     
262     firstMotifPositionId |= AliMpConstants::ManuMask(fPlaneType);
263     
264     AliDebugStream(2)
265       << fgkRowKeyword << " " 
266       << offX << " " << offY << " " << inRow << " " << nofMotifs << " " 
267       << firstMotifPositionId << " " << firstMotifPositionDId
268       << endl;
269
270     in >> nextKeyword;
271
272     //
273     // Process data
274     //
275     AliMpRow* row = fSector->GetRow(inRow);
276     AliMpVMotif* motif = subZone->GetMotif();
277     
278     // Create row segment and add it to its zone, row   
279     AliMpVRowSegment* rowSegment
280       = new AliMpRowSegment(row, motif, AliMpIntPair(offX, offY), nofMotifs, 
281                         firstMotifPositionId, firstMotifPositionDId);
282                         
283     subZone->AddRowSegment(rowSegment);
284     row->AddRowSegment(rowSegment);
285   }
286   while (!in.eof() && (nextKeyword == fgkRowKeyword));
287
288   if (in.eof()) return;
289
290   if (nextKeyword == fgkZoneKeyword) {
291     ReadZoneData(in);
292   }
293   else if (nextKeyword == fgkSubZoneKeyword) {
294     ReadSubZoneData(in, zone);
295   }   
296   else {
297     Fatal("ReadRowSegmentsData", "Wrong file format.");
298   } 
299 }   
300
301 //_____________________________________________________________________________
302 void AliMpSectorReader::ReadSectorSpecialData(ifstream& in, AliMp::XDirection direction)
303 {
304 /// Read sector input data
305 /// with a special (irregular) motifs.
306
307   TString keyword;
308   in >> keyword;
309
310   AliDebugStream(2) << keyword << endl;
311
312   if (keyword != fgkSectorSpecialKeyword) {
313      Fatal("ReadSectorSpecialData", "Wrong file format.");
314      return;
315   }   
316
317   TString nextKeyword;
318   in >> nextKeyword;
319
320   AliDebugStream(2) << keyword << endl;
321     
322   if (nextKeyword != fgkMotifKeyword) {
323     Fatal("ReadSectorSpecialData", "Wrong file format.");
324     return;
325   }  
326
327   ReadMotifsSpecialData(in);     
328   ReadRowSpecialData(in, direction); 
329 }  
330
331 //_____________________________________________________________________________
332 void AliMpSectorReader::ReadMotifsSpecialData(ifstream& in)
333 {
334 /// Read the special (irregular) motifs input data.
335
336   AliDebugStream(2) << fgkMotifKeyword << endl;
337
338   TString nextKeyword;
339   do {
340     Int_t zone;
341     in >> zone;
342     AliMpVMotif* motif =  ReadMotifData(in, fSector->GetZone(zone));
343     AliMpSubZone* subZone = new AliMpSubZone(motif); 
344     fSector->GetZone(zone)->AddSubZone(subZone); 
345   
346     in >> nextKeyword;
347
348     AliDebugStream(2) << nextKeyword << endl;      
349   }
350   while (nextKeyword == fgkMotifKeyword);
351     
352   if (nextKeyword != fgkRowSpecialKeyword) {
353      Fatal("ReadMotifSpecialData", "Wrong file format.");
354      return;
355   }      
356 }  
357
358 //_____________________________________________________________________________
359 void AliMpSectorReader::ReadRowSpecialData(ifstream& in, AliMp::XDirection direction)
360 {
361 /// Read row input data
362 /// with a special (irregular) motifs.
363
364   Int_t id;
365   in >> id;
366
367   AliDebugStream(2) << id << endl;      
368   
369   // Get the row and its border
370   AliMpRow* row = fSector->GetRow(id);
371
372   AliMpVRowSegmentSpecial* segment = 0;
373   if (direction == AliMp::kLeft) {
374     AliMpVRowSegment* firstNormalSeg = row->GetRowSegment(0);
375     Double_t offsetX = firstNormalSeg->LeftBorderX();
376   
377     // Create a special row segment
378     segment = new AliMpRowSegmentLSpecial(row, offsetX);
379     row->AddRowSegmentInFront(segment);
380   }
381   else { 
382     AliMpVRowSegment* precedentNormalSeg 
383       = row->GetRowSegment(row->GetNofRowSegments()-1);
384     Double_t offsetX = precedentNormalSeg->RightBorderX();
385   
386     // Create a special row segment
387     segment = new AliMpRowSegmentRSpecial(row, offsetX);
388     row->AddRowSegment(segment);
389   }  
390       
391   TString nextKeyword;
392   in >> nextKeyword;
393   
394   AliDebugStream(2) << nextKeyword << endl;
395     
396   if (nextKeyword != fgkPadRowsKeyword) {
397      Fatal("ReadRowSpecialData", "Wrong file format.");
398      return;
399   }  
400     
401   ReadRowSegmentSpecialData(in, segment, direction); 
402   
403   // Update row segment and set it to all subzones associated with
404   // contained motifs
405   
406   segment->UpdateMotifVector();
407   segment->UpdatePadsOffset();
408   
409   for (Int_t i=0; i<segment->GetNofMotifs(); i++) {
410     AliMpSubZone* subZone = 0;
411     Int_t j = 0;
412     while (!subZone && j<fSector->GetNofZones())
413       subZone = fSector->GetZone(++j)->FindSubZone(segment->GetMotif(i));
414     
415     subZone->AddRowSegment(segment);
416   }  
417 }  
418
419 //_____________________________________________________________________________
420 void AliMpSectorReader::ReadRowSegmentSpecialData(ifstream& in, 
421                                             AliMpVRowSegmentSpecial* segment,
422                                             AliMp::XDirection direction)
423 {
424 /// Read row segment input data
425 /// with a special (irregular) motifs.
426
427   Int_t nofPadRows;
428   in >> nofPadRows;
429   
430   AliDebugStream(2) << nofPadRows << endl;
431   
432   TString keyword;
433   in >> keyword;
434
435   AliDebugStream(2) << keyword << endl;
436     
437   if (keyword != fgkPadRowSegmentKeyword) {
438      Fatal("ReadRowSegmentSpecialData", "Wrong file format.");
439      return;
440   }  
441   
442   //
443   // Process data
444   //
445     
446   AliMpVRowSegmentSpecial::PadRowVector  newPadRows;
447   for (Int_t i=0; i<nofPadRows; i++) {
448     
449      // Create pad row
450      AliMpPadRow* padRow = new AliMpPadRow(direction);
451      segment->AddPadRow(padRow);
452      
453      // Keep the new rows in a temporary vector
454 #ifdef WITH_STL
455      newPadRows.push_back(padRow);
456 #endif
457 #ifdef WITH_ROOT
458      newPadRows.Add(padRow);
459 #endif
460   }   
461       
462   TString nextKeyword;
463   do {
464     // 
465     // Read data from file
466     //
467     Int_t    nofPadsInRow, motifPositionId;
468     TString   motifId, motifTypeId;
469     in >> nofPadsInRow;
470     in >> motifId;
471     in >> motifPositionId; 
472   
473     motifPositionId |= AliMpConstants::ManuMask(fPlaneType);
474
475     AliDebugStream(2)
476       << nofPadsInRow << " " << motifId << " " << motifPositionId << endl;
477
478     in >> nextKeyword;
479
480     AliDebugStream(2) << nextKeyword << endl;
481
482     //
483     // Process data
484     //
485     
486     for (Int_t i=0; i<nofPadRows; i++) {
487     
488       // Get pad row from the temporary vector
489 #ifdef WITH_STL
490       AliMpPadRow* padRow = newPadRows[i];
491 #endif
492 #ifdef WITH_ROOT
493       AliMpPadRow* padRow = (AliMpPadRow*)newPadRows[i];
494 #endif
495       
496       // Find motif
497       AliMpVMotif* motif = fSector->GetMotifMap()->FindMotif(motifId);
498       
499       if (!motif) {
500         Fatal("ReadRowSegmentSpecialData", "Unknown motif.");
501         return;
502       }
503
504       // Create pad row segment
505       padRow->AddPadRowSegment(dynamic_cast<AliMpMotif *>(motif), 
506                                motifPositionId, nofPadsInRow);
507     }  
508   }
509   while (!in.eof() && (nextKeyword == fgkPadRowSegmentKeyword));
510   
511   if (in.eof()) return;
512
513   if (nextKeyword == fgkPadRowsKeyword) {
514     ReadRowSegmentSpecialData(in, segment, direction);
515   }
516   else if (nextKeyword == fgkRowSpecialKeyword) {
517     ReadRowSpecialData(in, direction);
518   }   
519   else {
520     Fatal("ReadRowSegmentSpecialData", "Wrong file format.");
521   } 
522 }  
523
524 //
525 // public methods
526 //
527
528 //_____________________________________________________________________________
529 AliMpSector* AliMpSectorReader::BuildSector()
530 {
531 /// Read the mapping data from ascii data file
532 /// and create the basic objects:                                            \n
533 /// zones, subzones, rows, row segments, motifs.
534
535   // Open input file
536   ifstream in(AliMpFiles::SectorFilePath(fStationType, fPlaneType).Data(), ios::in);
537   if (!in) {
538      AliErrorStream()
539        << "File " << AliMpFiles::SectorFilePath(fStationType, fPlaneType) 
540        << " not found." << endl;
541      return 0;
542   }
543   
544   ReadSectorData(in);
545   fSector->SetRowSegmentOffsets();
546
547   // Open input file for special inner zone
548   TString sectorSpecialFileName 
549     = AliMpFiles::SectorSpecialFilePath(fStationType, fPlaneType);
550   if (!gSystem->AccessPathName(sectorSpecialFileName.Data())) {
551     ifstream in2(sectorSpecialFileName.Data(), ios::in);
552     if (!in2) { 
553        AliErrorStream()
554          << "File " << AliMpFiles::SectorSpecialFilePath(fStationType, fPlaneType) 
555          << " not found." << endl;
556        return 0;
557     }
558     
559     ReadSectorSpecialData(in2, AliMp::kLeft);
560   }   
561
562   // Open input file for special outer zone
563   TString sectorSpecialFileName2 
564     = AliMpFiles::SectorSpecialFilePath2(fStationType, fPlaneType);
565   if (!gSystem->AccessPathName(sectorSpecialFileName2.Data())) {
566     ifstream in3(sectorSpecialFileName2.Data(), ios::in);
567     if (!in3) { 
568        AliErrorStream()
569          << "File " << AliMpFiles::SectorSpecialFilePath2(fStationType, fPlaneType) 
570          << " not found."<< endl;       
571        return 0;
572     }
573     
574     ReadSectorSpecialData(in3, AliMp::kRight);
575   }   
576
577   fSector->Initialize();
578   
579   return fSector;
580 }  
581