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