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