Changes in tracker raw data decoding and mchview :
[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"
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
44ClassImp(AliMUONTrackerDataMaker)
45/// \endcond
46
47Int_t AliMUONTrackerDataMaker::fgkCounter(0);
48
49//_____________________________________________________________________________
50AliMUONTrackerDataMaker::AliMUONTrackerDataMaker(TRootIOCtor*)
51:
52AliMUONVTrackerDataMaker(),
53fRawReader(0x0),
54fAccumulatedData(0x0),
55fOneEventData(0x0),
56fDigitCalibrator(0x0),
57fCalibrationData(0x0),
58fSource(""),
59fOCDBPath(""),
60fNumberOfEvents(0),
61fRunNumber(0),
62fIsRunning(kFALSE),
63fIsOwnerOfRawReader(kFALSE),
64fIsEventByEvent(kFALSE)
65{
66}
67
68//_____________________________________________________________________________
69AliMUONTrackerDataMaker::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:
78AliMUONVTrackerDataMaker(),
79fRawReader(rawReader),
80fAccumulatedData(0x0),
81fOneEventData(new AliMUON2DMap(true)),
82fDigitCalibrator(0x0),
83fCalibrationData(0x0),
84fSource(""),
85fOCDBPath(cdbPath),
86fNumberOfEvents(0),
87fRunNumber(runNumber),
88fIsRunning(kFALSE),
89fIsOwnerOfRawReader(kFALSE),
90fIsEventByEvent(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//_____________________________________________________________________________
99AliMUONTrackerDataMaker::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:
107AliMUONVTrackerDataMaker(),
108fRawReader(rawReader),
109fAccumulatedData(0x0),
110fOneEventData(new AliMUON2DMap(true)),
111fDigitCalibrator(0x0),
112fCalibrationData(0x0),
113fSource(""),
114fOCDBPath(cdbPath),
115fNumberOfEvents(0),
116fRunNumber(0),
117fIsRunning(kFALSE),
118fIsOwnerOfRawReader(kTRUE),
119fIsEventByEvent(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//_____________________________________________________________________________
135AliMUONTrackerDataMaker::AliMUONTrackerDataMaker(AliRawReader* rawReader, Bool_t histogram)
136:
137AliMUONVTrackerDataMaker(),
138fRawReader(rawReader),
139fAccumulatedData(0x0),
140fOneEventData(new AliMUON2DMap(true)),
141fDigitCalibrator(0x0),
142fCalibrationData(0x0),
143fSource(""),
144fOCDBPath(""),
145fNumberOfEvents(0),
146fRunNumber(0),
147fIsRunning(kFALSE),
148fIsOwnerOfRawReader(kTRUE),
149fIsEventByEvent(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//_____________________________________________________________________________
163void
164AliMUONTrackerDataMaker::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//_____________________________________________________________________________
237AliMUONTrackerDataMaker::~AliMUONTrackerDataMaker()
238{
239 delete fOneEventData;
240 delete fAccumulatedData;
241 if ( fIsOwnerOfRawReader ) delete fRawReader;
242 delete fCalibrationData;
243 delete fDigitCalibrator;
244}
245
246//_____________________________________________________________________________
247Bool_t
248AliMUONTrackerDataMaker::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//_____________________________________________________________________________
276Bool_t
277AliMUONTrackerDataMaker::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//_____________________________________________________________________________
322Bool_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 }
383
384 if (charge > 0.0 )
385 {
386 param->SetValueAsDouble(manuChannel,0,charge);
387 }
388 }
389
390 Bool_t badEvent = stream.HasPaddingError() || stream.HasGlitchError();
391
392 if (!badEvent)
393 {
394 fAccumulatedData->Add(*fOneEventData,&nevents);
395 }
396
397 return !badEvent;
398}
399
400
401//_____________________________________________________________________________
402void
403AliMUONTrackerDataMaker::Print(Option_t*) const
404{
405 /// Printout
406
407 cout << "Source=" << Source() << " Running=" << ( IsRunning() ? "YES" : "NO")
408 << endl;
409}
410
411//_____________________________________________________________________________
412void AliMUONTrackerDataMaker::Rewind()
413{
414 /// Rewind events
415 if ( fIsOwnerOfRawReader )
416 {
417 fRawReader->RewindEvents();
418 fNumberOfEvents=0;
419 }
420 else
421 {
422 AliError("Wrong usage of this class : cannot rewind as I am not owner of the raw reader !");
423 }
424}
425
426//_____________________________________________________________________________
427Long64_t AliMUONTrackerDataMaker::Merge(TCollection* list)
428{
429 /// Merge objects in collection
430
431 if (!list) return 0;
432
433 if ( list->IsEmpty() ) return NumberOfEvents();
434
435 TIter next(list);
436 const TObject* o(0x0);
437
438 while ( ( o = next() ) )
439 {
440 const AliMUONTrackerDataMaker* data = dynamic_cast<const AliMUONTrackerDataMaker*>(o);
441 if (!o)
442 {
443 AliError(Form("Object named %s is not an AliMUONTrackerDataMaker ! Skipping it",
444 o->GetName()));
445 }
446 else
447 {
448 Bool_t ok = Add(*data);
449 if (!ok)
450 {
451 AliError("Got incompatible objects");
452 }
453 }
454 }
455
456 return NumberOfEvents();
457}
458
459//_____________________________________________________________________________
460void
461AliMUONTrackerDataMaker::SetRawReader(AliRawReader* rawReader)
462{
463 /// Change the rawreader (only works if isowner=true)
464
465 if ( fIsOwnerOfRawReader )
466 {
467 AliFatal("Improper use of this class ! Cannot change raw reader in this case");
468 }
469
470 fRawReader = rawReader;
471
472}