]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/MUONmapping/AliMpSectorReader.cxx
Correct use of ROOT_INCLUDE_DIR
[u/mrichter/AliRoot.git] / MUON / MUONmapping / 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 //-----------------------------------------------------------------------------
21 // Class AliMpSectorReader
22 // -----------------------
23 // Class that takes care of reading the sector data.
24 // Included in AliRoot: 2003/05/02
25 // Authors: David Guez, Ivana Hrivnacova; IPN Orsay
26 //-----------------------------------------------------------------------------
27
28 #include "AliMpSectorReader.h"
29 #include "AliMpSector.h"
30 #include "AliMpFiles.h"
31 #include "AliMpDataStreams.h"
32 #include "AliMpZone.h"
33 #include "AliMpSubZone.h"
34 #include "AliMpRow.h"
35 #include "AliMpVRowSegment.h"
36 #include "AliMpRowSegment.h"
37 #include "AliMpRowSegmentLSpecial.h"
38 #include "AliMpRowSegmentRSpecial.h"
39 #include "AliMpPadRow.h"
40 #include "AliMpMotifReader.h"
41 #include "AliMpMotifMap.h"
42 #include "AliMpMotif.h"
43 #include "AliMpMotifSpecial.h"
44 #include "AliMpMotifType.h"
45 #include "AliMpConnection.h"
46 #include "AliMpDirection.h"
47 #include "AliMpConstants.h"
48
49 #include "AliLog.h"
50
51 #include <Riostream.h>
52 #include <Rstrstream.h>
53 #include <TSystem.h>
54 #include <TMath.h>
55
56 #include <limits>
57
58 #if !defined(__HP_aCC) && !defined(__alpha)
59   #include <sstream>
60 #endif
61
62 /// \cond CLASSIMP
63 ClassImp(AliMpSectorReader)
64 /// \endcond
65
66 //
67 // static private methods
68 //
69
70 //_____________________________________________________________________________
71 const TString& AliMpSectorReader::GetSectorKeyword()
72 {
73   /// sector keyword
74   static const TString kSectorKeyword  = "SECTOR_DATA";
75   return kSectorKeyword;
76 }  
77
78 //_____________________________________________________________________________
79 const TString& AliMpSectorReader::GetZoneKeyword()
80 {
81   /// zone keyword
82   static const TString kZoneKeyword = "ZONE";
83   return kZoneKeyword;
84 }  
85
86 //_____________________________________________________________________________
87 const TString& AliMpSectorReader::GetSubZoneKeyword() 
88 {
89   /// subzone keyword
90   static const TString kSubZoneKeyword = "SUBZONE";
91   return kSubZoneKeyword;
92 }  
93
94 //_____________________________________________________________________________
95 const TString& AliMpSectorReader::GetRowKeyword()
96 {
97   /// row keyword
98   static const TString kRowKeyword = "ROW_SEGMENT";
99   return kRowKeyword;
100 }  
101
102 //_____________________________________________________________________________
103 const TString& AliMpSectorReader::GetSectorSpecialKeyword()
104 {
105   /// sector special keyword
106   static const TString kSectorSpecialKeyword = "SECTOR_SPECIAL_DATA";
107   return kSectorSpecialKeyword;
108 }  
109
110 //_____________________________________________________________________________
111 const TString& AliMpSectorReader::GetMotifKeyword()
112 {
113   /// motif keyword
114   static const TString kMotifKeyword = "MOTIF";
115   return kMotifKeyword;
116 }  
117
118 //_____________________________________________________________________________
119 const TString& AliMpSectorReader::GetRowSpecialKeyword()
120 {
121   /// row special keyword
122   static const TString kRowSpecialKeyword = "ROW";
123   return kRowSpecialKeyword;
124 }  
125
126 //_____________________________________________________________________________
127 const TString& AliMpSectorReader::GetPadRowsKeyword()
128 {
129   /// pad rows keyword
130   static const TString kPadRowsKeyword = "PAD_ROWS";
131   return kPadRowsKeyword;
132 }  
133
134 //_____________________________________________________________________________
135 const TString& AliMpSectorReader::GetPadRowSegmentKeyword()
136 {
137   /// pad row segment keyword
138   static const TString kPadRowSegmentKeyword = "PAD_ROW_SEGMENT";
139   return kPadRowSegmentKeyword;
140 }  
141
142 //
143 // ctors, dtor
144 //
145   
146 //_____________________________________________________________________________
147 AliMpSectorReader::AliMpSectorReader(AliMq::Station12Type station,
148                                      AliMp::PlaneType plane) 
149   : TObject(),
150     fStationType(station),
151     fPlaneType(plane),
152     fSector(0),
153     fMotifReader(new AliMpMotifReader(AliMp::kStation12, station, plane))
154  
155 {
156 /// Standard constructor
157 }
158
159 //_____________________________________________________________________________
160 AliMpSectorReader::~AliMpSectorReader() 
161 {
162 /// Destructor  
163
164   delete fMotifReader;
165 }
166
167 //
168 // private methods
169 //
170
171 //_____________________________________________________________________________
172 void  AliMpSectorReader::ReadSectorData(const AliMpDataStreams& dataStreams,
173                                         istream& in)
174 {
175 /// Read sector input data;
176 /// prepare zones and rows vectors to be filled in.
177
178   TString keyword;
179   in >> keyword;
180   
181   AliDebugStream(2) << keyword << endl;
182
183   if (keyword != GetSectorKeyword()) {
184      AliErrorStream() << "Wrong file format." << endl;
185      return;
186   }   
187     
188   Int_t nofZones, nofRows;
189   TString directionStr;
190   Double_t offsetX, offsetY;
191   in >> nofZones;
192   in >> nofRows;
193   in >> directionStr;
194   in >> offsetX;
195   in >> offsetY;
196   
197   AliMp::Direction direction;
198   direction = (directionStr == "Y") ? AliMp::kY  :  AliMp::kX;
199
200   AliDebugStream(2) << nofZones << " " <<  nofRows << endl;
201   
202   if ( nofZones < 0 || nofZones >= std::numeric_limits<Int_t>::max() ||
203        nofRows < 0  || nofRows >= std::numeric_limits<Int_t>::max() ) {
204     AliErrorStream() << "Wrong nofZones/nofRows value." << endl;
205     return;
206   }         
207
208   fSector = new AliMpSector("Not defined", nofZones, nofRows,direction,
209                             offsetX, offsetY);
210   
211   TString nextKeyword;
212   in >> nextKeyword;
213     
214   if (nextKeyword != GetZoneKeyword()) {
215      AliErrorStream() << "Wrong file format." << endl;
216      return;
217   }      
218   
219   ReadZoneData(dataStreams, in);
220 }  
221
222 //_____________________________________________________________________________
223 void AliMpSectorReader::ReadZoneData(const AliMpDataStreams& dataStreams,
224                                      istream& in)
225 {
226 /// Read zone input data;
227 /// create zone and adds it to zones vector.
228
229   Int_t zoneID;
230   Double_t  sizex, sizey; 
231   in >> zoneID;    
232   in >> sizex;
233   in >> sizey;
234   AliDebugStream(2)
235      << GetZoneKeyword() << " " <<  zoneID << "  " 
236      << sizex << " " << sizey << endl;
237   
238   AliMpZone* zone =  fSector->GetZone(zoneID);
239   zone->SetPadDimensions(sizex/2.,sizey/2.); 
240       
241   TString nextKeyword;
242   in >> nextKeyword;
243     
244   if (nextKeyword != GetSubZoneKeyword()) {
245      AliErrorStream() << "Wrong file format." << endl;
246      return;
247   }  
248     
249   ReadSubZoneData(dataStreams, in, zone);
250 }
251
252 //_____________________________________________________________________________
253 void AliMpSectorReader::ReadSubZoneData(const AliMpDataStreams& dataStreams,
254                                         istream& in, AliMpZone* zone)
255 {
256 /// Read subzone input data;
257 /// create subzone and its to the specified zone.
258
259   AliDebugStream(2) << GetSubZoneKeyword() << endl;
260
261   AliMpVMotif* motif = ReadMotifData(dataStreams, in, zone);
262   AliMpSubZone* subZone = new AliMpSubZone(motif); 
263   zone->AddSubZone(subZone); 
264
265   TString nextKeyword;
266   in >> nextKeyword;
267     
268   if (nextKeyword != GetRowKeyword()) {
269      AliErrorStream() << "Wrong file format." << endl;
270      return;
271   }  
272     
273   ReadRowSegmentsData(dataStreams, in, zone, subZone);
274 }   
275
276 //_____________________________________________________________________________
277 AliMpVMotif*  AliMpSectorReader::ReadMotifData(
278                                      const AliMpDataStreams& dataStreams,
279                                      istream& in, AliMpZone* zone)
280 {
281 /// Read the motif input data.
282
283   TString  motifID;
284   TString  motifTypeID;
285   in >> motifID;
286   in >> motifTypeID;
287
288   AliDebugStream(2) << motifID << " " << motifTypeID << endl;
289   
290   AliMpMotifMap* motifMap = fSector->GetMotifMap();
291
292   AliMpMotifType* motifType = 0;
293   AliMpVMotif* motif 
294     = motifMap->FindMotif(motifID, motifTypeID, 
295                           zone->GetPadDimensionX(), zone->GetPadDimensionY());
296   if (!motif) {    
297     motifType = motifMap->FindMotifType(motifTypeID);
298     if (!motifType) {
299       motifType = fMotifReader->BuildMotifType(dataStreams, motifTypeID);
300       motifMap->AddMotifType(motifType);
301     }
302     
303     if (zone->GetPadDimensionX() != 0. && zone->GetPadDimensionY() != 0.) 
304       motif = new AliMpMotif(motifID, motifType, 
305                      zone->GetPadDimensionX(), zone->GetPadDimensionY());
306     else 
307       motif = fMotifReader->BuildMotifSpecial(dataStreams, motifID, motifType);
308       
309     if (motif) 
310       motifMap->AddMotif(motif);
311
312   }  
313
314   return motif;   
315 }  
316
317 //_____________________________________________________________________________
318 void AliMpSectorReader::ReadRowSegmentsData(const AliMpDataStreams& dataStreams,
319                                       istream& in,
320                                       AliMpZone* zone, AliMpSubZone* subZone)
321 {
322 /// Read row segments input data of a specified zone and subzone;
323 /// creates row segment and add it to the specified subzone
324 /// and a corresponding row in the rows vector.
325
326   TString nextKeyword;
327   do {
328     // 
329     // Read data from file
330     //
331     Int_t offX, offY, inRow, nofMotifs, firstMotifPositionId, firstMotifPositionDId;
332     in >> offX;
333     in >> offY;
334     in >> inRow;
335     in >> nofMotifs;
336     in >> firstMotifPositionId;
337     in >> firstMotifPositionDId;
338     
339     firstMotifPositionId |= AliMpConstants::ManuMask(fPlaneType);
340     
341     AliDebugStream(2)
342       << GetRowKeyword() << " " 
343       << offX << " " << offY << " " << inRow << " " << nofMotifs << " " 
344       << firstMotifPositionId << " " << firstMotifPositionDId
345       << endl;
346
347     in >> nextKeyword;
348
349     //
350     // Process data
351     //
352     AliMpRow* row = fSector->GetRow(inRow);
353     AliMpVMotif* motif = subZone->GetMotif();
354     
355     // Create row segment and add it to its zone, row   
356     AliMpVRowSegment* rowSegment
357       = new AliMpRowSegment(row, motif, offX, offY, nofMotifs, 
358                         firstMotifPositionId, firstMotifPositionDId);
359                         
360     subZone->AddRowSegment(rowSegment);
361     row->AddRowSegment(rowSegment);
362   }
363   while (!in.eof() && (nextKeyword == GetRowKeyword()));
364
365   if (in.eof()) return;
366
367   if (nextKeyword == GetZoneKeyword()) {
368     ReadZoneData(dataStreams, in);
369   }
370   else if (nextKeyword == GetSubZoneKeyword()) {
371     ReadSubZoneData(dataStreams, in, zone);
372   }   
373   else {
374     AliErrorStream() << "Wrong file format." << endl;
375   } 
376 }   
377
378 //_____________________________________________________________________________
379 void AliMpSectorReader::ReadSectorSpecialData(
380                             const AliMpDataStreams& dataStreams,
381                             istream& in, AliMp::XDirection direction)
382 {
383 /// Read sector input data
384 /// with a special (irregular) motifs.
385
386   TString keyword;
387   in >> keyword;
388
389   AliDebugStream(2) << keyword << endl;
390
391   if (keyword != GetSectorSpecialKeyword()) {
392      AliErrorStream() << "Wrong file format." << endl;
393      return;
394   }   
395
396   TString nextKeyword;
397   in >> nextKeyword;
398
399   AliDebugStream(2) << keyword << endl;
400     
401   if (nextKeyword != GetMotifKeyword()) {
402     AliErrorStream() << "Wrong file format." << endl;
403     return;
404   }  
405
406   ReadMotifsSpecialData(dataStreams, in);
407   ReadRowSpecialData(dataStreams, in, direction);
408 }  
409
410 //_____________________________________________________________________________
411 void AliMpSectorReader::ReadMotifsSpecialData(
412                             const AliMpDataStreams& dataStreams,
413                             istream& in)
414 {
415 /// Read the special (irregular) motifs input data.
416
417   AliDebugStream(2) << GetMotifKeyword() << endl;
418
419   TString nextKeyword;
420   do {
421     Int_t zone;
422     in >> zone;
423     AliMpVMotif* motif =  ReadMotifData(dataStreams, in, fSector->GetZone(zone));
424     AliMpSubZone* subZone = new AliMpSubZone(motif); 
425     fSector->GetZone(zone)->AddSubZone(subZone); 
426   
427     in >> nextKeyword;
428
429     AliDebugStream(2) << nextKeyword << endl;      
430   }
431   while (nextKeyword == GetMotifKeyword());
432     
433   if (nextKeyword != GetRowSpecialKeyword()) {
434      AliErrorStream() << "Wrong file format." << endl;
435      return;
436   }      
437 }  
438
439 //_____________________________________________________________________________
440 void AliMpSectorReader::ReadRowSpecialData(
441                             const AliMpDataStreams& dataStreams,
442                             istream& in, AliMp::XDirection direction)
443 {
444 /// Read row input data
445 /// with a special (irregular) motifs.
446
447   Int_t id;
448   in >> id;
449
450   AliDebugStream(2) << id << endl;      
451   
452   // Get the row and its border
453   AliMpRow* row = fSector->GetRow(id);
454
455   AliMpVRowSegmentSpecial* segment = 0;
456   if (direction == AliMp::kLeft) {
457     AliMpVRowSegment* firstNormalSeg = row->GetRowSegment(0);
458     Double_t offsetX = firstNormalSeg->LeftBorderX();
459   
460     // Create a special row segment
461     segment = new AliMpRowSegmentLSpecial(row, offsetX);
462     row->AddRowSegmentInFront(segment);
463   }
464   else { 
465     AliMpVRowSegment* precedentNormalSeg 
466       = row->GetRowSegment(row->GetNofRowSegments()-1);
467     Double_t offsetX = precedentNormalSeg->RightBorderX();
468   
469     // Create a special row segment
470     segment = new AliMpRowSegmentRSpecial(row, offsetX);
471     row->AddRowSegment(segment);
472   }  
473       
474   TString nextKeyword;
475   in >> nextKeyword;
476   
477   AliDebugStream(2) << nextKeyword << endl;
478     
479   if (nextKeyword != GetPadRowsKeyword()) {
480      AliErrorStream() << "Wrong file format." << endl;
481      return;
482   }  
483     
484   ReadRowSegmentSpecialData(dataStreams, in, segment, direction);
485   
486   // Update row segment and set it to all subzones associated with
487   // contained motifs
488   
489   segment->UpdateMotifVector();
490   segment->UpdatePadsOffset();
491   
492   for (Int_t i=0; i<segment->GetNofMotifs(); i++) {
493     AliMpSubZone* subZone = 0;
494     Int_t j = 0;
495     while (!subZone && j<fSector->GetNofZones())
496       subZone = fSector->GetZone(++j)->FindSubZone(segment->GetMotif(i));
497     
498     if (subZone) subZone->AddRowSegment(segment);
499   }  
500 }  
501
502 //_____________________________________________________________________________
503 void AliMpSectorReader::ReadRowSegmentSpecialData(
504                             const AliMpDataStreams& dataStreams,
505                             istream& in,
506                             AliMpVRowSegmentSpecial* segment,
507                             AliMp::XDirection direction)
508 {
509 /// Read row segment input data
510 /// with a special (irregular) motifs.
511
512   Int_t nofPadRows;
513   in >> nofPadRows;
514   
515   AliDebugStream(2) << nofPadRows << endl;
516   
517   if ( nofPadRows < 0 || nofPadRows >= std::numeric_limits<Int_t>::max()) {
518     AliErrorStream() << "Wrong nofPadRows value." << endl;
519     return;
520   }         
521
522   TString keyword;
523   in >> keyword;
524
525   AliDebugStream(2) << keyword << endl;
526     
527   if (keyword != GetPadRowSegmentKeyword()) {
528      AliErrorStream() << "Wrong file format." << endl;
529      return;
530   }  
531   
532   //
533   // Process data
534   //
535     
536   TObjArray newPadRows;
537   for (Int_t i=0; i<nofPadRows; i++) {
538     
539      // Create pad row
540      AliMpPadRow* padRow = new AliMpPadRow(direction);
541      segment->AddPadRow(padRow);
542      
543      // Keep the new rows in a temporary vector
544      newPadRows.Add(padRow);
545   }   
546       
547   TString nextKeyword;
548   do {
549     // 
550     // Read data from file
551     //
552     Int_t    nofPadsInRow, motifPositionId;
553     TString   motifId, motifTypeId;
554     in >> nofPadsInRow;
555     in >> motifId;
556     in >> motifPositionId; 
557   
558     motifPositionId |= AliMpConstants::ManuMask(fPlaneType);
559
560     AliDebugStream(2)
561       << nofPadsInRow << " " << motifId << " " << motifPositionId << endl;
562
563     in >> nextKeyword;
564
565     AliDebugStream(2) << nextKeyword << endl;
566
567     //
568     // Process data
569     //
570     
571     for (Int_t i=0; i<nofPadRows; i++) {
572     
573       // Get pad row from the temporary vector
574       AliMpPadRow* padRow = (AliMpPadRow*)newPadRows[i];
575       
576       // Find motif
577       AliMpVMotif* motif = fSector->GetMotifMap()->FindMotif(motifId);
578       
579       if (!motif) {
580         AliErrorStream() << "Unknown motif" << endl;
581         return;
582       }
583
584       // Create pad row segment
585       padRow->AddPadRowSegment(dynamic_cast<AliMpMotif *>(motif), 
586                                motifPositionId, nofPadsInRow);
587     }  
588   }
589   while (!in.eof() && (nextKeyword == GetPadRowSegmentKeyword()));
590   
591   if (in.eof()) return;
592
593   if (nextKeyword == GetPadRowsKeyword()) {
594     ReadRowSegmentSpecialData(dataStreams, in, segment, direction);
595   }
596   else if (nextKeyword == GetRowSpecialKeyword()) {
597     ReadRowSpecialData(dataStreams, in, direction);
598   }   
599   else {
600     AliErrorStream() << "Wrong file format." << endl;
601   } 
602 }  
603
604 //
605 // public methods
606 //
607
608 //_____________________________________________________________________________
609 AliMpSector* AliMpSectorReader::BuildSector(const AliMpDataStreams& dataStreams)
610 {
611 /// Read the mapping data from stream and create the basic objects:         \n
612 /// zones, subzones, rows, row segments, motifs.
613
614   // Open input stream
615   //
616   istream& in 
617     = dataStreams.
618         CreateDataStream(AliMpFiles::SectorFilePath(fStationType,fPlaneType));
619
620   ReadSectorData(dataStreams, in);
621   delete &in;
622   
623   fSector->SetRowSegmentOffsets();
624
625   // Open input stream for special inner zone
626   
627   // add is data function
628   
629   TString sectorSpecialFileName 
630     = AliMpFiles::SectorSpecialFilePath(fStationType, fPlaneType);
631   if ( dataStreams.IsDataStream(sectorSpecialFileName) ) {
632     istream& in2 
633       = dataStreams.
634           CreateDataStream(sectorSpecialFileName);
635   
636     ReadSectorSpecialData(dataStreams, in2, AliMp::kLeft);
637     
638     delete &in2;
639   }  
640
641   // Open input file for special outer zone
642   TString sectorSpecialFileName2 
643     = AliMpFiles::SectorSpecialFilePath2(fStationType, fPlaneType);
644   if ( dataStreams.IsDataStream(sectorSpecialFileName2) ) {
645     istream& in3
646       = dataStreams.
647           CreateDataStream(sectorSpecialFileName2);
648     
649     ReadSectorSpecialData(dataStreams, in3, AliMp::kRight);
650     
651     delete &in3;
652   }   
653
654   fSector->Initialize();
655   
656   return fSector;
657 }  
658