]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/mapping/AliMpMotifMap.cxx
Code for MUON Station1 (I.Hrivnacova)
[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 //
9 // Authors: David Guez, Ivana Hrivnacova; IPN Orsay
10
11 #include <Riostream.h>
12
13 #include "AliMpMotifMap.h"
14 #include "AliMpVMotif.h"
15 #include "AliMpMotif.h"
16 #include "AliMpMotifSpecial.h"
17 #include "AliMpMotifType.h"
18 #include "AliMpMotifPosition.h"
19
20 ClassImp(AliMpMotifMap)
21
22 //_____________________________________________________________________________
23 AliMpMotifMap::AliMpMotifMap() 
24   : TObject()
25 {
26 //
27 }
28
29 //_____________________________________________________________________________
30 AliMpMotifMap::~AliMpMotifMap() {
31 //  
32
33   // Delete all registered motifs, motif types, motif positions
34   
35   for (MotifMapIterator im=fMotifs.begin(); im != fMotifs.end(); im++) {
36     delete im->second;
37   }  
38   
39   for (MotifTypeMapIterator it=fMotifTypes.begin(); 
40        it != fMotifTypes.end(); it++) {
41     delete it->second;
42   }
43   
44   for (MotifPositionMapIterator ip=fMotifPositions.begin(); 
45        ip != fMotifPositions.end(); ip++) {
46     delete ip->second;
47   }  
48 }
49
50 // 
51 // private methods
52 //
53
54 //_____________________________________________________________________________
55 void  AliMpMotifMap::PrintMotifs() const
56 {
57 // Prints all the motifs and their motif types 
58 // for all motifs in the motifs map.
59 // ---
60
61   if (fMotifs.size()) {
62     cout << "Dump of Motif Map - " << fMotifs.size() << " entries:" << endl;
63     Int_t counter = 0;        
64     for (MotifMapIterator i=fMotifs.begin(); i != fMotifs.end(); i++) {
65       const TString& id  = (*i).first;
66       AliMpVMotif* motif = (*i).second;
67       cout << "Map element " 
68            << setw(3) << counter++ << "   " 
69            << id.Data() << "   " 
70            << motif->GetID().Data() << "  "
71            << motif->GetMotifType()->GetID() << "    "
72            << motif->Dimensions().X() << " "
73            << motif->Dimensions().Y()
74            << endl;
75     }
76     cout << endl;
77   }
78 }
79
80 //_____________________________________________________________________________
81 void  AliMpMotifMap::PrintMotifTypes() const
82 {
83 // Prints all the the motifs types and their motif dimensions
84 // for all motif types in the motif types map.
85 // ---
86
87   if (fMotifTypes.size()) {
88     cout << "Dump of Motif Type Map - " << fMotifTypes.size() << " entries:" << endl;
89     Int_t counter = 0;        
90     for (MotifTypeMapIterator i=fMotifTypes.begin(); i != fMotifTypes.end(); i++) {
91       const TString& id  = (*i).first;
92       AliMpMotifType* motifType = (*i).second;
93       cout << "Map element " 
94            << setw(3) << counter++ << "   " 
95            << id.Data() << "   " 
96            << motifType->GetID().Data() << "  "
97            << motifType->GetNofPadsX() << "  " 
98            << motifType->GetNofPadsY() << "  "
99            << endl;
100     }
101     cout << endl;
102   }
103 }
104
105 //_____________________________________________________________________________
106 void  AliMpMotifMap::PrintMotifPositions() const
107 {
108 // Prints all the the motifs positions.
109 // ---
110
111   if (fMotifPositions.size()) {
112     cout << "Dump of Motif Position Map - " << fMotifPositions.size() << " entries:" << endl;
113     Int_t counter = 0;        
114     for (MotifPositionMapIterator i=fMotifPositions.begin(); 
115                                   i != fMotifPositions.end(); i++) {
116
117       AliMpMotifPosition* motifPosition = (*i).second;
118       cout << "Map element " 
119            << setw(3) << counter++ << "   " 
120            << motifPosition->GetID() << "  "
121            << motifPosition->GetMotif()->GetID() << "  " 
122            << motifPosition->Position().X() << "  "
123            << motifPosition->Position().Y() << "    "
124            << endl;
125     }
126     cout << endl;
127   }
128 }
129
130 //_____________________________________________________________________________
131 void  AliMpMotifMap::PrintMotifPositions2() const
132 {
133 // Prints all the the motifs positions from the second map
134 // (by global indices)
135 // ---
136
137   if (fMotifPositions2.size()) {
138     cout << "Dump of Motif Position Map 2 - " << fMotifPositions2.size() << " entries:" << endl;
139     Int_t counter = 0;        
140     for (MotifPositionMap2Iterator i=fMotifPositions2.begin(); 
141                                    i != fMotifPositions2.end(); i++) {
142
143       AliMpMotifPosition* motifPosition = (*i).second;
144       cout << "Map element " 
145            << setw(3) << counter++ << "   " 
146            << setw(3) << motifPosition->GetLowIndicesLimit().GetFirst() << "  "
147            << setw(3) << motifPosition->GetLowIndicesLimit().GetSecond() << "  "
148            << setw(3) << motifPosition->GetHighIndicesLimit().GetFirst()  << " " 
149            << setw(3) << motifPosition->GetHighIndicesLimit().GetSecond()  << " "
150            << motifPosition->GetID() << "  "
151            << endl;
152     }
153     cout << endl;
154   }
155 }
156
157 //
158 // public methods
159 //
160
161 //_____________________________________________________________________________
162 Bool_t AliMpMotifMap::AddMotif(AliMpVMotif* motif, Bool_t warn)
163 {
164 // Adds the specified motif 
165 // if the motif with this ID is not yet present.
166 // ---
167
168   AliMpVMotif* found = FindMotif(motif->GetID());
169   if (found) {    
170     if (warn && found == motif) 
171       Warning("AddMotif", "The motif is already in map.");
172     if (warn && found != motif) 
173       Warning("AddMotif", "Another motif with the same ID is already in map.");      
174     return false;
175   }  
176
177   fMotifs[motif->GetID()] = motif;
178   return true;
179 }
180
181 //_____________________________________________________________________________
182 Bool_t AliMpMotifMap::AddMotifType(AliMpMotifType* motifType, Bool_t warn)
183 {
184 // Adds the specified motif type
185 // if the motif with this ID is not yet present.
186 // ---
187
188   AliMpMotifType* found = FindMotifType(motifType->GetID());
189   if (found) {    
190     if (warn && found == motifType) 
191       Warning("AddMotifType", "The motif type is already in map.");
192     if (warn && found != motifType) 
193       Warning("AddMotifType", 
194               "Another motif type with the same ID is already in map.");      
195     return false;
196   }  
197
198   fMotifTypes[motifType->GetID()] = motifType;
199   return true;
200 }
201
202 //_____________________________________________________________________________
203 Bool_t AliMpMotifMap::AddMotifPosition(AliMpMotifPosition* motifPosition, Bool_t warn)
204 {
205 // Adds the specified motif position
206 // if this position is not yet present.
207 // ---
208
209   AliMpMotifPosition* found = FindMotifPosition(motifPosition->GetID());
210   if (found) {    
211     if (warn && found == motifPosition) 
212       Warning("AddMotifPosition", "This motif position is already in map.");
213     if (warn && found != motifPosition) 
214       Warning("AddMotifposition", 
215               "Another motif position with the same ID is already in map.");      
216     return false;
217   }  
218
219   fMotifPositions[motifPosition->GetID()] = motifPosition;
220   return true;
221 }
222
223 //_____________________________________________________________________________
224 void AliMpMotifMap::FillMotifPositionMap2()
225 {
226 // Fills the second map (by global indices) of motif positions.
227 // ---
228
229   if (fMotifPositions2.size() > 0 ) {
230     Warning("FillMotifPositionMap2", "Map has been already filled.");
231     return;
232   }  
233
234   for (MotifPositionMapIterator ip=fMotifPositions.begin(); 
235        ip != fMotifPositions.end(); ip++) {
236
237     fMotifPositions2[(*ip).second->GetLowIndicesLimit()] = (*ip).second;
238   }  
239
240 }
241
242 //_____________________________________________________________________________
243 void  AliMpMotifMap::Print() const
244 {
245 // Prints the motifs and motif types maps.
246 // ---
247
248   PrintMotifs();
249   PrintMotifTypes();
250   PrintMotifPositions();
251   PrintMotifPositions2();
252 }
253
254 //_____________________________________________________________________________
255 void  AliMpMotifMap::PrintGlobalIndices(const char* fileName) const
256 {
257 // Prints all the motifs positions and their global indices.
258 // ---
259
260   ofstream out(fileName, ios::out);
261
262   if (fMotifPositions.size()) {
263     for (MotifPositionMapIterator i=fMotifPositions.begin(); 
264                                    i != fMotifPositions.end(); i++) {
265
266       AliMpMotifPosition* motifPosition = (*i).second;
267       out << setw(5) << motifPosition->GetID() << "     "
268           << setw(3) << motifPosition->GetLowIndicesLimit().GetFirst()  << " " 
269           << setw(3) << motifPosition->GetLowIndicesLimit().GetSecond() 
270          << endl;
271     }
272     out << endl;
273   }
274 }
275
276 //_____________________________________________________________________________
277 void  AliMpMotifMap::UpdateGlobalIndices(const char* fileName)
278 {
279 // Updates the motifs positions global indices
280 // from the file.
281 // ---
282
283   ifstream in(fileName, ios::in);
284
285   Int_t motifPositionId, offx, offy;
286     
287   do {
288     in >> motifPositionId >> offx >> offy;
289     
290     if (in.eof()) {
291       FillMotifPositionMap2();
292       return;
293     }  
294     
295     AliMpMotifPosition* motifPosition = FindMotifPosition(motifPositionId);
296           
297     if (motifPosition) {
298        cout << "Processing " 
299             << motifPosition->GetID() << " " << offx << " " << offy << endl; 
300
301        motifPosition->SetLowIndicesLimit(AliMpIntPair(offx, offy));
302        
303        Int_t offx2 
304          = offx + motifPosition->GetMotif()->GetMotifType()->GetNofPadsX() - 1;
305          
306        Int_t offy2 
307          = offy + motifPosition->GetMotif()->GetMotifType()->GetNofPadsY() - 1;
308        
309        motifPosition->SetHighIndicesLimit(AliMpIntPair(offx2, offy2));
310     }
311     else {   
312        cerr <<"Motif position " << motifPositionId << endl;
313        Warning("UpdateGlobalIndices", "Motif position not found !!!");
314     }
315   }    
316   while (!in.eof());
317 }
318
319
320 //_____________________________________________________________________________
321 AliMpVMotif* AliMpMotifMap::FindMotif(const TString& motifID) const
322 {
323 // Finds the motif with the specified ID.
324 // ---
325   
326   MotifMapIterator i = fMotifs.find(motifID);
327   
328   if (i != fMotifs.end()) 
329     return (*i).second;
330   else                 
331     return 0;
332 }
333
334 //_____________________________________________________________________________
335 AliMpVMotif* AliMpMotifMap::FindMotif(const TString& motifID, 
336                              const TString& motifTypeID,
337                              TVector2 padDimensions ) const
338 {
339 // Finds the motif with the specified ID and returns it
340 // only if its motif type and motif dimensions agree
341 // with the given motifTypeID and motifDimensions.
342 // Disagreement causes fatal error.
343 // ---
344   
345   AliMpVMotif* motif = FindMotif(motifID);
346
347   if (motif && motif->GetMotifType()->GetID() != motifTypeID) {
348       Fatal("FindMotif", 
349             "Motif has been already defined with a different type.");
350       return 0;     
351   }
352
353   // check pad dimension in case of a normal motif
354   if (motif && 
355       dynamic_cast<AliMpMotif*>(motif) && 
356       ( motif->GetPadDimensions(0).X() != padDimensions.X() ||
357         motif->GetPadDimensions(0).Y() != padDimensions.Y())) { 
358       
359       Fatal("FindMotifType", 
360             "Motif type has been already defined with different dimensions.");
361       return 0;
362
363   } 
364
365   // check case of a special motif
366   if (motif && 
367       (padDimensions.X() == 0. && padDimensions.Y() == 0.) &&
368       !dynamic_cast<AliMpMotifSpecial*>(motif)) {
369
370       Fatal("FindMotifType", 
371             "Motif type has been already defined with different dimensions.");
372       return 0;
373
374   } 
375   
376   return motif;
377 }
378
379 //_____________________________________________________________________________
380 AliMpMotifType* AliMpMotifMap::FindMotifType(const TString& motifTypeID) const
381 {
382 // Finds the motif type with the specified motif type ID.
383 // ---
384   
385   MotifTypeMapIterator i = fMotifTypes.find(motifTypeID);
386   
387   if (i != fMotifTypes.end()) 
388     return (*i).second;
389   else                 
390     return 0;
391 }
392
393 //_____________________________________________________________________________
394 AliMpMotifPosition* AliMpMotifMap::FindMotifPosition(Int_t motifPositionID) const
395 {
396 // Finds the motif position with the specified motif position ID.
397 // ---
398   
399   MotifPositionMapIterator i = fMotifPositions.find(motifPositionID);
400   
401   if (i != fMotifPositions.end()) 
402     return (*i).second;
403   else                 
404     return 0;
405 }
406
407 //_____________________________________________________________________________
408 AliMpMotifPosition* AliMpMotifMap::FindMotifPosition(AliMpIntPair indices) const
409 {
410 // Finds the last motif position which has the global indices (low limit)
411 // less then the indices specified.
412 // ---
413
414   MotifPositionMap2Iterator found 
415     = fMotifPositions2.lower_bound(indices);
416   
417   if (found == fMotifPositions2.end()) found--; 
418
419   MotifPositionMap2Iterator i=found;
420   do {
421     AliMpIntPair low = (*i).second->GetLowIndicesLimit();
422     AliMpIntPair up = (*i).second->GetHighIndicesLimit();
423     
424     if ( indices.GetFirst()  >= low.GetFirst() &&
425          indices.GetSecond() >= low.GetSecond() &&
426          indices.GetFirst()  <= up.GetFirst() &&
427          indices.GetSecond() <= up.GetSecond())
428          
429          return (*i).second;                     
430   }
431   while ( i-- != fMotifPositions2.begin());
432   
433   return 0;
434 }