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