]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONTrackerDataMaker.cxx
Introducing a new OCDB object, MUON/Calib/KillMap, which allows to selectively
[u/mrichter/AliRoot.git] / MUON / AliMUONTrackerDataMaker.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 /// \class AliMUONTrackerDataMaker
19 /// 
20 /// Implementation of VTrackerDataMaker to read raw data and 
21 /// calibrate it (if required)
22 /// 
23 /// \author Laurent Aphecetche, Subatech
24
25 #include "AliMUONTrackerDataMaker.h"
26
27 #include "AliCDBManager.h"
28 #include "AliCDBStorage.h"
29 #include "AliCodeTimer.h"
30 #include "AliDAQ.h"
31 #include "AliLog.h"
32 #include "AliMUON2DMap.h"
33 #include "AliMUONCalibParamND.h"
34 #include "AliMUONCalibrationData.h"
35 #include "AliMUONDigitCalibrator.h"
36 #include "AliMUONRawStreamTrackerHP.h"
37 #include "AliMUONTrackerData.h"
38 #include "AliMpDDLStore.h"
39 #include "AliRawEventHeaderBase.h"
40 #include "AliRawReader.h"
41 #include "Riostream.h"
42
43 /// \cond CLASSIMP
44 ClassImp(AliMUONTrackerDataMaker)
45 /// \endcond
46
47 Int_t AliMUONTrackerDataMaker::fgkCounter(0);
48
49 //_____________________________________________________________________________
50 AliMUONTrackerDataMaker::AliMUONTrackerDataMaker(TRootIOCtor*) 
51
52 AliMUONVTrackerDataMaker(),
53 fRawReader(0x0),
54 fAccumulatedData(0x0),
55 fOneEventData(0x0),
56 fDigitCalibrator(0x0),
57 fCalibrationData(0x0), 
58 fSource(""),
59 fOCDBPath(""),
60 fNumberOfEvents(0),
61 fRunNumber(0),
62 fIsRunning(kFALSE),
63 fIsOwnerOfRawReader(kFALSE),
64 fIsEventByEvent(kFALSE)
65 {
66 }
67
68 //_____________________________________________________________________________
69 AliMUONTrackerDataMaker::AliMUONTrackerDataMaker(const AliMUONRecoParam* recoParam,
70                                                  Int_t runNumber,
71                                                  AliRawReader* rawReader,
72                                                  const char* cdbPath,
73                                                  const char* calibMode,
74                                                  Bool_t histogram,
75                                                  Double_t xmin,
76                                                  Double_t xmax)
77 :
78 AliMUONVTrackerDataMaker(),
79 fRawReader(rawReader),
80 fAccumulatedData(0x0),
81 fOneEventData(new AliMUON2DMap(true)),
82 fDigitCalibrator(0x0),
83 fCalibrationData(0x0), 
84 fSource(""),
85 fOCDBPath(cdbPath),
86 fNumberOfEvents(0),
87 fRunNumber(runNumber),
88 fIsRunning(kFALSE),
89 fIsOwnerOfRawReader(kFALSE),
90 fIsEventByEvent(kFALSE)
91 {
92   /// Ctor in which this object will NOT be the owner of the reader
93   /// and can NOT apply rewind to it, nor use Next on it. 
94   Ctor(recoParam,runNumber,calibMode,histogram,xmin,xmax);
95 }
96
97
98 //_____________________________________________________________________________
99 AliMUONTrackerDataMaker::AliMUONTrackerDataMaker(const AliMUONRecoParam* recoParam,
100                                                  AliRawReader* rawReader,
101                                                  const char* cdbPath,
102                                                  const char* calibMode,
103                                                  Bool_t histogram,
104                                                  Double_t xmin,
105                                                  Double_t xmax)
106 :
107 AliMUONVTrackerDataMaker(),
108 fRawReader(rawReader),
109 fAccumulatedData(0x0),
110 fOneEventData(new AliMUON2DMap(true)),
111 fDigitCalibrator(0x0),
112 fCalibrationData(0x0), 
113 fSource(""),
114 fOCDBPath(cdbPath),
115 fNumberOfEvents(0),
116 fRunNumber(0),
117 fIsRunning(kFALSE),
118 fIsOwnerOfRawReader(kTRUE),
119 fIsEventByEvent(kFALSE)
120 {
121   /// Ctor in which we take the ownership of the rawReader, so we can rewind
122   /// and advance it as we wish
123   
124   if (fRawReader) 
125   {
126     fRawReader->NextEvent(); // to be sure to get run number available
127     fRunNumber = fRawReader->GetRunNumber();
128     fRawReader->RewindEvents();
129   }
130   
131   Ctor(recoParam,fRunNumber,calibMode,histogram,xmin,xmax);
132 }
133
134 //_____________________________________________________________________________
135 AliMUONTrackerDataMaker::AliMUONTrackerDataMaker(AliRawReader* rawReader, Bool_t histogram)
136 :
137 AliMUONVTrackerDataMaker(),
138 fRawReader(rawReader),
139 fAccumulatedData(0x0),
140 fOneEventData(new AliMUON2DMap(true)),
141 fDigitCalibrator(0x0),
142 fCalibrationData(0x0), 
143 fSource(""),
144 fOCDBPath(""),
145 fNumberOfEvents(0),
146 fRunNumber(0),
147 fIsRunning(kFALSE),
148 fIsOwnerOfRawReader(kTRUE),
149 fIsEventByEvent(kFALSE)
150 {
151   if (fRawReader) 
152   {
153     fRawReader->NextEvent(); // to be sure to get run number available
154     fRunNumber = fRawReader->GetRunNumber();
155     fRawReader->RewindEvents();
156   }
157   
158   Ctor(0x0,fRunNumber,"",histogram);
159   
160 }
161
162 //_____________________________________________________________________________
163 void 
164 AliMUONTrackerDataMaker::Ctor(const AliMUONRecoParam* recoParam,
165                               Int_t runNumber,
166                               const char* calibMode,
167                               Bool_t histogram,
168                               Double_t xmin, Double_t xmax)
169 {
170   /// "designated constructor"
171   Bool_t calibrate = ( fOCDBPath.Length() > 0 );
172   
173   TString name;
174   TString type("RAW");
175   
176   if ( calibrate ) 
177   {
178     TString scalib(calibMode);
179     scalib.ToUpper();
180     if ( scalib == "GAIN" ) type = "CALC";
181     if ( scalib == "NOGAIN" ) type = "CALZ";
182     if ( scalib == "GAINCONSTANTCAPA") type = "CALG";
183   }
184   
185   if ( !fRunNumber ) 
186   {
187     ++fgkCounter;
188     name = Form("%s%s_%d",(histogram?"H":""),type.Data(),fgkCounter);
189   }
190   else
191   {
192     name = Form("%s%s%d",(histogram?"H":""),type.Data(),fRunNumber);
193   }
194   
195   fAccumulatedData = new AliMUONTrackerData(name.Data(),"charge values",1);
196   fAccumulatedData->SetDimensionName(0,(calibrate ? "Calibrated charge" : "Raw charge"));
197   if (histogram)
198   {
199     fAccumulatedData->MakeHistogramForDimension(0,kTRUE,xmin,xmax);
200   }
201   
202   if ( calibrate ) 
203   {
204     fCalibrationData = new AliMUONCalibrationData(runNumber);
205     
206     // force the reading of calibration NOW
207     // FIXME: not really elegant and error prone (as we have the list of calib data twice, 
208     // once here and once in the digitcalibrator class, hence the change of them getting
209     // out of sync)
210     // But with the current CDBManager implementation, I don't know how to solve
211     // this better (e.g. to avoid clearing cache messages and so on).
212     
213     AliCDBStorage* storage = AliCDBManager::Instance()->GetDefaultStorage();
214     
215     if ( storage->GetURI() != fOCDBPath.Data() ) 
216     {
217       AliCDBManager::Instance()->SetDefaultStorage(fOCDBPath.Data());
218     }
219     
220     fCalibrationData->Pedestals();
221     fCalibrationData->Gains();
222     fCalibrationData->Neighbours();
223     fCalibrationData->HV();
224     fCalibrationData->Capacitances();
225     
226     if ( storage->GetURI() != fOCDBPath.Data() ) 
227     {
228       AliCDBManager::Instance()->SetDefaultStorage(storage);
229     }
230     
231     fDigitCalibrator = new AliMUONDigitCalibrator(*fCalibrationData,recoParam,calibMode);
232     //FIXME: get the reco param from GUI and/or from OCDB if not used from the QA code ?
233   }
234 }
235
236 //_____________________________________________________________________________
237 AliMUONTrackerDataMaker::~AliMUONTrackerDataMaker()
238 {
239   delete fOneEventData;
240   delete fAccumulatedData;
241   if ( fIsOwnerOfRawReader ) delete fRawReader;
242   delete fCalibrationData;
243   delete fDigitCalibrator;
244 }
245
246 //_____________________________________________________________________________
247 Bool_t 
248 AliMUONTrackerDataMaker::Add(const AliMUONTrackerDataMaker& other)
249 {
250   /// Adds other to this
251     
252   if (!fAccumulatedData) return kFALSE;
253   
254   if ( fIsEventByEvent )
255   {
256     AliError("Cannot add event by event objects !");
257     return kFALSE;
258   }
259   
260   if ( fRunNumber != other.fRunNumber ) fRunNumber = -1;
261   
262   fSource += "\n";
263   fSource += other.fSource;
264   
265   fNumberOfEvents += other.fNumberOfEvents;
266   
267   TList list;
268   list.Add(other.fAccumulatedData);
269   
270   fAccumulatedData->Merge(&list);
271   
272   return kTRUE;
273 }
274
275 //_____________________________________________________________________________
276 Bool_t 
277 AliMUONTrackerDataMaker::NextEvent()
278 {
279   /// Read and process next event
280   
281   if ( !fIsOwnerOfRawReader ) 
282   {
283     AliError("I'm not the owner of the raw reader. Cannot use NextEvent");
284     return kFALSE;
285   }
286   
287   AliCodeTimerAuto("");
288   
289   static Int_t nphysics(0);
290   static Int_t ngood(0);
291   
292   if ( !IsRunning() ) return kTRUE;
293   
294   Bool_t ok = fRawReader->NextEvent();
295   
296   if (!ok) 
297   {
298     return kFALSE;
299   }
300   
301   Int_t eventType = fRawReader->GetType();
302   
303   ++fNumberOfEvents;
304   
305   if (eventType != AliRawEventHeaderBase::kPhysicsEvent ) 
306   {
307     return kTRUE; // for the moment
308   }
309   
310   ++nphysics;
311   
312         Bool_t pok = ProcessEvent();
313         
314         if ( pok ) ++ngood;
315         
316         AliDebug(1,Form("n %10d nphysics %10d ngood %10d",fNumberOfEvents,nphysics,ngood));
317         
318         return kTRUE;  
319 }
320
321 //_____________________________________________________________________________
322 Bool_t AliMUONTrackerDataMaker::ProcessEvent()
323 {
324   /// Process current event 
325   /// 
326   /// Note that in case of calibration, we do not simply reuse the 
327   /// AliMUONDigitCalibrator::Calibrate(AliMUONVDigitStore&) method, 
328   /// as this would require filling first a digitStore, and then calibrate it,
329   /// and then convert it into a VStore, all this taking too much time.
330   /// But we *do* reuse the AliMUONDigitCalibrator::CalibrateDigit in order not to 
331   /// duplicate this critical piece of calibration code !
332   ///
333   
334   AliCodeTimerAuto("");
335   
336   AliMUONRawStreamTrackerHP stream(fRawReader);
337   
338   stream.DisableWarnings();
339 //  stream.EnabbleErrorLogger();
340   
341   const Int_t nddls = AliDAQ::NumberOfDdls("MUONTRK");
342   TArrayI nevents(nddls);
343   
344   for ( Int_t i = 0; i < nddls; ++i ) 
345   {
346     nevents[i] = 0;
347   }
348   
349   fOneEventData->Clear();
350   
351   Int_t buspatchId;
352   UShort_t  manuId;
353   UChar_t manuChannel;
354   UShort_t adc;
355   
356   stream.First();
357   
358   while ( stream.Next(buspatchId,manuId,manuChannel,adc,kTRUE) )
359   {    
360     Int_t detElemId = AliMpDDLStore::Instance()->GetDEfromBus(buspatchId);
361     
362     Int_t ddl = AliMpDDLStore::Instance()->GetDDLfromBus(buspatchId);
363     
364     nevents[ddl] = 1;
365     
366     AliMUONVCalibParam* param = static_cast<AliMUONVCalibParam*>(fOneEventData->FindObject(detElemId,manuId));
367     if (!param)
368     {
369       param = new AliMUONCalibParamND(1,64,detElemId,manuId,
370                                       AliMUONVCalibParam::InvalidFloatValue());
371       fOneEventData->Add(param);
372     }
373     
374     Double_t charge(adc);
375     
376     if ( fDigitCalibrator ) 
377     {
378       if ( fDigitCalibrator->IsValidDigit(detElemId, manuId, manuChannel) )
379       {
380         charge = fDigitCalibrator->CalibrateDigit(detElemId, manuId, manuChannel,adc,3.0);
381       }
382       else
383       {
384         charge = 0.0;
385       }
386     }
387     
388     if (charge > 0.0 ) 
389     {
390       param->SetValueAsDouble(manuChannel,0,charge);
391     }
392   }
393   
394         Bool_t badEvent = stream.HasPaddingError() || stream.HasGlitchError();
395
396         if (!badEvent)
397   {
398     fAccumulatedData->Add(*fOneEventData,&nevents);    
399   }
400   
401   return !badEvent;
402 }
403
404
405 //_____________________________________________________________________________
406 void 
407 AliMUONTrackerDataMaker::Print(Option_t*) const
408 {
409   /// Printout
410   
411   cout << "Source=" << Source() << " Running=" << ( IsRunning() ? "YES" : "NO")
412   << endl;
413 }
414
415 //_____________________________________________________________________________
416 void AliMUONTrackerDataMaker::Rewind()
417 {
418   /// Rewind events
419   if ( fIsOwnerOfRawReader ) 
420   {
421     fRawReader->RewindEvents();
422     fNumberOfEvents=0;  
423   }
424   else
425   {
426     AliError("Wrong usage of this class : cannot rewind as I am not owner of the raw reader !");
427   }
428 }
429
430 //_____________________________________________________________________________
431 Long64_t AliMUONTrackerDataMaker::Merge(TCollection* list)
432 {
433   /// Merge objects in collection
434   
435   if (!list) return 0;
436   
437   if ( list->IsEmpty() ) return NumberOfEvents();
438   
439   TIter next(list);
440   const TObject* o(0x0);
441   
442   while ( ( o = next() ) )
443   {
444     const AliMUONTrackerDataMaker* data = dynamic_cast<const AliMUONTrackerDataMaker*>(o);
445     if (!o)
446     {
447       AliError(Form("Object named %s is not an AliMUONTrackerDataMaker ! Skipping it",
448                     o->GetName()));
449     }
450     else
451     {
452       Bool_t ok = Add(*data);
453       if (!ok)
454       {
455         AliError("Got incompatible objects");
456       }
457     }
458   }
459   
460   return NumberOfEvents();
461 }
462
463 //_____________________________________________________________________________
464 void 
465 AliMUONTrackerDataMaker::SetRawReader(AliRawReader* rawReader)
466 {
467   /// Change the rawreader (only works if isowner=true)
468   
469   if ( fIsOwnerOfRawReader ) 
470         {
471     AliFatal("Improper use of this class ! Cannot change raw reader in this case");
472         }
473         
474   fRawReader = rawReader;
475   
476 }