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