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