]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/mapping/AliMpMotifMap.cxx
Removing useless ifs
[u/mrichter/AliRoot.git] / MUON / mapping / AliMpMotifMap.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: AliMpMotifMap.cxx,v 1.9 2005/09/26 16:11:20 ivana Exp $
18 // Category: motif
19 //
20 // Class AliMpMotifMap
21 // -------------------
22 // Class describing the motif map container, where motifs are
23 // mapped to their string IDs.
24 // Included in AliRoot: 2003/05/02
25 // Authors: David Guez, Ivana Hrivnacova; IPN Orsay
26
27 #include <Riostream.h>
28 #include <TVector2.h>
29
30 #include "AliMpMotifMap.h"
31 #include "AliMpVMotif.h"
32 #include "AliMpMotif.h"
33 #include "AliMpMotifSpecial.h"
34 #include "AliMpMotifType.h"
35 #include "AliMpMotifPosition.h"
36
37 ClassImp(AliMpMotifMap)
38
39 //_____________________________________________________________________________
40 AliMpMotifMap::AliMpMotifMap(Bool_t /*standardConstructor*/) 
41   : TObject()
42 #ifdef WITH_ROOT
43     ,fMotifs(true),
44      fMotifTypes(true),
45      fMotifPositions(true),
46      fMotifPositions2(true)
47 #endif         
48 {
49 /// Standard constructor
50   
51   //fMotifPositions2.SetOwner(false);
52 }
53
54 //_____________________________________________________________________________
55 AliMpMotifMap::AliMpMotifMap() 
56   : TObject(),
57     fMotifs(),
58     fMotifTypes(),
59     fMotifPositions(),
60     fMotifPositions2()
61 {
62 /// Default constructor
63 }
64
65 //_____________________________________________________________________________
66 AliMpMotifMap::~AliMpMotifMap() 
67 {
68 /// Destructor  
69
70   // Delete all registered motifs, motif types, motif positions
71   
72 #ifdef WITH_STL
73   for (MotifMapIterator im=fMotifs.begin(); im != fMotifs.end(); im++) {
74     delete im->second;
75   }  
76
77   for (MotifTypeMapIterator it=fMotifTypes.begin(); 
78        it != fMotifTypes.end(); it++) {
79     delete it->second;
80   }
81
82   for (MotifPositionMapIterator ip=fMotifPositions.begin(); 
83        ip != fMotifPositions.end(); ip++) {
84     delete ip->second;
85   }  
86 #endif  
87 }
88
89 // 
90 // private methods
91 //
92
93 //_____________________________________________________________________________
94 void  AliMpMotifMap::PrintMotif(const AliMpVMotif* motif) const
95 {
96 /// Print the motif.
97 // ---
98
99   cout << motif->GetID().Data() << "  "
100        << motif->GetMotifType()->GetID() << "    "
101        << motif->Dimensions().X() << " "
102        << motif->Dimensions().Y();
103 }
104
105 //_____________________________________________________________________________
106 void  AliMpMotifMap::PrintMotifType(const AliMpMotifType* motifType) const
107 {
108 /// Print the motif type.
109
110   cout << motifType->GetID().Data() << "  "
111        << motifType->GetNofPadsX() << "  " 
112        << motifType->GetNofPadsY() << "  ";
113 }
114
115 //_____________________________________________________________________________
116 void  AliMpMotifMap::PrintMotifPosition(
117                           const AliMpMotifPosition* motifPosition) const
118 {
119 /// Print the motif position.
120
121   cout << motifPosition->GetID() << "  "
122        << motifPosition->GetMotif()->GetID() << "  " 
123        << motifPosition->Position().X() << "  "
124        << motifPosition->Position().Y() << "    ";
125 }
126
127 //_____________________________________________________________________________
128 void  AliMpMotifMap::PrintMotifPosition2(
129                           const AliMpMotifPosition* motifPosition) const
130 {
131 /// Print the motif position.
132
133   cout << setw(3) << motifPosition->GetLowIndicesLimit().GetFirst() << "  "
134        << setw(3) << motifPosition->GetLowIndicesLimit().GetSecond() << "  "
135        << setw(3) << motifPosition->GetHighIndicesLimit().GetFirst()  << " " 
136        << setw(3) << motifPosition->GetHighIndicesLimit().GetSecond()  << " "
137        << motifPosition->GetID() << "  ";
138 }
139
140 //_____________________________________________________________________________
141 void  AliMpMotifMap::PrintMotifs() const
142 {
143 /// Print all the motifs and their motif types 
144 /// for all motifs in the motifs map.
145
146 #ifdef WITH_STL
147   if (fMotifs.size()) {
148     cout << "Dump of Motif Map - " << fMotifs.size() << " entries:" << endl;
149     Int_t counter = 0;        
150     for (MotifMapIterator i=fMotifs.begin(); i != fMotifs.end(); i++) {
151       const TString& id  = (*i).first;
152       cout << "Map element " 
153            << setw(3) << counter++ << "   " 
154            << id.Data() << "   " ;
155       PrintMotif((*i).second);     
156       cout << endl;
157     }
158     cout << endl;
159   }
160 #endif
161
162 #ifdef WITH_ROOT
163   if (fMotifs.GetSize()) {
164     cout << "Dump of Motif Map - " << fMotifs.GetSize() << " entries:" << endl;
165     Int_t counter = 0;        
166     TExMapIter i = fMotifs.GetIterator();
167     Long_t key, value;
168     while ( i.Next(key, value) ) {
169       TString id  = fMotifs.AliMpExMap::GetString(key);
170       AliMpVMotif* motif = (AliMpVMotif*)value;
171       cout << "Map element " 
172            << setw(3) << counter++ << "   " 
173            << id.Data() << "   " ;
174       PrintMotif(motif);           
175       cout << endl;
176     }
177     cout << endl;
178   }
179 #endif  
180 }
181
182 //_____________________________________________________________________________
183 void  AliMpMotifMap::PrintMotifTypes() const
184 {
185 /// Print all the the motifs types and their motif dimensions
186 /// for all motif types in the motif types map.
187
188 #ifdef WITH_STL
189   if (fMotifTypes.size()) {
190     cout << "Dump of Motif Type Map - " << fMotifTypes.size() << " entries:" << endl;
191     Int_t counter = 0;        
192     for (MotifTypeMapIterator i=fMotifTypes.begin(); i != fMotifTypes.end(); i++) {
193       const TString& id  = (*i).first;
194       cout << "Map element " 
195            << setw(3) << counter++ << "   " 
196            << id.Data() << "   ";
197       PrintMotifType((*i).second);         
198       cout << endl;
199     }
200     cout << endl;
201   }
202 #endif  
203
204 #ifdef WITH_ROOT
205   if (fMotifTypes.GetSize()) {
206     cout << "Dump of Motif Type Map - " << fMotifTypes.GetSize() << " entries:" << endl;
207     Int_t counter = 0;        
208     TExMapIter i = fMotifTypes.GetIterator();
209     Long_t key, value;
210     while ( i.Next(key, value) ) {
211       TString id  = AliMpExMap::GetString(key);
212       AliMpMotifType* motifType = (AliMpMotifType*)value;
213       cout << "Map element " 
214            << setw(3) << counter++ << "   " 
215            << id.Data() << "   " ;
216       PrintMotifType(motifType);           
217       cout << endl;
218     }
219     cout << endl;
220   }
221 #endif  
222 }
223
224 //_____________________________________________________________________________
225 void  AliMpMotifMap::PrintMotifPositions() const
226 {
227 /// Print all the the motifs positions.
228
229 #ifdef WITH_STL
230   if (fMotifPositions.size()) {
231     cout << "Dump of Motif Position Map - " << fMotifPositions.size() << " entries:" << endl;
232     Int_t counter = 0;        
233     for (MotifPositionMapIterator i=fMotifPositions.begin(); 
234                                   i != fMotifPositions.end(); i++) {
235
236       cout << "Map element " 
237            << setw(3) << counter++ << "   "; 
238       PrintMotifPosition((*i).second);     
239       cout << endl;
240     }
241     cout << endl;
242   }
243 #endif  
244
245 #ifdef WITH_ROOT
246   if (fMotifPositions.GetSize()) {
247     cout << "Dump of Motif Position Map - " << fMotifPositions.GetSize() << " entries:" << endl;
248     Int_t counter = 0;        
249     TExMapIter i = fMotifPositions.GetIterator();
250     Long_t key, value;
251     while ( i.Next(key, value) ) {
252       AliMpMotifPosition* motifPosition = (AliMpMotifPosition*)value;
253       cout << "Map element " 
254            << setw(3) << counter++ << "   "; 
255       PrintMotifPosition(motifPosition);           
256       cout << endl;
257     }
258     cout << endl;
259   }
260 #endif  
261 }
262
263 //_____________________________________________________________________________
264 void  AliMpMotifMap::PrintMotifPositions2() const
265 {
266 /// Print all the the motifs positions from the second map
267 /// (by global indices)
268
269 #ifdef WITH_STL
270   if (fMotifPositions2.size()) {
271     cout << "Dump of Motif Position Map 2 - " << fMotifPositions2.size() << " entries:" << endl;
272     Int_t counter = 0;        
273     for (MotifPositionMap2Iterator i=fMotifPositions2.begin(); 
274                                    i != fMotifPositions2.end(); i++) {
275
276       cout << "Map element " 
277            << setw(3) << counter++ << "   "; 
278       PrintMotifPosition2((*i).second);  
279       cout << endl;
280     }
281     cout << endl;
282   }
283 #endif  
284
285 #ifdef WITH_ROOT
286   if (fMotifPositions2.GetSize()) {
287     cout << "Dump of Motif Position Map 2 - " << fMotifPositions2.GetSize() << " entries:" << endl;
288     Int_t counter = 0;        
289     TExMapIter i = fMotifPositions2.GetIterator();
290     Long_t key, value;
291     while ( i.Next(key, value) ) {
292       AliMpMotifPosition* motifPosition = (AliMpMotifPosition*)value;
293       cout << "Map element " 
294            << setw(3) << counter++ << "   "; 
295       PrintMotifPosition2(motifPosition);          
296       cout << endl;
297     }
298     cout << endl;
299   }
300 #endif  
301 }
302
303 //
304 // public methods
305 //
306
307 //_____________________________________________________________________________
308 Bool_t AliMpMotifMap::AddMotif(AliMpVMotif* motif, Bool_t warn)
309 {
310 /// Add the specified motif 
311 /// if the motif with this ID is not yet present.
312
313   AliMpVMotif* found = FindMotif(motif->GetID());
314   if (found) {    
315     if (warn && found == motif) 
316       Warning("AddMotif", "The motif is already in map.");
317     if (warn && found != motif) 
318       Warning("AddMotif", "Another motif with the same ID is already in map.");      
319     return false;
320   }  
321
322 #ifdef WITH_STL
323   fMotifs[motif->GetID()] = motif;
324 #endif
325
326 #ifdef WITH_ROOT
327   fMotifs.Add(motif->GetID(), motif);
328 #endif
329
330   return true;
331 }
332
333 //_____________________________________________________________________________
334 Bool_t AliMpMotifMap::AddMotifType(AliMpMotifType* motifType, Bool_t warn)
335 {
336 /// Add the specified motif type
337 /// if the motif with this ID is not yet present.
338
339   AliMpMotifType* found = FindMotifType(motifType->GetID());
340   if (found) {    
341     if (warn && found == motifType) 
342       Warning("AddMotifType", "The motif type is already in map.");
343     if (warn && found != motifType) 
344       Warning("AddMotifType", 
345               "Another motif type with the same ID is already in map.");      
346     return false;
347   }  
348
349 #ifdef WITH_STL
350   fMotifTypes[motifType->GetID()] = motifType;
351 #endif
352
353 #ifdef WITH_ROOT
354   fMotifTypes.Add(motifType->GetID(), motifType);
355 #endif
356
357   return true;
358 }
359
360 //_____________________________________________________________________________
361 Bool_t AliMpMotifMap::AddMotifPosition(AliMpMotifPosition* motifPosition, Bool_t warn)
362 {
363 /// Add the specified motif position
364 /// if this position is not yet present.
365
366   AliMpMotifPosition* found = FindMotifPosition(motifPosition->GetID());
367   if (found) { 
368     if (warn && found == motifPosition) {
369       cerr << "ID: " << motifPosition->GetID() 
370            << "  found: " << found 
371            << "  new:   " << motifPosition << endl;   
372       Warning("AddMotifPosition", "This motif position is already in map.");
373     }  
374     if (warn && found != motifPosition) { 
375       cerr << "ID: " << motifPosition->GetID() 
376            << "  found: " << found 
377            << "  new:   " << motifPosition << endl;   
378       Warning("AddMotifposition", 
379               "Another motif position with the same ID is already in map.");
380     }               
381     return false;
382   }  
383
384 #ifdef WITH_STL
385   fMotifPositions[motifPosition->GetID()] = motifPosition;
386 #endif
387
388 #ifdef WITH_ROOT
389   fMotifPositions.Add(motifPosition->GetID(), motifPosition);
390 #endif
391
392   return true;
393 }
394
395 //_____________________________________________________________________________
396 void AliMpMotifMap::FillMotifPositionMap2()
397 {
398 /// Fill the second map (by global indices) of motif positions.
399
400 #ifdef WITH_STL
401   if (fMotifPositions2.size() > 0 ) {
402     Warning("FillMotifPositionMap2", "Map has been already filled.");
403     return;
404   }  
405
406   for (MotifPositionMapIterator ip=fMotifPositions.begin(); 
407        ip != fMotifPositions.end(); ip++) {
408
409     fMotifPositions2[(*ip).second->GetLowIndicesLimit()] = (*ip).second;
410   }  
411 #endif
412
413 #ifdef WITH_ROOT
414   if (fMotifPositions2.GetSize() > 0 ) {
415     Warning("FillMotifPositionMap2", "Map has been already filled.");
416     return;
417   }  
418
419   TExMapIter i = fMotifPositions.GetIterator();
420   Long_t key, value;
421   while ( i.Next(key, value) ) {
422     AliMpMotifPosition* motifPosition = (AliMpMotifPosition*)value;
423     fMotifPositions2.Add(motifPosition->GetLowIndicesLimit(), motifPosition);
424   }
425 #endif
426
427 }
428
429 //_____________________________________________________________________________
430 void  AliMpMotifMap::Print(const char* /*option*/) const
431 {
432 /// Print the motifs and motif types maps.
433
434   PrintMotifs();
435   PrintMotifTypes();
436   PrintMotifPositions();
437   PrintMotifPositions2();
438 }
439
440 //_____________________________________________________________________________
441 void  AliMpMotifMap::PrintGlobalIndices(const char* fileName) const
442 {
443 /// Print all the motifs positions and their global indices.
444
445   ofstream out(fileName, ios::out);
446
447 #ifdef WITH_STL
448   if (fMotifPositions.size()) {
449     for (MotifPositionMapIterator i=fMotifPositions.begin(); 
450                                    i != fMotifPositions.end(); i++) {
451
452       AliMpMotifPosition* motifPosition = (*i).second;
453       out << setw(5) << motifPosition->GetID() << "     "
454           << setw(3) << motifPosition->GetLowIndicesLimit().GetFirst()  << " " 
455           << setw(3) << motifPosition->GetLowIndicesLimit().GetSecond() 
456          << endl;
457     }
458     out << endl;
459   }
460 #endif
461
462 #ifdef WITH_ROOT
463   if (fMotifPositions.GetSize()) {
464     TExMapIter i = fMotifPositions.GetIterator();
465     Long_t key, value;
466     while ( i.Next(key, value) ) {
467       AliMpMotifPosition* motifPosition = (AliMpMotifPosition*)value;
468       out << setw(5) << motifPosition->GetID() << "     "
469           << setw(3) << motifPosition->GetLowIndicesLimit().GetFirst()  << " " 
470           << setw(3) << motifPosition->GetLowIndicesLimit().GetSecond() 
471          << endl;
472     }
473     out << endl;
474   }
475 #endif
476 }
477
478 //_____________________________________________________________________________
479 void  AliMpMotifMap::UpdateGlobalIndices(const char* fileName)
480 {
481 /// Updates the motifs positions global indices
482 /// from the file.
483
484   ifstream in(fileName, ios::in);
485
486   Int_t motifPositionId, offx, offy;
487     
488   do {
489     in >> motifPositionId >> offx >> offy;
490     
491     if (in.eof()) {
492       FillMotifPositionMap2();
493       return;
494     }  
495     
496     AliMpMotifPosition* motifPosition = FindMotifPosition(motifPositionId);
497           
498     if (motifPosition) {
499        cout << "Processing " 
500             << motifPosition->GetID() << " " << offx << " " << offy << endl; 
501
502        motifPosition->SetLowIndicesLimit(AliMpIntPair(offx, offy));
503        
504        Int_t offx2 
505          = offx + motifPosition->GetMotif()->GetMotifType()->GetNofPadsX() - 1;
506          
507        Int_t offy2 
508          = offy + motifPosition->GetMotif()->GetMotifType()->GetNofPadsY() - 1;
509        
510        motifPosition->SetHighIndicesLimit(AliMpIntPair(offx2, offy2));
511     }
512     else {   
513        cerr <<"Motif position " << motifPositionId << endl;
514        Warning("UpdateGlobalIndices", "Motif position not found !!!");
515     }
516   }    
517   while (!in.eof());
518 }
519
520
521 //_____________________________________________________________________________
522 AliMpVMotif* AliMpMotifMap::FindMotif(const TString& motifID) const
523 {
524 /// Finds the motif with the specified ID.
525   
526 #ifdef WITH_STL
527   MotifMapIterator i = fMotifs.find(motifID);
528   if (i != fMotifs.end()) 
529     return (*i).second;
530   else                 
531     return 0;
532 #endif
533
534 #ifdef WITH_ROOT
535   return (AliMpVMotif*)fMotifs.GetValue(motifID);
536 #endif
537 }
538
539 //_____________________________________________________________________________
540 AliMpVMotif* AliMpMotifMap::FindMotif(const TString& motifID, 
541                                       const TString& motifTypeID,
542                                       const TVector2& padDimensions ) const
543 {
544 /// Finds the motif with the specified ID and returns it
545 /// only if its motif type and motif dimensions agree
546 /// with the given motifTypeID and motifDimensions.
547 /// Disagreement causes fatal error.
548  
549   AliMpVMotif* motif = FindMotif(motifID);
550
551   if (motif && motif->GetMotifType()->GetID() != motifTypeID) {
552       Fatal("FindMotif", 
553             "Motif has been already defined with a different type.");
554       return 0;     
555   }
556
557   // check pad dimension in case of a normal motif
558   if (motif && 
559       dynamic_cast<AliMpMotif*>(motif) && 
560       ( motif->GetPadDimensions(0).X() != padDimensions.X() ||
561         motif->GetPadDimensions(0).Y() != padDimensions.Y())) { 
562       
563       Fatal("FindMotifType", 
564             "Motif type has been already defined with different dimensions.");
565       return 0;
566
567   } 
568
569   // check case of a special motif
570   if (motif && 
571       (padDimensions.X() == 0. && padDimensions.Y() == 0.) &&
572       !dynamic_cast<AliMpMotifSpecial*>(motif)) {
573
574       Fatal("FindMotifType", 
575             "Motif type has been already defined with different dimensions.");
576       return 0;
577
578   } 
579   
580   return motif;
581 }
582
583 //_____________________________________________________________________________
584 AliMpMotifType* AliMpMotifMap::FindMotifType(const TString& motifTypeID) const
585 {
586 /// Find the motif type with the specified motif type ID.
587   
588 #ifdef WITH_STL
589   MotifTypeMapIterator i = fMotifTypes.find(motifTypeID);
590   if (i != fMotifTypes.end()) 
591     return (*i).second;
592   else                 
593     return 0;
594 #endif
595
596 #ifdef WITH_ROOT
597   return (AliMpMotifType*)fMotifTypes.GetValue(motifTypeID);
598 #endif
599 }
600
601 //_____________________________________________________________________________
602 AliMpMotifPosition* 
603 AliMpMotifMap::FindMotifPosition(Int_t motifPositionID) const
604 {
605 /// Find the motif position with the specified motif position ID.
606   
607 #ifdef WITH_STL
608   MotifPositionMapIterator i = fMotifPositions.find(motifPositionID);
609   if (i != fMotifPositions.end()) 
610     return (*i).second;
611   else                 
612     return 0;
613 #endif
614
615 #ifdef WITH_ROOT
616   return (AliMpMotifPosition*)fMotifPositions.GetValue(motifPositionID);
617 #endif
618 }
619
620 /*
621 //_____________________________________________________________________________
622 AliMpMotifPosition* 
623 AliMpMotifMap::FindMotifPosition(const AliMpIntPair& indices) const
624 {
625 /// Find the last motif position which has the global indices (low limit)
626 /// less then the indices specified.
627
628 #ifdef WITH_STL
629   MotifPositionMap2Iterator found 
630     = fMotifPositions2.lower_bound(indices);
631   
632   if (found == fMotifPositions2.end()) found--; 
633
634   MotifPositionMap2Iterator i=found;
635   do {
636     AliMpIntPair low = (*i).second->GetLowIndicesLimit();
637     AliMpIntPair up = (*i).second->GetHighIndicesLimit();
638     
639     if ( indices.GetFirst()  >= low.GetFirst() &&
640          indices.GetSecond() >= low.GetSecond() &&
641          indices.GetFirst()  <= up.GetFirst() &&
642          indices.GetSecond() <= up.GetSecond())
643          
644          return (*i).second;                     
645   }
646   while ( i-- != fMotifPositions2.begin());
647   
648   return 0;
649 #endif
650
651 #ifdef WITH_ROOT
652   // HOW TO DO THIS WITH ROOT ????
653   // Fortunately it seems not to be used anywhere
654   Fatal("FindMotifPosition", "Difficult in Root to do this.");
655   return 0;
656 #endif
657 }
658 */