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