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