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