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