]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/mapping/AliMpSectorReader.cxx
One more attempt to fix TAINTED_SCALAR defect reported by Coverity
[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 //-----------------------------------------------------------------------------
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 const TString  AliMpSectorReader::fgkSectorKeyword  = "SECTOR_DATA";
67 const TString  AliMpSectorReader::fgkZoneKeyword    = "ZONE";
68 const TString  AliMpSectorReader::fgkSubZoneKeyword = "SUBZONE";
69 const TString  AliMpSectorReader::fgkRowKeyword     = "ROW_SEGMENT";
70 const TString  AliMpSectorReader::fgkEofKeyword     = "EOF";
71 const TString  AliMpSectorReader::fgkSectorSpecialKeyword  = "SECTOR_SPECIAL_DATA";
72 const TString  AliMpSectorReader::fgkMotifKeyword          = "MOTIF";
73 const TString  AliMpSectorReader::fgkRowSpecialKeyword     = "ROW";
74 const TString  AliMpSectorReader::fgkPadRowsKeyword        = "PAD_ROWS";
75 const TString  AliMpSectorReader::fgkPadRowSegmentKeyword  = "PAD_ROW_SEGMENT";
76
77 //_____________________________________________________________________________
78 AliMpSectorReader::AliMpSectorReader(const AliMpDataStreams& dataStreams,
79                                      AliMq::Station12Type station, 
80                                      AliMp::PlaneType plane) 
81   : TObject(),
82     fkDataStreams(dataStreams),
83     fStationType(station),
84     fPlaneType(plane),
85     fSector(0),
86     fMotifReader(new AliMpMotifReader(dataStreams, AliMp::kStation12, station, plane))
87  
88 {
89 /// Standard constructor
90 }
91
92 //_____________________________________________________________________________
93 AliMpSectorReader::~AliMpSectorReader() 
94 {
95 /// Destructor  
96
97   delete fMotifReader;
98 }
99
100 //
101 // private methods
102 //
103
104 //_____________________________________________________________________________
105 void  AliMpSectorReader::ReadSectorData(istream& in)
106 {
107 /// Read sector input data;
108 /// prepare zones and rows vectors to be filled in.
109
110   TString keyword;
111   in >> keyword;
112   
113   AliDebugStream(2) << keyword << endl;
114
115   if (keyword != fgkSectorKeyword) {
116      AliErrorStream() << "Wrong file format." << endl;
117      return;
118   }   
119     
120   Int_t nofZones, nofRows;
121   TString directionStr;
122   Double_t offsetX, offsetY;
123   in >> nofZones;
124   in >> nofRows;
125   in >> directionStr;
126   in >> offsetX;
127   in >> offsetY;
128   
129   AliMp::Direction direction;
130   direction = (directionStr == "Y") ? AliMp::kY  :  AliMp::kX;
131
132   AliDebugStream(2) << nofZones << " " <<  nofRows << endl;
133   
134   if ( nofZones < 0 || nofZones >= std::numeric_limits<Int_t>::max() ||
135        nofRows < 0  || nofRows >= std::numeric_limits<Int_t>::max() ) {
136     AliErrorStream() << "Wrong nofZones/nofRows value." << endl;
137     return;
138   }         
139
140   fSector = new AliMpSector("Not defined", nofZones, nofRows,direction,
141                             offsetX, offsetY);
142   
143   TString nextKeyword;
144   in >> nextKeyword;
145     
146   if (nextKeyword != fgkZoneKeyword) {
147      AliErrorStream() << "Wrong file format." << endl;
148      return;
149   }      
150   
151   ReadZoneData(in);
152 }  
153
154 //_____________________________________________________________________________
155 void AliMpSectorReader::ReadZoneData(istream& in)
156 {
157 /// Read zone input data;
158 /// create zone and adds it to zones vector.
159
160   Int_t zoneID;
161   Double_t  sizex, sizey; 
162   in >> zoneID;    
163   in >> sizex;
164   in >> sizey;
165   AliDebugStream(2)
166      << fgkZoneKeyword << " " <<  zoneID << "  " 
167      << sizex << " " << sizey << endl;
168   
169   AliMpZone* zone =  fSector->GetZone(zoneID);
170   zone->SetPadDimensions(sizex/2.,sizey/2.); 
171       
172   TString nextKeyword;
173   in >> nextKeyword;
174     
175   if (nextKeyword != fgkSubZoneKeyword) {
176      AliErrorStream() << "Wrong file format." << endl;
177      return;
178   }  
179     
180   ReadSubZoneData(in, zone);
181 }
182
183 //_____________________________________________________________________________
184 void AliMpSectorReader::ReadSubZoneData(istream& in, AliMpZone* zone)
185 {
186 /// Read subzone input data;
187 /// create subzone and its to the specified zone.
188
189   AliDebugStream(2) << fgkSubZoneKeyword << endl;
190
191   AliMpVMotif* motif = ReadMotifData(in, zone);
192   AliMpSubZone* subZone = new AliMpSubZone(motif); 
193   zone->AddSubZone(subZone); 
194
195   TString nextKeyword;
196   in >> nextKeyword;
197     
198   if (nextKeyword != fgkRowKeyword) {
199      AliErrorStream() << "Wrong file format." << endl;
200      return;
201   }  
202     
203   ReadRowSegmentsData(in, zone, subZone);
204 }   
205
206 //_____________________________________________________________________________
207 AliMpVMotif*  AliMpSectorReader::ReadMotifData(istream& in, AliMpZone* zone)
208 {
209 /// Read the motif input data.
210
211   TString  motifID;
212   TString  motifTypeID;
213   in >> motifID;
214   in >> motifTypeID;
215
216   AliDebugStream(2) << motifID << " " << motifTypeID << endl;
217   
218   AliMpMotifMap* motifMap = fSector->GetMotifMap();
219
220   AliMpMotifType* motifType = 0;
221   AliMpVMotif* motif 
222     = motifMap->FindMotif(motifID, motifTypeID, 
223                           zone->GetPadDimensionX(), zone->GetPadDimensionY());
224   if (!motif) {    
225     motifType = motifMap->FindMotifType(motifTypeID);
226     if (!motifType) {
227       motifType = fMotifReader->BuildMotifType(motifTypeID);     
228       motifMap->AddMotifType(motifType);
229     }
230     
231     if (zone->GetPadDimensionX() != 0. && zone->GetPadDimensionY() != 0.) 
232       motif = new AliMpMotif(motifID, motifType, 
233                      zone->GetPadDimensionX(), zone->GetPadDimensionY());
234     else 
235       motif = fMotifReader->BuildMotifSpecial(motifID, motifType);
236       
237     if (motif) 
238       motifMap->AddMotif(motif);
239
240   }  
241
242   return motif;   
243 }  
244
245 //_____________________________________________________________________________
246 void AliMpSectorReader::ReadRowSegmentsData(istream& in, 
247                                       AliMpZone* zone, AliMpSubZone* subZone)
248 {
249 /// Read row segments input data of a specified zone and subzone;
250 /// creates row segment and add it to the specified subzone
251 /// and a corresponding row in the rows vector.
252
253   TString nextKeyword;
254   do {
255     // 
256     // Read data from file
257     //
258     Int_t offX, offY, inRow, nofMotifs, firstMotifPositionId, firstMotifPositionDId;
259     in >> offX;
260     in >> offY;
261     in >> inRow;
262     in >> nofMotifs;
263     in >> firstMotifPositionId;
264     in >> firstMotifPositionDId;
265     
266     firstMotifPositionId |= AliMpConstants::ManuMask(fPlaneType);
267     
268     AliDebugStream(2)
269       << fgkRowKeyword << " " 
270       << offX << " " << offY << " " << inRow << " " << nofMotifs << " " 
271       << firstMotifPositionId << " " << firstMotifPositionDId
272       << endl;
273
274     in >> nextKeyword;
275
276     //
277     // Process data
278     //
279     AliMpRow* row = fSector->GetRow(inRow);
280     AliMpVMotif* motif = subZone->GetMotif();
281     
282     // Create row segment and add it to its zone, row   
283     AliMpVRowSegment* rowSegment
284       = new AliMpRowSegment(row, motif, offX, offY, nofMotifs, 
285                         firstMotifPositionId, firstMotifPositionDId);
286                         
287     subZone->AddRowSegment(rowSegment);
288     row->AddRowSegment(rowSegment);
289   }
290   while (!in.eof() && (nextKeyword == fgkRowKeyword));
291
292   if (in.eof()) return;
293
294   if (nextKeyword == fgkZoneKeyword) {
295     ReadZoneData(in);
296   }
297   else if (nextKeyword == fgkSubZoneKeyword) {
298     ReadSubZoneData(in, zone);
299   }   
300   else {
301     AliErrorStream() << "Wrong file format." << endl;
302   } 
303 }   
304
305 //_____________________________________________________________________________
306 void AliMpSectorReader::ReadSectorSpecialData(istream& in, AliMp::XDirection direction)
307 {
308 /// Read sector input data
309 /// with a special (irregular) motifs.
310
311   TString keyword;
312   in >> keyword;
313
314   AliDebugStream(2) << keyword << endl;
315
316   if (keyword != fgkSectorSpecialKeyword) {
317      AliErrorStream() << "Wrong file format." << endl;
318      return;
319   }   
320
321   TString nextKeyword;
322   in >> nextKeyword;
323
324   AliDebugStream(2) << keyword << endl;
325     
326   if (nextKeyword != fgkMotifKeyword) {
327     AliErrorStream() << "Wrong file format." << endl;
328     return;
329   }  
330
331   ReadMotifsSpecialData(in);     
332   ReadRowSpecialData(in, direction); 
333 }  
334
335 //_____________________________________________________________________________
336 void AliMpSectorReader::ReadMotifsSpecialData(istream& in)
337 {
338 /// Read the special (irregular) motifs input data.
339
340   AliDebugStream(2) << fgkMotifKeyword << endl;
341
342   TString nextKeyword;
343   do {
344     Int_t zone;
345     in >> zone;
346     AliMpVMotif* motif =  ReadMotifData(in, fSector->GetZone(zone));
347     AliMpSubZone* subZone = new AliMpSubZone(motif); 
348     fSector->GetZone(zone)->AddSubZone(subZone); 
349   
350     in >> nextKeyword;
351
352     AliDebugStream(2) << nextKeyword << endl;      
353   }
354   while (nextKeyword == fgkMotifKeyword);
355     
356   if (nextKeyword != fgkRowSpecialKeyword) {
357      AliErrorStream() << "Wrong file format." << endl;
358      return;
359   }      
360 }  
361
362 //_____________________________________________________________________________
363 void AliMpSectorReader::ReadRowSpecialData(istream& in, AliMp::XDirection direction)
364 {
365 /// Read row input data
366 /// with a special (irregular) motifs.
367
368   Int_t id;
369   in >> id;
370
371   AliDebugStream(2) << id << endl;      
372   
373   // Get the row and its border
374   AliMpRow* row = fSector->GetRow(id);
375
376   AliMpVRowSegmentSpecial* segment = 0;
377   if (direction == AliMp::kLeft) {
378     AliMpVRowSegment* firstNormalSeg = row->GetRowSegment(0);
379     Double_t offsetX = firstNormalSeg->LeftBorderX();
380   
381     // Create a special row segment
382     segment = new AliMpRowSegmentLSpecial(row, offsetX);
383     row->AddRowSegmentInFront(segment);
384   }
385   else { 
386     AliMpVRowSegment* precedentNormalSeg 
387       = row->GetRowSegment(row->GetNofRowSegments()-1);
388     Double_t offsetX = precedentNormalSeg->RightBorderX();
389   
390     // Create a special row segment
391     segment = new AliMpRowSegmentRSpecial(row, offsetX);
392     row->AddRowSegment(segment);
393   }  
394       
395   TString nextKeyword;
396   in >> nextKeyword;
397   
398   AliDebugStream(2) << nextKeyword << endl;
399     
400   if (nextKeyword != fgkPadRowsKeyword) {
401      AliErrorStream() << "Wrong file format." << endl;
402      return;
403   }  
404     
405   ReadRowSegmentSpecialData(in, segment, direction); 
406   
407   // Update row segment and set it to all subzones associated with
408   // contained motifs
409   
410   segment->UpdateMotifVector();
411   segment->UpdatePadsOffset();
412   
413   for (Int_t i=0; i<segment->GetNofMotifs(); i++) {
414     AliMpSubZone* subZone = 0;
415     Int_t j = 0;
416     while (!subZone && j<fSector->GetNofZones())
417       subZone = fSector->GetZone(++j)->FindSubZone(segment->GetMotif(i));
418     
419     subZone->AddRowSegment(segment);
420   }  
421 }  
422
423 //_____________________________________________________________________________
424 void AliMpSectorReader::ReadRowSegmentSpecialData(istream& in, 
425                                             AliMpVRowSegmentSpecial* segment,
426                                             AliMp::XDirection direction)
427 {
428 /// Read row segment input data
429 /// with a special (irregular) motifs.
430
431   Int_t nofPadRows;
432   in >> nofPadRows;
433   
434   AliDebugStream(2) << nofPadRows << endl;
435   
436   if ( nofPadRows < 0 ) {
437     AliErrorStream() << "Wrong nofPadRows value." << endl;
438     return;
439   }         
440
441   TString keyword;
442   in >> keyword;
443
444   AliDebugStream(2) << keyword << endl;
445     
446   if (keyword != fgkPadRowSegmentKeyword) {
447      AliErrorStream() << "Wrong file format." << endl;
448      return;
449   }  
450   
451   //
452   // Process data
453   //
454     
455   TObjArray newPadRows;
456   for (Int_t i=0; i<nofPadRows; i++) {
457     
458      // Create pad row
459      AliMpPadRow* padRow = new AliMpPadRow(direction);
460      segment->AddPadRow(padRow);
461      
462      // Keep the new rows in a temporary vector
463      newPadRows.Add(padRow);
464   }   
465       
466   TString nextKeyword;
467   do {
468     // 
469     // Read data from file
470     //
471     Int_t    nofPadsInRow, motifPositionId;
472     TString   motifId, motifTypeId;
473     in >> nofPadsInRow;
474     in >> motifId;
475     in >> motifPositionId; 
476   
477     motifPositionId |= AliMpConstants::ManuMask(fPlaneType);
478
479     AliDebugStream(2)
480       << nofPadsInRow << " " << motifId << " " << motifPositionId << endl;
481
482     in >> nextKeyword;
483
484     AliDebugStream(2) << nextKeyword << endl;
485
486     //
487     // Process data
488     //
489     
490     for (Int_t i=0; i<nofPadRows; i++) {
491     
492       // Get pad row from the temporary vector
493       AliMpPadRow* padRow = (AliMpPadRow*)newPadRows[i];
494       
495       // Find motif
496       AliMpVMotif* motif = fSector->GetMotifMap()->FindMotif(motifId);
497       
498       if (!motif) {
499         AliErrorStream() << "Unknown motif" << endl;
500         return;
501       }
502
503       // Create pad row segment
504       padRow->AddPadRowSegment(dynamic_cast<AliMpMotif *>(motif), 
505                                motifPositionId, nofPadsInRow);
506     }  
507   }
508   while (!in.eof() && (nextKeyword == fgkPadRowSegmentKeyword));
509   
510   if (in.eof()) return;
511
512   if (nextKeyword == fgkPadRowsKeyword) {
513     ReadRowSegmentSpecialData(in, segment, direction);
514   }
515   else if (nextKeyword == fgkRowSpecialKeyword) {
516     ReadRowSpecialData(in, direction);
517   }   
518   else {
519     AliErrorStream() << "Wrong file format." << endl;
520   } 
521 }  
522
523 //
524 // public methods
525 //
526
527 //_____________________________________________________________________________
528 AliMpSector* AliMpSectorReader::BuildSector()
529 {
530 /// Read the mapping data from stream and create the basic objects:         \n
531 /// zones, subzones, rows, row segments, motifs.
532
533   // Open input stream
534   //
535   istream& in 
536     = fkDataStreams.
537         CreateDataStream(AliMpFiles::SectorFilePath(fStationType,fPlaneType));
538
539   ReadSectorData(in);
540   delete &in;
541   
542   fSector->SetRowSegmentOffsets();
543
544   // Open input stream for special inner zone
545   
546   // add is data function
547   
548   TString sectorSpecialFileName 
549     = AliMpFiles::SectorSpecialFilePath(fStationType, fPlaneType);
550   if ( fkDataStreams.IsDataStream(sectorSpecialFileName) ) {
551     istream& in2 
552       = fkDataStreams.
553           CreateDataStream(sectorSpecialFileName);
554   
555     ReadSectorSpecialData(in2, AliMp::kLeft);
556     
557     delete &in2;
558   }  
559
560   // Open input file for special outer zone
561   TString sectorSpecialFileName2 
562     = AliMpFiles::SectorSpecialFilePath2(fStationType, fPlaneType);
563   if ( fkDataStreams.IsDataStream(sectorSpecialFileName2) ) {
564     istream& in3
565       = fkDataStreams.
566           CreateDataStream(sectorSpecialFileName2);
567     
568     ReadSectorSpecialData(in3, AliMp::kRight);
569     
570     delete &in3;
571   }   
572
573   fSector->Initialize();
574   
575   return fSector;
576 }  
577