]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONCDB.cxx
Removing an AliInfo
[u/mrichter/AliRoot.git] / MUON / AliMUONCDB.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 //-----------------------------------------------------------------------------
19 /// \class AliMUONCDB
20 ///
21 /// Helper class to experience the OCDB
22 /// It allows to generate dummy (but complete) containers for all the
23 /// calibration data types we have for tracker and trigger, and to write
24 /// them into OCDB.
25 ///
26 /// For more information, please see READMECDB
27 ///
28 // \author Laurent Aphecetche
29 //-----------------------------------------------------------------------------
30
31 #include "AliMUONCDB.h"
32
33 #include "AliMUON1DArray.h"
34 #include "AliMUON1DMap.h"
35 #include "AliMUON2DMap.h"
36 #include "AliMUON2DStoreValidator.h"
37 #include "AliMUONCalibParamNF.h"
38 #include "AliMUONCalibParamNI.h"
39 #include "AliMUONConstants.h"
40 #include "AliMUONTrackerIO.h"
41 #include "AliMUONTriggerEfficiencyCells.h"
42 #include "AliMUONTriggerLut.h"
43 #include "AliMUONVStore.h"
44 #include "AliMUONVCalibParam.h"
45 #include "AliMUONVCalibParam.h"
46 #include "AliMUONGlobalCrateConfig.h"
47 #include "AliMUONRegionalTriggerConfig.h"
48
49 #include "AliMpCDB.h"
50 #include "AliMpConstants.h"
51 #include "AliMpDDLStore.h"
52 #include "AliMpDEManager.h"
53 #include "AliMpDetElement.h"
54 #include "AliMpFiles.h"
55 #include "AliMpHVNamer.h"
56 #include "AliMpManuIterator.h"
57 #include "AliMpSegmentation.h"
58 #include "AliMpStationType.h"
59 #include "AliMpVSegmentation.h"
60
61 #include "AliCodeTimer.h"
62 #include "AliCDBEntry.h"
63 #include "AliCDBManager.h"
64 #include "AliDCSValue.h"
65 #include "AliLog.h"
66
67 #include <Riostream.h>
68 #include <TArrayI.h>
69 #include <TClass.h>
70 #include <TH1F.h>
71 #include <TList.h>
72 #include <TMap.h>
73 #include <TObjString.h>
74 #include <TROOT.h>
75 #include <TRandom.h>
76 #include <TStopwatch.h>
77 #include <TSystem.h>
78 #include <TMath.h>
79
80
81 /// \cond CLASSIMP
82 ClassImp(AliMUONCDB)
83 /// \endcond
84
85 namespace
86 {
87   //_____________________________________________________________________________
88 AliMUONVStore* Create2DMap()
89 {
90   return new AliMUON2DMap(true);
91 }
92
93   //_____________________________________________________________________________
94 void getBoundaries(const AliMUONVStore& store, Int_t dim,
95                    Float_t* xmin, Float_t* xmax)
96 {
97   /// Assuming the store contains AliMUONVCalibParam objects, compute the
98   /// limits of the value contained in the VCalibParam, for each of its dimensions
99   /// xmin and xmax must be of dimension dim
100   
101   for ( Int_t i = 0; i < dim; ++i ) 
102   {
103     xmin[i]=1E30;
104     xmax[i]=-1E30;
105   }
106   
107   TIter next(store.CreateIterator());
108   AliMUONVCalibParam* value;
109   
110   while ( ( value = dynamic_cast<AliMUONVCalibParam*>(next() ) ) )
111   {
112     Int_t detElemId = value->ID0();
113     Int_t manuId = value->ID1();
114     
115     const AliMpVSegmentation* seg = 
116       AliMpSegmentation::Instance()->GetMpSegmentationByElectronics(detElemId,manuId);
117         
118     for ( Int_t manuChannel = 0; manuChannel < value->Size(); ++manuChannel )
119     {
120       AliMpPad pad = seg->PadByLocation(AliMpIntPair(manuId,manuChannel),kFALSE);
121       if (!pad.IsValid()) continue;
122       
123       for ( Int_t i = 0; i < dim; ++i ) 
124       {
125         Float_t x0 = value->ValueAsFloat(manuChannel,i);
126       
127         xmin[i] = TMath::Min(xmin[i],x0);
128         xmax[i] = TMath::Max(xmax[i],x0);
129       }
130     }
131   }
132
133   for ( Int_t i = 0; i < dim; ++i ) 
134   {
135     if ( TMath::Abs(xmin[i]-xmax[i]) < 1E-3 ) 
136     {
137       xmin[i] -= 1;
138       xmax[i] += 1;
139     }
140   }
141 }
142
143 //_____________________________________________________________________________
144 Double_t GetRandom(Double_t mean, Double_t sigma, Bool_t mustBePositive)
145 {
146   Double_t x(-1);
147   if ( mustBePositive ) 
148   {
149     while ( x < 0 ) 
150     {
151       x = gRandom->Gaus(mean,sigma);
152     }
153   }
154   else
155   {
156     x = gRandom->Gaus(mean,sigma);
157   }
158   return x;
159 }
160
161 }
162
163 //_____________________________________________________________________________
164 AliMUONCDB::AliMUONCDB(const char* cdbpath)
165 : TObject(),
166   fCDBPath(cdbpath),
167   fMaxNofChannelsToGenerate(-1)
168 {
169   /// ctor
170     // Load mapping
171     if ( ! AliMpCDB::LoadDDLStore() ) {
172       AliFatal("Could not access mapping from OCDB !");
173     }
174 }
175
176 //_____________________________________________________________________________
177 AliMUONCDB::~AliMUONCDB()
178 {
179   /// dtor
180 }
181
182 //_____________________________________________________________________________
183 AliMUONVStore* 
184 AliMUONCDB::Diff(AliMUONVStore& store1, AliMUONVStore& store2, 
185                  const char* opt)
186 {
187   /// creates a store which contains store1-store2
188   /// if opt="abs" the difference is absolute one,
189   /// if opt="rel" then what is stored is (store1-store2)/store1
190   /// if opt="percent" then what is stored is rel*100
191   ///
192   /// WARNING Works only for stores which holds AliMUONVCalibParam objects
193   
194   TString sopt(opt);
195   sopt.ToUpper();
196   
197   if ( !sopt.Contains("ABS") && !sopt.Contains("REL") && !sopt.Contains("PERCENT") )
198   {
199     AliErrorClass(Form("opt %s not supported. Only ABS, REL, PERCENT are",opt));
200     return 0x0;
201   }
202   
203   AliMUONVStore* d = static_cast<AliMUONVStore*>(store1.Clone());
204   
205   TIter next(d->CreateIterator());
206   
207   AliMUONVCalibParam* param;
208   
209   while ( ( param = dynamic_cast<AliMUONVCalibParam*>(next() ) ) )
210   {
211     Int_t detElemId = param->ID0();
212     Int_t manuId = param->ID1();
213     
214     AliMUONVCalibParam* param2 = dynamic_cast<AliMUONVCalibParam*>(store2.FindObject(detElemId,manuId));
215     //FIXME: this might happen. Handle it.
216     if (!param2) 
217     {
218       cerr << "param2 is null : FIXME : this might happen !" << endl;
219       delete d;
220       return 0;
221     }
222     
223     for ( Int_t i = 0; i < param->Size(); ++i )
224     {
225       for ( Int_t j = 0; j < param->Dimension(); ++j )
226       {
227         Float_t value(0);
228         if ( sopt.Contains("ABS") )
229         {
230           value = param->ValueAsFloat(i,j) - param2->ValueAsFloat(i,j);
231         }
232         else if ( sopt.Contains("REL") || sopt.Contains("PERCENT") )
233         {
234           if ( param->ValueAsFloat(i,j) ) 
235           {
236             value = (param->ValueAsFloat(i,j) - param2->ValueAsFloat(i,j))/param->ValueAsFloat(i,j);
237           }
238           else 
239           {
240             continue;
241           }
242           if ( sopt.Contains("PERCENT") ) value *= 100.0;
243         }
244         param->SetValueAsFloat(i,j,value);
245       }      
246     }
247   }
248   return d;
249 }
250
251 //_____________________________________________________________________________
252 void 
253 AliMUONCDB::Plot(const AliMUONVStore& store, const char* name, Int_t nbins)
254 {
255   /// Make histograms of each dimension of the AliMUONVCalibParam
256   /// contained inside store.
257   /// It produces histograms named name_0, name_1, etc...
258   
259   TIter next(store.CreateIterator());
260   AliMUONVCalibParam* param;
261   Int_t n(0);
262   const Int_t kNStations = AliMpConstants::NofTrackingChambers()/2;
263   Int_t* nPerStation = new Int_t[kNStations];
264   TH1** h(0x0);
265   
266   for ( Int_t i = 0; i < kNStations; ++i ) nPerStation[i]=0;
267   
268   while ( ( param = static_cast<AliMUONVCalibParam*>(next()) ) )
269   {
270     if (!h)
271     {
272       Int_t dim = param->Dimension();
273       h = new TH1*[dim];
274       Float_t* xmin = new Float_t[dim];
275       Float_t* xmax = new Float_t[dim];
276       getBoundaries(store,dim,xmin,xmax);
277       
278       for ( Int_t i = 0; i < dim; ++i ) 
279       {
280         h[i] = new TH1F(Form("%s_%d",name,i),Form("%s_%d",name,i),
281                             nbins,xmin[i],xmax[i]);
282         AliInfo(Form("Created histogram %s",h[i]->GetName()));
283       }
284     }
285     
286     Int_t detElemId = param->ID0();
287     Int_t manuId = param->ID1();
288     Int_t station = AliMpDEManager::GetChamberId(detElemId)/2;
289     
290     const AliMpVSegmentation* seg = 
291       AliMpSegmentation::Instance()->GetMpSegmentationByElectronics(detElemId,manuId);
292     
293     for ( Int_t manuChannel = 0; manuChannel < param->Size(); ++manuChannel )
294     {
295       AliMpPad pad = seg->PadByLocation(AliMpIntPair(manuId,manuChannel),kFALSE);
296       if (!pad.IsValid()) continue;
297
298       ++n;
299       ++nPerStation[station];
300       
301       for ( Int_t dim = 0; dim < param->Dimension(); ++dim ) 
302       {
303         h[dim]->Fill(param->ValueAsFloat(manuChannel,dim));
304       }
305     }
306   } 
307   
308   for ( Int_t i = 0; i < kNStations; ++i )
309   {
310     AliInfo(Form("Station %d %d ",(i+1),nPerStation[i]));
311   }
312
313   AliInfo(Form("Number of channels = %d",n));
314   
315   delete[] nPerStation;
316 }
317
318 //_____________________________________________________________________________
319 Int_t 
320 AliMUONCDB::MakeHVStore(TMap& aliasMap, Bool_t defaultValues)
321 {
322   /// Create a HV store
323   
324   AliMpHVNamer hvNamer;
325   
326   TObjArray* aliases = hvNamer.GenerateAliases();
327   
328   Int_t nSwitch(0);
329   Int_t nChannels(0);
330   
331   for ( Int_t i = 0; i < aliases->GetEntries(); ++i ) 
332   {
333     TObjString* alias = static_cast<TObjString*>(aliases->At(i));
334     TString& aliasName = alias->String();
335     if ( aliasName.Contains("sw") ) 
336     {
337       // HV Switch (St345 only)
338       TObjArray* valueSet = new TObjArray;
339       valueSet->SetOwner(kTRUE);
340       
341       Bool_t value = kTRUE;
342       
343       if (!defaultValues)
344       {
345         Float_t r = gRandom->Uniform();
346         if ( r < 0.007 ) value = kFALSE;      
347       } 
348       
349       for ( UInt_t timeStamp = 0; timeStamp < 60*3; timeStamp += 60 )
350       {
351         AliDCSValue* dcsValue = new AliDCSValue(value,timeStamp);
352         valueSet->Add(dcsValue);
353       }
354       aliasMap.Add(new TObjString(*alias),valueSet);
355       ++nSwitch;
356     }
357     else
358     {
359       TObjArray* valueSet = new TObjArray;
360       valueSet->SetOwner(kTRUE);
361       for ( UInt_t timeStamp = 0; timeStamp < 60*15; timeStamp += 120 )
362       {
363         Float_t value = 1500;
364         if (!defaultValues) value = GetRandom(1750,62.5,true);
365         AliDCSValue* dcsValue = new AliDCSValue(value,timeStamp);
366         valueSet->Add(dcsValue);
367       }
368       aliasMap.Add(new TObjString(*alias),valueSet);
369       ++nChannels;
370     }
371   }
372   
373   delete aliases;
374   
375   AliInfo(Form("%d HV channels and %d switches",nChannels,nSwitch));
376   
377   return nChannels+nSwitch;
378 }
379
380 //_____________________________________________________________________________
381 Int_t 
382 AliMUONCDB::MakePedestalStore(AliMUONVStore& pedestalStore, Bool_t defaultValues)
383 {
384   /// Create a pedestal store. if defaultValues=true, ped.mean=ped.sigma=1,
385   /// otherwise mean and sigma are from a gaussian (with parameters
386   /// defined below by the kPedestal* constants)
387
388   AliCodeTimerAuto("");
389   
390   Int_t nchannels(0);
391   Int_t nmanus(0);
392   
393   const Int_t kChannels(AliMpConstants::ManuNofChannels());
394   const Float_t kPedestalMeanMean(200);
395   const Float_t kPedestalMeanSigma(10);
396   const Float_t kPedestalSigmaMean(1.0);
397   const Float_t kPedestalSigmaSigma(0.2);
398
399   Int_t detElemId;
400   Int_t manuId;
401     
402   AliMpManuIterator it;
403   
404   while ( it.Next(detElemId,manuId) )
405   {
406     ++nmanus;
407
408     AliMUONVCalibParam* ped = 
409       new AliMUONCalibParamNF(2,kChannels,detElemId,manuId,AliMUONVCalibParam::InvalidFloatValue());
410
411     AliMpDetElement* de = AliMpDDLStore::Instance()->GetDetElement(detElemId);
412     
413     for ( Int_t manuChannel = 0; manuChannel < kChannels; ++manuChannel )
414     {
415       if ( ! de->IsConnectedChannel(manuId,manuChannel) ) continue;
416       
417       ++nchannels;
418       
419       Float_t meanPedestal;
420       Float_t sigmaPedestal;
421       
422       if ( defaultValues ) 
423       {
424         meanPedestal = 0.0;
425         sigmaPedestal = 1.0;
426       }
427       else
428       {
429         Bool_t positive(kTRUE);
430         meanPedestal = 0.0;
431         while ( meanPedestal == 0.0 ) // avoid strict zero 
432         {
433           meanPedestal = GetRandom(kPedestalMeanMean,kPedestalMeanSigma,positive);
434         }
435         sigmaPedestal = GetRandom(kPedestalSigmaMean,kPedestalSigmaSigma,positive);
436       }
437       ped->SetValueAsFloat(manuChannel,0,meanPedestal);
438       ped->SetValueAsFloat(manuChannel,1,sigmaPedestal);
439       
440     }
441     Bool_t ok = pedestalStore.Add(ped);
442     if (!ok)
443     {
444       AliError(Form("Could not set DetElemId=%d manuId=%d",detElemId,manuId));
445     }
446     if ( fMaxNofChannelsToGenerate > 0 && nchannels >= fMaxNofChannelsToGenerate ) break;
447   }
448   
449   AliInfo(Form("%d Manus and %d channels.",nmanus,nchannels));
450   return nchannels;
451 }
452
453 //_____________________________________________________________________________
454 Int_t
455 AliMUONCDB::MakeCapacitanceStore(AliMUONVStore& capaStore, const char* file)
456 {
457   /// Read the capacitance values from file and append them to the capaStore
458   
459   return AliMUONTrackerIO::ReadCapacitances(file,capaStore);
460 }
461
462 //_____________________________________________________________________________
463 Int_t 
464 AliMUONCDB::MakeCapacitanceStore(AliMUONVStore& capaStore, Bool_t defaultValues)
465 {
466   /// Create a capacitance store. if defaultValues=true, all capa are 1.0,
467   /// otherwise they are from a gaussian with parameters defined in the
468   /// kCapa* constants below.
469
470   AliCodeTimerAuto("");
471   
472   Int_t nchannels(0);
473   Int_t nmanus(0);
474   Int_t nmanusOK(0); // manus for which we got the serial number
475     
476   const Float_t kCapaMean(0.3);
477   const Float_t kCapaSigma(0.1);
478   const Float_t kInjectionGainMean(3);
479   const Float_t kInjectionGainSigma(1);
480
481   Int_t detElemId;
482   Int_t manuId;
483   
484   AliMpManuIterator it;
485   
486   while ( it.Next(detElemId,manuId) )
487   {
488     ++nmanus;
489     
490     AliMpDetElement* de = AliMpDDLStore::Instance()->GetDetElement(detElemId); 
491     Int_t serialNumber = de->GetManuSerialFromId(manuId);
492       
493     if ( serialNumber <= 0 ) continue;
494     
495     ++nmanusOK;
496     
497     AliMUONVCalibParam* capa = static_cast<AliMUONVCalibParam*>(capaStore.FindObject(serialNumber));
498     
499     if (!capa)
500     {
501       capa = new AliMUONCalibParamNF(2,AliMpConstants::ManuNofChannels(),serialNumber,0,1.0);
502       Bool_t ok = capaStore.Add(capa);
503       if (!ok)
504       {
505         AliError(Form("Could not set serialNumber=%d manuId=%d",serialNumber,manuId));
506       }      
507     }
508     
509     for ( Int_t manuChannel = 0; manuChannel < capa->Size(); ++manuChannel )
510     {
511       if ( ! de->IsConnectedChannel(manuId,manuChannel) ) continue;
512       
513       ++nchannels;
514       
515       Float_t capaValue;
516       Float_t injectionGain;
517       
518       if ( defaultValues ) 
519       {
520         capaValue = 1.0;
521         injectionGain = 1.0;
522       }
523       else
524       {
525         capaValue = GetRandom(kCapaMean,kCapaSigma,kTRUE);
526         injectionGain = GetRandom(kInjectionGainMean,kInjectionGainSigma,kTRUE);
527       }
528       capa->SetValueAsFloat(manuChannel,0,capaValue);
529       capa->SetValueAsFloat(manuChannel,1,injectionGain);
530     }
531   }
532   
533   Float_t percent = 0;
534   if ( nmanus ) percent = 100*nmanusOK/nmanus;
535   AliInfo(Form("%5d manus with serial number (out of %5d manus = %3.0f%%)",
536                nmanusOK,nmanus,percent));
537   AliInfo(Form("%5d channels",nchannels));
538   if ( percent < 100 ) 
539   {
540     AliWarning("Did not get all serial numbers. capaStore is incomplete !!!!");
541   }
542   return nchannels;
543   
544 }
545
546 //_____________________________________________________________________________
547 Int_t 
548 AliMUONCDB::MakeGainStore(AliMUONVStore& gainStore, Bool_t defaultValues)
549 {  
550   /// Create a gain store. if defaultValues=true, all gains set so that
551   /// charge = (adc-ped)
552   ///
553   /// otherwise parameters are taken from gaussians with parameters 
554   /// defined in the k* constants below.
555   
556   AliCodeTimerAuto("");
557   
558   Int_t nchannels(0);
559   Int_t nmanus(0);
560     
561   const Int_t kSaturation(3000);
562   const Double_t kA0Mean(1.2);
563   const Double_t kA0Sigma(0.1);
564   const Double_t kA1Mean(1E-5);
565   const Double_t kA1Sigma(1E-6);
566   const Double_t kQualMean(0xFF);
567   const Double_t kQualSigma(0x10);
568   const Int_t kThresMean(1600);
569   const Int_t kThresSigma(100);
570   
571   Int_t detElemId;
572   Int_t manuId;
573   
574   AliMpManuIterator it;
575   
576   while ( it.Next(detElemId,manuId) )
577   {
578     ++nmanus;
579
580     AliMUONVCalibParam* gain = 
581       new AliMUONCalibParamNF(5,AliMpConstants::ManuNofChannels(),
582                               detElemId,
583                               manuId,
584                               AliMUONVCalibParam::InvalidFloatValue());
585
586     AliMpDetElement* de = AliMpDDLStore::Instance()->GetDetElement(detElemId);
587
588     for ( Int_t manuChannel = 0; manuChannel < gain->Size(); ++manuChannel )
589     {
590       if ( ! de->IsConnectedChannel(manuId,manuChannel) ) continue;
591       
592       ++nchannels;
593       
594       if ( defaultValues ) 
595       {
596         gain->SetValueAsFloat(manuChannel,0,1.0);
597         gain->SetValueAsFloat(manuChannel,1,0.0);
598         gain->SetValueAsInt(manuChannel,2,4095); 
599         gain->SetValueAsInt(manuChannel,3,1);
600         gain->SetValueAsInt(manuChannel,4,kSaturation);
601       }
602       else
603       {
604         Bool_t positive(kTRUE);
605         gain->SetValueAsFloat(manuChannel,0,GetRandom(kA0Mean,kA0Sigma,positive));
606         gain->SetValueAsFloat(manuChannel,1,GetRandom(kA1Mean,kA1Sigma,!positive));
607         gain->SetValueAsInt(manuChannel,2,(Int_t)TMath::Nint(GetRandom(kThresMean,kThresSigma,positive)));
608         gain->SetValueAsInt(manuChannel,3,(Int_t)TMath::Nint(GetRandom(kQualMean,kQualSigma,positive)));
609         gain->SetValueAsInt(manuChannel,4,kSaturation);        
610       }
611       
612     }
613     Bool_t ok = gainStore.Add(gain);
614     if (!ok)
615     {
616       AliError(Form("Could not set DetElemId=%d manuId=%d",detElemId,manuId));
617     }
618     if ( fMaxNofChannelsToGenerate > 0 && nchannels >= fMaxNofChannelsToGenerate ) break;
619   }
620   
621   AliInfo(Form("%d Manus and %d channels.",nmanus,nchannels));
622   return nchannels;
623 }
624
625 //_____________________________________________________________________________
626 Int_t
627 AliMUONCDB::MakeLocalTriggerMaskStore(AliMUONVStore& localBoardMasks) const
628 {
629   /// Generate local trigger masks store. All masks are set to FFFF
630   
631   AliCodeTimerAuto("");
632   
633   Int_t ngenerated(0);
634   // Generate fake mask values for all localboards and put that into
635   // one single container (localBoardMasks)
636   for ( Int_t i = 1; i <= AliMpConstants::TotalNofLocalBoards(); ++i )
637   {
638     AliMUONVCalibParam* localBoard = new AliMUONCalibParamNI(1,8,i,0,0);
639     for ( Int_t x = 0; x < 2; ++x )
640     {
641       for ( Int_t y = 0; y < 4; ++y )
642       {
643         Int_t index = x*4+y;
644         localBoard->SetValueAsInt(index,0,0xFFFF);
645         ++ngenerated;
646       }
647     }
648     localBoardMasks.Add(localBoard);
649   }
650   return ngenerated;
651 }
652
653 //_____________________________________________________________________________
654 Int_t
655 AliMUONCDB::MakeRegionalTriggerConfigStore(AliMUONRegionalTriggerConfig& rtm) const
656 {
657   /// Make a regional trigger config store. Mask is set to FFFF for each local board (Ch.F.)
658   
659   AliCodeTimerAuto("");
660   
661     return rtm.ReadData(AliMpFiles::LocalTriggerBoardMapping());
662       
663 }
664
665
666 //_____________________________________________________________________________
667 Int_t 
668 AliMUONCDB::MakeGlobalTriggerConfigStore(AliMUONGlobalCrateConfig& gtm) const
669 {
670   /// Make a global trigger config store. All masks (disable) set to 0x00 for each Darc board (Ch.F.)
671   
672   AliCodeTimerAuto("");
673
674     return gtm.ReadData(AliMpFiles::GlobalTriggerBoardMapping());
675 }
676
677
678 //_____________________________________________________________________________
679 AliMUONTriggerLut* 
680 AliMUONCDB::MakeTriggerLUT(const char* file) const
681 {
682   /// Make a triggerlut object, from a file.
683   
684   AliCodeTimerAuto("");
685   
686   AliMUONTriggerLut* lut = new AliMUONTriggerLut;
687   lut->ReadFromFile(file);
688   return lut;
689 }
690
691 //_____________________________________________________________________________
692 AliMUONTriggerEfficiencyCells*
693 AliMUONCDB::MakeTriggerEfficiency(const char* file) const
694 {
695   /// Make a trigger efficiency object from a file.
696   
697   AliCodeTimerAuto("");
698   
699   return new AliMUONTriggerEfficiencyCells(file);
700 }
701
702 //_____________________________________________________________________________
703 void 
704 AliMUONCDB::WriteToCDB(const char* calibpath, TObject* object, 
705                        Int_t startRun, Int_t endRun, 
706                        const char* filename)
707 {
708   /// Write a given object to OCDB
709   
710   AliCDBId id(calibpath,startRun,endRun);
711   AliCDBMetaData md;
712   md.SetAliRootVersion(gROOT->GetVersion());
713   md.SetComment(gSystem->ExpandPathName(filename));
714   md.SetResponsible("Uploaded using AliMUONCDB class");
715   AliCDBManager* man = AliCDBManager::Instance();
716   man->SetDefaultStorage(fCDBPath);
717   man->Put(object,id,&md);
718 }
719
720 //_____________________________________________________________________________
721 void 
722 AliMUONCDB::WriteToCDB(const char* calibpath, TObject* object, 
723                        Int_t startRun, Int_t endRun, Bool_t defaultValues)
724 {
725   /// Write a given object to OCDB
726   
727   AliCDBId id(calibpath,startRun,endRun);
728   AliCDBMetaData md;
729   md.SetAliRootVersion(gROOT->GetVersion());
730   if ( defaultValues )
731   {
732     md.SetComment("Test with default values");
733   }
734   else
735   {
736     md.SetComment("Test with random values");
737   }
738   md.SetResponsible("AliMUONCDB tester class");
739   
740   AliCDBManager* man = AliCDBManager::Instance();
741   man->SetDefaultStorage(fCDBPath);
742   man->Put(object,id,&md);
743 }
744
745 //_____________________________________________________________________________
746 Int_t 
747 AliMUONCDB::MakeNeighbourStore(AliMUONVStore& neighbourStore)
748 {
749   /// Fill the neighbours store with, for each channel, a TObjArray of its
750   /// neighbouring pads (including itself)
751   
752   AliCodeTimerAuto("");
753   
754   AliInfo("Generating NeighbourStore. This will take a while. Please be patient.");
755   
756   Int_t nchannels(0);
757   
758   TObjArray tmp;
759
760   Int_t detElemId;
761   Int_t manuId;
762   
763   AliMpManuIterator it;
764   
765   while ( it.Next(detElemId,manuId) )
766   {
767     const AliMpVSegmentation* seg = 
768       AliMpSegmentation::Instance()->GetMpSegmentationByElectronics(detElemId,manuId);
769     
770     AliMUONVCalibParam* calibParam = static_cast<AliMUONVCalibParam*>(neighbourStore.FindObject(detElemId,manuId));
771     if (!calibParam)
772     {
773       Int_t dimension(11);
774       Int_t size(AliMpConstants::ManuNofChannels());
775       Int_t defaultValue(-1);
776       Int_t packingFactor(size);
777       
778       calibParam = new AliMUONCalibParamNI(dimension,size,detElemId,manuId,defaultValue,packingFactor);
779       Bool_t ok = neighbourStore.Add(calibParam);
780       if (!ok)
781       {
782         AliError(Form("Could not set DetElemId=%d manuId=%d",detElemId,manuId));
783         return -1;
784       }      
785     }
786     
787     for ( Int_t manuChannel = 0; manuChannel < AliMpConstants::ManuNofChannels(); ++manuChannel )
788     {
789       AliMpPad pad = seg->PadByLocation(AliMpIntPair(manuId,manuChannel),kFALSE);
790       
791       if (pad.IsValid()) 
792       {
793         ++nchannels;
794
795         seg->GetNeighbours(pad,tmp,true,true);
796         Int_t nofPadNeighbours = tmp.GetEntriesFast();
797             
798         for ( Int_t i = 0; i < nofPadNeighbours; ++i )
799         {
800           AliMpPad* p = static_cast<AliMpPad*>(tmp.UncheckedAt(i));
801           Int_t x;
802 //          Bool_t ok =
803           calibParam->PackValues(p->GetLocation().GetFirst(),p->GetLocation().GetSecond(),x);
804 //          if (!ok)
805 //          {
806 //            AliError("Could not pack value. Something is seriously wrong. Please check");
807 //            StdoutToAliError(pad->Print(););
808 //            return -1;
809 //          }
810           calibParam->SetValueAsInt(manuChannel,i,x);
811         }
812       }
813     }
814     }
815   
816   return nchannels;
817 }
818
819 //_____________________________________________________________________________
820 void
821 AliMUONCDB::SetMaxNofChannelsToGenerate(Int_t n)
822 {
823   /// Set the maximum number of channels to generate (used for testing only)
824   /// n < 0 means no limit
825   fMaxNofChannelsToGenerate = n;
826 }
827
828 //_____________________________________________________________________________
829 void
830 AliMUONCDB::WriteLocalTriggerMasks(Int_t startRun, Int_t endRun)
831 {  
832   /// Write local trigger masks to OCDB
833   
834   AliMUONVStore* ltm = new AliMUON1DArray(AliMpConstants::TotalNofLocalBoards()+1);
835   Int_t ngenerated = MakeLocalTriggerMaskStore(*ltm);
836   AliInfo(Form("Ngenerated = %d",ngenerated));
837   if (ngenerated>0)
838   {
839     WriteToCDB("MUON/Calib/LocalTriggerBoardMasks",ltm,startRun,endRun,true);
840   }
841   delete ltm;
842 }
843
844 //_____________________________________________________________________________
845 void
846 AliMUONCDB::WriteRegionalTriggerConfig(Int_t startRun, Int_t endRun)
847 {  
848   /// Write regional trigger masks to OCDB
849   
850   AliMUONRegionalTriggerConfig* rtm = new AliMUONRegionalTriggerConfig();
851   Int_t ngenerated = MakeRegionalTriggerConfigStore(*rtm);
852   AliInfo(Form("Ngenerated = %d",ngenerated));
853   if (ngenerated>0)
854   {
855     WriteToCDB("MUON/Calib/RegionalTriggerConfig",rtm,startRun,endRun,true);
856   }
857   delete rtm;
858 }
859
860
861 //_____________________________________________________________________________
862 void
863 AliMUONCDB::WriteGlobalTriggerConfig(Int_t startRun, Int_t endRun)
864 {  
865   /// Write global trigger masks to OCDB
866   
867   AliMUONGlobalCrateConfig* gtm = new AliMUONGlobalCrateConfig();
868
869   Int_t ngenerated = MakeGlobalTriggerConfigStore(*gtm);
870   AliInfo(Form("Ngenerated = %d",ngenerated));
871   if (ngenerated>0)
872   {
873     WriteToCDB("MUON/Calib/GlobalTriggerCrateConfig",gtm,startRun,endRun,true);
874   }
875   delete gtm;
876 }
877
878
879 //_____________________________________________________________________________
880 void
881 AliMUONCDB::WriteTriggerLut(Int_t startRun, Int_t endRun)
882 {  
883   /// Write trigger LUT to OCDB
884   
885   AliMUONTriggerLut* lut = MakeTriggerLUT();
886   if (lut)
887   {
888     WriteToCDB("MUON/Calib/TriggerLut",lut,startRun,endRun,true);
889   }
890   delete lut;
891 }
892
893 //_____________________________________________________________________________
894 void
895 AliMUONCDB::WriteTriggerEfficiency(Int_t startRun, Int_t endRun)
896 {  
897   /// Write trigger efficiency to OCDB
898   
899   AliMUONTriggerEfficiencyCells* eff = MakeTriggerEfficiency();
900   if (eff)
901   {
902     WriteToCDB("MUON/Calib/TriggerEfficiency",eff,startRun,endRun,true);
903   }
904   delete eff;
905 }
906
907 //_____________________________________________________________________________
908 void 
909 AliMUONCDB::WriteNeighbours(Int_t startRun, Int_t endRun)
910 {
911   /// Write neighbours to OCDB
912   
913   AliMUONVStore* neighbours = Create2DMap();
914   Int_t ngenerated = MakeNeighbourStore(*neighbours);
915   AliInfo(Form("Ngenerated = %d",ngenerated));
916   if (ngenerated>0)
917   {
918     WriteToCDB("MUON/Calib/Neighbours",neighbours,startRun,endRun,true);
919   }
920   delete neighbours;
921 }
922
923 //_____________________________________________________________________________
924 void 
925 AliMUONCDB::WriteHV(Bool_t defaultValues,
926                     Int_t startRun, Int_t endRun)
927 {
928   /// generate HV values (either cste = 1500 V) if defaultValues=true or random
929   /// if defaultValues=false, see makeHVStore) and
930   /// store them into CDB located at cdbpath, with a validity period
931   /// ranging from startRun to endRun
932   
933   TMap* hvStore = new TMap;
934   Int_t ngenerated = MakeHVStore(*hvStore,defaultValues);
935   AliInfo(Form("Ngenerated = %d",ngenerated));
936   if (ngenerated>0)
937   {
938     WriteToCDB("MUON/Calib/HV",hvStore,startRun,endRun,defaultValues);
939   }
940   delete hvStore;
941 }
942
943 //_____________________________________________________________________________
944 void 
945 AliMUONCDB::WritePedestals(Bool_t defaultValues,
946                            Int_t startRun, Int_t endRun)
947 {
948   /// generate pedestal values (either 0 if defaultValues=true or random
949   /// if defaultValues=false, see makePedestalStore) and
950   /// store them into CDB located at cdbpath, with a validity period
951   /// ranging from startRun to endRun
952   
953   AliMUONVStore* pedestalStore = Create2DMap();
954   Int_t ngenerated = MakePedestalStore(*pedestalStore,defaultValues);
955   AliInfo(Form("Ngenerated = %d",ngenerated));
956   WriteToCDB("MUON/Calib/Pedestals",pedestalStore,startRun,endRun,defaultValues);
957   delete pedestalStore;
958 }
959
960
961 //_____________________________________________________________________________
962 void 
963 AliMUONCDB::WriteGains(Bool_t defaultValues,
964                        Int_t startRun, Int_t endRun)
965 {
966   /// generate gain values (either 1 if defaultValues=true or random
967   /// if defaultValues=false, see makeGainStore) and
968   /// store them into CDB located at cdbpath, with a validity period
969   /// ranging from startRun to endRun
970   
971   AliMUONVStore* gainStore = Create2DMap();
972   Int_t ngenerated = MakeGainStore(*gainStore,defaultValues);
973   AliInfo(Form("Ngenerated = %d",ngenerated));  
974   WriteToCDB("MUON/Calib/Gains",gainStore,startRun,endRun,defaultValues);
975   delete gainStore;
976 }
977
978 //_____________________________________________________________________________
979 void 
980 AliMUONCDB::WriteCapacitances(const char* filename,
981                               Int_t startRun, Int_t endRun)
982 {
983   /// read manu capacitance and injection gain values from file 
984   /// and store them into CDB located at cdbpath, with a validity period
985   /// ranging from startRun to endRun
986   
987   AliMUONVStore* capaStore = new AliMUON1DMap(16828);
988   Int_t ngenerated = MakeCapacitanceStore(*capaStore,filename);
989   AliInfo(Form("Ngenerated = %d",ngenerated));
990   if ( ngenerated > 0 ) 
991   {
992     WriteToCDB("MUON/Calib/Capacitances",capaStore,startRun,endRun,filename);
993   }
994   delete capaStore;
995 }
996
997 //_____________________________________________________________________________
998 void 
999 AliMUONCDB::WriteCapacitances(Bool_t defaultValues,
1000                               Int_t startRun, Int_t endRun)
1001 {
1002   /// generate manu capacitance values (either 1 if defaultValues=true or random
1003   /// if defaultValues=false, see makeCapacitanceStore) and
1004   /// store them into CDB located at cdbpath, with a validity period
1005   /// ranging from startRun to endRun
1006   
1007   AliMUONVStore* capaStore = new AliMUON1DMap(16828);
1008   Int_t ngenerated = MakeCapacitanceStore(*capaStore,defaultValues);
1009   AliInfo(Form("Ngenerated = %d",ngenerated));
1010   WriteToCDB("MUON/Calib/Capacitances",capaStore,startRun,endRun,defaultValues);
1011   delete capaStore;
1012 }
1013
1014 //_____________________________________________________________________________
1015 void
1016 AliMUONCDB::WriteTrigger(Int_t startRun, Int_t endRun)
1017 {
1018   /// Writes all Trigger related calibration to CDB
1019   WriteLocalTriggerMasks(startRun,endRun);
1020   WriteRegionalTriggerConfig(startRun,endRun);
1021   WriteGlobalTriggerConfig(startRun,endRun);
1022   WriteTriggerLut(startRun,endRun);
1023   WriteTriggerEfficiency(startRun,endRun);
1024 }
1025
1026 //_____________________________________________________________________________
1027 void
1028 AliMUONCDB::WriteTracker(Bool_t defaultValues, Int_t startRun, Int_t endRun)
1029 {
1030   /// Writes all Tracker related calibration to CDB
1031   WriteHV(defaultValues,startRun,endRun);
1032   WritePedestals(defaultValues,startRun,endRun);
1033   WriteGains(defaultValues,startRun,endRun);
1034   WriteCapacitances(defaultValues,startRun,endRun);
1035   WriteNeighbours(startRun,endRun);
1036 }