]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONTrackerConditionDataMaker.cxx
Fixing (?) the HV display
[u/mrichter/AliRoot.git] / MUON / AliMUONTrackerConditionDataMaker.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 "AliMUONTrackerConditionDataMaker.h"
19
20 ///\class AliMUONTrackerConditionDataMaker
21 ///
22 /// Producer of AliMUONVTrackerData from OCDB or Ascii file condition data
23 ///
24 /// \author Laurent Aphecetche, Subatech
25
26 ///\cond CLASSIMP
27 ClassImp(AliMUONTrackerConditionDataMaker)
28 ///\endcond
29
30 #include "AliCDBManager.h"
31 #include "AliCDBStorage.h"
32 #include "AliDCSValue.h"
33 #include "AliLog.h"
34 #include "AliMpManuIterator.h"
35 #include "AliMUON2DMap.h"
36 #include "AliMUONCalibParamND.h"
37 #include "AliMUONCalibParamNF.h"
38 #include "AliMUONCalibParamNI.h"
39 #include "AliMUONCalibrationData.h"
40 #include "AliMUONDigitCalibrator.h"
41 #include "AliMUONPadStatusMaker.h"
42 #include "AliMUONPadStatusMapMaker.h"
43 #include "AliMUONTrackerData.h"
44 #include "AliMUONTrackerIO.h"
45 #include "AliMpArrayI.h"
46 #include "AliMpConstants.h"
47 #include "AliMpDCSNamer.h"
48 #include "AliMpDDLStore.h"
49 #include "AliMpDEManager.h"
50 #include "AliMpDetElement.h"
51 #include "TClass.h"
52 #include "TMap.h"
53 #include "TObjString.h"
54 #include "Riostream.h"
55 #include "TString.h"
56 #include <sstream>
57
58 //_____________________________________________________________________________
59 AliMUONTrackerConditionDataMaker::AliMUONTrackerConditionDataMaker(Int_t runNumber, const char* ocdbPath, const char* type):
60 AliMUONVTrackerDataMaker(),
61 fData(0x0),
62 fSource(Form("%s-%010d-%s",ocdbPath,runNumber,type))
63 {
64   /// ctor from OCDB
65
66   AliCDBStorage* storage = AliCDBManager::Instance()->GetDefaultStorage();
67         
68         AliCDBManager::Instance()->SetDefaultStorage(ocdbPath);
69
70   Int_t startOfValidity;
71   AliMUONVStore* store = CreateStore(runNumber,ocdbPath,type,startOfValidity);
72   AliInfo(Form("runNumber=%d ocdbPath=%s type=%s startOfValidity=%d store=%p",
73                runNumber,ocdbPath,type,startOfValidity,store));
74   if ( store )
75   {
76     fData = CreateData(type,*store,startOfValidity);
77   }  
78
79   delete store;
80   
81   AliCDBManager::Instance()->SetDefaultStorage(storage);
82 }
83
84 //_____________________________________________________________________________
85 AliMUONTrackerConditionDataMaker::AliMUONTrackerConditionDataMaker(const char* filename, const char* type):
86 AliMUONVTrackerDataMaker(),
87 fData(0x0),
88 fSource(Form("%s-%s",filename,type))
89 {
90   /// ctor from an ASCII file
91   
92   TString sFilename(gSystem->ExpandPathName(filename));
93   
94   std::ifstream in(sFilename.Data());
95   if (in.good()) 
96   {
97     std::ostringstream stream;
98     char line[1024];
99     while ( in.getline(line,1024) )
100     {
101       stream << line << "\n";    
102     }
103   
104     in.close();
105   
106     Int_t dummy;
107     
108     AliMUONVStore* store = CreateStore(-1,stream.str().c_str(),type,dummy);
109     
110     if ( store )
111     {
112       fData = CreateData(type,*store,dummy);
113     }
114     delete store;
115   }
116 }
117
118 //_____________________________________________________________________________
119 AliMUONTrackerConditionDataMaker::AliMUONTrackerConditionDataMaker(const char* data, const char* type, Bool_t) :
120 AliMUONVTrackerDataMaker(),
121 fData(0x0),
122 fSource(Form("direct-%s",type))
123 {
124   /// ctor from a string containing the ASCII data
125   /// the last parameter is there just to distinguish this ctor from the previous one
126   
127   Int_t dummy;
128   
129   AliMUONVStore* store = CreateStore(-1,data,type,dummy);
130   
131   if ( store )
132   {
133     fData = CreateData(type,*store,dummy);
134   }
135   delete store;
136   
137 }
138
139 //_____________________________________________________________________________
140 AliMUONTrackerConditionDataMaker::~AliMUONTrackerConditionDataMaker()
141 {
142   /// dtor
143   delete fData;
144 }
145
146
147 //_____________________________________________________________________________
148 AliMUONVTrackerData*
149 AliMUONTrackerConditionDataMaker::CreateData(const char* type, AliMUONVStore& store, Int_t startOfValidity)
150 {
151   /// Create the data source 
152   AliMUONVTrackerData* data(0x0);
153   
154   TString stype(type);
155   stype.ToUpper();
156   
157   if ( stype == "CAPACITANCES" )
158   {    
159     data = new AliMUONTrackerData(Form("CAPA%d",startOfValidity),"Capacitances",2,kTRUE);
160     data->SetDimensionName(0,"Capa");
161     data->SetDimensionName(1,"Injection gain");    
162   }
163   else if ( stype == "CONFIG" ) 
164   {
165     data = new AliMUONTrackerData(Form("CONFIG%d",startOfValidity),"Configuration",1);
166     data->SetDimensionName(0,"there");
167     data->DisableChannelLevel();
168   }
169   else if ( stype == "GAINS" ) 
170   {
171     data = new AliMUONTrackerData(Form("GAIN%d",startOfValidity),"Gains",7,kTRUE);
172     data->SetDimensionName(0,"gain");
173     data->SetDimensionName(1,"a1");
174     data->SetDimensionName(2,"a2");
175     data->SetDimensionName(3,"thres");
176     data->SetDimensionName(4,"qual1");
177     data->SetDimensionName(5,"qual2");
178     data->SetDimensionName(6,"sat");    
179   }
180   else if ( stype == "HV" ) 
181   {
182     data = new AliMUONTrackerData(Form("HV%d",startOfValidity),"High Voltages",1); //,!isSingleEvent);
183                 data->SetDimensionName(0,"HV");
184   }
185   else if ( stype == "OCCUPANCY" ) 
186   {
187     data = new AliMUONTrackerData(Form("OCC%d",startOfValidity),"OccupancyMap",store);
188     data->SetDimensionName(0,"One");
189     return data; // important to return now to avoid the data->Add(store) later on...
190   }
191   else if ( stype == "PEDESTALS" ) 
192   {
193     data  = new AliMUONTrackerData(Form("PED%d",startOfValidity),"Pedestals",2,kTRUE);
194     data->SetDimensionName(0,"Mean");
195     data->SetDimensionName(1,"Sigma");    
196   }
197   else if ( stype == "STATUS" ) 
198   {
199     data = new AliMUONTrackerData(Form("STATUS%d",startOfValidity),"Status",1,kTRUE);
200     data->SetDimensionName(0,"Bits");
201   }
202   else if ( stype == "STATUSMAP" ) 
203   {
204     data = new AliMUONTrackerData(Form("STATUSMAP%d",startOfValidity),"Status map",2,kTRUE);
205     data->SetDimensionName(0,"Bits");
206     data->SetDimensionName(1,"Dead");
207   }
208
209   if (!data)
210   {
211     AliErrorClass(Form("Could not create data for type=%s",type));
212     return 0x0;
213   }
214   
215   data->Add(store);
216   
217   return data;
218 }
219
220 //_____________________________________________________________________________
221 AliMUONVStore*
222 AliMUONTrackerConditionDataMaker::CreateHVStore(TMap& m)
223 {
224   /// Create a store from hv values
225   
226   AliMUONVStore* store = new AliMUON2DMap(kTRUE);
227   
228   TIter next(&m);
229   TObjString* s;
230   AliMpDCSNamer hvNamer("TRACKER");
231   
232   while ( ( s = static_cast<TObjString*>(next()) ) )
233   {
234     TString name(s->String());
235     
236     Int_t hvIndex = hvNamer.DCSIndexFromDCSAlias(name.Data());
237
238     Int_t detElemId = hvNamer.DetElemIdFromDCSAlias(name.Data());
239     
240     if ( hvIndex >= 0 && detElemId < 0 )
241     {
242       // skip switches
243       continue;      
244     }
245     
246     if ( !AliMpDEManager::IsValidDetElemId(detElemId) )
247     {
248       AliErrorClass(Form("Got an invalid DE = %d from alias = %s",
249                          detElemId,name.Data()));
250       continue;
251     }
252
253     if ( hvIndex > 1 && AliMpDEManager::GetStationType(detElemId) == AliMp::kStation12 )
254     {
255       // skip all but first sector (as we'll loop on the 3 sectors below
256       continue;      
257     }
258     
259     Int_t nPCBs = hvNamer.NumberOfPCBs(detElemId);
260     Int_t nindex = nPCBs ? nPCBs : 3;
261     
262     AliMpDetElement* de = AliMpDDLStore::Instance()->GetDetElement(detElemId);
263     
264     for ( int i = 0 ; i < nindex; ++i )
265     {
266       Float_t switchValue(1.0);
267       
268       if ( nPCBs ) 
269       {
270         TString switchName(hvNamer.DCSSwitchName(detElemId,i));
271
272         TPair* p = static_cast<TPair*>(m.FindObject(switchName.Data()));
273         TObjArray* a = static_cast<TObjArray*>(p->Value());
274         
275         switchValue = AliMUONPadStatusMaker::SwitchValue(*a);                                           
276       }
277       
278       const AliMpArrayI* manus = de->ManusForHV(i);
279       
280       if (!manus) continue;
281       
282       TPair* p = static_cast<TPair*>(m.FindObject(name.Data()));
283       TObjArray* a = static_cast<TObjArray*>(p->Value());
284       TIter n2(a);
285       AliDCSValue* v;
286       Float_t hvValue(0);
287       Int_t n(0);
288       while ( ( v = static_cast<AliDCSValue*>(n2()) ) )
289       {
290         hvValue += v->GetFloat();
291         ++n;
292       }
293       hvValue *= switchValue;  
294       
295       if ( n ) hvValue /= n;
296       
297       Int_t nofChannels(AliMpConstants::ManuNofChannels());
298       
299       for ( Int_t k = 0 ; k < manus->GetSize(); ++k )
300       {
301         Int_t manuId = manus->GetValue(k);
302         AliMUONVCalibParam* param = static_cast<AliMUONVCalibParam*>(store->FindObject(detElemId,manuId));
303         if ( ! param ) 
304         {
305           param = new AliMUONCalibParamND(1,nofChannels,detElemId,manuId,0);
306           store->Add(param);
307         }
308         for ( Int_t j = 0 ; j < nofChannels; ++j )
309         {
310           param->SetValueAsDouble(j,0,hvValue);
311         }
312       }
313     }
314   }
315   
316   return store;
317   
318 }
319
320 //_____________________________________________________________________________
321 AliMUONVStore*
322 AliMUONTrackerConditionDataMaker::CreateStatusStore(Int_t runNumber)
323 {
324   /// Get the status store
325   
326   AliMUONDigitCalibrator calibrator(runNumber);
327   
328   AliMUONVStore* sm = new AliMUON2DMap(kTRUE);
329   
330   AliMpManuIterator it;
331   Int_t detElemId, manuId;
332   
333   while (it.Next(detElemId,manuId))
334   {
335     AliMUONVCalibParam* np = new AliMUONCalibParamNI(1,AliMpConstants::ManuNofChannels(),detElemId,manuId);
336     for ( Int_t i = 0; i < np->Size(); ++i ) 
337     {
338       Int_t value = calibrator.PadStatus(detElemId,manuId,i);
339       np->SetValueAsInt(i,0,value); // "raw" value of the status
340     }
341     sm->Add(np);
342   }
343   
344   return sm;
345 }
346
347 //_____________________________________________________________________________
348 AliMUONVStore*
349 AliMUONTrackerConditionDataMaker::CreateStatusMapStore(Int_t runNumber)
350 {
351   /// Get the status map, and polish it a bit for representation purposes
352   
353   AliMUONDigitCalibrator calibrator(runNumber);
354   
355   AliMUONVStore* sm = new AliMUON2DMap(kTRUE);
356   
357   AliMpManuIterator it;
358   Int_t detElemId, manuId;
359   
360   while (it.Next(detElemId,manuId))
361   {
362     AliMUONVCalibParam* np = new AliMUONCalibParamNI(2,AliMpConstants::ManuNofChannels(),detElemId,manuId);
363     for ( Int_t i = 0; i < np->Size(); ++i ) 
364     {
365       Int_t value = calibrator.StatusMap(detElemId,manuId,i);
366       Int_t channelIsDead = ( value & AliMUONPadStatusMapMaker::SelfDeadMask() );
367       np->SetValueAsInt(i,0,value); // "raw" value of the status map
368       np->SetValueAsInt(i,1,channelIsDead); // simple 0 or 1 for this channel
369     }
370     sm->Add(np);
371   }
372   
373   return sm;
374 }
375
376 //_____________________________________________________________________________
377 AliMUONVStore*
378 AliMUONTrackerConditionDataMaker::CreateStore(Int_t runNumber, 
379                                               const char* source, 
380                                               const char* type, 
381                                               Int_t& startOfValidity)
382 {
383   /// Create the store by reading it from OCDB or from an ASCII file
384   
385   TString stype(type);
386   stype.ToUpper();
387   
388   AliMUONVStore* store(0x0);
389   
390   startOfValidity = 0;
391   
392   Bool_t ocdb = (runNumber>=0);
393   
394   if ( stype == "CAPACITANCES" )
395   {    
396     if ( ocdb ) 
397     {
398       store = AliMUONCalibrationData::CreateCapacitances(runNumber,&startOfValidity);    
399     }
400     else
401     {
402       store = new AliMUON2DMap(20000);
403       AliMUONTrackerIO::DecodeCapacitances(source,*store);
404     }
405   }
406   else if ( stype == "CONFIG" ) 
407   {
408     AliMUONVStore* tmp(0x0);
409     if ( ocdb ) 
410     {
411       tmp = AliMUONCalibrationData::CreateConfig(runNumber,&startOfValidity);
412     }
413     else
414     {
415       tmp = new AliMUON2DMap(kTRUE);
416       Bool_t changed(kFALSE);
417       AliMUONTrackerIO::DecodeConfig(source,*tmp,changed);
418     }
419     if ( tmp ) 
420     {
421       store = ExpandConfig(*tmp);      
422     }
423     delete tmp;
424   }
425   else if ( stype == "GAINS" ) 
426   {
427     AliMUONVStore* gains(0x0);
428     if ( ocdb ) 
429     {
430       gains = AliMUONCalibrationData::CreateGains(runNumber,&startOfValidity);
431     }
432     else
433     {
434       gains = new AliMUON2DMap(kTRUE);
435       TString comment;
436       AliMUONTrackerIO::DecodeGains(source,*gains,comment);
437     }
438     store = PatchGainStore(*gains);
439     delete gains;
440   }
441   else if ( stype == "OCCUPANCY" ) 
442   {
443     if ( ocdb ) 
444     {
445       store = AliMUONCalibrationData::CreateOccupancyMap(runNumber,&startOfValidity);
446     }
447     else
448     {
449       store = new AliMUON2DMap(kTRUE);
450       AliMUONTrackerIO::DecodeOccupancy(source,*store);
451     }
452   }
453   else if ( stype == "PEDESTALS" ) 
454   {
455     if ( ocdb ) 
456     {
457       store = AliMUONCalibrationData::CreatePedestals(runNumber,&startOfValidity);
458     }
459     else
460     {
461       store = new AliMUON2DMap(kTRUE);
462       AliMUONTrackerIO::DecodePedestals(source,*store);
463     }
464   }
465   
466   /// Below are source that can only be accessed from OCDB
467   if (!store && !ocdb) 
468   {
469     return 0x0;
470   }
471   
472   if ( stype == "HV" ) 
473   {
474     TMap* m = AliMUONCalibrationData::CreateHV(runNumber,&startOfValidity);
475                 store = CreateHVStore(*m);
476     delete m;
477   }
478   else if ( stype == "STATUS" ) 
479   {
480     store = CreateStatusStore(runNumber);
481   }
482   else if ( stype == "STATUSMAP" ) 
483   {
484     store = CreateStatusMapStore(runNumber);
485   }
486   
487   return store;
488 }
489
490 //_____________________________________________________________________________
491 AliMUONVStore*
492 AliMUONTrackerConditionDataMaker::ExpandConfig(const AliMUONVStore& manuConfig)
493 {
494   /// Convert the config from manu level to channel level (just to
495   /// be able to add it correctly to the trackerdata...)
496   
497   AliMUONVStore* store = manuConfig.Create();
498   
499   TIter next(manuConfig.CreateIterator());
500   AliMUONVCalibParam* p;
501   
502   while ( ( p = static_cast<AliMUONVCalibParam*>(next()) ) )
503   {
504     AliMUONVCalibParam* c = new AliMUONCalibParamNF(1,AliMpConstants::ManuNofChannels(),p->ID0(),p->ID1(),0.0);
505     
506     AliMpDetElement* de = AliMpDDLStore::Instance()->GetDetElement(p->ID0());
507     
508     for ( Int_t i = 0; i < c->Size(); ++i ) 
509     {
510       if ( de->IsExistingChannel(p->ID1(),i) )
511       {
512         c->SetValueAsFloat(i,0,1.0);        
513       }
514     }
515     
516     store->Add(c);
517   }
518   return store;
519 }
520
521 //_____________________________________________________________________________
522 Long64_t 
523 AliMUONTrackerConditionDataMaker::Merge(TCollection*)
524 {
525   /// Merge
526   AliError("Not implemented. Does it have sense ?");
527   return 0;
528 }
529
530 //_____________________________________________________________________________
531 AliMUONVStore*
532 AliMUONTrackerConditionDataMaker::PatchGainStore(const AliMUONVStore& gains)
533 {
534   /// Polish the gain store : 
535   /// a) adding a dimension, computed from a1, and called gain = 1/a1/0.2 
536   ///     where 0.2 is internal capa in pF, and gain is then in mV/fC
537   /// b) splitting the quality in two
538   
539   AliMUONVStore* store = gains.Create();
540   
541   TIter next(gains.CreateIterator());
542   AliMUONVCalibParam* param;
543   
544   while ( ( param = static_cast<AliMUONVCalibParam*>(next()) ) ) 
545   {
546     AliMUONVCalibParam* nd = new AliMUONCalibParamND(param->Dimension()+2,
547                                                      param->Size(),
548                                                      param->ID0(),
549                                                      param->ID1());
550     for ( Int_t i = 0; i < param->Size(); ++i ) 
551     {
552       
553       Int_t qual = param->ValueAsInt(i,3);
554                         Int_t q1 = (qual & 0xF0) >> 4;  // linear fit quality
555                         Int_t q2 = qual & 0xF;          // parabolic fit quality
556                         Double_t gain = 0.0;
557       
558       if ( param->ValueAsFloat(i,0) > 1E-9 ) gain = 1.0/param->ValueAsFloat(i,0)/0.2;
559                         
560       nd->SetValueAsDouble(i,0,gain); // gain
561       nd->SetValueAsDouble(i,1,param->ValueAsFloat(i,0)); // a1
562       nd->SetValueAsDouble(i,2,param->ValueAsFloat(i,1)); // a2
563       nd->SetValueAsInt(i,3,param->ValueAsInt(i,2)); // thres
564       nd->SetValueAsInt(i,4,q1); // qual1
565       nd->SetValueAsInt(i,5,q2); // qual2
566       nd->SetValueAsInt(i,6,param->ValueAsInt(i,4)); // sat
567     }
568     store->Add(nd);
569   }
570   
571   return store;
572 }
573