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