o add Reset function to CalPad and CalROC o Add functionality to AliTPCdataQA - Reset...
[u/mrichter/AliRoot.git] / MUON / AliMUONDigitizerV3.cxx
CommitLineData
92aeef15 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
9edefa04 18
92aeef15 19#include "AliMUONDigitizerV3.h"
20
4ce497c4 21#include "AliMUON.h"
92aeef15 22#include "AliMUONCalibrationData.h"
92aeef15 23#include "AliMUONConstants.h"
92aeef15 24#include "AliMUONDigit.h"
ad26d4f6 25#include "AliMUONLogger.h"
4f8a3d8b 26#include "AliMUONTriggerElectronics.h"
40e382ae 27#include "AliMUONTriggerStoreV1.h"
05314992 28#include "AliMUONVCalibParam.h"
40e382ae 29#include "AliMUONVDigitStore.h"
331a617a 30#include "AliMUONGeometryTransformer.h" //ADDED for trigger noise
05315e71 31#include "AliMUONRecoParam.h"
d5315275 32#include "AliMUONTriggerChamberEfficiency.h"
33#include "AliMUONTriggerUtilities.h"
331a617a 34
88544f7e 35#include "AliMpCDB.h"
331a617a 36#include "AliMpSegmentation.h"
40e382ae 37#include "AliMpCathodType.h"
38#include "AliMpConstants.h"
4ce497c4 39#include "AliMpDEIterator.h"
92aeef15 40#include "AliMpDEManager.h"
4ce497c4 41#include "AliMpPad.h"
40e382ae 42#include "AliMpStationType.h"
4ce497c4 43#include "AliMpVSegmentation.h"
d5315275 44#include "AliMpDDLStore.h"
331a617a 45
46#include "AliCDBManager.h"
47#include "AliCodeTimer.h"
48#include "AliLog.h"
92aeef15 49#include "AliRun.h"
f21fc003 50#include "AliDigitizationInput.h"
c93255fe 51#include "AliLoader.h"
92aeef15 52#include "AliRunLoader.h"
331a617a 53
866c3232 54#include <Riostream.h>
55#include <TF1.h>
40e382ae 56#include <TFile.h>
866c3232 57#include <TMath.h>
58#include <TRandom.h>
59#include <TString.h>
40e382ae 60#include <TSystem.h>
c93255fe 61#include <TTree.h>
40e382ae 62
3d1463c8 63//-----------------------------------------------------------------------------
9265505b 64/// \class AliMUONDigitizerV3
05315e71 65///
4ce497c4 66/// The digitizer is performing the transformation to go from SDigits (digits
67/// w/o any electronic noise) to Digits (w/ electronic noise, and decalibration)
68///
69/// The decalibration is performed by doing the reverse operation of the
70/// calibration, that is we do (Signal+pedestal)/gain -> ADC
71///
72/// Note also that the digitizer takes care of merging sdigits that belongs
73/// to the same pad, either because we're merging several input sdigit files
74/// or with a single file because the sdigitizer does not merge sdigits itself
75/// (for performance reason mainly, and because anyway we know we have to do it
76/// here, at the digitization level).
77///
05315e71 78/// August 2011. In order to remove the need for specific MC OCDB storages,
79/// we're introducing a dependence of simulation on AliMUONRecoParam (stored
80/// in MUON/Calib/RecoParam in OCDB), which is normally (or conceptually, if
81/// you will) only a reconstruction object. That's not a pretty solution, but,
82/// well, we have to do it...
83/// This dependence comes from the fact that we must know how to decalibrate
84/// the digits, so that the decalibration (done here) - calibration (done during
85/// reco) process is (as much as possible) neutral.
86///
87///
c4ee792d 88/// \author Laurent Aphecetche
05315e71 89///
3d1463c8 90//-----------------------------------------------------------------------------
4ce497c4 91
92namespace
93{
94 AliMUON* muon()
95 {
96 return static_cast<AliMUON*>(gAlice->GetModule("MUON"));
97 }
8c0b5e70 98
99 //ADDED for trigger noise
100 const AliMUONGeometryTransformer* GetTransformer()
101 {
102 return muon()->GetGeometryTransformer();
103 }
4ce497c4 104}
105
5c083cba 106Double_t AliMUONDigitizerV3::fgNSigmas = 4.0;
4ce497c4 107
9265505b 108/// \cond CLASSIMP
92aeef15 109ClassImp(AliMUONDigitizerV3)
9265505b 110/// \endcond
92aeef15 111
112//_____________________________________________________________________________
f21fc003 113AliMUONDigitizerV3::AliMUONDigitizerV3(AliDigitizationInput* digInput,
8c0b5e70 114 Int_t generateNoisyDigits)
f21fc003 115: AliDigitizer(digInput),
92aeef15 116fIsInitialized(kFALSE),
92aeef15 117fCalibrationData(0x0),
118fTriggerProcessor(0x0),
8c0b5e70 119fNoiseFunctionTrig(0x0),
ec9acc85 120fGenerateNoisyDigits(generateNoisyDigits),
852fefb4 121fLogger(new AliMUONLogger(4207)), /* 4207 = 25% of the 16828 MCH manus */
40e382ae 122fTriggerStore(new AliMUONTriggerStoreV1),
123fDigitStore(0x0),
9ec6a948 124fOutputDigitStore(0x0),
d5315275 125fInputDigitStores(0x0),
05315e71 126fRecoParam(0x0),
d5315275 127fTriggerEfficiency(0x0),
128fTriggerUtilities(0x0),
129fEfficiencyResponse(2*AliMUONConstants::NTriggerCh()*AliMUONConstants::NTriggerCircuit())
92aeef15 130{
9265505b 131 /// Ctor.
132
f21fc003 133 AliDebug(1,Form("AliDigitizationInput=%p",fDigInput));
ec9acc85 134
92aeef15 135}
136
137//_____________________________________________________________________________
138AliMUONDigitizerV3::~AliMUONDigitizerV3()
139{
9265505b 140 /// Dtor. Note we're the owner of some pointers.
141
92aeef15 142 AliDebug(1,"dtor");
4ce497c4 143
92c23b09 144 // delete fCalibrationData;
92aeef15 145 delete fTriggerProcessor;
8c0b5e70 146 delete fNoiseFunctionTrig;
40e382ae 147 delete fTriggerStore;
148 delete fDigitStore;
149 delete fOutputDigitStore;
ca8c8223 150 delete fInputDigitStores;
d5315275 151 delete fTriggerUtilities;
9ec6a948 152
ad26d4f6 153 AliInfo("Summary of messages");
154 fLogger->Print();
4ce497c4 155
ad26d4f6 156 delete fLogger;
92aeef15 157}
158
159//_____________________________________________________________________________
160void
40e382ae 161AliMUONDigitizerV3::ApplyResponseToTrackerDigit(AliMUONVDigit& digit, Bool_t addNoise)
92aeef15 162{
9265505b 163 /// For tracking digits, starting from an ideal digit's charge, we :
164 ///
cf27231a 165 /// - "divide" by a gain (thus decalibrating the digit)
9265505b 166 /// - add a pedestal (thus decalibrating the digit)
b74ca120 167 /// - add some electronics noise (thus leading to a realistic adc), if requested to do so
9265505b 168 /// - sets the signal to zero if below 3*sigma of the noise
59b91539 169
05315e71 170 Float_t charge = digit.Charge();
171
172 if (!digit.IsChargeInFC())
173 {
174 charge *= AliMUONConstants::DefaultADC2MV()*AliMUONConstants::DefaultA0()*AliMUONConstants::DefaultCapa();
175 fLogger->Log("CHECK ME ! WAS NOT SUPPOSED TO BE HERE !!! ARE YOU RECONSTRUCTING OLD SIMULATIONS ? ");
176 AliError("CHECK ME ! WAS NOT SUPPOSED TO BE HERE !!! ARE YOU RECONSTRUCTING OLD SIMULATIONS ? ");
177 }
cf27231a 178
179 // We set the charge to 0, as the only relevant piece of information
180 // after Digitization is the ADC value.
181 digit.SetCharge(0);
b74ca120 182
4ce497c4 183 Int_t detElemId = digit.DetElemId();
92aeef15 184 Int_t manuId = digit.ManuId();
92aeef15 185
4ce497c4 186 AliMUONVCalibParam* pedestal = fCalibrationData->Pedestals(detElemId,manuId);
92aeef15 187 if (!pedestal)
ad26d4f6 188 {
189 fLogger->Log(Form("%s:%d:Could not get pedestal for DE=%4d manuId=%4d. Disabling.",
190 __FILE__,__LINE__,
191 detElemId,manuId));
ad26d4f6 192 digit.SetADC(0);
193 return;
194 }
59b91539 195
627ba19f 196 Int_t manuChannel = digit.ManuChannel();
197
198 if ( pedestal->ValueAsFloat(manuChannel,0) == AliMUONVCalibParam::InvalidFloatValue() ||
199 pedestal->ValueAsFloat(manuChannel,1) == AliMUONVCalibParam::InvalidFloatValue() )
200 {
201 // protection against invalid pedestal value
202 digit.SetADC(0);
203 return;
204 }
dbd649c1 205
206 TString calibrationMode(fRecoParam->GetCalibrationMode());
207 calibrationMode.ToUpper();
208
4ce497c4 209 AliMUONVCalibParam* gain = fCalibrationData->Gains(detElemId,manuId);
92aeef15 210 if (!gain)
ad26d4f6 211 {
dbd649c1 212
213 if (!calibrationMode.Contains("NOGAIN") )
214 {
215 fLogger->Log(Form("%s:%d:Could not get gain for DE=%4d manuId=%4d. Disabling.",
216 __FILE__,__LINE__,
217 detElemId,manuId));
218 digit.SetADC(0);
219 return;
220 }
ad26d4f6 221 }
cf27231a 222
dbd649c1 223 Int_t adc = DecalibrateTrackerDigit(*pedestal,gain,manuChannel,charge,addNoise,
05315e71 224 digit.IsNoiseOnly(),
dbd649c1 225 calibrationMode);
59b91539 226
92aeef15 227 digit.SetADC(adc);
228}
229
d5315275 230
231//_____________________________________________________________________________
232void
233AliMUONDigitizerV3::ApplyResponseToTriggerDigit(AliMUONVDigit& digit)
234{
235 /// For trigger digits, starting from an ideal digit, we :
236 ///
237 /// - apply efficiency (on demand)
238 /// - apply trigger masks
239
240 Int_t detElemId = digit.DetElemId();
241 Int_t localCircuit = digit.ManuId();
242 Int_t strip = digit.ManuChannel();
243 Int_t cathode = digit.Cathode();
244 Int_t trigCh = detElemId/100 - 11;
245
0a2dcc83 246 // Masked channels
247 Bool_t isMasked = fTriggerUtilities->IsMasked(digit);
248 AliDebug(1,Form("detElemId %i cath %i board %i strip %i is masked %i\n", detElemId, cathode, localCircuit, strip, isMasked));
249 if ( isMasked ) {
250 digit.SetCharge(0);
251 digit.SetADC(0);
252 //AliDebug(1,Form("ch %i cath %i board %i strip %i masked\n", trigCh, cathode, localCircuit, strip));
253 return;
254 }
255
256
d5315275 257 Int_t arrayIndex = GetArrayIndex(cathode, trigCh, localCircuit);
258
259 // Trigger chamber efficiency
260 if ( fTriggerEfficiency ) {
261 if ( fEfficiencyResponse[arrayIndex] < 0 ) {
262 Bool_t isTrig[2] = {kTRUE, kTRUE};
263 fTriggerEfficiency->IsTriggered(detElemId, localCircuit, isTrig[0], isTrig[1]);
264 Int_t arrayIndexBend = GetArrayIndex(0, trigCh, localCircuit);
265 Int_t arrayIndexNonBend = GetArrayIndex(1, trigCh, localCircuit);
266 fEfficiencyResponse[arrayIndexBend] = isTrig[0];
267 fEfficiencyResponse[arrayIndexNonBend] = isTrig[1];
268 }
0a2dcc83 269 AliDebug(1,Form("detElemId %i cath %i board %i strip %i efficiency %i\n", detElemId, cathode, localCircuit, strip, fEfficiencyResponse[arrayIndex]));
d5315275 270 if ( fEfficiencyResponse[arrayIndex] == 0 ) {
271 digit.SetCharge(0);
272 digit.SetADC(0);
273 //AliDebug(1,Form("ch %i cath %i board %i strip %i NOT efficient\n", trigCh, cathode, localCircuit, strip));
274 return;
275 }
0a2dcc83 276 }
d5315275 277}
278
279
280
92aeef15 281//_____________________________________________________________________________
282void
40e382ae 283AliMUONDigitizerV3::ApplyResponse(const AliMUONVDigitStore& store,
284 AliMUONVDigitStore& filteredStore)
92aeef15 285{
9265505b 286 /// Loop over all chamber digits, and apply the response to them
287 /// Note that this method may remove digits.
288
40e382ae 289 filteredStore.Clear();
290
4ce497c4 291 const Bool_t kAddNoise = kTRUE;
292
40e382ae 293 TIter next(store.CreateIterator());
294 AliMUONVDigit* digit;
295
d5315275 296 if ( fTriggerEfficiency ) fEfficiencyResponse.Reset(-1);
297
40e382ae 298 while ( ( digit = static_cast<AliMUONVDigit*>(next()) ) )
4ce497c4 299 {
40e382ae 300 AliMp::StationType stationType = AliMpDEManager::GetStationType(digit->DetElemId());
301
302 if ( stationType != AliMp::kStationTrigger )
92aeef15 303 {
ac6411cf 304 Bool_t addNoise = kAddNoise;
305 if (digit->IsConverted()) addNoise = kFALSE; // No need to add extra noise to a converted real digit
306 ApplyResponseToTrackerDigit(*digit,addNoise);
92aeef15 307 }
d5315275 308 else {
309 ApplyResponseToTriggerDigit(*digit);
310 }
7d7d22a6 311
cf27231a 312 if ( digit->ADC() > 0 || digit->Charge() > 0 )
40e382ae 313 {
314 filteredStore.Add(*digit,AliMUONVDigitStore::kIgnore);
92aeef15 315 }
92aeef15 316 }
40e382ae 317}
92aeef15 318
319//_____________________________________________________________________________
b74ca120 320Int_t
321AliMUONDigitizerV3::DecalibrateTrackerDigit(const AliMUONVCalibParam& pedestals,
dbd649c1 322 const AliMUONVCalibParam* gains,
b74ca120 323 Int_t channel,
324 Float_t charge,
ec9acc85 325 Bool_t addNoise,
05315e71 326 Bool_t noiseOnly,
327 const TString& calibrationMode)
b74ca120 328{
329 /// Decalibrate (i.e. go from charge to adc) a tracker digit, given its
330 /// pedestal and gain parameters.
331 /// Must insure before calling that channel is valid (i.e. between 0 and
332 /// pedestals or gains->GetSize()-1, but also corresponding to a valid channel
333 /// otherwise results are not predictible...)
05315e71 334 ///
335 /// This method is completely tied to what happens in its sister method :
336 /// AliMUONDigitCalibrator::CalibrateDigit, which is doing the reverse work...
337 ///
338
b74ca120 339 static const Int_t kMaxADC = (1<<12)-1; // We code the charge on a 12 bits ADC.
340
05315e71 341 Bool_t nogain = calibrationMode.Contains("NOGAIN");
342
343 Float_t a1(0.0);
344 Int_t thres(4095);
345 Int_t qual(0xF);
346 Float_t capa(AliMUONConstants::DefaultCapa()); // capa = 0.2 and a0 = 1.25
347 Float_t a0(AliMUONConstants::DefaultA0()); // is equivalent to gain = 4 mV/fC
348 Float_t adc2mv(AliMUONConstants::DefaultADC2MV()); // 1 ADC channel = 0.61 mV
349
350 if ( ! nogain )
351 {
dbd649c1 352 if (!gains)
353 {
354 AliFatalClass("Cannot make gain decalibration without gain values !");
355 }
356 a0 = gains->ValueAsFloat(channel,0);
357 a1 = gains->ValueAsFloat(channel,1);
358 thres = gains->ValueAsInt(channel,2);
359 qual = gains->ValueAsInt(channel,3);
05315e71 360 }
361
b74ca120 362 Float_t pedestalMean = pedestals.ValueAsFloat(channel,0);
363 Float_t pedestalSigma = pedestals.ValueAsFloat(channel,1);
364
0a2dcc83 365 AliDebugClass(2,Form("DE %04d MANU %04d CH %02d PEDMEAN %7.2f PEDSIGMA %7.2f",
5d950f54 366 pedestals.ID0(),pedestals.ID1(),channel,pedestalMean,pedestalSigma));
ec9acc85 367
b74ca120 368 if ( qual <= 0 ) return 0;
369
370 Float_t chargeThres = a0*thres;
371
372 Float_t padc(0); // (adc - ped) value
373
05315e71 374 if ( nogain || charge <= chargeThres || TMath::Abs(a1) < 1E-12 )
b74ca120 375 {
376 // linear part only
377
378 if ( TMath::Abs(a0) > 1E-12 )
379 {
380 padc = charge/a0;
381 }
382 }
383 else
384 {
05315e71 385 // FIXME: when we'll use capacitances and real gains, revise this part
386 // to take capa properly into account...
387
388 AliWarningClass("YOU PROBABLY NEED TO REVISE THIS PART OF CODE !!!");
389
b74ca120 390 // linear + parabolic part
391 Double_t qt = chargeThres - charge;
392 Double_t delta = a0*a0-4*a1*qt;
393 if ( delta < 0 )
394 {
395 AliErrorClass(Form("delta=%e DE %d Manu %d Channel %d "
396 " charge %e a0 %e a1 %e thres %d ped %e pedsig %e",
397 delta,pedestals.ID0(),pedestals.ID1(),
398 channel, charge, a0, a1, thres, pedestalMean,
399 pedestalSigma));
400 }
401 else
402 {
403 delta = TMath::Sqrt(delta);
404
405 padc = ( ( -a0 + delta ) > 0 ? ( -a0 + delta ) : ( -a0 - delta ) );
406
407 padc /= 2*a1;
408
409 if ( padc < 0 )
410 {
411 if ( TMath::Abs(padc) > 1E-3)
412 {
413 // this is more than a precision problem : let's signal it !
414 AliErrorClass(Form("padc=%e DE %d Manu %d Channel %d "
415 " charge %e a0 %e a1 %e thres %d ped %e pedsig %e delta %e",
416 padc,pedestals.ID0(),pedestals.ID1(),
417 channel, charge, a0, a1, thres, pedestalMean,
418 pedestalSigma,delta));
419 }
420
421 // ok. consider we're just at thres, let it be zero.
422 padc = 0;
423 }
424
425 padc += thres;
426
427 }
428 }
429
05315e71 430 padc /= capa*adc2mv;
431
b74ca120 432 Int_t adc(0);
433
ec9acc85 434 Float_t adcNoise = 0.0;
b74ca120 435
ec9acc85 436 if ( addNoise )
437 {
438 if ( noiseOnly )
439 {
440 adcNoise = NoiseFunction()->GetRandom()*pedestalSigma;
441 }
442 else
443 {
444 adcNoise = gRandom->Gaus(0.0,pedestalSigma);
445 }
446 }
b74ca120 447
ec9acc85 448 adc = TMath::Nint(padc + pedestalMean + adcNoise + 0.5);
449
5c083cba 450 if ( adc < TMath::Nint(pedestalMean + fgNSigmas*pedestalSigma + 0.5) )
ec9acc85 451 {
d537c8e4 452 // this is an error only in specific cases
453 if ( !addNoise || (addNoise && noiseOnly) )
454 {
1b54215c 455 AliDebugClass(1,Form(" DE %04d Manu %04d Channel %02d "
456 " a0 %7.2f a1 %7.2f thres %04d ped %7.2f pedsig %7.2f adcNoise %7.2f "
457 " charge=%7.2f padc=%7.2f adc=%04d ZS=%04d fgNSigmas=%e addNoise %d noiseOnly %d ",
458 pedestals.ID0(),pedestals.ID1(),channel,
459 a0, a1, thres, pedestalMean, pedestalSigma, adcNoise,
460 charge, padc, adc,
461 TMath::Nint(pedestalMean + fgNSigmas*pedestalSigma + 0.5),
462 fgNSigmas,addNoise,noiseOnly));
d537c8e4 463 }
464
ec9acc85 465 adc = 0;
b74ca120 466 }
467
468 // be sure we stick to 12 bits.
469 if ( adc > kMaxADC )
470 {
471 adc = kMaxADC;
472 }
473
474 return adc;
475}
476
477//_____________________________________________________________________________
92aeef15 478void
ca8c8223 479AliMUONDigitizerV3::CreateInputDigitStores()
9ec6a948 480{
ca8c8223 481 /// Create input digit stores
482 ///
9ec6a948 483
ca8c8223 484 if (fInputDigitStores)
485 {
486 AliFatal("Should be called only once !");
487 }
488
489 fInputDigitStores = new TObjArray;
490
491 fInputDigitStores->SetOwner(kTRUE);
9ec6a948 492
f21fc003 493 for ( Int_t iFile = 0; iFile < fDigInput->GetNinputs(); ++iFile )
9ec6a948 494 {
f21fc003 495 AliLoader* inputLoader = GetLoader(fDigInput->GetInputFolderName(iFile));
9ec6a948 496
497 inputLoader->LoadSDigits("READ");
498
499 TTree* iTreeS = inputLoader->TreeS();
500 if (!iTreeS)
501 {
502 AliFatal(Form("Could not get access to input file #%d",iFile));
503 }
504
ca8c8223 505 fInputDigitStores->AddAt(AliMUONVDigitStore::Create(*iTreeS),iFile);
9ec6a948 506 }
507}
508
509//_____________________________________________________________________________
510void
f21fc003 511AliMUONDigitizerV3::Digitize(Option_t*)
92aeef15 512{
9265505b 513 /// Main method.
514 /// We first loop over input files, and merge the sdigits we found there.
515 /// Second, we digitize all the resulting sdigits
516 /// Then we generate noise-only digits (for tracker only)
517 /// And we finally generate the trigger outputs.
4ce497c4 518
99c136e1 519 AliCodeTimerAuto("",0)
b74ca120 520
f21fc003 521 if ( fDigInput->GetNinputs() == 0 )
92aeef15 522 {
523 AliWarning("No input set. Nothing to do.");
524 return;
525 }
526
527 if ( !fIsInitialized )
528 {
529 AliError("Not initialized. Cannot perform the work. Sorry");
530 return;
531 }
532
f21fc003 533 Int_t nInputFiles = fDigInput->GetNinputs();
92aeef15 534
f21fc003 535 AliLoader* outputLoader = GetLoader(fDigInput->GetOutputFolderName());
40e382ae 536
537 outputLoader->MakeDigitsContainer();
538
539 TTree* oTreeD = outputLoader->TreeD();
540
541 if (!oTreeD)
92aeef15 542 {
40e382ae 543 AliFatal("Cannot create output TreeD");
92aeef15 544 }
40e382ae 545
92aeef15 546 // Loop over all the input files, and merge the sdigits found in those
547 // files.
40e382ae 548
92aeef15 549 for ( Int_t iFile = 0; iFile < nInputFiles; ++iFile )
5d950f54 550 {
f21fc003 551 AliLoader* inputLoader = GetLoader(fDigInput->GetInputFolderName(iFile));
40e382ae 552
553 inputLoader->LoadSDigits("READ");
554
555 TTree* iTreeS = inputLoader->TreeS();
556 if (!iTreeS)
92aeef15 557 {
558 AliFatal(Form("Could not get access to input file #%d",iFile));
559 }
9ec6a948 560
ca8c8223 561 if (!fInputDigitStores)
9ec6a948 562 {
ca8c8223 563 CreateInputDigitStores();
9ec6a948 564 }
40e382ae 565
ca8c8223 566 AliMUONVDigitStore* dstore = static_cast<AliMUONVDigitStore*>(fInputDigitStores->At(iFile));
567
568 dstore->Connect(*iTreeS);
40e382ae 569
ca8c8223 570 iTreeS->GetEvent(0);
571
f21fc003 572 MergeWithSDigits(fDigitStore,*dstore,fDigInput->GetMask(iFile));
05314992 573
40e382ae 574 inputLoader->UnloadSDigits();
05314992 575
ca8c8223 576 dstore->Clear();
92aeef15 577 }
5d950f54 578
92aeef15 579
580 // At this point, we do have digit arrays (one per chamber) which contains
581 // the merging of all the sdigits of the input file(s).
582 // We now massage them to apply the detector response, i.e. this
583 // is here that we do the "digitization" work.
584
40e382ae 585 if (!fOutputDigitStore)
586 {
587 fOutputDigitStore = fDigitStore->Create();
588 }
589
8c0b5e70 590 if ( fGenerateNoisyDigits>=2 )
591 {
592 // Generate noise-only digits for trigger.
593 GenerateNoisyDigitsForTrigger(*fDigitStore);
594 }
40e382ae 595 ApplyResponse(*fDigitStore,*fOutputDigitStore);
5d950f54 596
4ce497c4 597 if ( fGenerateNoisyDigits )
598 {
599 // Generate noise-only digits for tracker.
40e382ae 600 GenerateNoisyDigits(*fOutputDigitStore);
4ce497c4 601 }
602
92aeef15 603 // We generate the global and local trigger decisions.
40e382ae 604 fTriggerProcessor->Digits2Trigger(*fOutputDigitStore,*fTriggerStore);
605
606 // Prepare output tree
607 Bool_t okD = fOutputDigitStore->Connect(*oTreeD,kFALSE);
608 Bool_t okT = fTriggerStore->Connect(*oTreeD,kFALSE);
609 if (!okD || !okT)
610 {
611 AliError(Form("Could not make branch : Digit %d Trigger %d",okD,okT));
612 return;
613 }
92aeef15 614
615 // Fill the output treeD
40e382ae 616 oTreeD->Fill();
92aeef15 617
618 // Write to the output tree(D).
619 // Please note that as GlobalTrigger, LocalTrigger and Digits are in the same
620 // tree (=TreeD) in different branches, this WriteDigits in fact writes all of
621 // the 3 branches.
40e382ae 622 outputLoader->WriteDigits("OVERWRITE");
92aeef15 623
40e382ae 624 outputLoader->UnloadDigits();
4ce497c4 625
40e382ae 626 // Finally, we clean up after ourselves.
627 fTriggerStore->Clear();
628 fDigitStore->Clear();
629 fOutputDigitStore->Clear();
92aeef15 630}
631
4ce497c4 632
633//_____________________________________________________________________________
4ce497c4 634void
40e382ae 635AliMUONDigitizerV3::GenerateNoisyDigits(AliMUONVDigitStore& digitStore)
4ce497c4 636{
9265505b 637 /// According to a given probability, generate digits that
638 /// have a signal above the noise cut (ped+n*sigma_ped), i.e. digits
639 /// that are "only noise".
4ce497c4 640
99c136e1 641 AliCodeTimerAuto("",0)
b74ca120 642
4ce497c4 643 for ( Int_t i = 0; i < AliMUONConstants::NTrackingCh(); ++i )
644 {
645 AliMpDEIterator it;
646
647 it.First(i);
648
649 while ( !it.IsDone() )
650 {
651 for ( Int_t cathode = 0; cathode < 2; ++cathode )
652 {
40e382ae 653 GenerateNoisyDigitsForOneCathode(digitStore,it.CurrentDEId(),cathode);
4ce497c4 654 }
655 it.Next();
656 }
657 }
4ce497c4 658}
659
660//_____________________________________________________________________________
661void
40e382ae 662AliMUONDigitizerV3::GenerateNoisyDigitsForOneCathode(AliMUONVDigitStore& digitStore,
663 Int_t detElemId, Int_t cathode)
4ce497c4 664{
9265505b 665 /// Generate noise-only digits for one cathode of one detection element.
666 /// Called by GenerateNoisyDigits()
4ce497c4 667
9265505b 668 const AliMpVSegmentation* seg
866c3232 669 = AliMpSegmentation::Instance()->GetMpSegmentation(detElemId,AliMp::GetCathodType(cathode));
4ce497c4 670 Int_t nofPads = seg->NofPads();
671
672 Int_t maxIx = seg->MaxPadIndexX();
673 Int_t maxIy = seg->MaxPadIndexY();
674
5c083cba 675 static const Double_t kProbToBeOutsideNsigmas = TMath::Erfc(fgNSigmas/TMath::Sqrt(2.0)) / 2. ;
4ce497c4 676
84aac932 677 Int_t nofNoisyPads = TMath::Nint(kProbToBeOutsideNsigmas*nofPads);
4ce497c4 678 if ( !nofNoisyPads ) return;
679
680 nofNoisyPads =
681 TMath::Nint(gRandom->Gaus(nofNoisyPads,
682 nofNoisyPads/TMath::Sqrt(nofNoisyPads)));
683
684 AliDebug(3,Form("DE %d cath %d nofNoisyPads %d",detElemId,cathode,nofNoisyPads));
685
40e382ae 686 for ( Int_t i = 0; i < nofNoisyPads; ++i )
4ce497c4 687 {
688 Int_t ix(-1);
689 Int_t iy(-1);
40e382ae 690 AliMpPad pad;
691
4ce497c4 692 do {
693 ix = gRandom->Integer(maxIx+1);
694 iy = gRandom->Integer(maxIy+1);
168e9c4d 695 pad = seg->PadByIndices(ix,iy,kFALSE);
40e382ae 696 } while ( !pad.IsValid() );
697
168e9c4d 698 Int_t manuId = pad.GetManuId();
699 Int_t manuChannel = pad.GetManuChannel();
40e382ae 700
4ce497c4 701 AliMUONVCalibParam* pedestals = fCalibrationData->Pedestals(detElemId,manuId);
702
ad26d4f6 703 if (!pedestals)
704 {
705 // no pedestal available for this channel, simply give up
40e382ae 706 continue;
ad26d4f6 707 }
708
40e382ae 709 AliMUONVDigit* d = digitStore.CreateDigit(detElemId,manuId,manuChannel,cathode);
710
711 d->SetPadXY(ix,iy);
712
ec9acc85 713 d->SetCharge(0.0); // charge is zero, the ApplyResponseToTrackerDigit will add the noise
40e382ae 714 d->NoiseOnly(kTRUE);
ec9acc85 715 ApplyResponseToTrackerDigit(*d,kTRUE);
cf27231a 716 if ( d->ADC() > 0 )
4ce497c4 717 {
40e382ae 718 Bool_t ok = digitStore.Add(*d,AliMUONVDigitStore::kDeny);
719 // this can happen (that we randomly chose a digit that is
720 // already there). We simply ignore this, but log the occurence
721 // to cross-check that it's not too frequent.
722 if (!ok)
723 {
724 fLogger->Log("Collision while adding noiseOnly digit");
725 }
726 else
727 {
728 fLogger->Log("Added noiseOnly digit");
729 }
4ce497c4 730 }
40e382ae 731 delete d;
4ce497c4 732 }
733}
734
8c0b5e70 735
736//_____________________________________________________________________________
737void
738AliMUONDigitizerV3::GenerateNoisyDigitsForTrigger(AliMUONVDigitStore& digitStore)
739{
740 /// Generate noise-only digits for one cathode of one detection element.
741 /// Called by GenerateNoisyDigits()
742
743 if ( !fNoiseFunctionTrig )
744 {
745 fNoiseFunctionTrig = new TF1("AliMUONDigitizerV3::fNoiseFunctionTrig","landau",
746 50.,270.);
747
748 fNoiseFunctionTrig->SetParameters(3.91070e+02, 9.85026, 9.35881e-02);
749 }
750
751 AliMpPad pad[2];
752 AliMUONVDigit *d[2]={0x0};
753
754 for ( Int_t chamberId = AliMUONConstants::NTrackingCh(); chamberId < AliMUONConstants::NCh(); ++chamberId )
755 {
756
757 Int_t nofNoisyPads = 50;
758
759 Float_t r=-1, fi = 0., gx, gy, x, y, z, xg01, yg01, zg, xg02, yg02;
760 AliMpDEIterator it;
761
762 AliDebug(3,Form("Chamber %d nofNoisyPads %d",chamberId,nofNoisyPads));
763
764 for ( Int_t i = 0; i < nofNoisyPads; ++i )
765 {
766 //printf("Generating noise %i\n",i);
767 Int_t ix(-1);
768 Int_t iy(-1);
769 Bool_t isOk = kFALSE;
770 Int_t detElemId = -1;
771 do {
772 //r = gRandom->Landau(9.85026, 9.35881e-02);
773 r = fNoiseFunctionTrig->GetRandom();
774 fi = 2. * TMath::Pi() * gRandom->Rndm();
775 //printf("r = %f\tfi = %f\n", r, fi);
776 gx = r * TMath::Cos(fi);
777 gy = r * TMath::Sin(fi);
778
779 for ( it.First(chamberId); ! it.IsDone(); it.Next() ){
780 Int_t currDetElemId = it.CurrentDEId();
781 const AliMpVSegmentation* seg
782 = AliMpSegmentation::Instance()->GetMpSegmentation(currDetElemId,AliMp::GetCathodType(0));
783 if (!seg) continue;
6e97fbb8 784 Float_t deltax = seg->GetDimensionX();
785 Float_t deltay = seg->GetDimensionY();
8c0b5e70 786 GetTransformer()->Local2Global(currDetElemId, -deltax, -deltay, 0, xg01, yg01, zg);
787 GetTransformer()->Local2Global(currDetElemId, deltax, deltay, 0, xg02, yg02, zg);
788 Float_t xg1 = xg01, xg2 = xg02, yg1 = yg01, yg2 = yg02;
789 if(xg01>xg02){
790 xg1 = xg02;
791 xg2 = xg01;
792 }
793 if(yg01>yg02){
794 yg1 = yg02;
795 yg2 = yg01;
796 }
797 if(gx>=xg1 && gx<=xg2 && gy>=yg1 && gy<=yg2){
798 detElemId = currDetElemId;
799 GetTransformer()->Global2Local(detElemId, gx, gy, 0, x, y, z);
6e97fbb8 800 pad[0] = seg->PadByPosition(x,y,kFALSE);
8c0b5e70 801 if(!pad[0].IsValid()) continue;
802 isOk = kTRUE;
803 break;
804 }
805 } // loop on slats
806 } while ( !isOk );
807
808 const AliMpVSegmentation* seg1
809 = AliMpSegmentation::Instance()->GetMpSegmentation(detElemId,AliMp::GetCathodType(1));
6e97fbb8 810 pad[1] = seg1->PadByPosition(x,y,kFALSE);
8c0b5e70 811
812 for ( Int_t cathode = 0; cathode < 2; ++cathode ){
168e9c4d 813 Int_t manuId = pad[cathode].GetLocalBoardId(0);
814 Int_t manuChannel = pad[cathode].GetLocalBoardChannel(0);
8c0b5e70 815 d[cathode] = digitStore.CreateDigit(detElemId,manuId,manuChannel,cathode);
168e9c4d 816 ix = pad[cathode].GetIx();
817 iy = pad[cathode].GetIy();
8c0b5e70 818 d[cathode]->SetPadXY(ix,iy);
819 //d[cathode].SetSignal(1);
820 //d[cathode].SetPhysicsSignal(0);
821 d[cathode]->SetCharge(1);
822 d[cathode]->NoiseOnly(kTRUE);
823 AliDebug(3,Form("Adding a pure noise digit :"));
824
825 Bool_t ok = digitStore.Add(*d[cathode],AliMUONVDigitStore::kDeny);
826 if (!ok)
827 {
828 fLogger->Log("Collision while adding TriggerNoise digit");
829 }
830 else
831 {
832 fLogger->Log("Added triggerNoise digit");
833 }
834 } //loop on cathodes
835 } // loop on noisy pads
836 } // loop on chambers
837}
838
839
4ce497c4 840//_____________________________________________________________________________
40e382ae 841AliLoader*
842AliMUONDigitizerV3::GetLoader(const TString& folderName)
92aeef15 843{
40e382ae 844 /// Get a MUON loader
9265505b 845
ad26d4f6 846 AliDebug(2,Form("Getting access to folder %s",folderName.Data()));
40e382ae 847 AliLoader* loader = AliRunLoader::GetDetectorLoader("MUON",folderName.Data());
92aeef15 848 if (!loader)
849 {
850 AliError(Form("Could not get MuonLoader from folder %s",folderName.Data()));
851 return 0x0;
852 }
40e382ae 853 return loader;
92aeef15 854}
855
856//_____________________________________________________________________________
857Bool_t
858AliMUONDigitizerV3::Init()
859{
f21fc003 860 /// Initialization of the digitization :
40e382ae 861 /// a) create the calibrationData, according to run number
862 /// b) create the trigger processing task
9265505b 863
ad26d4f6 864 AliDebug(2,"");
92aeef15 865
866 if ( fIsInitialized )
867 {
868 AliError("Object already initialized.");
869 return kFALSE;
870 }
871
f21fc003 872 if (!fDigInput)
92aeef15 873 {
f21fc003 874 AliError("fDigInput is null !");
92aeef15 875 return kFALSE;
876 }
877
331a617a 878 // Load mapping
879 if ( ! AliMpCDB::LoadDDLStore() ) {
88544f7e 880 AliFatal("Could not access mapping from OCDB !");
881 }
882
92c23b09 883 if (!fCalibrationData)
884 AliFatal("Calibration data object not defined");
885
ad26d4f6 886 if ( !fCalibrationData->Pedestals() )
887 {
888 AliFatal("Could not access pedestals from OCDB !");
889 }
890 if ( !fCalibrationData->Gains() )
891 {
892 AliFatal("Could not access gains from OCDB !");
893 }
92c23b09 894
895
896 AliInfo("Using trigger configuration from CDB");
897
40e382ae 898 fTriggerProcessor = new AliMUONTriggerElectronics(fCalibrationData);
4ce497c4 899
ad26d4f6 900 AliDebug(1, Form("Will %s generate noise-only digits for tracker",
901 (fGenerateNoisyDigits ? "":"NOT")));
d5315275 902
903 fTriggerUtilities = new AliMUONTriggerUtilities(fCalibrationData);
904
905 if ( muon()->GetTriggerEffCells() ) {
906 // Apply trigger efficiency
907 AliDebug(1, "Will apply trigger efficiency");
908 fTriggerEfficiency = new AliMUONTriggerChamberEfficiency(fCalibrationData->TriggerEfficiency());
909 }
ad26d4f6 910
92aeef15 911 fIsInitialized = kTRUE;
912 return kTRUE;
913}
914
d5315275 915
916//_____________________________________________________________________________
917Int_t AliMUONDigitizerV3::GetArrayIndex(Int_t cathode, Int_t trigCh, Int_t localCircuit)
918{
919 /// Get index of array with trigger status map or efficiency
920 return
921 AliMUONConstants::NTriggerCircuit() * AliMUONConstants::NTriggerCh() * cathode +
922 AliMUONConstants::NTriggerCircuit() * trigCh + localCircuit-1;
923}
924
925
92aeef15 926//_____________________________________________________________________________
92aeef15 927void
40e382ae 928AliMUONDigitizerV3::MergeWithSDigits(AliMUONVDigitStore*& outputStore,
929 const AliMUONVDigitStore& input,
930 Int_t mask)
92aeef15 931{
9265505b 932 /// Merge the sdigits in inputData with the digits already present in outputData
92aeef15 933
40e382ae 934 if ( !outputStore ) outputStore = input.Create();
935
936 TIter next(input.CreateIterator());
937 AliMUONVDigit* sdigit;
938
939 while ( ( sdigit = static_cast<AliMUONVDigit*>(next()) ) )
940 {
941 // Update the track references using the mask.
942 // FIXME: this is dirty, for backward compatibility only.
943 // Should re-design all this way of keeping track of MC information...
944 if ( mask ) sdigit->PatchTracks(mask);
945 // Then add or update the digit to the output.
946 AliMUONVDigit* added = outputStore->Add(*sdigit,AliMUONVDigitStore::kMerge);
947 if (!added)
92aeef15 948 {
40e382ae 949 AliError("Could not add digit in merge mode");
92aeef15 950 }
92aeef15 951 }
952}
ec9acc85 953
954//_____________________________________________________________________________
955TF1*
956AliMUONDigitizerV3::NoiseFunction()
957{
958 /// Return noise function
959 static TF1* f = 0x0;
960 if (!f)
961 {
5c083cba 962 f = new TF1("AliMUONDigitizerV3::NoiseFunction","gaus",fgNSigmas,fgNSigmas*10);
ec9acc85 963 f->SetParameters(1,0,1);
964 }
965 return f;
966}
967
05315e71 968//_____________________________________________________________________________
969void AliMUONDigitizerV3::SetCalibrationData(AliMUONCalibrationData* calibrationData,
970 AliMUONRecoParam* recoParam)
971{
972 fCalibrationData = calibrationData;
973 fRecoParam = recoParam;
974 if (!fRecoParam)
975 {
976 AliError("Cannot work (e.g. decalibrate) without recoparams !");
977 }
978}
979