Fixing a backward compatibility issue
[u/mrichter/AliRoot.git] / MUON / mapping / AliMpMotifMap.cxx
CommitLineData
dee1d5f1 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$
13985652 17// $MpId: AliMpMotifMap.cxx,v 1.16 2006/05/24 13:58:41 ivana Exp $
5f91c9e8 18// Category: motif
3d1463c8 19
20//-----------------------------------------------------------------------------
5f91c9e8 21// Class AliMpMotifMap
22// -------------------
23// Class describing the motif map container, where motifs are
24// mapped to their string IDs.
dbe945cc 25// Included in AliRoot: 2003/05/02
5f91c9e8 26// Authors: David Guez, Ivana Hrivnacova; IPN Orsay
3d1463c8 27//-----------------------------------------------------------------------------
5f91c9e8 28
5f91c9e8 29#include "AliMpMotifMap.h"
630711ed 30
31#include "AliCodeTimer.h"
32#include "AliMpExMapIterator.h"
5f91c9e8 33#include "AliMpVMotif.h"
34#include "AliMpMotif.h"
35#include "AliMpMotifSpecial.h"
36#include "AliMpMotifType.h"
37#include "AliMpMotifPosition.h"
168e9c4d 38#include "AliMpEncodePair.h"
2c605e66 39
40#include "AliLog.h"
41
42#include <Riostream.h>
2c605e66 43#include <TArrayI.h>
5f91c9e8 44
13985652 45/// \cond CLASSIMP
5f91c9e8 46ClassImp(AliMpMotifMap)
13985652 47/// \endcond
5f91c9e8 48
5006ec94 49//_____________________________________________________________________________
630711ed 50AliMpMotifMap::AliMpMotifMap()
51 : TObject(),
52 fMotifs(),
53 fMotifTypes(),
54 fMotifPositions(),
55 fMotifPositions2()
5006ec94 56{
57/// Standard constructor
58
630711ed 59 fMotifPositions2.SetOwner(false);
5006ec94 60}
f79c58a5 61
5f91c9e8 62//_____________________________________________________________________________
630711ed 63AliMpMotifMap::AliMpMotifMap(TRootIOCtor* ioCtor)
5006ec94 64 : TObject(),
630711ed 65 fMotifs(ioCtor),
66 fMotifTypes(ioCtor),
67 fMotifPositions(ioCtor),
68 fMotifPositions2(ioCtor)
5f91c9e8 69{
630711ed 70/// Root IO constructor
71
630711ed 72 fMotifPositions2.SetOwner(false);
5f91c9e8 73}
74
75//_____________________________________________________________________________
dee1d5f1 76AliMpMotifMap::~AliMpMotifMap()
77{
78/// Destructor
5f91c9e8 79
80 // Delete all registered motifs, motif types, motif positions
5f91c9e8 81}
82
83//
84// private methods
85//
86
f79c58a5 87//_____________________________________________________________________________
88void AliMpMotifMap::PrintMotif(const AliMpVMotif* motif) const
89{
dee1d5f1 90/// Print the motif.
f79c58a5 91
92 cout << motif->GetID().Data() << " "
93 << motif->GetMotifType()->GetID() << " "
6e97fbb8 94 << motif->DimensionX() << " "
95 << motif->DimensionY();
f79c58a5 96}
97
98//_____________________________________________________________________________
99void AliMpMotifMap::PrintMotifType(const AliMpMotifType* motifType) const
100{
dee1d5f1 101/// Print the motif type.
f79c58a5 102
103 cout << motifType->GetID().Data() << " "
104 << motifType->GetNofPadsX() << " "
105 << motifType->GetNofPadsY() << " ";
106}
107
108//_____________________________________________________________________________
109void AliMpMotifMap::PrintMotifPosition(
110 const AliMpMotifPosition* motifPosition) const
111{
dee1d5f1 112/// Print the motif position.
f79c58a5 113
6d5f608a 114 cout << " ID " << motifPosition->GetID() << " "
115 << " Motif ID " << motifPosition->GetMotif()->GetID() << " "
6e97fbb8 116 << " Pos (X,Y) = (" << motifPosition->GetPositionX() << ","
117 << motifPosition->GetPositionY() << ")";
f79c58a5 118}
119
120//_____________________________________________________________________________
121void AliMpMotifMap::PrintMotifPosition2(
122 const AliMpMotifPosition* motifPosition) const
123{
dee1d5f1 124/// Print the motif position.
f79c58a5 125
168e9c4d 126 cout << setw(3) << motifPosition->GetLowLimitIx() << " "
127 << setw(3) << motifPosition->GetLowLimitIy() << " "
128 << setw(3) << motifPosition->GetHighLimitIx() << " "
129 << setw(3) << motifPosition->GetHighLimitIy() << " "
f79c58a5 130 << motifPosition->GetID() << " ";
131}
132
5f91c9e8 133//_____________________________________________________________________________
134void AliMpMotifMap::PrintMotifs() const
135{
dee1d5f1 136/// Print all the motifs and their motif types
137/// for all motifs in the motifs map.
5f91c9e8 138
f79c58a5 139 if (fMotifs.GetSize()) {
140 cout << "Dump of Motif Map - " << fMotifs.GetSize() << " entries:" << endl;
141 Int_t counter = 0;
630711ed 142 AliMpExMapIterator* it = fMotifs.CreateIterator();
143 Int_t key;
144 AliMpVMotif* motif;
145
146 while ( ( motif = static_cast<AliMpVMotif*>(it->Next(key)) ) )
147 {
148 TString id = fMotifs.AliMpExMap::GetString(key);
f79c58a5 149 cout << "Map element "
150 << setw(3) << counter++ << " "
151 << id.Data() << " " ;
152 PrintMotif(motif);
153 cout << endl;
154 }
155 cout << endl;
630711ed 156 delete it;
f79c58a5 157 }
5f91c9e8 158}
159
160//_____________________________________________________________________________
161void AliMpMotifMap::PrintMotifTypes() const
162{
dee1d5f1 163/// Print all the the motifs types and their motif dimensions
164/// for all motif types in the motif types map.
5f91c9e8 165
f79c58a5 166 if (fMotifTypes.GetSize()) {
167 cout << "Dump of Motif Type Map - " << fMotifTypes.GetSize() << " entries:" << endl;
630711ed 168 Int_t counter = 0;
169 AliMpExMapIterator* it = fMotifTypes.CreateIterator();
170 Int_t key;
171 AliMpMotifType* motifType;
172
173 while ( ( motifType = static_cast<AliMpMotifType*>(it->Next(key)) ) )
174 {
5006ec94 175 TString id = AliMpExMap::GetString(key);
f79c58a5 176 cout << "Map element "
177 << setw(3) << counter++ << " "
178 << id.Data() << " " ;
179 PrintMotifType(motifType);
180 cout << endl;
5f91c9e8 181 }
182 cout << endl;
630711ed 183 delete it;
5f91c9e8 184 }
185}
186
187//_____________________________________________________________________________
6d5f608a 188void
189AliMpMotifMap::GetAllMotifPositionsIDs(TArrayI& ecn) const
190{
490da820 191/// Fill the given array with all motif positions IDs (electronic card numbers)
192/// defined in the map
193
6d5f608a 194 ecn.Set(fMotifPositions.GetSize());
630711ed 195 TIter next(fMotifPositions.CreateIterator());
196 AliMpMotifPosition* motifPosition;
6d5f608a 197 Int_t i(0);
630711ed 198 while ( ( motifPosition = static_cast<AliMpMotifPosition*>(next()) ) )
6d5f608a 199 {
6d5f608a 200 ecn[i] = motifPosition->GetID();
201 ++i;
202 }
6d5f608a 203}
204
205//_____________________________________________________________________________
a70d5d20 206UInt_t AliMpMotifMap::GetNofMotifPositions() const
207{
208/// Return the number of all motif positions IDs (electronic card numbers)
209
a70d5d20 210 return fMotifPositions.GetSize();
a70d5d20 211}
212
213//_____________________________________________________________________________
214AliMpMotifPosition* AliMpMotifMap::GetMotifPosition(UInt_t index) const
215{
216/// Return the motif position which is in the map on the index-th position
217
630711ed 218 AliCodeTimerAuto("");
219
a70d5d20 220 if ( index >= GetNofMotifPositions() ) {
221 AliErrorStream() << "Index " << index << " outside limits." << endl;
222 return 0;
223 }
224
630711ed 225 TIter next(fMotifPositions.CreateIterator());
226 while (index-- > 0) next();
227 return static_cast<AliMpMotifPosition*>(next());
a70d5d20 228}
229
230//_____________________________________________________________________________
3283cfc7 231Int_t AliMpMotifMap::CalculateNofPads() const
232{
abaf1dad 233/// Calculate total number of pads in the map
234
3283cfc7 235 Int_t nofPads = 0;
236
630711ed 237 TIter next(fMotifPositions.CreateIterator());
238 AliMpMotifPosition* motifPosition;
239 while ( ( motifPosition = static_cast<AliMpMotifPosition*>(next()) ) )
240 {
3283cfc7 241 nofPads += motifPosition->GetMotif()->GetMotifType()->GetNofPads();
242 }
3283cfc7 243
3283cfc7 244 return nofPads;
245}
246
247//_____________________________________________________________________________
5f91c9e8 248void AliMpMotifMap::PrintMotifPositions() const
249{
abaf1dad 250/// Print all motif positions.
5f91c9e8 251
f79c58a5 252 if (fMotifPositions.GetSize()) {
dee1d5f1 253 cout << "Dump of Motif Position Map - " << fMotifPositions.GetSize() << " entries:" << endl;
f79c58a5 254 Int_t counter = 0;
630711ed 255 TIter next(fMotifPositions.CreateIterator());
256 AliMpMotifPosition* motifPosition;
257
258 while ( ( motifPosition = static_cast<AliMpMotifPosition*>(next()) ) )
259 {
f79c58a5 260 cout << "Map element "
261 << setw(3) << counter++ << " ";
262 PrintMotifPosition(motifPosition);
263 cout << endl;
264 }
265 cout << endl;
266 }
5f91c9e8 267}
268
269//_____________________________________________________________________________
270void AliMpMotifMap::PrintMotifPositions2() const
271{
abaf1dad 272/// Print all motif positions from the second map
dee1d5f1 273/// (by global indices)
5f91c9e8 274
630711ed 275 if (fMotifPositions2.GetSize())
276 {
f79c58a5 277 cout << "Dump of Motif Position Map 2 - " << fMotifPositions2.GetSize() << " entries:" << endl;
630711ed 278 TIter next(fMotifPositions2.CreateIterator());
279 AliMpMotifPosition* motifPosition(0x0);
f79c58a5 280 Int_t counter = 0;
630711ed 281
282 while ( ( motifPosition = static_cast<AliMpMotifPosition*>(next()) ) )
283 {
284 cout << "Map element " << setw(3) << counter++ << " ";
f79c58a5 285 PrintMotifPosition2(motifPosition);
286 cout << endl;
5f91c9e8 287 }
288 cout << endl;
289 }
290}
291
292//
293// public methods
294//
295
296//_____________________________________________________________________________
297Bool_t AliMpMotifMap::AddMotif(AliMpVMotif* motif, Bool_t warn)
298{
dee1d5f1 299/// Add the specified motif
300/// if the motif with this ID is not yet present.
5f91c9e8 301
302 AliMpVMotif* found = FindMotif(motif->GetID());
303 if (found) {
304 if (warn && found == motif)
2c605e66 305 AliWarningStream() << "The motif is already in map." << endl;
306
307 if (warn && found != motif) {
308 AliWarningStream()
309 << "Another motif with the same ID is already in map." << endl;
310 }
5f91c9e8 311 return false;
312 }
313
5006ec94 314 fMotifs.Add(motif->GetID(), motif);
f79c58a5 315
5f91c9e8 316 return true;
317}
318
319//_____________________________________________________________________________
320Bool_t AliMpMotifMap::AddMotifType(AliMpMotifType* motifType, Bool_t warn)
321{
dee1d5f1 322/// Add the specified motif type
323/// if the motif with this ID is not yet present.
5f91c9e8 324
325 AliMpMotifType* found = FindMotifType(motifType->GetID());
326 if (found) {
327 if (warn && found == motifType)
2c605e66 328 AliWarningStream() << "The motif type is already in map." << endl;
329
330 if (warn && found != motifType) {
331 AliWarningStream()
332 << "Another motif type with the same ID is already in map." << endl;
333 }
5f91c9e8 334 return false;
335 }
336
5006ec94 337 fMotifTypes.Add(motifType->GetID(), motifType);
f79c58a5 338
5f91c9e8 339 return true;
340}
341
342//_____________________________________________________________________________
343Bool_t AliMpMotifMap::AddMotifPosition(AliMpMotifPosition* motifPosition, Bool_t warn)
344{
dee1d5f1 345/// Add the specified motif position
346/// if this position is not yet present.
5f91c9e8 347
348 AliMpMotifPosition* found = FindMotifPosition(motifPosition->GetID());
7a854749 349 if (found) {
350 if (warn && found == motifPosition) {
2c605e66 351 AliWarningStream()
352 << "ID: " << motifPosition->GetID()
7a854749 353 << " found: " << found
2c605e66 354 << " new: " << motifPosition << endl
355 << "This motif position is already in map." << endl;
7a854749 356 }
2c605e66 357
7a854749 358 if (warn && found != motifPosition) {
2c605e66 359 AliWarningStream()
360 << "ID: " << motifPosition->GetID()
7a854749 361 << " found: " << found
2c605e66 362 << " new: " << motifPosition << endl
363 << "Another motif position with the same ID is already in map."
364 << endl;
365 }
366
5f91c9e8 367 return false;
368 }
369
630711ed 370 fMotifPositions.Add(motifPosition->GetID() << 16, motifPosition);
f79c58a5 371
5f91c9e8 372 return true;
373}
374
375//_____________________________________________________________________________
376void AliMpMotifMap::FillMotifPositionMap2()
377{
dee1d5f1 378/// Fill the second map (by global indices) of motif positions.
5f91c9e8 379
f79c58a5 380 if (fMotifPositions2.GetSize() > 0 ) {
2c605e66 381 AliWarningStream() <<"Map has been already filled." << endl;
f79c58a5 382 return;
383 }
384
630711ed 385 TIter next(fMotifPositions.CreateIterator());
386 AliMpMotifPosition* motifPosition(0x0);
387 while ( ( motifPosition = static_cast<AliMpMotifPosition*>(next()) ) )
388 {
168e9c4d 389 fMotifPositions2.Add(motifPosition->GetLowLimitIx(),
390 motifPosition->GetLowLimitIy(),
391 motifPosition);
f79c58a5 392 }
5f91c9e8 393}
394
395//_____________________________________________________________________________
6d5f608a 396void AliMpMotifMap::Print(const char* opt) const
5f91c9e8 397{
dee1d5f1 398/// Print the motifs and motif types maps.
5f91c9e8 399
6d5f608a 400 TString sopt(opt);
401
402 sopt.ToUpper();
403
404 if ( sopt.Contains("MOTIFS") || sopt == "ALL" ) PrintMotifs();
405 if ( sopt.Contains("MOTIFTYPES") || sopt == "ALL" ) PrintMotifTypes();
406 if ( sopt.Contains("MOTIFPOSITIONS") || sopt == "ALL" ) PrintMotifPositions();
407 if ( sopt.Contains("MOTIFPOSITIONS2") || sopt == "ALL" ) PrintMotifPositions2();
5f91c9e8 408}
409
410//_____________________________________________________________________________
411void AliMpMotifMap::PrintGlobalIndices(const char* fileName) const
412{
abaf1dad 413/// Print all motif positions and their global indices.
5f91c9e8 414
415 ofstream out(fileName, ios::out);
416
f79c58a5 417 if (fMotifPositions.GetSize()) {
630711ed 418 TIter next(fMotifPositions.CreateIterator());
419 AliMpMotifPosition* motifPosition;
420 while ( ( motifPosition = static_cast<AliMpMotifPosition*>(next()) ) )
421 {
f79c58a5 422 out << setw(5) << motifPosition->GetID() << " "
168e9c4d 423 << setw(3) << motifPosition->GetLowLimitIx() << " "
424 << setw(3) << motifPosition->GetLowLimitIy()
f79c58a5 425 << endl;
426 }
427 out << endl;
428 }
5f91c9e8 429}
430
431//_____________________________________________________________________________
432void AliMpMotifMap::UpdateGlobalIndices(const char* fileName)
433{
abaf1dad 434/// Update the motif positions global indices from the file.
5f91c9e8 435
436 ifstream in(fileName, ios::in);
437
438 Int_t motifPositionId, offx, offy;
439
440 do {
441 in >> motifPositionId >> offx >> offy;
442
443 if (in.eof()) {
444 FillMotifPositionMap2();
445 return;
446 }
447
448 AliMpMotifPosition* motifPosition = FindMotifPosition(motifPositionId);
449
450 if (motifPosition) {
2c605e66 451 AliDebugStream(1)
452 << "Processing "
5f91c9e8 453 << motifPosition->GetID() << " " << offx << " " << offy << endl;
454
168e9c4d 455 motifPosition->SetLowIndicesLimit(offx, offy);
5f91c9e8 456
457 Int_t offx2
458 = offx + motifPosition->GetMotif()->GetMotifType()->GetNofPadsX() - 1;
459
460 Int_t offy2
461 = offy + motifPosition->GetMotif()->GetMotifType()->GetNofPadsY() - 1;
462
168e9c4d 463 motifPosition->SetHighIndicesLimit(offx2, offy2);
5f91c9e8 464 }
465 else {
2c605e66 466 AliWarningStream()
467 << "Motif position " << motifPositionId << " not found" << endl;
5f91c9e8 468 }
469 }
470 while (!in.eof());
471}
472
473
474//_____________________________________________________________________________
475AliMpVMotif* AliMpMotifMap::FindMotif(const TString& motifID) const
476{
abaf1dad 477/// Find the motif with the specified ID.
5f91c9e8 478
630711ed 479 //AliCodeTimerAuto("");
480
5006ec94 481 return (AliMpVMotif*)fMotifs.GetValue(motifID);
5f91c9e8 482}
483
484//_____________________________________________________________________________
485AliMpVMotif* AliMpMotifMap::FindMotif(const TString& motifID,
7a854749 486 const TString& motifTypeID,
6e97fbb8 487 Double_t padDimensionX,
488 Double_t padDimensionY ) const
5f91c9e8 489{
abaf1dad 490/// Find the motif with the specified ID and returns it
dee1d5f1 491/// only if its motif type and motif dimensions agree
492/// with the given motifTypeID and motifDimensions.
493/// Disagreement causes fatal error.
494
630711ed 495 //AliCodeTimerAuto("");
496
5f91c9e8 497 AliMpVMotif* motif = FindMotif(motifID);
498
499 if (motif && motif->GetMotifType()->GetID() != motifTypeID) {
630711ed 500 AliFatal("Motif has been already defined with a different type.");
5f91c9e8 501 return 0;
502 }
503
504 // check pad dimension in case of a normal motif
6e97fbb8 505 if ( motif &&
506 dynamic_cast<AliMpMotif*>(motif) &&
507 ( motif->GetPadDimensionX(0) != padDimensionX ||
508 motif->GetPadDimensionY(0) != padDimensionY ) ) {
5f91c9e8 509
630711ed 510 AliFatal("Motif type has been already defined with different dimensions.");
5f91c9e8 511 return 0;
512
513 }
514
515 // check case of a special motif
6e97fbb8 516 if ( motif &&
517 ( padDimensionX == 0. && padDimensionY == 0.) &&
518 ! dynamic_cast<AliMpMotifSpecial*>(motif) ) {
5f91c9e8 519
630711ed 520 AliFatal("Motif type has been already defined with different dimensions.");
5f91c9e8 521 return 0;
522
523 }
524
525 return motif;
526}
527
528//_____________________________________________________________________________
529AliMpMotifType* AliMpMotifMap::FindMotifType(const TString& motifTypeID) const
530{
dee1d5f1 531/// Find the motif type with the specified motif type ID.
5f91c9e8 532
630711ed 533 //AliCodeTimerAuto("");
534
5006ec94 535 return (AliMpMotifType*)fMotifTypes.GetValue(motifTypeID);
5f91c9e8 536}
537
538//_____________________________________________________________________________
7a854749 539AliMpMotifPosition*
540AliMpMotifMap::FindMotifPosition(Int_t motifPositionID) const
5f91c9e8 541{
dee1d5f1 542/// Find the motif position with the specified motif position ID.
5f91c9e8 543
630711ed 544 //AliCodeTimerAuto("");
545
630711ed 546 return (AliMpMotifPosition*)fMotifPositions.GetValue(motifPositionID << 16);
5f91c9e8 547}