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