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