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