Adding more bins in QA (Alis)
[u/mrichter/AliRoot.git] / MUON / AliMUONDigitCalibrator.cxx
CommitLineData
d99769c3 1/**************************************************************************
66cdf5b3 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 **************************************************************************/
d99769c3 15
16// $Id$
17
18#include "AliMUONDigitCalibrator.h"
19
b9bd355c 20#include "AliCDBEntry.h"
21#include "AliCDBManager.h"
861d6ce8 22#include "AliLog.h"
d99769c3 23#include "AliMUONCalibrationData.h"
5d950f54 24#include "AliMUONConstants.h"
fe6ed686 25#include "AliMUONLogger.h"
d1c20d08 26#include "AliMUONPadStatusMaker.h"
27#include "AliMUONPadStatusMapMaker.h"
de487b6e 28#include "AliMUONRecoParam.h"
c795d086 29#include "AliMUONVCalibParam.h"
861d6ce8 30#include "AliMUONVDigit.h"
31#include "AliMUONVDigitStore.h"
32#include "AliMUONVStore.h"
33#include "AliMpBusPatch.h"
de98fdc9 34#include "AliMpConstants.h"
ab167304 35#include "AliMpCDB.h"
de98fdc9 36#include "AliMpDDLStore.h"
861d6ce8 37#include "AliMpDEIterator.h"
38#include "AliMpDetElement.h"
ab167304 39#include "AliMpManuStore.h"
d99769c3 40
3d1463c8 41//-----------------------------------------------------------------------------
7945aae7 42/// \class AliMUONDigitCalibrator
1171bb0a 43/// Class used to calibrate digits (either real or simulated ones).
44///
45/// The calibration consists of subtracting the pedestal
46/// and multiplying by a gain, so that
47/// Signal = (ADC-pedestal)*gain
48///
49/// Please note also that for the moment, if a digit lies on a dead channel
50/// we remove this digit from the list of digits.
51/// FIXME: this has to be revisited. By using the AliMUONDigit::fFlags we
52/// should in principle flag a digit as bad w/o removing it, but this
53/// then requires some changes in the cluster finder to deal with this extra
54/// information correctly (e.g. to set a quality for the cluster if it contains
55/// bad digits).
56///
7945aae7 57/// \author Laurent Aphecetche
3d1463c8 58//-----------------------------------------------------------------------------
7945aae7 59
1171bb0a 60
7945aae7 61/// \cond CLASSIMP
d99769c3 62ClassImp(AliMUONDigitCalibrator)
7945aae7 63/// \endcond
d99769c3 64
de98fdc9 65const Int_t AliMUONDigitCalibrator::fgkNoGain(0);
66const Int_t AliMUONDigitCalibrator::fgkGainConstantCapa(1);
67const Int_t AliMUONDigitCalibrator::fgkGain(2);
66cdf5b3 68const Int_t AliMUONDigitCalibrator::fgkInjectionGain(3);
de98fdc9 69
d99769c3 70//_____________________________________________________________________________
05315e71 71AliMUONDigitCalibrator::AliMUONDigitCalibrator(Int_t runNumber)
b9bd355c 72: TObject(),
73fLogger(new AliMUONLogger(20000)),
74fStatusMaker(0x0),
75fStatusMapMaker(0x0),
76fPedestals(0x0),
77fGains(0x0),
78fApplyGains(0),
79fCapacitances(0x0),
80fNumberOfBadPads(0),
81fNumberOfPads(0),
82fChargeSigmaCut(0),
83fMask(0)
84{
85 /// ctor
86
87 AliMUONRecoParam* recoParam(0x0);
88
89 AliCDBEntry* e = AliCDBManager::Instance()->Get("MUON/Calib/RecoParam",runNumber);
90 if (e)
91 {
92 TObject* o = e->GetObject();
93 if ( o->IsA() == TObjArray::Class() )
94 {
95 TObjArray* a = static_cast<TObjArray*>(o);
05315e71 96// a->SetOwner(kTRUE); // FIXME: this should be done but somehow makes the reco crash at the end at cleaning stage... investigate why ?
b9bd355c 97 TIter next(a);
98 AliMUONRecoParam* p;
99 while ( ( p = static_cast<AliMUONRecoParam*>(next()) ))
100 {
101 if ( p->IsDefault()) recoParam = p;
102 }
103 }
104 else
105 {
106 recoParam = static_cast<AliMUONRecoParam*>(o);
107 }
108 }
109 if (!recoParam)
110 {
111 AliError("Cannot get the recoParam. Failing");
112 return;
113 }
114
115 // OK. Now get all we need and work...
116
117 AliMUONCalibrationData calib(runNumber);
118
05315e71 119 Ctor(calib,recoParam,kFALSE);
b9bd355c 120}
121
122//_____________________________________________________________________________
de98fdc9 123AliMUONDigitCalibrator::AliMUONDigitCalibrator(const AliMUONCalibrationData& calib,
05315e71 124 const AliMUONRecoParam* recoParams)
42825ed9 125: TObject(),
3b6f7dce 126fLogger(new AliMUONLogger(20000)),
49e396d9 127fStatusMaker(0x0),
128fStatusMapMaker(0x0),
129fPedestals(0x0),
de98fdc9 130fGains(0x0),
131fApplyGains(0),
de487b6e 132fCapacitances(0x0),
133fNumberOfBadPads(0),
170f4046 134fNumberOfPads(0),
411a502a 135fChargeSigmaCut(0),
136fMask(0)
d99769c3 137{
42825ed9 138 /// ctor
de98fdc9 139
05315e71 140 Ctor(calib,recoParams);
de487b6e 141}
142
143//_____________________________________________________________________________
05315e71 144AliMUONDigitCalibrator::AliMUONDigitCalibrator(const AliMUONCalibrationData& calib, int /*b*/)
de487b6e 145: TObject(),
146fLogger(new AliMUONLogger(20000)),
147fStatusMaker(0x0),
148fStatusMapMaker(0x0),
149fPedestals(0x0),
150fGains(0x0),
151fApplyGains(0),
152fCapacitances(0x0),
153fNumberOfBadPads(0),
170f4046 154fNumberOfPads(0),
411a502a 155fChargeSigmaCut(0),
156fMask(0)
de487b6e 157{
158 /// ctor
159
05315e71 160 Ctor(calib,0x0);
de487b6e 161}
162
163//_____________________________________________________________________________
164void
05315e71 165AliMUONDigitCalibrator::Ctor(const AliMUONCalibrationData& calib,
b9bd355c 166 const AliMUONRecoParam* recoParams,
167 Bool_t deferredInitialization)
de487b6e 168{
169 /// designated ctor
170
05315e71 171 TString cMode("NOGAIN");
172 if (recoParams) cMode=recoParams->GetCalibrationMode();
de98fdc9 173 cMode.ToUpper();
174
175 if ( cMode == "NOGAIN" )
176 {
177 fApplyGains = fgkNoGain;
178 AliInfo("Will NOT apply gain correction");
179 }
3b6f7dce 180 else if ( cMode == "GAINCONSTANTCAPA" )
de98fdc9 181 {
182 fApplyGains = fgkGainConstantCapa;
183 AliInfo("Will apply gain correction, but with constant capacitance");
184 }
185 else if ( cMode == "GAIN" )
186 {
187 fApplyGains = fgkGain;
188 AliInfo("Will apply gain correction, with measured capacitances");
189 }
66cdf5b3 190 else if ( cMode == "INJECTIONGAIN")
191 {
192 fApplyGains = fgkInjectionGain;
193 AliInfo("Will apply injection gain correction, with EMELEC factory gains");
194 }
de98fdc9 195 else
196 {
05315e71 197 AliError(Form("Invalid calib mode = %s. Will use NOGAIN instead",cMode.Data()));
de98fdc9 198 fApplyGains = fgkNoGain;
199 }
66cdf5b3 200
ab167304 201 // Load mapping manu store
202 if ( ! AliMpCDB::LoadManuStore() ) {
203 AliFatal("Could not access manu store from OCDB !");
204 }
66cdf5b3 205
49e396d9 206 fStatusMaker = new AliMUONPadStatusMaker(calib);
207
170f4046 208 // Set default values, as loose as reasonable
66cdf5b3 209
170f4046 210 fChargeSigmaCut = 3.0;
211
411a502a 212 fMask = 0x8080; // reject pads where ped *or* hv are missing
de487b6e 213
de487b6e 214 if ( recoParams )
215 {
170f4046 216 // if we have reco params, we use limits and cuts from there :
217
411a502a 218 fStatusMaker->SetLimits(*recoParams);
219
220 fMask = recoParams->PadGoodnessMask();
de487b6e 221 //WARNING : getting this mask wrong is a very effective way of getting
222 //no digits at all out of this class ;-)
170f4046 223
224 fChargeSigmaCut = recoParams->ChargeSigmaCut();
de487b6e 225 }
8f29b706 226 else
227 {
228 fLogger->Log("No RecoParam available");
229 fLogger->Log(Form("SigmaCut=%e",fChargeSigmaCut));
230 }
49e396d9 231
411a502a 232 fStatusMapMaker = new AliMUONPadStatusMapMaker(*fStatusMaker,fMask,deferredInitialization);
49e396d9 233
234 fPedestals = calib.Pedestals();
66cdf5b3 235
de98fdc9 236 fGains = calib.Gains(); // we get gains whatever the calibMode is, in order
237 // to get the saturation value...
66cdf5b3 238
6b31d882 239 if ( fApplyGains == fgkGain || fApplyGains == fgkInjectionGain )
de98fdc9 240 {
241 fCapacitances = calib.Capacitances();
242 }
d99769c3 243}
244
1171bb0a 245//_____________________________________________________________________________
d99769c3 246AliMUONDigitCalibrator::~AliMUONDigitCalibrator()
247{
d1c20d08 248 /// dtor.
411a502a 249
250 if ( fNumberOfPads > 0 )
251 {
252 if ( fStatusMaker )
253 {
254 fStatusMaker->Report(fMask);
255 }
256
257 AliInfo("Summary of messages:");
258
259 fLogger->Print();
260
261 AliInfo(Form("We have seen %g pads, and rejected %g (%7.2f %%)",
262 fNumberOfPads,fNumberOfBadPads,
263 ( fNumberOfPads > 0 ) ? fNumberOfBadPads*100.0/fNumberOfPads : 0 ));
264 }
265
49e396d9 266 delete fStatusMaker;
267 delete fStatusMapMaker;
fe6ed686 268 delete fLogger;
d99769c3 269}
270
271//_____________________________________________________________________________
272void
42825ed9 273AliMUONDigitCalibrator::Calibrate(AliMUONVDigitStore& digitStore)
d99769c3 274{
42825ed9 275 /// Calibrate the digits contained in digitStore
276 TIter next(digitStore.CreateTrackerIterator());
277 AliMUONVDigit* digit;
66cdf5b3 278
0045b488 279 fStatusMapMaker->RefreshRejectProbabilities(); // this will do something only for simulations
280 // (and only for those simulations where the reject list contain probabilities which are
281 // different from zero or one)
861d6ce8 282
283 AliDebug(1,Form("# of digits = %d",digitStore.GetSize()));
c795d086 284
42825ed9 285 while ( ( digit = static_cast<AliMUONVDigit*>(next() ) ) )
d99769c3 286 {
de487b6e 287 if ( digit->IsCalibrated() )
288 {
289 fLogger->Log("ERROR : trying to calibrate a digit twice");
290 return;
291 }
292
293 digit->Calibrated(kTRUE);
05315e71 294 digit->ChargeInFC(kTRUE);
de487b6e 295
de487b6e 296 Float_t charge(0.0);
297 Int_t statusMap;
298 Bool_t isSaturated(kFALSE);
66cdf5b3 299
8bf22fd6 300 ++fNumberOfPads;
66cdf5b3 301
de487b6e 302 Bool_t ok = IsValidDigit(digit->DetElemId(),digit->ManuId(),digit->ManuChannel(),&statusMap);
66cdf5b3 303
de487b6e 304 digit->SetStatusMap(statusMap);
05315e71 305
de487b6e 306 if (ok)
307 {
de487b6e 308 charge = CalibrateDigit(digit->DetElemId(),digit->ManuId(),digit->ManuChannel(),
0045b488 309 digit->ADC(),fChargeSigmaCut,&isSaturated);
de487b6e 310 }
311 else
312 {
411a502a 313 ++fNumberOfBadPads;
de487b6e 314 }
315
316 digit->SetCharge(charge);
317 digit->Saturated(isSaturated);
05315e71 318
42825ed9 319 }
320}
321
322//_____________________________________________________________________________
de487b6e 323Float_t
324AliMUONDigitCalibrator::CalibrateDigit(Int_t detElemId, Int_t manuId, Int_t manuChannel,
325 Float_t adc, Float_t nsigmas,
326 Bool_t* isSaturated) const
327
42825ed9 328{
329 /// Calibrate one digit
db068204 330 /// Return the digit charge, in fC
cf27231a 331
8f29b706 332 if ( nsigmas < 0 )
333 {
334 nsigmas = fChargeSigmaCut;
335 }
336
337 fLogger->Log(Form("ChargeSigmaCut used = %e",nsigmas));
338
de487b6e 339 AliMUONVCalibParam* pedestal = static_cast<AliMUONVCalibParam*>
340 (fPedestals->FindObject(detElemId,manuId));
49e396d9 341
de487b6e 342 if (!pedestal)
42825ed9 343 {
de487b6e 344 // no pedestal -> no charge
345 fLogger->Log(Form("Got a null pedestal object for DE,manu=%d,%d",detElemId,manuId));
346 return 0.0;
42825ed9 347 }
de487b6e 348
349
350 AliMUONVCalibParam* gain = static_cast<AliMUONVCalibParam*>
351 (fGains->FindObject(detElemId,manuId));
352
353 if (!gain)
42825ed9 354 {
de487b6e 355 if ( fApplyGains != fgkNoGain )
d99769c3 356 {
de487b6e 357 // no gain -> no charge
358 fLogger->Log(Form("Got a null gain object for DE,manu=%d,%d",
359 detElemId,manuId));
360 return 0.0;
42825ed9 361 }
de487b6e 362 }
363
364 Float_t padc = adc-pedestal->ValueAsFloat(manuChannel,0);
de487b6e 365
66cdf5b3 366 // Gain (mV/fC) = 1/(a0*capa) with a0~1.25 and capa~0.2
367 Float_t charge(0);
5d950f54 368 Float_t capa(AliMUONConstants::DefaultCapa()); // capa = 0.2 and a0 = 1.25
369 Float_t a0(AliMUONConstants::DefaultA0()); // is equivalent to gain = 4 mV/fC
370 Float_t a1(0);
371 Float_t adc2mv(AliMUONConstants::DefaultADC2MV()); // 1 ADC channel = 0.61 mV
372 Float_t injGain(4); // By default the gain is set to 4 mV/fC
db068204 373 //
374 // Note that the ChargeMax (for one pad) is roughly 4096 * 0.61 mV/channel / 4 mV/fC = 625 fC
375
66cdf5b3 376 if ( fApplyGains == fgkGain || fApplyGains == fgkInjectionGain )
de487b6e 377 {
ab167304 378 Int_t serialNumber
66cdf5b3 379 = AliMpManuStore::Instance()->GetManuSerial(detElemId, manuId);
3b6f7dce 380
de487b6e 381 AliMUONVCalibParam* param = static_cast<AliMUONVCalibParam*>(fCapacitances->FindObject(serialNumber));
382
383 if ( param )
42825ed9 384 {
66cdf5b3 385 capa = param->ValueAsFloat(manuChannel,0);
386 injGain = param->ValueAsFloat(manuChannel,1);
db068204 387 if ( injGain < 0 )
388 {
389 fLogger->Log(Form("injGain is %e < 0 for serialNumber=%d",injGain,serialNumber));
390 return 0.0;
391 }
42825ed9 392 }
de487b6e 393 else
de98fdc9 394 {
db068204 395 // If capa not found in the OCDB we exit
66cdf5b3 396 fLogger->Log(Form("No capa (injGain) found for serialNumber=%d",serialNumber));
397 return 0.0;
de98fdc9 398 }
de487b6e 399 }
400
401 if ( padc > nsigmas*pedestal->ValueAsFloat(manuChannel,1) )
402 {
66cdf5b3 403 if ( fApplyGains == fgkGain || fApplyGains == fgkGainConstantCapa )
de98fdc9 404 {
66cdf5b3 405 a0 = gain->ValueAsFloat(manuChannel,0);
406 a1 = gain->ValueAsFloat(manuChannel,1);
de487b6e 407 Int_t thres = gain->ValueAsInt(manuChannel,2);
408 if ( padc < thres )
3b6f7dce 409 {
de487b6e 410 charge = a0*padc;
3b6f7dce 411 }
412 else
413 {
de487b6e 414 charge = a0*thres + a0*(padc-thres) + a1*(padc-thres)*(padc-thres);
3b6f7dce 415 }
66cdf5b3 416 charge *= capa*adc2mv;
de98fdc9 417 }
66cdf5b3 418 else if ( fApplyGains == fgkInjectionGain )
42825ed9 419 {
66cdf5b3 420
421 charge = padc*adc2mv/injGain;
42825ed9 422 }
66cdf5b3 423 else
424 {
425 charge = a0*padc*capa*adc2mv;
426 }
de487b6e 427 }
428
de487b6e 429 if ( isSaturated )
430 {
3b6f7dce 431 Int_t saturation(3000);
66cdf5b3 432
6b191dea 433 if ( gain && ( fApplyGains != fgkNoGain ) )
3b6f7dce 434 {
435 saturation = gain->ValueAsInt(manuChannel,4);
436 }
66cdf5b3 437
de98fdc9 438 if ( padc >= saturation )
42825ed9 439 {
de487b6e 440 *isSaturated = kTRUE;
441 }
442 else
443 {
ada26c2d 444 *isSaturated = kFALSE;
d99769c3 445 }
446 }
de487b6e 447
58e1d4ed 448 return ( charge > 0.0 ? charge : 0.0 );
d99769c3 449}
de487b6e 450
451//_____________________________________________________________________________
452Bool_t
453AliMUONDigitCalibrator::IsValidDigit(Int_t detElemId, Int_t manuId, Int_t manuChannel,
454 Int_t* statusMap) const
455
456{
457 /// Check if a given pad is ok or not.
458
05315e71 459 // initialize the statusmap to dead by default
460 if (statusMap) *statusMap = AliMUONPadStatusMapMaker::SelfDeadMask();
461
de487b6e 462 // First a protection against bad input parameters
463 AliMpDetElement* de = AliMpDDLStore::Instance()->GetDetElement(detElemId);
464 if (!de) return kFALSE; // not existing DE
465 if (!de->IsExistingChannel(manuId,manuChannel))
466 {
467 // non-existing (might happen when we get parity errors in read-out
468 // that spoils the manuId
469 return kFALSE;
470 }
471 if (!de->IsConnectedChannel(manuId,manuChannel))
472 {
473 // existing (in read-out), but not connected channel
474 return kFALSE;
475 }
476
477 // ok, now we have a valid channel number, so let's see if that pad
478 // behaves or not ;-)
479
05315e71 480 Int_t sm = StatusMap(detElemId,manuId,manuChannel);
de487b6e 481
482 if (statusMap) *statusMap = sm;
483
484 if ( ( sm & AliMUONPadStatusMapMaker::SelfDeadMask() ) != 0 )
485 {
486 // pad itself is bad (not testing its neighbours at this stage)
487 return kFALSE;
488 }
489
490 return kTRUE;
491}
492
493//_____________________________________________________________________________
494Int_t
495AliMUONDigitCalibrator::PadStatus(Int_t detElemId, Int_t manuId, Int_t manuChannel) const
496{
497 /// Return the status of the given pad
498 return fStatusMaker->PadStatus(detElemId,manuId,manuChannel);
499}
500
501//_____________________________________________________________________________
502Int_t
503AliMUONDigitCalibrator::StatusMap(Int_t detElemId, Int_t manuId, Int_t manuChannel) const
504{
505 /// Return the status map of the given pad
506 return fStatusMapMaker->StatusMap(detElemId,manuId,manuChannel);
507
508}
509