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.16 2006/05/24 13:58:41 ivana Exp $
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 //-----------------------------------------------------------------------------
29 #include "AliMpMotifMap.h"
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"
41 #include <Riostream.h>
46 ClassImp(AliMpMotifMap)
49 //_____________________________________________________________________________
50 AliMpMotifMap::AliMpMotifMap()
57 /// Standard constructor
60 fMotifPositions2.SetOwner(false);
64 //_____________________________________________________________________________
65 AliMpMotifMap::AliMpMotifMap(TRootIOCtor* ioCtor)
76 fMotifPositions(ioCtor),
77 fMotifPositions2(ioCtor)
80 /// Root IO constructor
83 fMotifPositions2.SetOwner(false);
87 //_____________________________________________________________________________
88 AliMpMotifMap::~AliMpMotifMap()
92 // Delete all registered motifs, motif types, motif positions
95 for (MotifMapIterator im=fMotifs.begin(); im != fMotifs.end(); im++) {
99 for (MotifTypeMapIterator it=fMotifTypes.begin();
100 it != fMotifTypes.end(); it++) {
104 for (MotifPositionMapIterator ip=fMotifPositions.begin();
105 ip != fMotifPositions.end(); ip++) {
116 //_____________________________________________________________________________
117 void AliMpMotifMap::PrintMotif(const AliMpVMotif* motif) const
121 cout << motif->GetID().Data() << " "
122 << motif->GetMotifType()->GetID() << " "
123 << motif->Dimensions().X() << " "
124 << motif->Dimensions().Y();
127 //_____________________________________________________________________________
128 void AliMpMotifMap::PrintMotifType(const AliMpMotifType* motifType) const
130 /// Print the motif type.
132 cout << motifType->GetID().Data() << " "
133 << motifType->GetNofPadsX() << " "
134 << motifType->GetNofPadsY() << " ";
137 //_____________________________________________________________________________
138 void AliMpMotifMap::PrintMotifPosition(
139 const AliMpMotifPosition* motifPosition) const
141 /// Print the motif position.
143 cout << " ID " << motifPosition->GetID() << " "
144 << " Motif ID " << motifPosition->GetMotif()->GetID() << " "
145 << " Pos (X,Y) = (" << motifPosition->Position().X() << ","
146 << motifPosition->Position().Y() << ")";
149 //_____________________________________________________________________________
150 void AliMpMotifMap::PrintMotifPosition2(
151 const AliMpMotifPosition* motifPosition) const
153 /// Print the motif position.
155 cout << setw(3) << motifPosition->GetLowIndicesLimit().GetFirst() << " "
156 << setw(3) << motifPosition->GetLowIndicesLimit().GetSecond() << " "
157 << setw(3) << motifPosition->GetHighIndicesLimit().GetFirst() << " "
158 << setw(3) << motifPosition->GetHighIndicesLimit().GetSecond() << " "
159 << motifPosition->GetID() << " ";
162 //_____________________________________________________________________________
163 void AliMpMotifMap::PrintMotifs() const
165 /// Print all the motifs and their motif types
166 /// for all motifs in the motifs map.
169 if (fMotifs.size()) {
170 cout << "Dump of Motif Map - " << fMotifs.size() << " entries:" << endl;
172 for (MotifMapIterator i=fMotifs.begin(); i != fMotifs.end(); i++) {
173 const TString& id = (*i).first;
174 cout << "Map element "
175 << setw(3) << counter++ << " "
176 << id.Data() << " " ;
177 PrintMotif((*i).second);
185 if (fMotifs.GetSize()) {
186 cout << "Dump of Motif Map - " << fMotifs.GetSize() << " entries:" << endl;
188 AliMpExMapIterator* it = fMotifs.CreateIterator();
192 while ( ( motif = static_cast<AliMpVMotif*>(it->Next(key)) ) )
194 TString id = fMotifs.AliMpExMap::GetString(key);
195 cout << "Map element "
196 << setw(3) << counter++ << " "
197 << id.Data() << " " ;
207 //_____________________________________________________________________________
208 void AliMpMotifMap::PrintMotifTypes() const
210 /// Print all the the motifs types and their motif dimensions
211 /// for all motif types in the motif types map.
214 if (fMotifTypes.size()) {
215 cout << "Dump of Motif Type Map - " << fMotifTypes.size() << " entries:" << endl;
217 for (MotifTypeMapIterator i=fMotifTypes.begin(); i != fMotifTypes.end(); i++) {
218 const TString& id = (*i).first;
219 cout << "Map element "
220 << setw(3) << counter++ << " "
222 PrintMotifType((*i).second);
230 if (fMotifTypes.GetSize()) {
231 cout << "Dump of Motif Type Map - " << fMotifTypes.GetSize() << " entries:" << endl;
233 AliMpExMapIterator* it = fMotifTypes.CreateIterator();
235 AliMpMotifType* motifType;
237 while ( ( motifType = static_cast<AliMpMotifType*>(it->Next(key)) ) )
239 TString id = AliMpExMap::GetString(key);
240 cout << "Map element "
241 << setw(3) << counter++ << " "
242 << id.Data() << " " ;
243 PrintMotifType(motifType);
252 //_____________________________________________________________________________
254 AliMpMotifMap::GetAllMotifPositionsIDs(TArrayI& ecn) const
256 /// Fill the given array with all motif positions IDs (electronic card numbers)
257 /// defined in the map
260 ecn.Set(fMotifPositions.size());
262 MotifPositionMapIterator it;
263 for (it=fMotifPositions.begin(); it != fMotifPositions.end(); it++) {
264 AliMpMotifPosition* motifPosition = (*it).second;
265 ecn[i++] = motifPosition->GetID();
270 ecn.Set(fMotifPositions.GetSize());
271 TIter next(fMotifPositions.CreateIterator());
272 AliMpMotifPosition* motifPosition;
274 while ( ( motifPosition = static_cast<AliMpMotifPosition*>(next()) ) )
276 ecn[i] = motifPosition->GetID();
282 //_____________________________________________________________________________
283 UInt_t AliMpMotifMap::GetNofMotifPositions() const
285 /// Return the number of all motif positions IDs (electronic card numbers)
288 return fMotifPositions.size();
292 return fMotifPositions.GetSize();
296 //_____________________________________________________________________________
297 AliMpMotifPosition* AliMpMotifMap::GetMotifPosition(UInt_t index) const
299 /// Return the motif position which is in the map on the index-th position
301 AliCodeTimerAuto("");
303 if ( index >= GetNofMotifPositions() ) {
304 AliErrorStream() << "Index " << index << " outside limits." << endl;
309 MotifPositionMapIterator it = fMotifPositions.begin();
310 std::advance(it, index);
315 TIter next(fMotifPositions.CreateIterator());
316 while (index-- > 0) next();
317 return static_cast<AliMpMotifPosition*>(next());
321 //_____________________________________________________________________________
322 Int_t AliMpMotifMap::CalculateNofPads() const
324 /// Calculate total number of pads in the map
329 MotifPositionMapIterator it;
330 for (it=fMotifPositions.begin(); it != fMotifPositions.end(); it++) {
331 AliMpMotifPosition* motifPosition = (*it).second;
332 nofPads += motifPosition->GetMotif()->GetMotifType()->GetNofPads();
337 TIter next(fMotifPositions.CreateIterator());
338 AliMpMotifPosition* motifPosition;
339 while ( ( motifPosition = static_cast<AliMpMotifPosition*>(next()) ) )
341 nofPads += motifPosition->GetMotif()->GetMotifType()->GetNofPads();
348 //_____________________________________________________________________________
349 void AliMpMotifMap::PrintMotifPositions() const
351 /// Print all motif positions.
354 if (fMotifPositions.size()) {
355 cout << "Dump of Motif Position Map - " << fMotifPositions.size() << " entries:" << endl;
357 for (MotifPositionMapIterator i=fMotifPositions.begin();
358 i != fMotifPositions.end(); i++) {
360 cout << "Map element "
361 << setw(3) << counter++ << " ";
362 PrintMotifPosition((*i).second);
370 if (fMotifPositions.GetSize()) {
371 cout << "Dump of Motif Position Map - " << fMotifPositions.GetSize() << " entries:" << endl;
373 TIter next(fMotifPositions.CreateIterator());
374 AliMpMotifPosition* motifPosition;
376 while ( ( motifPosition = static_cast<AliMpMotifPosition*>(next()) ) )
378 cout << "Map element "
379 << setw(3) << counter++ << " ";
380 PrintMotifPosition(motifPosition);
388 //_____________________________________________________________________________
389 void AliMpMotifMap::PrintMotifPositions2() const
391 /// Print all motif positions from the second map
392 /// (by global indices)
395 if (fMotifPositions2.size()) {
396 cout << "Dump of Motif Position Map 2 - " << fMotifPositions2.size() << " entries:" << endl;
398 for (MotifPositionMap2Iterator i=fMotifPositions2.begin();
399 i != fMotifPositions2.end(); i++) {
401 cout << "Map element "
402 << setw(3) << counter++ << " ";
403 PrintMotifPosition2((*i).second);
412 if (fMotifPositions2.GetSize())
414 cout << "Dump of Motif Position Map 2 - " << fMotifPositions2.GetSize() << " entries:" << endl;
415 TIter next(fMotifPositions2.CreateIterator());
416 AliMpMotifPosition* motifPosition(0x0);
419 while ( ( motifPosition = static_cast<AliMpMotifPosition*>(next()) ) )
421 cout << "Map element " << setw(3) << counter++ << " ";
422 PrintMotifPosition2(motifPosition);
434 //_____________________________________________________________________________
435 Bool_t AliMpMotifMap::AddMotif(AliMpVMotif* motif, Bool_t warn)
437 /// Add the specified motif
438 /// if the motif with this ID is not yet present.
440 AliMpVMotif* found = FindMotif(motif->GetID());
442 if (warn && found == motif)
443 AliWarningStream() << "The motif is already in map." << endl;
445 if (warn && found != motif) {
447 << "Another motif with the same ID is already in map." << endl;
453 fMotifs[motif->GetID()] = motif;
457 fMotifs.Add(motif->GetID(), motif);
463 //_____________________________________________________________________________
464 Bool_t AliMpMotifMap::AddMotifType(AliMpMotifType* motifType, Bool_t warn)
466 /// Add the specified motif type
467 /// if the motif with this ID is not yet present.
469 AliMpMotifType* found = FindMotifType(motifType->GetID());
471 if (warn && found == motifType)
472 AliWarningStream() << "The motif type is already in map." << endl;
474 if (warn && found != motifType) {
476 << "Another motif type with the same ID is already in map." << endl;
482 fMotifTypes[motifType->GetID()] = motifType;
486 fMotifTypes.Add(motifType->GetID(), motifType);
492 //_____________________________________________________________________________
493 Bool_t AliMpMotifMap::AddMotifPosition(AliMpMotifPosition* motifPosition, Bool_t warn)
495 /// Add the specified motif position
496 /// if this position is not yet present.
498 AliMpMotifPosition* found = FindMotifPosition(motifPosition->GetID());
500 if (warn && found == motifPosition) {
502 << "ID: " << motifPosition->GetID()
503 << " found: " << found
504 << " new: " << motifPosition << endl
505 << "This motif position is already in map." << endl;
508 if (warn && found != motifPosition) {
510 << "ID: " << motifPosition->GetID()
511 << " found: " << found
512 << " new: " << motifPosition << endl
513 << "Another motif position with the same ID is already in map."
521 fMotifPositions[motifPosition->GetID()] = motifPosition;
525 fMotifPositions.Add(motifPosition->GetID() << 16, motifPosition);
531 //_____________________________________________________________________________
532 void AliMpMotifMap::FillMotifPositionMap2()
534 /// Fill the second map (by global indices) of motif positions.
537 if (fMotifPositions2.size() > 0 ) {
538 AliWarningStream() << "Map has been already filled." << endl;
542 for (MotifPositionMapIterator ip=fMotifPositions.begin();
543 ip != fMotifPositions.end(); ip++) {
545 fMotifPositions2[(*ip).second->GetLowIndicesLimit()] = (*ip).second;
550 if (fMotifPositions2.GetSize() > 0 ) {
551 AliWarningStream() <<"Map has been already filled." << endl;
555 TIter next(fMotifPositions.CreateIterator());
556 AliMpMotifPosition* motifPosition(0x0);
557 while ( ( motifPosition = static_cast<AliMpMotifPosition*>(next()) ) )
559 fMotifPositions2.Add(motifPosition->GetLowIndicesLimit(), motifPosition);
565 //_____________________________________________________________________________
566 void AliMpMotifMap::Print(const char* opt) const
568 /// Print the motifs and motif types maps.
574 if ( sopt.Contains("MOTIFS") || sopt == "ALL" ) PrintMotifs();
575 if ( sopt.Contains("MOTIFTYPES") || sopt == "ALL" ) PrintMotifTypes();
576 if ( sopt.Contains("MOTIFPOSITIONS") || sopt == "ALL" ) PrintMotifPositions();
577 if ( sopt.Contains("MOTIFPOSITIONS2") || sopt == "ALL" ) PrintMotifPositions2();
580 //_____________________________________________________________________________
581 void AliMpMotifMap::PrintGlobalIndices(const char* fileName) const
583 /// Print all motif positions and their global indices.
585 ofstream out(fileName, ios::out);
588 if (fMotifPositions.size()) {
589 for (MotifPositionMapIterator i=fMotifPositions.begin();
590 i != fMotifPositions.end(); i++) {
592 AliMpMotifPosition* motifPosition = (*i).second;
593 out << setw(5) << motifPosition->GetID() << " "
594 << setw(3) << motifPosition->GetLowIndicesLimit().GetFirst() << " "
595 << setw(3) << motifPosition->GetLowIndicesLimit().GetSecond()
603 if (fMotifPositions.GetSize()) {
604 TIter next(fMotifPositions.CreateIterator());
605 AliMpMotifPosition* motifPosition;
606 while ( ( motifPosition = static_cast<AliMpMotifPosition*>(next()) ) )
608 out << setw(5) << motifPosition->GetID() << " "
609 << setw(3) << motifPosition->GetLowIndicesLimit().GetFirst() << " "
610 << setw(3) << motifPosition->GetLowIndicesLimit().GetSecond()
618 //_____________________________________________________________________________
619 void AliMpMotifMap::UpdateGlobalIndices(const char* fileName)
621 /// Update the motif positions global indices from the file.
623 ifstream in(fileName, ios::in);
625 Int_t motifPositionId, offx, offy;
628 in >> motifPositionId >> offx >> offy;
631 FillMotifPositionMap2();
635 AliMpMotifPosition* motifPosition = FindMotifPosition(motifPositionId);
640 << motifPosition->GetID() << " " << offx << " " << offy << endl;
642 motifPosition->SetLowIndicesLimit(AliMpIntPair(offx, offy));
645 = offx + motifPosition->GetMotif()->GetMotifType()->GetNofPadsX() - 1;
648 = offy + motifPosition->GetMotif()->GetMotifType()->GetNofPadsY() - 1;
650 motifPosition->SetHighIndicesLimit(AliMpIntPair(offx2, offy2));
654 << "Motif position " << motifPositionId << " not found" << endl;
661 //_____________________________________________________________________________
662 AliMpVMotif* AliMpMotifMap::FindMotif(const TString& motifID) const
664 /// Find the motif with the specified ID.
666 //AliCodeTimerAuto("");
669 MotifMapIterator i = fMotifs.find(motifID);
670 if (i != fMotifs.end())
677 return (AliMpVMotif*)fMotifs.GetValue(motifID);
681 //_____________________________________________________________________________
682 AliMpVMotif* AliMpMotifMap::FindMotif(const TString& motifID,
683 const TString& motifTypeID,
684 const TVector2& padDimensions ) const
686 /// Find the motif with the specified ID and returns it
687 /// only if its motif type and motif dimensions agree
688 /// with the given motifTypeID and motifDimensions.
689 /// Disagreement causes fatal error.
691 //AliCodeTimerAuto("");
693 AliMpVMotif* motif = FindMotif(motifID);
695 if (motif && motif->GetMotifType()->GetID() != motifTypeID) {
696 AliFatal("Motif has been already defined with a different type.");
700 // check pad dimension in case of a normal motif
702 dynamic_cast<AliMpMotif*>(motif) &&
703 ( motif->GetPadDimensions(0).X() != padDimensions.X() ||
704 motif->GetPadDimensions(0).Y() != padDimensions.Y())) {
706 AliFatal("Motif type has been already defined with different dimensions.");
711 // check case of a special motif
713 (padDimensions.X() == 0. && padDimensions.Y() == 0.) &&
714 !dynamic_cast<AliMpMotifSpecial*>(motif)) {
716 AliFatal("Motif type has been already defined with different dimensions.");
724 //_____________________________________________________________________________
725 AliMpMotifType* AliMpMotifMap::FindMotifType(const TString& motifTypeID) const
727 /// Find the motif type with the specified motif type ID.
729 //AliCodeTimerAuto("");
732 MotifTypeMapIterator i = fMotifTypes.find(motifTypeID);
733 if (i != fMotifTypes.end())
740 return (AliMpMotifType*)fMotifTypes.GetValue(motifTypeID);
744 //_____________________________________________________________________________
746 AliMpMotifMap::FindMotifPosition(Int_t motifPositionID) const
748 /// Find the motif position with the specified motif position ID.
750 //AliCodeTimerAuto("");
753 MotifPositionMapIterator i = fMotifPositions.find(motifPositionID);
754 if (i != fMotifPositions.end())
761 return (AliMpMotifPosition*)fMotifPositions.GetValue(motifPositionID << 16);
766 //_____________________________________________________________________________
768 AliMpMotifMap::FindMotifPosition(const AliMpIntPair& indices) const
770 /// Find the last motif position which has the global indices (low limit)
771 /// less then the indices specified.
774 MotifPositionMap2Iterator found
775 = fMotifPositions2.lower_bound(indices);
777 if (found == fMotifPositions2.end()) found--;
779 MotifPositionMap2Iterator i=found;
781 AliMpIntPair low = (*i).second->GetLowIndicesLimit();
782 AliMpIntPair up = (*i).second->GetHighIndicesLimit();
784 if ( indices.GetFirst() >= low.GetFirst() &&
785 indices.GetSecond() >= low.GetSecond() &&
786 indices.GetFirst() <= up.GetFirst() &&
787 indices.GetSecond() <= up.GetSecond())
791 while ( i-- != fMotifPositions2.begin());
797 // HOW TO DO THIS WITH ROOT ????
798 // Fortunately it seems not to be used anywhere
799 Fatal("FindMotifPosition", "Difficult in Root to do this.");