]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/mapping/AliMpMotifType.cxx
Commenting out the infamous speed eater = StdoutToAliDebug until a better solution...
[u/mrichter/AliRoot.git] / MUON / mapping / AliMpMotifType.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: AliMpMotifType.cxx,v 1.10 2006/05/24 13:58:41 ivana Exp $
18 // Category: motif
19 //
20 // Class AliMpMotifType
21 // --------------------
22 // Class that defines the motif properties.
23 // Included in AliRoot: 2003/05/02
24 // Authors: David Guez, Ivana Hrivnacova; IPN Orsay
25
26 #include "AliMpMotifType.h"
27 #include "AliMpMotifTypePadIterator.h"
28 #include "AliMpConnection.h"
29
30 #include "AliLog.h"
31 #include "AliMpFiles.h"
32 #include "TSystem.h"
33
34 #include <Riostream.h>
35
36 /// \cond CLASSIMP
37 ClassImp(AliMpMotifType)
38 /// \endcond
39
40 const Int_t AliMpMotifType::fgkPadNumForA = 65;
41
42 //______________________________________________________________________________
43 AliMpMotifType::AliMpMotifType(const TString &id) 
44   : TObject(),
45     fID(id),
46     fNofPadsX(0),   
47     fNofPadsY(0),
48     fVerboseLevel(0),
49 #ifdef WITH_STL
50     fConnections()
51 #endif
52 #ifdef WITH_ROOT
53     fConnections(true)
54 #endif
55 {
56   /// Standard constructor
57       AliDebug(1,Form("this=%p id=%s",this,id.Data()));
58 }
59
60 //______________________________________________________________________________
61 AliMpMotifType::AliMpMotifType() 
62   : TObject(),
63     fID(""),
64     fNofPadsX(0),   
65     fNofPadsY(0),
66     fVerboseLevel(0),
67     fConnections()
68 {
69   /// Default constructor
70       AliDebug(1,Form("this=%p",this));
71 }
72
73 //______________________________________________________________________________
74 AliMpMotifType::AliMpMotifType(const AliMpMotifType& rhs)
75 : TObject(),
76   fID(""),
77   fNofPadsX(0),   
78   fNofPadsY(0),
79   fVerboseLevel(0),
80   fConnections()
81 {
82   /// Copy constructor
83
84     AliDebug(1,Form("this=%p (copy ctor)",this));
85     rhs.Copy(*this);
86 }
87
88 //______________________________________________________________________________
89 AliMpMotifType&
90 AliMpMotifType::operator=(const AliMpMotifType& rhs)
91 {
92   /// Assignment operator
93
94   TObject::operator=(rhs);
95   rhs.Copy(*this);
96   return *this;  
97 }
98
99 //______________________________________________________________________________
100 TObject*
101 AliMpMotifType::Clone(const char* /*newname*/) const 
102 {
103   /// Returns a full copy of this object
104   return new AliMpMotifType(*this);
105 }
106
107 //______________________________________________________________________________
108 void
109 AliMpMotifType::Copy(TObject& object) const
110 {
111   /// Copy object
112
113   TObject::Copy(object);
114   AliMpMotifType& mt = static_cast<AliMpMotifType&>(object);
115   mt.fID = fID;
116   mt.fNofPadsX = fNofPadsX;
117   mt.fNofPadsY = fNofPadsY;
118   mt.fVerboseLevel = fVerboseLevel;
119   mt.fConnections = fConnections;
120 }
121
122 //______________________________________________________________________________
123 AliMpMotifType::~AliMpMotifType() 
124 {
125 /// Destructor
126
127 #ifdef WITH_STL
128  for(ConnectionMapCIterator i = fConnections.begin();
129   i!=fConnections.end();++i)
130    delete i->second;
131
132   fConnections.erase(fConnections.begin(),fConnections.end());
133 #endif  
134   
135   AliDebug(1,Form("this=%p",this));
136 //  StdoutToAliDebug(1,this->Print(););
137 }
138
139 //______________________________________________________________________________
140 AliMpVPadIterator* AliMpMotifType::CreateIterator() const
141 {
142 /// Create new motif type iterator
143
144   return new AliMpMotifTypePadIterator(this);
145 }
146
147 //______________________________________________________________________________
148 void AliMpMotifType::SetNofPads(Int_t nofPadsX, Int_t nofPadsY)
149 {
150   /// Change the number of pads in this motif
151
152   fNofPadsX = nofPadsX;
153   fNofPadsY = nofPadsY;
154 }
155
156
157 //______________________________________________________________________________
158 Int_t AliMpMotifType::PadNum(const TString &padName) const
159 {
160   /// Transform a pad name into the equivalent pad number
161
162   if ( (padName[0]>='A') && (padName[0]<='Z') )
163     return fgkPadNumForA+padName[0]-'A';
164   else
165     return atoi(padName.Data());
166 }
167
168 //______________________________________________________________________________
169 TString AliMpMotifType::PadName(Int_t padNum) const
170 {
171   /// Transform a pad number into its equivalent pad name
172
173   if (padNum<fgkPadNumForA)
174     return Form("%d",padNum);
175   else
176     return char('A'+padNum-fgkPadNumForA);
177 }
178
179 //______________________________________________________________________________
180 void AliMpMotifType::AddConnection(const AliMpIntPair &localIndices, 
181                                AliMpConnection* connection)
182 {
183   /// Add the connection to the map
184   
185 #ifdef WITH_STL
186   fConnections[localIndices]=connection;
187 #endif
188
189 #ifdef WITH_ROOT
190   fConnections.Add(localIndices, connection);
191 #endif   
192
193   connection->SetOwner(this);
194 }  
195
196 //______________________________________________________________________________
197 AliMpConnection *AliMpMotifType::FindConnectionByPadNum(Int_t padNum) const
198 {
199   /// Retrieve the AliMpConnection pointer from its pad num
200   
201 #ifdef WITH_STL
202  for(ConnectionMapCIterator i = fConnections.begin();
203   i!=fConnections.end();++i)
204    if (i->second->GetPadNum()==padNum) return i->second;
205  return 0;
206 #endif
207
208 #ifdef WITH_ROOT
209   TExMapIter i = fConnections.GetIterator();
210   Long_t key, value;
211   while ( i.Next(key, value) ) {
212     AliMpConnection* connection = (AliMpConnection*)value;
213     if (connection->GetPadNum()==padNum) return connection;
214   }  
215  return 0;
216 #endif
217 }
218
219 //______________________________________________________________________________
220 AliMpConnection *AliMpMotifType::FindConnectionByLocalIndices(
221                                        const AliMpIntPair& localIndices) const
222 {
223   /// Retrieve the AliMpConnection pointer from its position (in pad unit)
224   
225   if (!localIndices.IsValid()) return 0;
226
227 #ifdef WITH_STL
228   ConnectionMapCIterator i = fConnections.find(localIndices);
229  if (i != fConnections.end())
230    return i->second;
231  else return 0;
232 #endif
233
234 #ifdef WITH_ROOT
235   return (AliMpConnection*)fConnections.GetValue(localIndices);
236 #endif
237 }
238
239 //______________________________________________________________________________
240 AliMpConnection *AliMpMotifType::FindConnectionByGassiNum(Int_t gassiNum) const
241 {
242   /// Return the connection for the given gassiplex number
243   
244 #ifdef WITH_STL
245  for(ConnectionMapCIterator i = fConnections.begin();
246   i!=fConnections.end();++i)
247    if (i->second->GetGassiNum()==gassiNum) return i->second;
248  return 0;
249 #endif
250
251 #ifdef WITH_ROOT
252   TExMapIter i = fConnections.GetIterator();
253   Long_t key, value;
254   while ( i.Next(key, value) ) {
255     AliMpConnection* connection = (AliMpConnection*)value;
256     if (connection->GetGassiNum()==gassiNum) return connection;
257   }  
258  return 0;
259 #endif
260 }
261
262 //______________________________________________________________________________
263 AliMpConnection *AliMpMotifType::FindConnectionByKaptonNum(Int_t kaptonNum) const
264 {
265   /// Give the connection related to the given kapton number
266   
267 #ifdef WITH_STL
268  for(ConnectionMapCIterator i = fConnections.begin();
269   i!=fConnections.end();++i)
270    if (i->second->GetKaptonNum()==kaptonNum) return i->second;
271  return 0;
272 #endif
273
274 #ifdef WITH_ROOT
275   TExMapIter i = fConnections.GetIterator();
276   Long_t key, value;
277   while ( i.Next(key, value) ) {
278     AliMpConnection* connection = (AliMpConnection*)value;
279     if (connection->GetKaptonNum()==kaptonNum) return connection;
280   }  
281  return 0;
282 #endif
283 }
284 //______________________________________________________________________________
285 AliMpConnection *AliMpMotifType::FindConnectionByBergNum(Int_t bergNum) const
286 {
287   /// Retrieve the connection from a Berg connector number
288   
289 #ifdef WITH_STL
290  for(ConnectionMapCIterator i = fConnections.begin();
291   i!=fConnections.end();++i)
292    if (i->second->GetBergNum()==bergNum) return i->second;
293  return 0;
294 #endif
295
296 #ifdef WITH_ROOT
297   TExMapIter i = fConnections.GetIterator();
298   Long_t key, value;
299   while ( i.Next(key, value) ) {
300     AliMpConnection* connection = (AliMpConnection*)value;
301     if (connection->GetBergNum()==bergNum) return connection;
302   }  
303   return 0;
304 #endif
305 }
306
307
308 //______________________________________________________________________________
309 AliMpIntPair AliMpMotifType::FindLocalIndicesByConnection(
310                                  const AliMpConnection* connection) const
311 {
312   /// Retrieve the pad position from the connection pointer.
313   /// Not to be used widely, since it use a search in the
314   /// connection list...
315
316 #ifdef WITH_STL
317  for(ConnectionMapCIterator i = fConnections.begin();
318   i!=fConnections.end();++i)
319    if (i->second==connection) return i->first;
320 #endif
321
322 #ifdef WITH_ROOT
323   TExMapIter i = fConnections.GetIterator();
324   Long_t key, value;
325   while ( i.Next(key, value) ) {
326     AliMpConnection* aConnection = (AliMpConnection*)value;
327     if (aConnection == connection) return AliMpExMap::GetPair(key);
328   }  
329 #endif
330
331   return AliMpIntPair::Invalid();
332 }
333
334 //______________________________________________________________________________
335 AliMpIntPair AliMpMotifType::FindLocalIndicesByPadNum(Int_t padNum) const
336 {
337   /// Retrieve the AliMpConnection pointer from its pad num
338   
339 #ifdef WITH_STL
340  for(ConnectionMapCIterator i = fConnections.begin();
341   i!=fConnections.end();++i)
342    if (i->second->GetPadNum()==padNum) return i->first;
343 #endif
344    
345 #ifdef WITH_ROOT
346   TExMapIter i = fConnections.GetIterator();
347   Long_t key, value;
348   while ( i.Next(key, value) ) {
349     AliMpConnection* connection = (AliMpConnection*)value;
350     if (connection->GetPadNum() == padNum) return AliMpExMap::GetPair(key);
351   }  
352 #endif
353  return AliMpIntPair::Invalid();
354 }
355
356 //______________________________________________________________________________
357 AliMpIntPair AliMpMotifType::FindLocalIndicesByGassiNum(Int_t gassiNum) const
358 {
359   /// Return the connection for the given gassiplex number
360   
361 #ifdef WITH_STL
362  for(ConnectionMapCIterator i = fConnections.begin();
363   i!=fConnections.end();++i)
364    if (i->second->GetGassiNum()==gassiNum) return i->first;
365 #endif
366    
367 #ifdef WITH_ROOT
368   TExMapIter i = fConnections.GetIterator();
369   Long_t key, value;
370   while ( i.Next(key, value) ) {
371     AliMpConnection* connection = (AliMpConnection*)value;
372     if (connection->GetGassiNum()==gassiNum) return AliMpExMap::GetPair(key);
373   }  
374 #endif
375    
376  return AliMpIntPair::Invalid();
377 }
378
379 //______________________________________________________________________________
380 AliMpIntPair AliMpMotifType::FindLocalIndicesByKaptonNum(Int_t kaptonNum) const
381 {
382   /// Give the connection related to the given kapton number
383   
384 #ifdef WITH_STL
385  for(ConnectionMapCIterator i = fConnections.begin();
386   i!=fConnections.end();++i)
387    if (i->second->GetKaptonNum()==kaptonNum) return i->first;
388 #endif
389    
390 #ifdef WITH_ROOT
391   TExMapIter i = fConnections.GetIterator();
392   Long_t key, value;
393   while ( i.Next(key, value) ) {
394     AliMpConnection* connection = (AliMpConnection*)value;
395     if (connection->GetKaptonNum()==kaptonNum) return AliMpExMap::GetPair(key);
396   }  
397 #endif
398    
399  return AliMpIntPair::Invalid();
400 }
401
402 //______________________________________________________________________________
403 AliMpIntPair AliMpMotifType::FindLocalIndicesByBergNum(Int_t bergNum) const
404 {
405   /// Retrieve the connection from a Berg connector number
406   
407 #ifdef WITH_STL
408  for(ConnectionMapCIterator i = fConnections.begin();
409   i!=fConnections.end();++i)
410    if (i->second->GetBergNum()==bergNum) return i->first;
411 #endif
412    
413 #ifdef WITH_ROOT
414   TExMapIter i = fConnections.GetIterator();
415   Long_t key, value;
416   while ( i.Next(key, value) ) {
417     AliMpConnection* connection = (AliMpConnection*)value;
418     if (connection->GetBergNum()==bergNum) return AliMpExMap::GetPair(key);
419   }  
420 #endif
421    
422  return AliMpIntPair::Invalid();
423 }
424
425 //______________________________________________________________________________
426 Int_t  AliMpMotifType::GetNofPads() const   
427 {
428 /// Return the number of pads
429
430 #ifdef WITH_STL
431   return fConnections.size();
432 #endif
433    
434 #ifdef WITH_ROOT
435   return fConnections.GetSize();
436 #endif
437 }
438
439 //______________________________________________________________________________
440 Bool_t AliMpMotifType::HasPad(const AliMpIntPair& localIndices) const
441 {
442   /// Return true if the pad indexed by \a localIndices has a connection
443   
444   if (!localIndices.IsValid()) return false;
445
446 #ifdef WITH_STL
447   return fConnections.find(localIndices)!=fConnections.end();
448 #endif
449
450 #ifdef WITH_ROOT
451   TObject* value = fConnections.GetValue(localIndices);
452   return value!=0;
453 #endif
454 }
455
456 //______________________________________________________________________________
457 void AliMpMotifType::Print(Option_t *option) const
458 {
459   /// Print the map of the motif. In each cell, the value
460   /// printed depends of option, as the following:
461   /// - option="N" the "name" of the pad is written
462   /// - option="K" the Kapton connect. number attached to the pad is written
463   /// - option="B" the Berg connect. number attached to the pad is written
464   /// - option="G" the Gassiplex channel number attached to the pad is written
465   /// otherwise the number of the pad is written
466   ///
467   /// NOTE : this method is really not optimized, in case 'N' or '',
468   /// but the Print() this should not be very important in a Print() method
469
470   switch (option[0]){
471   case 'N':cout<<"Name mapping";
472     break;
473   case 'K':cout<<"Kapton mapping";
474     break;
475   case 'B':cout<<"Berg mapping";
476     break;
477   case 'G':cout<<"Gassiplex number mapping";
478     break;
479   default:cout<<"Pad mapping";
480   }
481   cout<<" in the motif "<<fID<<endl;
482   cout<<"-----------------------------------"<<endl;
483
484   for (Int_t j=fNofPadsY-1;j>=0;j--){
485     for (Int_t i=0;i<fNofPadsX;i++){
486       AliMpConnection *connexion = FindConnectionByLocalIndices(AliMpIntPair(i,j));
487       TString str;
488       if (connexion){
489         AliDebug(1,Form("i,j=%2d,%2d connexion=%p",i,j,connexion));
490         
491         switch (option[0]){
492           case 'N':str=PadName(connexion->GetPadNum());
493             break;
494           case 'K':str=Form("%d",connexion->GetKaptonNum());
495             break;
496           case 'B':str=Form("%d",connexion->GetBergNum());
497             break;
498           case 'G':str=Form("%d",connexion->GetGassiNum());
499             break;
500           default:str= Form("%d",connexion->GetPadNum());
501         }
502         cout<<setw(2)<<str;
503       } else cout<<setw(2)<<"--";
504       cout<<" ";
505     }
506     cout<<endl;
507   }
508 }
509
510 //_____________________________________________________________________________
511 Bool_t
512 AliMpMotifType::Save() const
513 {
514   return Save(fID.Data());
515 }
516
517 //_____________________________________________________________________________
518 Bool_t
519 AliMpMotifType::Save(const char* motifName) const
520 {
521   /// Generate the 2 files needed to describe the motif
522   
523   TString padPosFileName(AliMpFiles::PadPosFileName(motifName));
524   
525   TString motifTypeFileName(AliMpFiles::MotifFileName(motifName));
526
527   // first a protection : do not allow overwriting existing files...
528   Bool_t test = gSystem->AccessPathName(padPosFileName.Data());
529   if (test==kFALSE) // AccessPathName has a strange return value convention...
530   {
531     AliError("Cannot overwrite existing padPos file");
532     return kFALSE;
533   }
534   test = gSystem->AccessPathName(motifTypeFileName.Data());
535   if (test==kFALSE)
536   {
537     AliError("Cannot overwrite existing motifType file");
538     return kFALSE;    
539   }
540   
541   ofstream padPosFile(padPosFileName.Data());
542   ofstream motifFile(motifTypeFileName.Data());
543   
544   motifFile <<  "# Motif " << motifName << endl
545     << "#" << endl
546     << "#connecteur_berg kapton padname not_used" << endl
547     << "#for slats there's no kapton connector, so it's always 1" 
548     << " (zero make the reader" << endl
549     << "#abort, so it's not a valid value here)." << endl
550     << "#" << endl;
551   
552   for ( Int_t ix = 0; ix < GetNofPadsX(); ++ix ) 
553   {
554     for ( Int_t iy = 0; iy < GetNofPadsY(); ++iy ) 
555     {
556       AliMpConnection* con = FindConnectionByLocalIndices(AliMpIntPair(ix,iy));
557       if (con)
558       {
559         motifFile << con->GetBergNum() << "\t1\t" << con->GetPadNum() << "\t-" << endl;
560         padPosFile << con->GetPadNum() << "\t" << ix << "\t" << iy << endl;
561       }
562     }
563   }
564   
565   padPosFile.close();
566   motifFile.close();
567   
568   return kTRUE;
569 }
570
571
572