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