]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/MUONCDB.C
Correcting preprocessor variable (ALI_MUON_GEOMETRY_MODULE_TRANSFORMS_H)
[u/mrichter/AliRoot.git] / MUON / MUONCDB.C
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 /// By Laurent Aphecetche
19
20 #if !defined(__CINT__) || defined(__MAKECINT__)
21 #include "MUONCDB.h"
22 #include "AliCDBEntry.h"
23 #include "AliCDBManager.h"
24 #include "AliMUON2DMap.h"
25 #include "AliMUONV2DStore.h"
26 #include "AliMUONVCalibParam.h"
27 #include "AliMUONCalibParam1I.h"
28 #include "AliMUONCalibParam2F.h"
29 #include "AliMpDEIterator.h"
30 #include "AliMpDEManager.h"
31 #include "AliMpSegFactory.h"
32 #include "AliMpStationType.h"
33 #include "AliMpVSegmentation.h"
34 #include "Riostream.h"
35 #include "TH1F.h"
36 #include "TList.h"
37 #include "TRandom.h"
38 #include "TStopwatch.h"
39 #include "TSystem.h"
40 #endif
41
42 //static const char* CDBPath = "local://$ALICE_ROOT/MUON/CDB/Random";
43 static const char* CDBPath = "local://$ALICE_ROOT/MUON/CDB/Default";
44
45 //_____________________________________________________________________________
46 AliMpSegFactory* segFactory()
47 {
48   static AliMpSegFactory* sf = new AliMpSegFactory();
49   return sf;
50 }
51
52 //_____________________________________________________________________________
53 AliMUONV2DStore* readCDB(const char* calibType)
54 {
55   AliCDBManager* man = AliCDBManager::Instance();
56   man->SetDefaultStorage(CDBPath);
57
58   AliCDBEntry* entry = man->Get(calibType,0);
59
60   if (entry)
61     {
62       return (AliMUONV2DStore*)entry->GetObject();
63     }
64   return 0;
65 }
66
67 //_____________________________________________________________________________
68 void checkCDB(const char* calibType)
69 {
70   TString c(calibType);
71   Float_t refValue(0);
72   
73   if ( c == "MUON/Calib/DeadChannels" )
74   {
75     refValue=5;
76   }
77    
78   AliMUONV2DStore* store = readCDB(calibType);
79   if (!store) return;
80   
81   TIter next(manuList());
82   AliMpIntPair* p;
83   
84   while ( ( p = (AliMpIntPair*)next() ) )
85   {
86     Int_t detElemId = p->GetFirst();
87     Int_t manuId = p->GetSecond();
88     
89     AliMUONVCalibParam* value = 
90       dynamic_cast<AliMUONVCalibParam*>(store->Get(detElemId,manuId));
91     
92     if (value)
93     {
94       for ( Int_t manuChannel = 0; manuChannel < value->Size(); ++manuChannel )
95       {
96         Float_t testValue = value->ValueAsFloat(manuChannel,0);
97         if ( testValue && testValue != refValue )
98         {
99           cout << "Got a strange value for DE=" << detElemId << " manuId="
100           << manuId << " manuChannel=" << manuChannel << " was expecting "
101           << refValue << " and I got " << testValue << endl;
102         }
103       }
104     }
105     else
106     {
107       cout << "Got a null value for DE=" << detElemId << " manuId="
108       << manuId << endl;
109     }
110   }
111   
112   delete store;
113 }
114
115
116 //_____________________________________________________________________________
117 void plotCDB(const char* calibType)
118 {
119   TString c(calibType);
120   
121   TH1* h = 0;
122   TH1* h2 = 0;
123   
124   if ( c == "MUON/Calib/Gains" )
125   {
126     h = new TH1F("gains_mean","mean gain",100,0,1.5);
127     h2 = new TH1F("saturation","adc saturation",4096,-0.5,4095.5);
128   }
129   else if ( c == "MUON/Calib/Pedestals" )
130   {
131     h = new TH1F("pedestals_mean","pedestals_mean",4096,-0.5,4095.5);
132     h2 = new TH1F("pedestals_sigma","pedestals_sigma",100,0,20);
133   }
134   else if ( c == "MUON/Calib/DeadChannels" )
135   {
136     h = new TH1F("dead_channels","dead channels per DE",1500,-0.5,1499.5);
137   }
138   else
139   {
140     cerr << "Don't know how to deal with " << calibType << endl;
141     return;
142   }
143   
144   AliMUONV2DStore* store = readCDB(calibType);
145   if (!store) return;
146
147   TIter next(manuList());
148   AliMpIntPair* p;
149   Int_t n(0);
150   Int_t ndead(0);
151   Int_t nPerStation[7];
152   
153   for ( Int_t i = 0; i < 7; ++i ) nPerStation[i]=0;
154   
155   while ( ( p = (AliMpIntPair*)next() ) )
156   {
157     Int_t detElemId = p->GetFirst();
158     Int_t manuId = p->GetSecond();
159     
160     Int_t station = detElemId/100 - 1;
161     
162     AliMUONVCalibParam* value = 
163     dynamic_cast<AliMUONVCalibParam*>(store->Get(detElemId,manuId));
164     
165     if (value)
166     {
167       for ( Int_t manuChannel = 0; manuChannel < value->Size(); ++manuChannel )
168       {
169         ++n;
170         ++nPerStation[station];
171         if (h2)
172         {
173           h->Fill(value->ValueAsFloat(manuChannel,0));
174           h2->Fill(value->ValueAsFloat(manuChannel,1));
175         }
176         else
177         {
178           if( value->ValueAsInt(manuChannel) )
179           {
180             h->Fill(detElemId);
181             ++ndead;
182           }
183         }
184       }
185     }
186     else
187     {
188       cout << "Got a null value for DE=" << detElemId << " manuId="
189         << manuId << endl;
190     }
191   }
192   
193   cout << "Number of channels = " << n << endl;
194   for ( Int_t i = 0; i < 7; ++i )
195   {
196     cout << "Station " << (i+1) << " " << nPerStation[i] << endl;
197   }
198   
199   if (n && c == "MUON/Calib/DeadChannels")
200   {
201     cout << "Number of dead channels=" << ndead << endl;
202   }
203   delete store;
204 }
205
206 //_____________________________________________________________________________
207 void testReadStore(const AliMUONV2DStore& store, Int_t n)
208 {
209   TIter next(manuList());
210   AliMpIntPair* p;
211   
212   while ( ( p = (AliMpIntPair*)next() ) )
213   {
214     for ( Int_t i = 0; i < n; ++i )
215     {
216       store.Get(p->GetFirst(),p->GetSecond());
217     }
218   }
219
220
221 //_____________________________________________________________________________
222 Int_t makeStores(AliMUONV2DStore& pedestalStore,
223                  AliMUONV2DStore& gainStore,
224                  AliMUONV2DStore& deadStore,
225                  Bool_t defaultValues)
226 {  
227   TIter next(manuList());
228   
229   AliMpIntPair* p;
230   
231   Int_t ngenerated(0);
232   
233   Bool_t replace = kFALSE;
234   
235   const Int_t nChannels(64);
236   
237   while ( ( p = (AliMpIntPair*)next() ) )
238   {
239     AliMUONVCalibParam* ped = new AliMUONCalibParam2F(nChannels);
240     AliMUONVCalibParam* gain = new AliMUONCalibParam2F(nChannels);
241     AliMUONVCalibParam* dead = new AliMUONCalibParam1I(nChannels);
242
243     for ( Int_t manuChannel = 0; manuChannel < nChannels; ++manuChannel )
244     {
245       Float_t meanPedestal;
246       Float_t sigmaPedestal;
247       Float_t meanGain;
248       Float_t saturation(3000);
249     
250       if ( defaultValues ) 
251       {
252         meanPedestal = 0.0;
253         sigmaPedestal = 1.0;
254         meanGain = 1.0;
255       }
256       else
257       {
258         meanPedestal = -1;
259         while ( meanPedestal < 0 )
260         {
261           meanPedestal = gRandom->Gaus(150,10);
262         }
263         sigmaPedestal = -1;
264         while ( sigmaPedestal < 0 )
265         {
266           sigmaPedestal = gRandom->Gaus(1,0.2);
267         }
268         meanGain = -1;
269         while ( meanGain < 0 )
270         {
271           meanGain = gRandom->Gaus(1,0.05);
272         }
273       }
274       ped->SetValueAsFloat(manuChannel,0,meanPedestal);
275       ped->SetValueAsFloat(manuChannel,1,sigmaPedestal);
276       gain->SetValueAsFloat(manuChannel,0,meanGain);
277       gain->SetValueAsFloat(manuChannel,1,saturation);
278       
279       if (!defaultValues)
280       {
281         // probability that this channel is dead ~ 1%
282         if ( gRandom->Uniform(100.0) < 1.0 ) 
283         {
284           Int_t reason = 5; // that value could be used to distinguish
285           // why the channel is dead or how it was flagged bad (online,
286           // offline, by chance...). 5 is of course a fake number.
287           dead->SetValueAsInt(manuChannel,0,reason);
288         }
289       }
290     }
291     Int_t detElemId = p->GetFirst();
292     Int_t manuId = p->GetSecond();
293     Bool_t ok1 = pedestalStore.Set(detElemId,manuId,ped,replace);
294     Bool_t ok2 = gainStore.Set(detElemId,manuId,gain,replace);
295     Bool_t ok3 = deadStore.Set(detElemId,manuId,dead,replace);
296     if (!ok1 || !ok2 || !ok3)
297     {
298       cout << "Could not set DetElemId=" << detElemId << " manuId="
299         << manuId << endl;
300     }
301     else
302     {
303       ++ngenerated;
304     }
305   }
306   
307   return ngenerated;
308 }
309
310 //_____________________________________________________________________________
311 TList* manuList(Bool_t reset)
312 {
313   static TList* fgmanuList = new TList;
314   
315   if (reset) 
316   {
317     fgmanuList->Delete();
318     return fgmanuList;
319   }
320   
321   if (!fgmanuList->IsEmpty()) return fgmanuList;
322   
323   TStopwatch timer;
324   
325   cout << "Generating manu list. Please wait" << endl;
326   
327   fgmanuList->SetOwner(kTRUE);
328   
329   timer.Start();
330   
331   AliMpDEIterator it;
332   
333   it.First();
334   
335   while ( !it.IsDone() )
336   {
337     Int_t detElemId = it.CurrentDE();
338     AliMpStationType stationType = AliMpDEManager::GetStationType(detElemId);
339     if ( stationType != kStationTrigger ) 
340     {
341       for ( Int_t cath = 0; cath <=1 ; ++cath )
342       {
343         AliMpVSegmentation* seg = segFactory()->CreateMpSegmentation(detElemId,cath);
344         
345         TArrayI manus;
346         
347         seg->GetAllElectronicCardIDs(manus);
348         
349         for ( Int_t im = 0; im < manus.GetSize(); ++im )
350         {
351           fgmanuList->Add(new AliMpIntPair(detElemId,manus[im]));
352         }        
353       }
354     }
355     it.Next();
356   }
357   
358   cout << "Time to make the manu list = ";
359   timer.Print();
360   
361   return fgmanuList;
362 }
363
364 //_____________________________________________________________________________
365 void testMakeStores(Int_t readLoop)
366 {
367   manuList();
368   
369   AliMUONV2DStore* pedestalStore = new AliMUON2DMap;
370   AliMUONV2DStore* gainStore = new AliMUON2DMap;
371   AliMUONV2DStore* deadStore = new AliMUON2DMap;
372   
373   TStopwatch timer;
374   
375   cout << "Creating" << endl;
376   
377   timer.Start(kTRUE);
378   makeStores(*pedestalStore,*gainStore,*deadStore,true);
379   timer.Print();
380   
381   cout << "Reading..." << endl;
382   timer.Start(kTRUE);
383   testReadStore(*pedestalStore,readLoop);
384   testReadStore(*gainStore,readLoop);
385   testReadStore(*deadStore,readLoop);
386   cout << timer.CpuTime()/readLoop << " CPUs (mean of " << readLoop 
387     <<" samples." << endl;
388   
389   delete pedestalStore;
390   delete gainStore;
391   delete deadStore;
392 }
393
394 //_____________________________________________________________________________
395 void generateCalibrations(const char* cdbpath, Bool_t defaultValues)
396 {
397   //
398   //
399   //
400
401   AliMUONV2DStore* pedestalStore = new AliMUON2DMap;
402   AliMUONV2DStore* gainStore = new AliMUON2DMap;
403   AliMUONV2DStore* deadStore = new AliMUON2DMap; 
404   
405   makeStores(*pedestalStore,*gainStore,*deadStore,defaultValues);
406   
407   Int_t ever = 99999999;
408   
409   AliCDBId id("MUON/Calib/Pedestals",0,ever);
410   AliCDBMetaData md;
411   md.SetBeamPeriod(1);
412   md.SetAliRootVersion(gROOT->GetVersion());
413   if ( defaultValues )
414   {
415     md.SetComment("Test with default values");
416   }
417   else
418   {
419     md.SetComment("Test with random values");
420   }
421   md.SetResponsible("Laurent Aphecetche");
422   
423   AliCDBManager* man = AliCDBManager::Instance();
424   man->SetDefaultStorage(cdbpath);
425   man->Put(pedestalStore,id,&md);
426   
427   id.SetPath("MUON/Calib/Gains");
428   
429   man->Put(gainStore,id,(AliCDBMetaData*)md.Clone());
430   
431   id.SetPath("MUON/Calib/DeadChannels");
432   
433   man->Put(deadStore,id,(AliCDBMetaData*)md.Clone());
434   
435   delete deadStore;
436   delete pedestalStore;
437   delete gainStore;
438 }
439
440