1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
17 // $MpId: AliMpMotifMap.cxx,v 1.7 2005/08/26 15:43:36 ivana Exp $
20 // Class AliMpMotifMap
21 // -------------------
22 // Class describing the motif map container, where motifs are
23 // mapped to their string IDs.
24 // Included in AliRoot: 2003/05/02
25 // Authors: David Guez, Ivana Hrivnacova; IPN Orsay
27 #include <Riostream.h>
30 #include "AliMpMotifMap.h"
31 #include "AliMpVMotif.h"
32 #include "AliMpMotif.h"
33 #include "AliMpMotifSpecial.h"
34 #include "AliMpMotifType.h"
35 #include "AliMpMotifPosition.h"
37 ClassImp(AliMpMotifMap)
40 const Int_t AliMpMotifMap::fgkSeparator = 100;
43 //_____________________________________________________________________________
44 AliMpMotifMap::AliMpMotifMap()
47 /// Default constructor
50 //_____________________________________________________________________________
51 AliMpMotifMap::~AliMpMotifMap()
55 // Delete all registered motifs, motif types, motif positions
58 for (MotifMapIterator im=fMotifs.begin(); im != fMotifs.end(); im++) {
62 for (MotifTypeMapIterator it=fMotifTypes.begin();
63 it != fMotifTypes.end(); it++) {
67 for (MotifPositionMapIterator ip=fMotifPositions.begin();
68 ip != fMotifPositions.end(); ip++) {
74 MotifMapIterator im(&fMotifs);
76 while ( im.Next(mkey, mvalue) ) delete (AliMpMotif*)mvalue;
78 MotifMapIterator it(&fMotifTypes);
80 while ( it.Next(tkey, tvalue) ) delete (AliMpMotifType*)tvalue;
82 MotifMapIterator ip(&fMotifPositions);
84 while ( it.Next(pkey, pvalue) ) delete (AliMpMotifPosition*)pvalue;
93 //_____________________________________________________________________________
94 Int_t AliMpMotifMap::GetIndex(const TString& s) const
96 /// Convert the TString to integer.
99 Fatal("GetIndex", "String too long.");
104 for (Int_t i=s.Length(); i>=0; --i) index = index*100 + int(s(i));
108 //______________________________________________________________________________
109 Int_t AliMpMotifMap::GetIndex(const AliMpIntPair& pair) const
111 /// Convert the pair of integers to integer.
113 if (pair.GetFirst() >= fgkSeparator || pair.GetSecond() >= fgkSeparator)
114 Fatal("GetIndex", "Index out of limit.");
116 return pair.GetFirst()*fgkSeparator + pair.GetSecond() + 1;
119 //_____________________________________________________________________________
120 TString AliMpMotifMap::GetString(Int_t index) const
122 /// Convert the integer index to the string.
126 Char_t c = index%100;
133 //______________________________________________________________________________
134 AliMpIntPair AliMpMotifMap::GetPair(Int_t index) const
136 /// Convert the integer index to the pair of integers.
138 return AliMpIntPair((index-1)/fgkSeparator, (index-1)%fgkSeparator);
142 //_____________________________________________________________________________
143 void AliMpMotifMap::PrintMotif(const AliMpVMotif* motif) const
148 cout << motif->GetID().Data() << " "
149 << motif->GetMotifType()->GetID() << " "
150 << motif->Dimensions().X() << " "
151 << motif->Dimensions().Y();
154 //_____________________________________________________________________________
155 void AliMpMotifMap::PrintMotifType(const AliMpMotifType* motifType) const
157 /// Print the motif type.
159 cout << motifType->GetID().Data() << " "
160 << motifType->GetNofPadsX() << " "
161 << motifType->GetNofPadsY() << " ";
164 //_____________________________________________________________________________
165 void AliMpMotifMap::PrintMotifPosition(
166 const AliMpMotifPosition* motifPosition) const
168 /// Print the motif position.
170 cout << motifPosition->GetID() << " "
171 << motifPosition->GetMotif()->GetID() << " "
172 << motifPosition->Position().X() << " "
173 << motifPosition->Position().Y() << " ";
176 //_____________________________________________________________________________
177 void AliMpMotifMap::PrintMotifPosition2(
178 const AliMpMotifPosition* motifPosition) const
180 /// Print the motif position.
182 cout << setw(3) << motifPosition->GetLowIndicesLimit().GetFirst() << " "
183 << setw(3) << motifPosition->GetLowIndicesLimit().GetSecond() << " "
184 << setw(3) << motifPosition->GetHighIndicesLimit().GetFirst() << " "
185 << setw(3) << motifPosition->GetHighIndicesLimit().GetSecond() << " "
186 << motifPosition->GetID() << " ";
189 //_____________________________________________________________________________
190 void AliMpMotifMap::PrintMotifs() const
192 /// Print all the motifs and their motif types
193 /// for all motifs in the motifs map.
196 if (fMotifs.size()) {
197 cout << "Dump of Motif Map - " << fMotifs.size() << " entries:" << endl;
199 for (MotifMapIterator i=fMotifs.begin(); i != fMotifs.end(); i++) {
200 const TString& id = (*i).first;
201 cout << "Map element "
202 << setw(3) << counter++ << " "
203 << id.Data() << " " ;
204 PrintMotif((*i).second);
212 if (fMotifs.GetSize()) {
213 cout << "Dump of Motif Map - " << fMotifs.GetSize() << " entries:" << endl;
215 MotifMapIterator i(&fMotifs);
217 while ( i.Next(key, value) ) {
218 TString id = GetString(key);
219 AliMpVMotif* motif = (AliMpVMotif*)value;
220 cout << "Map element "
221 << setw(3) << counter++ << " "
222 << id.Data() << " " ;
231 //_____________________________________________________________________________
232 void AliMpMotifMap::PrintMotifTypes() const
234 /// Print all the the motifs types and their motif dimensions
235 /// for all motif types in the motif types map.
238 if (fMotifTypes.size()) {
239 cout << "Dump of Motif Type Map - " << fMotifTypes.size() << " entries:" << endl;
241 for (MotifTypeMapIterator i=fMotifTypes.begin(); i != fMotifTypes.end(); i++) {
242 const TString& id = (*i).first;
243 cout << "Map element "
244 << setw(3) << counter++ << " "
246 PrintMotifType((*i).second);
254 if (fMotifTypes.GetSize()) {
255 cout << "Dump of Motif Type Map - " << fMotifTypes.GetSize() << " entries:" << endl;
257 MotifTypeMapIterator i(&fMotifTypes);
259 while ( i.Next(key, value) ) {
260 TString id = GetString(key);
261 AliMpMotifType* motifType = (AliMpMotifType*)value;
262 cout << "Map element "
263 << setw(3) << counter++ << " "
264 << id.Data() << " " ;
265 PrintMotifType(motifType);
273 //_____________________________________________________________________________
274 void AliMpMotifMap::PrintMotifPositions() const
276 /// Print all the the motifs positions.
279 if (fMotifPositions.size()) {
280 cout << "Dump of Motif Position Map - " << fMotifPositions.size() << " entries:" << endl;
282 for (MotifPositionMapIterator i=fMotifPositions.begin();
283 i != fMotifPositions.end(); i++) {
285 cout << "Map element "
286 << setw(3) << counter++ << " ";
287 PrintMotifPosition((*i).second);
295 if (fMotifPositions.GetSize()) {
296 cout << "Dump of Motif Position Map - " << fMotifPositions.GetSize() << " entries:" << endl;
298 MotifPositionMapIterator i(&fMotifPositions);
300 while ( i.Next(key, value) ) {
301 AliMpMotifPosition* motifPosition = (AliMpMotifPosition*)value;
302 cout << "Map element "
303 << setw(3) << counter++ << " ";
304 PrintMotifPosition(motifPosition);
312 //_____________________________________________________________________________
313 void AliMpMotifMap::PrintMotifPositions2() const
315 /// Print all the the motifs positions from the second map
316 /// (by global indices)
319 if (fMotifPositions2.size()) {
320 cout << "Dump of Motif Position Map 2 - " << fMotifPositions2.size() << " entries:" << endl;
322 for (MotifPositionMap2Iterator i=fMotifPositions2.begin();
323 i != fMotifPositions2.end(); i++) {
325 cout << "Map element "
326 << setw(3) << counter++ << " ";
327 PrintMotifPosition2((*i).second);
335 if (fMotifPositions2.GetSize()) {
336 cout << "Dump of Motif Position Map 2 - " << fMotifPositions2.GetSize() << " entries:" << endl;
338 MotifPositionMapIterator i(&fMotifPositions2);
340 while ( i.Next(key, value) ) {
341 AliMpMotifPosition* motifPosition = (AliMpMotifPosition*)value;
342 cout << "Map element "
343 << setw(3) << counter++ << " ";
344 PrintMotifPosition2(motifPosition);
356 //_____________________________________________________________________________
357 Bool_t AliMpMotifMap::AddMotif(AliMpVMotif* motif, Bool_t warn)
359 /// Add the specified motif
360 /// if the motif with this ID is not yet present.
362 AliMpVMotif* found = FindMotif(motif->GetID());
364 if (warn && found == motif)
365 Warning("AddMotif", "The motif is already in map.");
366 if (warn && found != motif)
367 Warning("AddMotif", "Another motif with the same ID is already in map.");
372 fMotifs[motif->GetID()] = motif;
376 fMotifs.Add(GetIndex(motif->GetID()), (Long_t)motif);
382 //_____________________________________________________________________________
383 Bool_t AliMpMotifMap::AddMotifType(AliMpMotifType* motifType, Bool_t warn)
385 /// Add the specified motif type
386 /// if the motif with this ID is not yet present.
388 AliMpMotifType* found = FindMotifType(motifType->GetID());
390 if (warn && found == motifType)
391 Warning("AddMotifType", "The motif type is already in map.");
392 if (warn && found != motifType)
393 Warning("AddMotifType",
394 "Another motif type with the same ID is already in map.");
399 fMotifTypes[motifType->GetID()] = motifType;
403 fMotifTypes.Add(GetIndex(motifType->GetID()), (Long_t)motifType);
409 //_____________________________________________________________________________
410 Bool_t AliMpMotifMap::AddMotifPosition(AliMpMotifPosition* motifPosition, Bool_t warn)
412 /// Add the specified motif position
413 /// if this position is not yet present.
415 AliMpMotifPosition* found = FindMotifPosition(motifPosition->GetID());
417 if (warn && found == motifPosition) {
418 cerr << "ID: " << motifPosition->GetID()
419 << " found: " << found
420 << " new: " << motifPosition << endl;
421 Warning("AddMotifPosition", "This motif position is already in map.");
423 if (warn && found != motifPosition) {
424 cerr << "ID: " << motifPosition->GetID()
425 << " found: " << found
426 << " new: " << motifPosition << endl;
427 Warning("AddMotifposition",
428 "Another motif position with the same ID is already in map.");
434 fMotifPositions[motifPosition->GetID()] = motifPosition;
438 fMotifPositions.Add(motifPosition->GetID(), (Long_t)motifPosition);
444 //_____________________________________________________________________________
445 void AliMpMotifMap::FillMotifPositionMap2()
447 /// Fill the second map (by global indices) of motif positions.
450 if (fMotifPositions2.size() > 0 ) {
451 Warning("FillMotifPositionMap2", "Map has been already filled.");
455 for (MotifPositionMapIterator ip=fMotifPositions.begin();
456 ip != fMotifPositions.end(); ip++) {
458 fMotifPositions2[(*ip).second->GetLowIndicesLimit()] = (*ip).second;
463 if (fMotifPositions2.GetSize() > 0 ) {
464 Warning("FillMotifPositionMap2", "Map has been already filled.");
468 MotifPositionMapIterator i(&fMotifPositions);
470 while ( i.Next(key, value) ) {
471 AliMpMotifPosition* motifPosition = (AliMpMotifPosition*)value;
472 fMotifPositions2.Add(GetIndex(motifPosition->GetLowIndicesLimit()),
473 (Long_t)motifPosition);
479 //_____________________________________________________________________________
480 void AliMpMotifMap::Print(const char* /*option*/) const
482 /// Print the motifs and motif types maps.
486 PrintMotifPositions();
487 PrintMotifPositions2();
490 //_____________________________________________________________________________
491 void AliMpMotifMap::PrintGlobalIndices(const char* fileName) const
493 /// Print all the motifs positions and their global indices.
495 ofstream out(fileName, ios::out);
498 if (fMotifPositions.size()) {
499 for (MotifPositionMapIterator i=fMotifPositions.begin();
500 i != fMotifPositions.end(); i++) {
502 AliMpMotifPosition* motifPosition = (*i).second;
503 out << setw(5) << motifPosition->GetID() << " "
504 << setw(3) << motifPosition->GetLowIndicesLimit().GetFirst() << " "
505 << setw(3) << motifPosition->GetLowIndicesLimit().GetSecond()
513 if (fMotifPositions.GetSize()) {
514 MotifPositionMapIterator i(&fMotifPositions);
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()
528 //_____________________________________________________________________________
529 void AliMpMotifMap::UpdateGlobalIndices(const char* fileName)
531 /// Updates the motifs positions global indices
534 ifstream in(fileName, ios::in);
536 Int_t motifPositionId, offx, offy;
539 in >> motifPositionId >> offx >> offy;
542 FillMotifPositionMap2();
546 AliMpMotifPosition* motifPosition = FindMotifPosition(motifPositionId);
549 cout << "Processing "
550 << motifPosition->GetID() << " " << offx << " " << offy << endl;
552 motifPosition->SetLowIndicesLimit(AliMpIntPair(offx, offy));
555 = offx + motifPosition->GetMotif()->GetMotifType()->GetNofPadsX() - 1;
558 = offy + motifPosition->GetMotif()->GetMotifType()->GetNofPadsY() - 1;
560 motifPosition->SetHighIndicesLimit(AliMpIntPair(offx2, offy2));
563 cerr <<"Motif position " << motifPositionId << endl;
564 Warning("UpdateGlobalIndices", "Motif position not found !!!");
571 //_____________________________________________________________________________
572 AliMpVMotif* AliMpMotifMap::FindMotif(const TString& motifID) const
574 /// Finds the motif with the specified ID.
577 MotifMapIterator i = fMotifs.find(motifID);
578 if (i != fMotifs.end())
585 Long_t value = fMotifs.GetValue(GetIndex(motifID));
587 return (AliMpVMotif*)value;
593 //_____________________________________________________________________________
594 AliMpVMotif* AliMpMotifMap::FindMotif(const TString& motifID,
595 const TString& motifTypeID,
596 const TVector2& padDimensions ) const
598 /// Finds the motif with the specified ID and returns it
599 /// only if its motif type and motif dimensions agree
600 /// with the given motifTypeID and motifDimensions.
601 /// Disagreement causes fatal error.
603 AliMpVMotif* motif = FindMotif(motifID);
605 if (motif && motif->GetMotifType()->GetID() != motifTypeID) {
607 "Motif has been already defined with a different type.");
611 // check pad dimension in case of a normal motif
613 dynamic_cast<AliMpMotif*>(motif) &&
614 ( motif->GetPadDimensions(0).X() != padDimensions.X() ||
615 motif->GetPadDimensions(0).Y() != padDimensions.Y())) {
617 Fatal("FindMotifType",
618 "Motif type has been already defined with different dimensions.");
623 // check case of a special motif
625 (padDimensions.X() == 0. && padDimensions.Y() == 0.) &&
626 !dynamic_cast<AliMpMotifSpecial*>(motif)) {
628 Fatal("FindMotifType",
629 "Motif type has been already defined with different dimensions.");
637 //_____________________________________________________________________________
638 AliMpMotifType* AliMpMotifMap::FindMotifType(const TString& motifTypeID) const
640 /// Find the motif type with the specified motif type ID.
643 MotifTypeMapIterator i = fMotifTypes.find(motifTypeID);
644 if (i != fMotifTypes.end())
651 Long_t value = fMotifTypes.GetValue(GetIndex(motifTypeID));
653 return (AliMpMotifType*)value;
659 //_____________________________________________________________________________
661 AliMpMotifMap::FindMotifPosition(Int_t motifPositionID) const
663 /// Find the motif position with the specified motif position ID.
666 MotifPositionMapIterator i = fMotifPositions.find(motifPositionID);
667 if (i != fMotifPositions.end())
674 Long_t value = fMotifPositions.GetValue(motifPositionID);
676 return (AliMpMotifPosition*)value;
683 //_____________________________________________________________________________
685 AliMpMotifMap::FindMotifPosition(const AliMpIntPair& indices) const
687 /// Find the last motif position which has the global indices (low limit)
688 /// less then the indices specified.
691 MotifPositionMap2Iterator found
692 = fMotifPositions2.lower_bound(indices);
694 if (found == fMotifPositions2.end()) found--;
696 MotifPositionMap2Iterator i=found;
698 AliMpIntPair low = (*i).second->GetLowIndicesLimit();
699 AliMpIntPair up = (*i).second->GetHighIndicesLimit();
701 if ( indices.GetFirst() >= low.GetFirst() &&
702 indices.GetSecond() >= low.GetSecond() &&
703 indices.GetFirst() <= up.GetFirst() &&
704 indices.GetSecond() <= up.GetSecond())
708 while ( i-- != fMotifPositions2.begin());
714 // HOW TO DO THIS WITH ROOT ????
715 // Fortunately it seems not to be used anywhere
716 Fatal("FindMotifPosition", "Difficult in Root to do this.");