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