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