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