]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/mapping/AliMpHVNamer.cxx
Reverting back the TPC set branch (Marian)
[u/mrichter/AliRoot.git] / MUON / mapping / AliMpHVNamer.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
18 #include "AliMpHVNamer.h"
19
20 #include "AliCodeTimer.h"
21 #include "AliLog.h"
22 #include "AliMpArea.h"
23 #include "AliMpDEIterator.h"
24 #include "AliMpDEManager.h"
25 #include "AliMpHelper.h"
26 #include "AliMpMotifMap.h"
27 #include "AliMpMotifPosition.h"
28 #include "AliMpSector.h"
29 #include "AliMpSectorSegmentation.h"
30 #include "AliMpSegmentation.h"
31 #include "AliMpSlat.h"
32 #include "AliMpSlatSegmentation.h"
33 #include <Riostream.h>
34 #include <TMap.h>
35 #include <TObjArray.h>
36 #include <TObjString.h>
37 #include <TString.h>
38 #include <TSystem.h>
39
40 //-----------------------------------------------------------------------------
41 /// \class AliMpHVNamer
42 /// 
43 /// A utility class to manage HV DCS aliases names, in particular the
44 /// two conventions used to number the detection elements within a detector.
45 ///
46 /// \author: Laurent Aphecetche, Subatech
47 //-----------------------------------------------------------------------------
48
49 /// \cond CLASSIMP
50 ClassImp(AliMpHVNamer)
51 /// \endcond
52
53 const char* AliMpHVNamer::fgHVChannelSt345Pattern[] = 
54 { "MchHvLvLeft/Chamber%02dLeft/Slat%02d.actual.vMon",
55   "MchHvLvRight/Chamber%02dRight/Slat%02d.actual.vMon" 
56 };
57
58 const char* AliMpHVNamer::fgHVChannelSt12Pattern[] = 
59 {
60   "MchHvLvLeft/Chamber%02dLeft/Quad%dSect%d.actual.vMon",
61   "MchHvLvRight/Chamber%02dRight/Quad%dSect%d.actual.vMon",
62 };
63
64 const char* AliMpHVNamer::fgHVSwitchSt345Pattern = "MchDE%04dsw%d.inValue";
65
66 //_____________________________________________________________________________
67 AliMpHVNamer::AliMpHVNamer()
68 {
69  /// default ctor 
70 }
71
72 //_____________________________________________________________________________
73 AliMpHVNamer::~AliMpHVNamer()
74 {
75   /// dtor
76 }
77
78 //_____________________________________________________________________________
79 void 
80 AliMpHVNamer::AliasesAsLdif(const char* ldiffile) const
81 {
82 /// Export the aliases in LDIF format
83
84   ofstream out(ldiffile);
85   
86   TObjArray* a = CompactAliases();
87   
88   TIter next(a);
89   TObjString* s;
90
91   // Some header. host name and port probably not up to date.
92   out << "#MCH config" << endl
93     << "dn: det=MCH,o=alice,dc=cern,dc=ch" << endl
94     << "objectClass: AliShuttleDetector" << endl
95     << "det: MCH" << endl
96     << "StrictRunOrder: 1" << endl
97     << "responsible: aphecetc@in2p3.fr" << endl
98     << "DCSHost: aldcs053.cern.ch" << endl
99     << "DCSPort: 4242" <<endl;
100   
101   while ( ( s = (TObjString*)(next()) ) )
102   {
103     out << "DCSalias: " << s->String().Data() << endl;
104   }
105   
106   out.close();
107   
108   delete a;
109 }
110
111 //_____________________________________________________________________________
112 TObjArray*
113 AliMpHVNamer::CompactAliases() const
114 {
115   /// Generate a compact list of aliases, for Shuttle test
116   /// This one is completely hand-made, in contrast with GenerateAliases()
117   /// method
118
119   TObjArray* a = new TObjArray;
120   a->SetOwner(kTRUE);
121   
122   // St 12 (HV Channels)
123   a->Add(new TObjString("MchHvLvRight/Chamber[00..03]Right/Quad0Sect[0..2].actual.vMon"));
124   a->Add(new TObjString("MchHvLvLeft/Chamber[00..03]Left/Quad1Sect[0..2].actual.vMon"));
125   a->Add(new TObjString("MchHvLvLeft/Chamber[00..03]Left/Quad2Sect[0..2].actual.vMon"));
126   a->Add(new TObjString("MchHvLvRight/Chamber[00..03]Right/Quad3Sect[0..2].actual.vMon"));
127   
128   // St345 (HV Channels)
129   
130   a->Add(new TObjString("MchHvLvRight/Chamber[04..09]Right/Slat[00..08].actual.vMon"));
131   a->Add(new TObjString("MchHvLvLeft/Chamber[04..09]Left/Slat[00..08].actual.vMon"));
132
133   a->Add(new TObjString("MchHvLvRight/Chamber[06..09]Right/Slat[09..12].actual.vMon"));
134   a->Add(new TObjString("MchHvLvLeft/Chamber[06..09]Left/Slat[09..12].actual.vMon"));
135
136   // St345 (HV Switches)
137   AliMpDEIterator it;
138   
139   it.First();
140   
141   while (!it.IsDone())
142   {
143     Int_t detElemId = it.CurrentDEId();
144     if ( AliMpDEManager::GetStationType(detElemId) == AliMp::kStation345 )
145     {
146           a->Add(new TObjString(Form("MchDE%04dsw[0..%d].inValue",detElemId,NumberOfPCBs(detElemId)-1)));
147      }
148     it.Next();
149   }
150   return a;
151 }
152
153 //_____________________________________________________________________________
154 Int_t 
155 AliMpHVNamer::DCS2DE(Int_t chamberId, Int_t side, Int_t dcsNumber) const
156 {
157   /// Convert DCS "slat number" (old convention) to DE (new) convention.
158   ///
159   /// \param chamberId : chamber number (starting at 0)
160   /// \param side : 0 for Left, 1 for Right
161   /// \param dcsNumber : slat number in DCS HV convention
162   ///
163   /// note that dcsNumber should be >=0 and < number of DEs/2 in chamber
164
165   Int_t nofDE = AliMpDEManager::GetNofDEInChamber(chamberId);
166   
167   Int_t half = nofDE/2;
168   
169   dcsNumber = half - dcsNumber;
170   
171   Int_t quarter = nofDE/4;
172   Int_t threeQuarter = half + quarter;
173   
174   Int_t de(-1);
175   
176   if ( side == 0 ) // left
177   {
178     de = threeQuarter + 1 - dcsNumber;
179   }
180   else if ( side == 1 ) // right
181   {
182     if ( dcsNumber <= quarter )
183     {
184       de = dcsNumber + threeQuarter;
185     }
186     else
187     {
188       de = dcsNumber - quarter - 1;
189     }
190   }
191   
192   return (chamberId+1)*100 + de;
193 }
194
195 //_____________________________________________________________________________
196 Int_t
197 AliMpHVNamer::DetElemId2DCS(Int_t detElemId, Int_t& side) const
198 {
199   /// Convert DE to DCS "slat number"
200   /// @see DCS2DE
201   
202   Int_t chamberId = AliMpDEManager::GetChamberId(detElemId);
203   if ( chamberId < 0 ) 
204   {
205     AliDebug(1,Form("DetElemId %d invalid",detElemId));
206     return -1;
207   }
208   Int_t dcsNumber = (detElemId-(chamberId+1)*100);
209
210   switch ( AliMpDEManager::GetStationType(detElemId) )
211   {
212     case AliMp::kStation1:
213     case AliMp::kStation2:
214     {
215       switch (dcsNumber)
216       {
217         case 0:
218         case 3:
219           side = 1; // right
220           break;
221         case 1:
222         case 2:
223           side = 0; // left
224         default:
225           break;
226       }
227     }
228       break;
229     case AliMp::kStation345:
230     {
231       Int_t nofDE = AliMpDEManager::GetNofDEInChamber(chamberId);
232       
233       Int_t quarter = nofDE/4;
234       
235       Int_t half = nofDE/2;
236       
237       Int_t threeQuarter = half + quarter;  
238       
239       side = -1;
240       
241       if ( dcsNumber <= quarter ) 
242       {
243         dcsNumber += quarter + 1 ;
244         side = 1; // right
245       }
246       else if ( dcsNumber <= threeQuarter )
247       {
248         dcsNumber = ( threeQuarter - dcsNumber + 1 );
249         side = 0; // left
250       }
251       else if ( dcsNumber > threeQuarter ) 
252       {
253         dcsNumber = dcsNumber - threeQuarter;
254         side = 1; // right
255       }
256       else
257       {
258         AliFatal("oups");
259       }  
260       // dcs convention change : numbering from top, not from bottom
261       dcsNumber = half-dcsNumber;
262     }
263       break;
264     default:
265       break;
266   }
267   return dcsNumber;
268 }
269
270 //_____________________________________________________________________________
271 const char* 
272 AliMpHVNamer::DCSHVChannelName(Int_t detElemId, Int_t sector) const
273 {
274   /// Return the alias name of the HV Channel for a given HV area 
275   /// \param detElemId 
276   /// \param sector = 0,1 or 2 for St12, and is unused for st345
277   
278   Int_t chamberId = AliMpDEManager::GetChamberId(detElemId);
279   if ( chamberId < 0 ) return 0x0;
280
281   Int_t side(-1);
282   Int_t dcsNumber = DetElemId2DCS(detElemId,side);
283                                   
284   switch (AliMpDEManager::GetStationType(detElemId))
285   {
286     case AliMp::kStation1:
287     case AliMp::kStation2:
288       return Form(fgHVChannelSt12Pattern[side],chamberId,dcsNumber,sector);
289       break;
290     case AliMp::kStation345:
291       return Form(fgHVChannelSt345Pattern[side],chamberId,dcsNumber);
292       break;
293     default:
294       return 0x0;
295       break;
296   }
297 }
298
299 //_____________________________________________________________________________
300 const char* 
301 AliMpHVNamer::DCSHVSwitchName(Int_t detElemId, Int_t pcbNumber) const
302 {
303   /// Return the alias name of the HV Switch for a given PCB 
304   /// within a slat of St345
305   
306   if (AliMpDEManager::GetStationType(detElemId) == AliMp::kStation345)
307   {
308     return Form(fgHVSwitchSt345Pattern,detElemId,pcbNumber);
309   }
310   return 0x0;
311 }
312
313 //_____________________________________________________________________________
314 Int_t 
315 AliMpHVNamer::HVIndexFromDCSAlias(const char* dcsAlias) const
316 {
317   /// Converts the dcs alias to a hv index 
318   ///
319   /// dcsAlias has one of the following 3 forms :
320   ///
321   /// MchHvLv[Left|Right]/Chamber##[Left|Right]/Chamber##[Left|Right]Slat##.actual.vMon
322   ///
323   /// MchHvLv[Left|Right]/Chamber##[Left|Right]/Chamber##[Left|Right]Quad#Sect#.actual.vMon
324   ///
325   /// MchDE####dsw#.inValue
326   
327   TString sDcsAlias(dcsAlias);
328   Int_t de(-1);
329   Int_t sw(-1);
330   
331   int side(-1);
332   
333   if ( sDcsAlias.Contains("Left") )
334   {
335     side = 0;
336   }
337   else if ( sDcsAlias.Contains("Right") )
338   {
339     side = 1;
340   }
341   else
342   {
343     /// it's a switch
344     sscanf(sDcsAlias.Data(),fgHVSwitchSt345Pattern,&de,&sw);
345     return sw;
346   }
347   
348   int n1(-1);
349   int n3(-1);
350   int n4(-1);
351   
352   if ( sDcsAlias.Contains("Quad") )
353   {
354     sscanf(sDcsAlias.Data(),fgHVChannelSt12Pattern[side],&n1,&n3,&n4);    
355     return n4;
356   }
357   
358   return -2;
359 }
360
361 //_____________________________________________________________________________
362 Int_t 
363 AliMpHVNamer::DetElemIdFromDCSAlias(const char* dcsAlias) const
364 {
365   /// Converts the dcs alias to a detection element identifier
366   ///
367   /// dcsAlias has one of the following 2 forms :
368   ///
369   /// MchHvLv[Left|Right]/Chamber##[Left|Right]/Chamber##[Left|Right]Slat##.actual.vMon
370   ///
371   /// MchHvLv[Left|Right]/Chamber##[Left|Right]/Chamber##[Left|Right]Quad#Sect#.actual.vMon
372   
373   AliDebug(1,Form("dcsAlias=%s",dcsAlias));
374   
375   TString sDcsAlias(dcsAlias);
376   
377   int side(-1);
378   
379   if ( sDcsAlias.Contains("Left") )
380   {
381     side = 0;
382   }
383   else if ( sDcsAlias.Contains("Right") )
384   {
385     side = 1;
386   }
387   else
388   {
389     return -2;
390   }
391   
392   int n1(-1);
393   int n3(-1);
394   int n4(-1);
395   int detElemId(-1);
396   
397   if ( sDcsAlias.Contains("Slat") )
398   {
399     sscanf(sDcsAlias.Data(),fgHVChannelSt345Pattern[side],&n1,&n3);
400     detElemId = DCS2DE(n1,side,n3);
401     AliDebug(1,Form("Slat side=%d n1=%d n3=%d de=%d",side,n1,n3,detElemId));
402   }
403   else if ( sDcsAlias.Contains("Quad") )
404   {
405     sscanf(sDcsAlias.Data(),fgHVChannelSt12Pattern[side],&n1,&n3,&n4);    
406     detElemId = 100*(n1+1) + n3;
407     AliDebug(1,Form("Quad side=%d n1=%d n3=%d n4=%d de=%d",side,n1,n3,n4,detElemId));
408   }
409   else
410   {
411     return -3;
412   }
413   
414   if ( !AliMpDEManager::IsValidDetElemId(detElemId)  )
415   {
416     AliError(Form("Invalid aliasName %s",dcsAlias));
417     return -1;
418   }
419   
420   return detElemId;
421 }
422
423 //_____________________________________________________________________________
424 TObjArray*
425 AliMpHVNamer::GenerateAliases() const
426 {
427   /// Generate DCS alias names, for MUON Tracker High Voltage system.
428   ///
429   /// We first generate aliases of HV channels :
430   ///
431   /// St 1 ch  1 : 12 channels
432   ///      ch  2 : 12 channels 
433   /// St 2 ch  3 : 12 channels
434   ///      ch  4 : 12 channels
435   /// St 3 ch  5 : 18 channels
436   ///      ch  6 : 18 channels
437   /// St 4 ch  7 : 26 channels
438   ///      ch  8 : 26 channels
439   /// St 5 ch  9 : 26 channels
440   ///      ch 10 : 26 channels
441   ///
442   /// then aliases of HV switches (only for St345) : 1 switch per PCB.
443   ///
444   /// Returns a TObjArray of TObjString(=alias name)
445   
446   TObjArray* aliases = new TObjArray;
447   aliases->SetOwner(kTRUE);
448   
449   AliMpDEIterator it;
450   
451   it.First();
452   
453   while (!it.IsDone())
454   {
455     Int_t detElemId = it.CurrentDEId();
456     switch ( AliMpDEManager::GetStationType(detElemId) )
457     {
458       case AliMp::kStation1:
459       case AliMp::kStation2:
460         for ( int sector = 0; sector < 3; ++sector)
461         {
462           aliases->Add(new TObjString(DCSHVChannelName(detElemId,sector)));
463         }
464         break;
465       case AliMp::kStation345:
466         aliases->Add(new TObjString(DCSHVChannelName(detElemId)));
467         for ( Int_t i = 0; i < NumberOfPCBs(detElemId); ++i )
468         {
469           aliases->Add(new TObjString(DCSHVSwitchName(detElemId,i)));
470         }
471         break;
472       default:
473         break;
474     }
475     it.Next();
476   }
477   
478   return aliases;
479 }
480
481 //_____________________________________________________________________________
482 Int_t 
483 AliMpHVNamer::ManuId2Index(Int_t detElemId, Int_t manuId) const
484 {
485   /// Convert (de,manu) to hv index, depending on the station
486   
487   AliMp::StationType stationType = AliMpDEManager::GetStationType(detElemId);
488   if ( stationType == AliMp::kStation345 ) 
489   {
490     return ManuId2PCBIndex(detElemId,manuId);
491   }
492   else if ( stationType == AliMp::kStation1 || stationType == AliMp::kStation2 ) 
493   {
494     return ManuId2Sector(detElemId,manuId);
495   }
496   return -1;
497 }
498
499 //_____________________________________________________________________________
500 Int_t 
501 AliMpHVNamer::ManuId2PCBIndex(Int_t detElemId, Int_t manuId) const
502 {
503   /// Returns the index of PCB (within a St345 slat) for a given manu number.
504   /// Returns -1 if (detElemId,manuId) is incorrect
505   
506   AliCodeTimerAuto("")
507   
508   const AliMpSlatSegmentation* seg = static_cast<const AliMpSlatSegmentation*>
509     (AliMpSegmentation::Instance()->GetMpSegmentationByElectronics(detElemId,manuId));
510         if (!seg) return -1;
511         
512   const AliMpSlat* slat = seg->Slat();
513   
514   return slat->FindPCBIndexByMotifPositionID(manuId);
515 }
516
517 //_____________________________________________________________________________
518 Int_t 
519 AliMpHVNamer::ManuId2Sector(Int_t detElemId, Int_t manuId) const
520 {
521   /// Return the HV-sector number (within a St12 quadrant) for a given manu number.
522   
523   AliCodeTimerAuto("")
524   
525   const AliMpSectorSegmentation* seg = static_cast<const AliMpSectorSegmentation*>
526   (AliMpSegmentation::Instance()->GetMpSegmentationByElectronics(detElemId,manuId));
527         if (!seg) return -1;
528         
529   const AliMpSector* sector = seg->GetSector();
530   const AliMpMotifMap* motifMap = sector->GetMotifMap();
531   const AliMpMotifPosition* motifPos = motifMap->FindMotifPosition(manuId);
532
533   TVector2 lowerLeft(motifPos->Position()-motifPos->Dimensions());
534   
535   Double_t x = lowerLeft.X()*10.0; // cm -> mm
536   Int_t isector(-1);
537
538   AliMp::StationType stationType = AliMpDEManager::GetStationType(detElemId);
539   
540   if ( stationType == AliMp::kStation1 ) 
541   {
542     if ( x < -10 ) AliFatal("");
543     
544     if ( x < 291.65 ) isector = 0;
545     else if ( x < 585.65 ) isector = 1;
546     else if ( x < 879.65 ) isector = 2;
547   }
548   else
549   {
550     if ( x < -140 ) AliFatal("");
551     
552     if ( x < 283.75 ) isector = 0;
553     else if ( x < 603.75 ) isector = 1;
554     else if ( x < 1158.75 ) isector = 2;
555   }
556   
557   return isector;
558 }
559
560 //_____________________________________________________________________________
561 Int_t 
562 AliMpHVNamer::NumberOfPCBs(Int_t detElemId) const
563 {
564   /// Returns the number of PCB in a given detection element
565   /// Only works for St345
566   
567   AliMp::StationType stationType = AliMpDEManager::GetStationType(detElemId);
568   if ( stationType != AliMp::kStation345 )
569   {
570     return 0;
571   }
572   else
573   {
574     const AliMpSlatSegmentation* seg = static_cast<const AliMpSlatSegmentation*>
575     (AliMpSegmentation::Instance()->GetMpSegmentation(detElemId,AliMp::kCath0));
576     const AliMpSlat* slat = seg->Slat();
577     return slat->GetSize();
578   }
579 }