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