]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONCalibrationData.cxx
Coverity 19233
[u/mrichter/AliRoot.git] / MUON / AliMUONCalibrationData.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 "AliMUONCalibrationData.h"
19
20 #include "AliCDBEntry.h"
21 #include "AliCDBManager.h"
22 #include "AliCodeTimer.h"
23 #include "AliDCSValue.h"
24 #include "AliLog.h"
25 #include "AliMpDCSNamer.h"
26 #include "AliMpIntPair.h"
27 #include "AliMUONGlobalCrateConfig.h"
28 #include "AliMUONRegionalTriggerConfig.h"
29 #include "AliMUONRejectList.h"
30 #include "AliMUONTriggerEfficiencyCells.h"
31 #include "AliMUONTriggerLut.h"
32 #include "AliMUONVCalibParam.h"
33 #include "AliMUONVStore.h"
34 #include "AliMUONVStore.h"
35
36 #include <Riostream.h>
37 #include <TClass.h>
38 #include <TMap.h>
39 #include <TMath.h>
40
41 //-----------------------------------------------------------------------------
42 /// \class AliMUONCalibrationData
43 ///
44 /// For the moment, this class stores pedestals, gains, hv (for tracker)
45 /// and lut, masks and efficiencies (for trigger) that are fetched from the CDB.
46 ///
47 /// This class is to be considered as a convenience class.
48 /// Its aim is to ease retrieval of calibration data from the 
49 /// condition database.
50 ///
51 /// It acts as a "facade" to a bunch of underlying 
52 /// containers/calibration classes.
53 ///
54 /// \author Laurent Aphecetche
55 //-----------------------------------------------------------------------------
56
57 /// \cond CLASSIMP
58 ClassImp(AliMUONCalibrationData)
59 /// \endcond
60
61 AliMUONVStore* AliMUONCalibrationData::fgBypassPedestals(0x0);
62 AliMUONVStore* AliMUONCalibrationData::fgBypassGains(0x0);
63
64 namespace  
65 {
66   void MarkForDeletion(Int_t* indices, Int_t first, Int_t last)
67   {
68     for ( Int_t i = first; i <= last; ++i ) 
69     {
70       indices[i] = 1;
71     }
72   }
73 }
74
75 //_____________________________________________________________________________
76 AliMUONCalibrationData::AliMUONCalibrationData(Int_t runNumber, 
77                                                Bool_t deferredInitialization) 
78 : TObject(), 
79 fIsValid(kTRUE),
80 fRunNumber(runNumber), 
81 fGains(0x0), 
82 fPedestals(0x0),
83 fHV(0x0),
84 fTriggerDCS(0x0),
85 fLocalTriggerBoardMasks(0x0),
86 fRegionalTriggerConfig(0x0),
87 fGlobalTriggerCrateConfig(0x0),
88 fTriggerLut(0x0),
89 fTriggerEfficiency(0x0),
90 fCapacitances(0x0),
91 fNeighbours(0x0),
92 fOccupancyMap(0x0),
93 fRejectList(0x0),
94 fConfig(0x0)
95 {
96 /// Default ctor.
97
98   // If deferredInitialization is false, we read *all* calibrations
99   // at once.
100   // So when using this class to access only one kind of calibrations (e.g.
101   // only pedestals), you should put deferredInitialization to kTRUE, which
102   // will instruct this object to fetch the data only when neeeded.
103
104   if ( deferredInitialization == kFALSE )
105   {
106     Gains();
107     Pedestals();
108     OccupancyMap();
109     RejectList();
110     HV();
111     TriggerDCS();
112     LocalTriggerBoardMasks(0);
113     RegionalTriggerConfig();
114     GlobalTriggerCrateConfig();
115     TriggerLut();
116     TriggerEfficiency();
117     Capacitances();
118     Neighbours();
119     Config();
120   }
121 }
122
123 //_____________________________________________________________________________
124 AliMUONCalibrationData::~AliMUONCalibrationData()
125 {
126   /// Destructor. Note that we're the owner of our pointers if the OCDB cache
127   /// is not set. Otherwise the cache is supposed to take care of them...
128   if (!(AliCDBManager::Instance()->GetCacheFlag())) Reset();
129 }
130
131 //_____________________________________________________________________________
132 AliMUONVStore*
133 AliMUONCalibrationData::Capacitances() const
134 {
135   /// Create (if needed) and return the internal store for capacitances.
136   
137   if (!fCapacitances)
138   {
139     fCapacitances = CreateCapacitances(fRunNumber);
140   }
141   return fCapacitances;
142 }
143
144 //_____________________________________________________________________________
145 AliMUONVStore*
146 AliMUONCalibrationData::CreateCapacitances(Int_t runNumber, Int_t* startOfValidity)
147 {
148   /// Create capa store from OCDB for a given run
149   
150   return dynamic_cast<AliMUONVStore*>(CreateObject(runNumber,"MUON/Calib/Capacitances",startOfValidity));
151 }
152
153 //_____________________________________________________________________________
154 AliMUONVStore*
155 AliMUONCalibrationData::CreateGains(Int_t runNumber, Int_t* startOfValidity)
156 {
157   /// Create a new gain store from the OCDB for a given run
158   return dynamic_cast<AliMUONVStore*>(CreateObject(runNumber,"MUON/Calib/Gains",startOfValidity));
159 }
160
161 //_____________________________________________________________________________
162 AliMUONGlobalCrateConfig*
163 AliMUONCalibrationData::CreateGlobalTriggerCrateConfig(Int_t runNumber, Int_t* startOfValidity)
164 {
165   /// Create the internal store for GlobalTriggerCrateConfig from OCDB
166   
167   return dynamic_cast<AliMUONGlobalCrateConfig*>(CreateObject(runNumber,"MUON/Calib/GlobalTriggerCrateConfig",startOfValidity));
168 }
169
170
171 //______________________________________________________________________________
172 Bool_t AliMUONCalibrationData::CheckHVGroup(TObjArray& values, Int_t first, Int_t last, Double_t& value, Int_t& slope, TString* msg)
173 {
174   // Get the HV of the values between first and last indices
175   // return the HV slope  (in Volt per second) and a message
176   // Return kFALSE if we must discard the group
177   //
178   
179   if (msg) *msg="";
180   
181   if ( last < first ) return kFALSE;
182   if ( last - first < 2 ) return kFALSE;
183   
184   Double_t a(0.0);
185   Double_t b(0.0);
186
187   Float_t HVSAME(1); // 1 volts
188
189   AliDCSValue* vfirst = static_cast<AliDCSValue*>(values.UncheckedAt(first));
190   AliDCSValue* vlast = static_cast<AliDCSValue*>(values.UncheckedAt(last));
191
192   Int_t deltaHV = TMath::Nint(TMath::Abs(vfirst->GetFloat()-vlast->GetFloat()));
193   
194   if ( deltaHV < HVSAME ) return kFALSE;
195
196   for ( Int_t i = first; i <= last; ++i )
197   {
198     AliDCSValue* v = static_cast<AliDCSValue*>(values.UncheckedAt(i));
199
200     Double_t y = v->GetFloat() - vfirst->GetFloat();
201     Double_t x = v->GetTimeStamp() - vfirst->GetTimeStamp();
202   
203     a += x*y;
204     b += x*x;
205   }
206   
207   value = a/b;
208   slope = value > 0 ? 1 : -1;
209   value = TMath::Abs(value);
210   
211   UInt_t deltaTime = vlast->GetTimeStamp() - vfirst->GetTimeStamp();
212   
213   if (msg)
214   {
215     if (slope>0) (*msg) = Form("RU%d[%d:%d](%d)",TMath::Nint(value),first,last,deltaTime);
216     if (slope<0) (*msg) = Form("RD%d[%d:%d](%d)",TMath::Nint(value),first,last,deltaTime);
217     
218     if ( TMath::Nint(value) == 0 )
219     {
220       // this is to protect for the few cases 
221       // (see e.g. MchHvLvLeft/Chamber00Left/Quad2Sect0.actual.vMon in run 134497)
222       // where we can have *lots* of values (2483 in this example) but that
223       // are more or less constant...
224       //
225       // or simply to remove small ramps
226       //
227       slope = 0;
228       value = (vfirst->GetFloat()+vlast->GetFloat())/2.0;
229       *msg = Form("FLUCT%d[%d:%d]",TMath::Nint(value),first,last);
230     }
231   }
232   
233   return kTRUE;
234 }
235
236 //______________________________________________________________________________
237 Bool_t AliMUONCalibrationData::PatchHVValues(TObjArray& values,
238                                              TString* msg)
239 {
240   /// We do here a little bit of massaging of the HV values, if needed.
241   ///
242   /// The main point is to "gather" values that are within a given small amount
243   /// of time (typically 60 seconds) and infer a slope from those values
244   /// slope > 0 means it is a ramp-up, slope < 0 that's a ramp-down
245   ///
246   /// This is to avoid both the "ramp-down-before-end-of-run" and the
247   /// "ramp-up-after-start-of-run" syndroms...
248   ///
249   /// Return kFALSE is the kind of HV (trouble) case we have here
250   /// has not been identified...
251   ///
252   
253   UInt_t DELTATIME(60); // in seconds
254   Int_t IENDRU(60); // in seconds
255   
256   // Start by finding groups of values which are not separated (each) by more than
257   // deltaTime
258   
259   Bool_t gather(kFALSE);
260   Int_t ifirst(0);
261   Int_t ilast(0);
262   TObjArray groups;
263   groups.SetOwner(kTRUE);
264   
265   for ( Int_t i = values.GetLast(); i > 0; --i ) 
266   {
267     AliDCSValue* vi = static_cast<AliDCSValue*>(values.UncheckedAt(i));
268     AliDCSValue* vj = static_cast<AliDCSValue*>(values.UncheckedAt(i-1));
269
270     if ( vi->GetTimeStamp() - vj->GetTimeStamp() < DELTATIME )
271     {
272       if ( !gather ) 
273       {
274         gather = kTRUE;
275         ifirst = i;    
276       }
277       ilast=i;
278     }
279     else
280     {
281       if ( gather ) 
282       {
283         ilast=i;
284         
285         groups.Add(new AliMpIntPair(ilast,ifirst));
286       }
287       gather = kFALSE;
288     }
289   }
290   
291   if (gather)
292   {
293     groups.Add(new AliMpIntPair(0,ifirst));
294   }
295                  
296   TIter nextGroup(&groups,kIterBackward);
297   AliMpIntPair* p;
298   TString internalMsg;
299   Int_t ngroups(0);
300
301   Int_t nRU(0);
302   Int_t nRD(0);
303   Int_t nStartRU(0);
304   Int_t nEndAndShortRU(0);
305   Int_t nEndRD(0);
306   Int_t nTripRD(0);
307   Int_t nFluct(0);
308   
309   while ( ( p = static_cast<AliMpIntPair*>(nextGroup()) ) )
310   {
311     Double_t value;
312     Int_t slope;
313     
314     TString groupMsg;
315     
316     AliDebugClass(1,Form("group %d:%d",p->GetFirst(),p->GetSecond()));
317     
318     Bool_t ok = CheckHVGroup(values,p->GetFirst(),p->GetSecond(),value,slope,&groupMsg);
319     
320     if (!ok) continue;
321     
322     ++ngroups;
323     
324     if ( slope > 0 )
325     {
326       if ( p->GetFirst() == 0 ) 
327       {
328         // start with a ramp-up
329         ++nStartRU;
330       }
331       else if ( p->GetSecond() == values.GetLast() && TMath::Nint(value) < IENDRU )
332       {
333         ++nEndAndShortRU;
334       }
335       else
336       {
337         // ramp-up in the middle of nowhere...
338         ++nRU;
339       }
340     }
341     else if ( slope < 0 )
342     {
343       if ( p->GetSecond() == values.GetLast() ) 
344       {
345         // end with a ramp-down
346         ++nEndRD;
347       }
348       else
349       {
350         // ramp-down in the middle of nowhere
351         ++nRD;
352       }
353
354       AliDCSValue* d = static_cast<AliDCSValue*>(values.At(p->GetSecond()));
355       
356       if ( d->GetFloat() < AliMpDCSNamer::TrackerHVOFF() )
357       {
358         ++nTripRD;
359       }
360     }
361     else
362     {
363       ++nFluct;
364     }
365     
366     internalMsg += groupMsg;
367     internalMsg += " ";    
368   }
369   
370   /*
371    
372    Once we have "decoded" the groups we try to find out which of 
373    the following cases we're facing :
374   
375    case A = -------- = OK(1)
376   
377    case B = ----
378                 \
379                 \   = OK, once we have removed the ramp-down (2)
380   
381    case C =    ----- 
382               /
383              /       = OK, once we have removed the ramp-up (3)
384   
385    case D =    ----- 
386               /     \
387              /       \ = OK, once we have removed the ramp-down (2) and the ramp-up (3)
388   
389    case E = ----
390                 \
391                  \____ = TRIP = BAD (here the ramp-down slope should be bigger than in case C)
392   
393    case F = ----         
394                 \      ----- = BAD (trip + ramp-up at end of run)
395                  \____/   
396   
397    case G = fluctuations (within a range defined in CheckHVGroup...)
398    
399    case H =            
400                    /
401                   /   = ramp-up right at the end-of-run = OK (4)
402             ------
403    
404    (1) OK means the group is identified correctly, still the value can be below ready...
405    (2) ramp-down values will be removed if the ramp is indeed the last values in the serie
406        i.e. it's really an end-of-run problem (otherwise it's not case B)
407    (3) ramp-up values will be removed if the ramp is indeed the first values in the serie
408        i.e. it's really a start-of-run problem (otherwise it's not case C)
409    (4) OK if short enough...
410    
411    Any other case is unknown and we'll :
412    a) return kFALSE
413    b) assume the channel is OFF.
414   
415   
416   */
417   
418   AliDebugClass(1,Form("msg=%s ngroupds=%d",internalMsg.Data(),ngroups));
419   AliDebugClass(1,Form("nRU %d nRD %d nStartRU %d nEndRD %d nTripRD %d nFluct %d",
420                        nRU,nRD,nStartRU,nEndRD,nTripRD,nFluct));
421   
422   TString hvCase("OTHER");
423   int dummy(0),a(-1),b(-1);
424   char r[81];
425   Int_t nvalues = values.GetSize();  
426   Int_t* indices = new Int_t[nvalues];
427   memset(indices,0,nvalues*sizeof(Int_t));
428          
429   AliDCSValue* vfirst = static_cast<AliDCSValue*>(values.UncheckedAt(0));
430   AliDCSValue* vlast = static_cast<AliDCSValue*>(values.UncheckedAt(values.GetLast()));
431
432   UInt_t meanTimeStamp = ( vfirst->GetTimeStamp() + vlast->GetTimeStamp() ) / 2;
433   
434   if ( ngroups == 0 ) 
435   {
436     hvCase = "A"; 
437   }
438   else if ( nTripRD > 0 )
439   {
440     if ( nRU > 0 && nRD > 0 )
441     {
442       hvCase = "F";
443     }
444     else
445     {
446       hvCase = "E";
447     }
448     internalMsg += "TRIP ";
449     MarkForDeletion(indices,0,values.GetLast());
450     values.Add(new AliDCSValue(static_cast<Float_t>(0),meanTimeStamp));
451   }
452   else if ( nStartRU > 0 && nRU == 0 && nRD == 0 && nEndRD == 0 )
453   {
454     hvCase = "C";
455     sscanf(internalMsg.Data(),"RU%10d[%10d:%10d]%80s",&dummy,&a,&b,r);
456     MarkForDeletion(indices,a,b);
457   }
458   else if ( nStartRU > 0 && nEndRD > 0 && nRD == 0 && nRU == 0 )
459   {
460     hvCase = "D";
461     sscanf(internalMsg.Data(),"RU%10d[%10d:%10d]%80s",&dummy,&a,&b,r);    
462     MarkForDeletion(indices,a,b-1);
463     Int_t i = internalMsg.Index("RD",strlen("RD"),0,TString::kExact);
464     sscanf(internalMsg(i,internalMsg.Length()-i).Data(),
465            "RD%10d[%10d:%10d]%80s",&dummy,&a,&b,r);    
466     MarkForDeletion(indices,a+1,b);
467   }
468   else if ( nEndRD > 0 && nStartRU == 0 && nRU == 0 && nRD == 0 )
469   {
470     hvCase = "B";
471     Int_t i = internalMsg.Index("RD",strlen("RD"),0,TString::kExact);
472     sscanf(internalMsg(i,internalMsg.Length()-i).Data(),
473            "RD%10d[%10d:%10d]%80s",&dummy,&a,&b,r);    
474     MarkForDeletion(indices,a,b);
475   }
476   else if ( nFluct > 0 )
477   {
478     hvCase = "G";
479     TObjArray* af = internalMsg.Tokenize(" ");
480     TIter next(af);
481     TObjString* str;
482     while ( ( str = static_cast<TObjString*>(next()) ) )
483     {
484       TString s(str->String());
485       if ( s.BeginsWith("FLUCT") )
486       {
487         sscanf(s.Data(),"FLUCT%d[%d:%d]",&dummy,&a,&b);
488         MarkForDeletion(indices,a,b);
489       }
490     }
491     delete af;
492   }
493   else if ( nEndAndShortRU > 0 && nStartRU == 0 && nRU == 0 && nRD == 0 && nEndRD == 0 )
494   {
495     hvCase = "H";
496     sscanf(internalMsg.Data(),"RU%10d[%10d:%10d]%80s",&dummy,&a,&b,r);
497     MarkForDeletion(indices,a,b);
498   }
499   else
500   {
501     // last chance... 
502     // here we know it's not a trip, so let's assume everything is OK
503     // if first and last value are in the same ballpark
504
505     const Double_t HVFLUCT(20); // volts
506     
507     if ( TMath::Abs(vfirst->GetFloat() - vlast->GetFloat()) < HVFLUCT )
508     {
509       hvCase = "Z";
510     }
511     MarkForDeletion(indices,1,nvalues-1);
512   }
513   
514   for ( Int_t i = 0; i < nvalues; ++i ) 
515   {
516     if ( indices[i] )
517     {
518       values.RemoveAt(i);
519     }
520   }
521   
522   values.Compress();
523
524   delete[] indices;
525   
526   if ( !values.GetEntries() )
527   {
528     AliErrorClass(Form("No value left after patch... Check that !!! initial # of values=%d msg=%s",
529                        nvalues,internalMsg.Data()));
530     hvCase = "OTHER";
531   }
532
533   // take the max of the remaining values
534   TIter nextA(&values);
535   AliDCSValue* val;
536   Float_t maxval(-9999);
537   
538   while ( ( val = static_cast<AliDCSValue*>(nextA()) ) )
539   {
540     if ( val->GetFloat() > maxval )
541     {
542       maxval = val->GetFloat();
543     }
544   }
545   
546   values.Clear();
547   
548   values.Add(new AliDCSValue(maxval,meanTimeStamp));
549   
550   // once the case is inferred, add a "CASE:%10d",hvCase.Data()
551   // to the msg
552   // so we can them sum up for all channels and get a summary per run...
553   
554   internalMsg += Form("CASE:%s",hvCase.Data());
555  
556   if (msg) *msg = internalMsg.Data();
557   
558   return hvCase=="OTHER" ? kFALSE : kTRUE;
559 }
560
561 //_____________________________________________________________________________
562 TMap*
563 AliMUONCalibrationData::CreateHV(Int_t runNumber, 
564                                  Int_t* startOfValidity, 
565                                  Bool_t patched,
566                                  TList* messages)
567 {
568   /// Create a new HV map from the OCDB for a given run
569   TMap* hvMap = dynamic_cast<TMap*>(CreateObject(runNumber,"MUON/Calib/HV",startOfValidity));
570
571   if (!hvMap) return 0x0;
572   
573   if (patched)
574   {
575     TIter next(hvMap);
576     TObjString* hvChannelName;
577     
578     while ( ( hvChannelName = static_cast<TObjString*>(next()) ) )
579     {
580       TString name(hvChannelName->String());
581       
582       if ( name.Contains("sw") ) continue; // skip switches
583       
584       TPair* hvPair = static_cast<TPair*>(hvMap->FindObject(name.Data()));
585       TObjArray* values = static_cast<TObjArray*>(hvPair->Value());
586       if (!values)
587       {
588         AliErrorClass(Form("Could not get values for alias %s",name.Data()));
589       }
590       else
591       {
592         TString msg;
593         
594         AliDebugClass(1,Form("channel %s",name.Data()));
595         Bool_t ok = PatchHVValues(*values,&msg);
596         
597         if ( messages ) 
598         {
599           messages->Add(new TObjString(Form("%s:%s",hvChannelName->String().Data(),msg.Data())));
600         }
601         
602         if (!ok)
603         {
604           AliErrorClass(Form("PatchHVValue was not successfull ! This is serious ! "
605                              "You'll have to check the logic for channel %s in run %09d",
606                              name.Data(),runNumber));
607         }
608       }
609     }
610     
611   }
612   
613   if ( messages ) 
614   {
615     Int_t a(0),b(0),c(0),d(0),e(0),f(0),g(0),h(0),u(0),z(0);
616     TIter next(messages);
617     TObjString* msg;
618     char hvCase('u');
619     
620     while ( ( msg = static_cast<TObjString*>(next()) ) )
621     {
622       Int_t i = msg->String().Index("CASE",strlen("CASE"),0,TString::kExact);
623       
624       if ( i >= 0 )
625       {
626         sscanf(msg->String()(i,msg->String().Length()-i).Data(),"CASE:%10c",&hvCase);
627       }
628
629       switch (hvCase)
630       {
631         case 'A': ++a; break;
632         case 'B': ++b; break;
633         case 'C': ++c; break;
634         case 'D': ++d; break;
635         case 'E': ++e; break;
636         case 'F': ++f; break;
637         case 'G': ++g; break;
638         case 'H': ++h; break;
639         case 'Z': ++z; break;
640         default: ++u; break;
641       }
642     }
643     
644     messages->Add(new TObjString(Form("SUMMARY : # of cases A(%3d) B(%3d) C(%3d) D(%3d) E(%3d) F(%3d) G(%3d) H(%3d) Z(%3d) OTHER(%3d)",
645                                       a,b,c,d,e,f,g,h,z,u)));
646   }
647   
648   return hvMap;
649 }
650
651 //_____________________________________________________________________________
652 TMap*
653 AliMUONCalibrationData::CreateTriggerDCS(Int_t runNumber, Int_t* startOfValidity)
654 {
655   /// Create a new Trigger HV and curent map from the OCDB for a given run
656   return dynamic_cast<TMap*>(CreateObject(runNumber,"MUON/Calib/TriggerDCS",startOfValidity));
657 }
658
659 //_____________________________________________________________________________
660 AliMUONVStore*
661 AliMUONCalibrationData::CreateLocalTriggerBoardMasks(Int_t runNumber, Int_t* startOfValidity)
662 {
663   /// Get the internal store for LocalTriggerBoardMasks from OCDB
664   
665   return dynamic_cast<AliMUONVStore*>(CreateObject(runNumber,"MUON/Calib/LocalTriggerBoardMasks",startOfValidity));
666 }
667
668 //_____________________________________________________________________________
669 AliMUONVStore*
670 AliMUONCalibrationData::CreateNeighbours(Int_t runNumber, Int_t* startOfValidity)
671 {
672   /// Create a neighbour store from the OCDB for a given run
673   return dynamic_cast<AliMUONVStore*>(CreateObject(runNumber,"MUON/Calib/Neighbours",startOfValidity));
674 }
675
676 //_____________________________________________________________________________
677 TObject*
678 AliMUONCalibrationData::CreateObject(Int_t runNumber, const char* path, Int_t* startOfValidity)
679 {
680   /// Access the CDB for a given path (e.g. MUON/Calib/Pedestals),
681   /// and return the corresponding TObject.
682   
683   AliCodeTimerAutoClass(Form("%09d : %s",runNumber,path),0);
684   
685   AliCDBManager* man = AliCDBManager::Instance();
686   
687   AliCDBEntry* entry =  man->Get(path,runNumber);
688   
689   if (entry)
690   {
691                 if ( startOfValidity ) *startOfValidity = entry->GetId().GetFirstRun();
692                 
693     TObject* object = entry->GetObject();
694     if (!(man->GetCacheFlag()))
695     {
696       entry->SetOwner(kFALSE);
697       delete entry;      
698     }
699 //    else
700 //    {
701 //      entry->SetOwner(kTRUE); //FIXME : this should be done but is causing problems with RecoParams at the end of the reco : investigate why...
702 //    }
703     return object;
704   }
705         else
706         {
707                 if ( startOfValidity )  *startOfValidity = AliCDBRunRange::Infinity();
708   }
709         
710   {
711     
712     AliCodeTimerAutoClass(Form("Failed to get %s for run %09d",path,runNumber),1);
713
714   }
715   
716   return 0x0;
717 }
718
719 //_____________________________________________________________________________
720 AliMUONVStore*
721 AliMUONCalibrationData::CreateOccupancyMap(Int_t runNumber, Int_t* startOfValidity)
722 {
723   /// Create a new occupancy map store from the OCDB for a given run
724   return dynamic_cast<AliMUONVStore*>(CreateObject(runNumber,"MUON/Calib/OccupancyMap",startOfValidity));
725 }
726
727 //_____________________________________________________________________________
728 AliMUONRejectList*
729 AliMUONCalibrationData::CreateRejectList(Int_t runNumber, Int_t* startOfValidity)
730 {
731   /// Create a new rejectlist store from the OCDB for a given run
732   return dynamic_cast<AliMUONRejectList*>(CreateObject(runNumber,"MUON/Calib/RejectList",startOfValidity));
733 }
734
735 //_____________________________________________________________________________
736 AliMUONVStore*
737 AliMUONCalibrationData::CreatePedestals(Int_t runNumber, Int_t* startOfValidity)
738 {
739   /// Create a new pedestal store from the OCDB for a given run
740   return dynamic_cast<AliMUONVStore*>(CreateObject(runNumber,"MUON/Calib/Pedestals",startOfValidity));
741 }
742
743 //_____________________________________________________________________________
744 AliMUONVStore*
745 AliMUONCalibrationData::CreateConfig(Int_t runNumber, Int_t* startOfValidity)
746 {
747   /// Create a new config store from the OCDB for a given run
748   return dynamic_cast<AliMUONVStore*>(CreateObject(runNumber,"MUON/Calib/Config",startOfValidity));
749 }
750
751
752 //_____________________________________________________________________________
753 AliMUONRegionalTriggerConfig*
754 AliMUONCalibrationData::CreateRegionalTriggerConfig(Int_t runNumber, Int_t* startOfValidity)
755 {
756   /// Create the internal store for RegionalTriggerConfig from OCDB
757   
758   return dynamic_cast<AliMUONRegionalTriggerConfig*>(CreateObject(runNumber,"MUON/Calib/RegionalTriggerConfig",startOfValidity));
759 }
760
761 //_____________________________________________________________________________
762 AliMUONTriggerEfficiencyCells* 
763 AliMUONCalibrationData::CreateTriggerEfficiency(Int_t runNumber, Int_t* startOfValidity)
764 {
765   /// Create trigger efficiency object from OCBD
766   
767   return dynamic_cast<AliMUONTriggerEfficiencyCells*>(CreateObject(runNumber,"MUON/Calib/TriggerEfficiency",startOfValidity));
768 }
769
770 //_____________________________________________________________________________
771 AliMUONTriggerLut* 
772 AliMUONCalibrationData::CreateTriggerLut(Int_t runNumber, Int_t* startOfValidity)
773 {
774   /// Create trigger LUT from OCDB
775   
776   return dynamic_cast<AliMUONTriggerLut*>(CreateObject(runNumber,"MUON/Calib/TriggerLut",startOfValidity));
777 }
778
779 //_____________________________________________________________________________
780 AliMUONVStore*
781 AliMUONCalibrationData::Gains() const
782 {
783   /// Create (if needed) and return the internal store for gains.
784   if (fgBypassGains) return fgBypassGains;
785   
786   if (!fGains)
787   {
788     fGains = CreateGains(fRunNumber);
789   }
790   return fGains;
791 }
792  
793 //_____________________________________________________________________________
794 AliMUONVCalibParam*
795 AliMUONCalibrationData::Gains(Int_t detElemId, Int_t manuId) const
796 {
797 /// Return the gains for a given (detElemId, manuId) pair
798 /// Note that, unlike the DeadChannel case, if the result is 0x0, that's an
799 /// error (meaning that we should get gains for all channels).
800
801   AliMUONVStore* gains = Gains();
802   if (!gains)
803   {
804     return 0x0;
805   }
806   
807   return static_cast<AliMUONVCalibParam*>(gains->FindObject(detElemId,manuId));
808 }
809
810 //_____________________________________________________________________________
811 AliMUONGlobalCrateConfig* 
812 AliMUONCalibrationData::GlobalTriggerCrateConfig() const
813 {
814   /// Return the config for the global trigger board.
815   
816   if (!fGlobalTriggerCrateConfig)
817   {
818     fGlobalTriggerCrateConfig = CreateGlobalTriggerCrateConfig(fRunNumber);
819   }
820   return fGlobalTriggerCrateConfig;
821 }
822
823
824 //_____________________________________________________________________________
825 TMap*
826 AliMUONCalibrationData::HV(Bool_t patched) const
827 {
828   /// Return the calibration for a given (detElemId, manuId) pair
829   
830   if (!fHV)
831   {
832     fHV = CreateHV(fRunNumber,0,patched);
833   }
834   return fHV;
835 }
836
837 //_____________________________________________________________________________
838 TMap*
839 AliMUONCalibrationData::TriggerDCS() const
840 {
841   /// Return the calibration for a given (detElemId, manuId) pair
842   
843   if (!fTriggerDCS)
844   {
845     fTriggerDCS = CreateTriggerDCS(fRunNumber);
846   }
847   return fTriggerDCS;
848 }
849
850 //_____________________________________________________________________________
851 AliMUONVStore*
852 AliMUONCalibrationData::Neighbours() const
853 {
854   /// Create (if needed) and return the internal store for neighbours.
855   if (!fNeighbours)
856   {
857     fNeighbours = CreateNeighbours(fRunNumber);
858   }
859   return fNeighbours;
860 }
861
862 //_____________________________________________________________________________
863 AliMUONVCalibParam* 
864 AliMUONCalibrationData::LocalTriggerBoardMasks(Int_t localBoardNumber) const
865 {
866 /// Return the masks for a given trigger local board.
867
868   if (!fLocalTriggerBoardMasks)
869   {
870     fLocalTriggerBoardMasks = CreateLocalTriggerBoardMasks(fRunNumber);
871   }
872
873   if ( fLocalTriggerBoardMasks ) 
874   {
875     AliMUONVCalibParam* ltbm = 
876       static_cast<AliMUONVCalibParam*>(fLocalTriggerBoardMasks->FindObject(localBoardNumber));
877     if (!ltbm)
878     {
879       AliError(Form("Could not get mask for localBoardNumber=%d",localBoardNumber));
880     }
881     return ltbm;  
882   }
883   return 0x0;
884 }
885
886 //_____________________________________________________________________________
887 AliMUONVStore*
888 AliMUONCalibrationData::OccupancyMap() const
889 {
890   /// Get occupancy map
891   if (!fOccupancyMap)
892   {
893     fOccupancyMap = CreateOccupancyMap(fRunNumber);
894   }
895   return fOccupancyMap;
896 }
897
898 //_____________________________________________________________________________
899 AliMUONRejectList*
900 AliMUONCalibrationData::RejectList() const
901 {
902   /// Get reject list
903   if (!fRejectList)
904   {
905     fRejectList = CreateRejectList(fRunNumber);
906   }
907   return fRejectList;
908 }
909
910 //_____________________________________________________________________________
911 void
912 AliMUONCalibrationData::BypassStores(AliMUONVStore* ped, AliMUONVStore* gain)
913 {
914   /// Force the use of those pedestals and gains
915   fgBypassPedestals = ped;
916   fgBypassGains = gain;
917   
918 }
919
920 //_____________________________________________________________________________
921 AliMUONVStore*
922 AliMUONCalibrationData::Pedestals() const
923 {
924   /// Return pedestals
925   
926   if (fgBypassPedestals) return fgBypassPedestals;
927   
928   if (!fPedestals)
929   {
930     fPedestals = CreatePedestals(fRunNumber);
931   }
932   return fPedestals;
933 }
934
935 //_____________________________________________________________________________
936 AliMUONVStore*
937 AliMUONCalibrationData::Config() const
938 {
939   /// Return config
940   
941   if (!fConfig)
942   {
943     fConfig = CreateConfig(fRunNumber);
944   }
945   return fConfig;
946 }
947
948 //_____________________________________________________________________________
949 AliMUONVCalibParam*
950 AliMUONCalibrationData::Pedestals(Int_t detElemId, Int_t manuId) const
951 {
952   /// Return the pedestals for a given (detElemId, manuId) pair.
953   /// A return value of 0x0 is considered an error, meaning we should get
954   /// pedestals for all channels.
955   
956   AliMUONVStore* pedestals = Pedestals();
957   if (!pedestals) 
958   {
959     return 0x0;
960   }
961   
962   return static_cast<AliMUONVCalibParam*>(pedestals->FindObject(detElemId,manuId));
963 }
964
965 //_____________________________________________________________________________
966 void
967 AliMUONCalibrationData::Print(Option_t*) const
968 {
969   /// A very basic dump of our guts.
970
971   cout << "RunNumber " << RunNumber()
972   << " fGains=" << fGains
973   << " fPedestals=" << fPedestals
974   << " fConfig=" << fConfig
975   << " fHV=" << fHV
976   << " fTriggerDCS=" << fTriggerDCS
977   << " fLocalTriggerBoardMasks=" << fLocalTriggerBoardMasks
978   << " fRegionalTriggerConfig=" << fRegionalTriggerConfig
979   << " fGlobalTriggerCrateConfig=" << fGlobalTriggerCrateConfig
980   << " fTriggerLut=" << fTriggerLut
981   << endl;
982 }
983
984
985 //_____________________________________________________________________________
986 AliMUONRegionalTriggerConfig* 
987 AliMUONCalibrationData::RegionalTriggerConfig() const
988 {
989   /// Return the config for the regional trigger board.
990   
991   if (!fRegionalTriggerConfig)
992   {
993     fRegionalTriggerConfig = CreateRegionalTriggerConfig(fRunNumber);
994     }
995   return fRegionalTriggerConfig;
996 }
997
998
999 //_____________________________________________________________________________
1000 AliMUONTriggerEfficiencyCells*
1001 AliMUONCalibrationData::TriggerEfficiency() const
1002 {
1003 /// Return the trigger efficiency.
1004
1005   if (!fTriggerEfficiency)
1006   {
1007     fTriggerEfficiency = CreateTriggerEfficiency(fRunNumber);
1008   }
1009   return fTriggerEfficiency;
1010 }
1011
1012
1013 //_____________________________________________________________________________
1014 AliMUONTriggerLut*
1015 AliMUONCalibrationData::TriggerLut() const
1016 {
1017 /// Return the trigger look up table.
1018
1019   if (!fTriggerLut)
1020   {
1021     fTriggerLut = CreateTriggerLut(fRunNumber);
1022   }
1023   return fTriggerLut;
1024 }
1025
1026 //_____________________________________________________________________________
1027 void
1028 AliMUONCalibrationData::Reset()
1029 {
1030 /// Reset all data
1031
1032   AliCodeTimerAuto("",0);
1033   
1034   delete fConfig;
1035   fConfig = 0x0;
1036   delete fPedestals;
1037   fPedestals = 0x0;
1038   delete fGains;
1039   fGains = 0x0;
1040   delete fHV;
1041   fHV = 0x0;
1042   delete fTriggerDCS;
1043   fTriggerDCS = 0x0;
1044   delete fLocalTriggerBoardMasks;
1045   fLocalTriggerBoardMasks = 0x0;
1046   delete fRegionalTriggerConfig;
1047   fRegionalTriggerConfig = 0x0;
1048   delete fGlobalTriggerCrateConfig;
1049   fGlobalTriggerCrateConfig = 0x0;
1050   
1051   delete fTriggerLut;
1052   fTriggerLut = 0x0;
1053   delete fTriggerEfficiency;
1054   fTriggerEfficiency = 0x0;
1055   delete fCapacitances;
1056   fCapacitances = 0x0;
1057   delete fNeighbours;
1058   fNeighbours = 0x0;
1059 }
1060
1061 //_____________________________________________________________________________
1062 void
1063 AliMUONCalibrationData::Check(Int_t runNumber)
1064 {
1065   /// Self-check to see if we can read all data for a given run 
1066   /// from the current OCDB...
1067   
1068   if ( ! CreateCapacitances(runNumber) )
1069   {
1070     AliErrorClass("Could not read capacitances");
1071   }
1072   else
1073   {
1074     AliInfoClass("Capacitances read OK");
1075   }
1076
1077   if ( ! CreateGains(runNumber) ) 
1078   {
1079     AliErrorClass("Could not read gains");
1080   }
1081   else
1082   {
1083     AliInfoClass("Gains read OK");
1084   }
1085
1086   if ( ! CreateGlobalTriggerCrateConfig(runNumber) ) 
1087   {
1088     AliErrorClass("Could not read Trigger Crate Config");
1089   }
1090   else
1091   {
1092     AliInfoClass("TriggerBoardMasks read OK");
1093   }
1094
1095   if ( !  CreateHV(runNumber) )
1096   {
1097     AliErrorClass("Could not read HV");
1098   }
1099   else
1100   {
1101     AliInfoClass("HV read OK");
1102   }
1103
1104   if ( !  CreateTriggerDCS(runNumber) )
1105   {
1106     AliErrorClass("Could not read Trigger HV and Currents");
1107   }
1108   else
1109   {
1110     AliInfoClass("Trigger HV and Currents read OK");
1111   }
1112
1113   if ( ! CreateNeighbours(runNumber) )
1114   {
1115     AliErrorClass("Could not read Neighbours");
1116   }
1117   else
1118   {
1119     AliInfoClass("Neighbours read OK");
1120   }
1121
1122   if ( !  CreateLocalTriggerBoardMasks(runNumber) )
1123   {
1124     AliErrorClass("Could not read LocalTriggerBoardMasks");
1125   }
1126   else
1127   {
1128     AliInfoClass("LocalTriggerBoardMasks read OK");
1129   }
1130   
1131   if ( ! CreatePedestals(runNumber) )
1132   {
1133     AliErrorClass("Could not read pedestals");
1134   }
1135   else
1136   {
1137     AliInfoClass("Pedestals read OK");
1138   }
1139
1140   if ( ! CreateConfig(runNumber) )
1141   {
1142     AliErrorClass("Could not read config");
1143   }
1144   else
1145   {
1146     AliInfoClass("Config read OK");
1147   }
1148   
1149   if ( ! CreateRegionalTriggerConfig(runNumber) )
1150   {
1151     AliErrorClass("Could not read RegionalTriggerConfig");
1152   }
1153   else
1154   {
1155     AliInfoClass("RegionalTriggerBoardMasks read OK");
1156   }
1157   
1158   if ( ! CreateTriggerLut(runNumber) )
1159   {
1160     AliErrorClass("Could not read TriggerLut");
1161   }
1162   else
1163   {
1164     AliInfoClass("TriggerLut read OK");
1165   }
1166
1167   if ( ! CreateTriggerEfficiency(runNumber) )
1168   {
1169     AliErrorClass("Could not read TriggerEfficiency");
1170   }
1171   else    
1172   {
1173     AliInfoClass("TriggerEfficiency read OK");
1174   }
1175 }