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