Formatting changes.
[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
18#include "AliMUONDigitizerV3.h"
19
4ce497c4 20#include "AliMUON.h"
92aeef15 21#include "AliMUONCalibrationData.h"
92aeef15 22#include "AliMUONConstants.h"
23#include "AliMUONData.h"
4ce497c4 24#include "AliMUONDataIterator.h"
92aeef15 25#include "AliMUONDigit.h"
4ce497c4 26#include "AliMUONSegmentation.h"
4ce497c4 27#include "AliMUONTriggerEfficiencyCells.h"
4f8a3d8b 28#include "AliMUONTriggerElectronics.h"
05314992 29#include "AliMUONVCalibParam.h"
9265505b 30
4ce497c4 31#include "AliMpDEIterator.h"
92aeef15 32#include "AliMpDEManager.h"
4ce497c4 33#include "AliMpIntPair.h"
34#include "AliMpPad.h"
92aeef15 35#include "AliMpStationType.h"
9265505b 36#include "AliMpSegmentation.h"
4ce497c4 37#include "AliMpVSegmentation.h"
66f4c572 38#include "AliMpDEManager.h"
9265505b 39
92aeef15 40#include "AliRun.h"
41#include "AliRunDigitizer.h"
42#include "AliRunLoader.h"
9265505b 43#include "AliLog.h"
44
92aeef15 45#include "Riostream.h"
4ce497c4 46#include "TF1.h"
92aeef15 47#include "TRandom.h"
48#include "TString.h"
49
4ce497c4 50///
9265505b 51/// \class AliMUONDigitizerV3
4ce497c4 52/// The digitizer is performing the transformation to go from SDigits (digits
53/// w/o any electronic noise) to Digits (w/ electronic noise, and decalibration)
54///
55/// The decalibration is performed by doing the reverse operation of the
56/// calibration, that is we do (Signal+pedestal)/gain -> ADC
57///
58/// Note also that the digitizer takes care of merging sdigits that belongs
59/// to the same pad, either because we're merging several input sdigit files
60/// or with a single file because the sdigitizer does not merge sdigits itself
61/// (for performance reason mainly, and because anyway we know we have to do it
62/// here, at the digitization level).
63///
64
65namespace
66{
67 AliMUON* muon()
68 {
69 return static_cast<AliMUON*>(gAlice->GetModule("MUON"));
70 }
4ce497c4 71}
72
73const Double_t AliMUONDigitizerV3::fgkNSigmas=3;
74
9265505b 75/// \cond CLASSIMP
92aeef15 76ClassImp(AliMUONDigitizerV3)
9265505b 77/// \endcond
92aeef15 78
79//_____________________________________________________________________________
80AliMUONDigitizerV3::AliMUONDigitizerV3(AliRunDigitizer* manager,
4ce497c4 81 Bool_t generateNoisyDigits)
92aeef15 82: AliDigitizer(manager),
92aeef15 83fIsInitialized(kFALSE),
84fOutputData(0x0),
85fCalibrationData(0x0),
86fTriggerProcessor(0x0),
4ce497c4 87fTriggerEfficiency(0x0),
ccea41d4 88fFindDigitIndexTimer(),
89fGenerateNoisyDigitsTimer(),
90fExecTimer(),
4ce497c4 91fNoiseFunction(0x0),
92fGenerateNoisyDigits(generateNoisyDigits)
92aeef15 93{
9265505b 94 /// Ctor.
95
92aeef15 96 AliDebug(1,Form("AliRunDigitizer=%p",fManager));
4ce497c4 97 fGenerateNoisyDigitsTimer.Start(kTRUE); fGenerateNoisyDigitsTimer.Stop();
98 fExecTimer.Start(kTRUE); fExecTimer.Stop();
99 fFindDigitIndexTimer.Start(kTRUE); fFindDigitIndexTimer.Stop();
92aeef15 100}
101
102//_____________________________________________________________________________
103AliMUONDigitizerV3::~AliMUONDigitizerV3()
104{
9265505b 105 /// Dtor. Note we're the owner of some pointers.
106
92aeef15 107 AliDebug(1,"dtor");
4ce497c4 108
92aeef15 109 delete fOutputData;
110 delete fCalibrationData;
111 delete fTriggerProcessor;
4ce497c4 112 delete fNoiseFunction;
113
a7b01aa5 114 AliDebug(1, Form("Execution time for FindDigitIndex() : R:%.2fs C:%.2fs",
4ce497c4 115 fFindDigitIndexTimer.RealTime(),fFindDigitIndexTimer.CpuTime()));
116 if ( fGenerateNoisyDigits )
117 {
a7b01aa5 118 AliDebug(1, Form("Execution time for GenerateNoisyDigits() : R:%.2fs C:%.2fs",
4ce497c4 119 fGenerateNoisyDigitsTimer.RealTime(),
120 fGenerateNoisyDigitsTimer.CpuTime()));
121 }
a7b01aa5 122 AliDebug(1, Form("Execution time for Exec() : R:%.2fs C:%.2fs",
4ce497c4 123 fExecTimer.RealTime(),fExecTimer.CpuTime()));
124
92aeef15 125}
126
127//_____________________________________________________________________________
128void
4ce497c4 129AliMUONDigitizerV3::ApplyResponseToTrackerDigit(AliMUONDigit& digit, Bool_t addNoise)
92aeef15 130{
9265505b 131 /// For tracking digits, starting from an ideal digit's charge, we :
132 ///
133 /// - add some noise (thus leading to a realistic charge), if requested to do so
134 /// - divide by a gain (thus decalibrating the digit)
135 /// - add a pedestal (thus decalibrating the digit)
136 /// - sets the signal to zero if below 3*sigma of the noise
59b91539 137
4ce497c4 138 static const Int_t kMaxADC = (1<<12)-1; // We code the charge on a 12 bits ADC.
59b91539 139
4ce497c4 140 Float_t signal = digit.Signal();
59b91539 141
142 if ( !addNoise )
143 {
144 digit.SetADC(TMath::Nint(signal));
145 return;
146 }
92aeef15 147
4ce497c4 148 Int_t detElemId = digit.DetElemId();
92aeef15 149
150 Int_t manuId = digit.ManuId();
151 Int_t manuChannel = digit.ManuChannel();
152
4ce497c4 153 AliMUONVCalibParam* pedestal = fCalibrationData->Pedestals(detElemId,manuId);
92aeef15 154 if (!pedestal)
59b91539 155 {
156 AliFatal(Form("Could not get pedestal for DE=%d manuId=%d",
157 detElemId,manuId));
158 }
05314992 159 Float_t pedestalMean = pedestal->ValueAsFloat(manuChannel,0);
160 Float_t pedestalSigma = pedestal->ValueAsFloat(manuChannel,1);
59b91539 161
4ce497c4 162 AliMUONVCalibParam* gain = fCalibrationData->Gains(detElemId,manuId);
92aeef15 163 if (!gain)
59b91539 164 {
165 AliFatal(Form("Could not get gain for DE=%d manuId=%d",
166 detElemId,manuId));
167 }
05314992 168 Float_t gainMean = gain->ValueAsFloat(manuChannel,0);
4ce497c4 169
59b91539 170 Float_t adcNoise = gRandom->Gaus(0.0,pedestalSigma);
171
92aeef15 172 Int_t adc;
59b91539 173
05314992 174 if ( gainMean < 1E-6 )
92aeef15 175 {
59b91539 176 AliError(Form("Got a too small gain %e for DE=%d manuId=%d manuChannel=%d. "
177 "Setting signal to 0.",
178 gainMean,detElemId,manuId,manuChannel));
92aeef15 179 adc = 0;
180 }
59b91539 181 else
182 {
183 adc = TMath::Nint( signal / gainMean + pedestalMean + adcNoise);///
184
185 if ( adc <= pedestalMean + fgkNSigmas*pedestalSigma )
186 {
187 adc = 0;
188 }
189 }
190
05314992 191 // be sure we stick to 12 bits.
4ce497c4 192 if ( adc > kMaxADC )
59b91539 193 {
194 adc = kMaxADC;
195 }
196
92aeef15 197 digit.SetPhysicsSignal(TMath::Nint(signal));
198 digit.SetSignal(adc);
199 digit.SetADC(adc);
200}
201
202//_____________________________________________________________________________
4ce497c4 203void
204AliMUONDigitizerV3::ApplyResponseToTriggerDigit(AliMUONDigit& digit,
205 AliMUONData* data)
206{
9265505b 207 /// \todo add comment
208
4ce497c4 209 if ( !fTriggerEfficiency ) return;
210
211 AliMUONDigit* correspondingDigit = FindCorrespondingDigit(digit,data);
212
a0dc51a6 213 if(!correspondingDigit)return;//reject bad correspondences
214
4ce497c4 215 Int_t detElemId = digit.DetElemId();
9265505b 216
217 AliMpSegmentation* segmentation = AliMpSegmentation::Instance();
4ce497c4 218 const AliMpVSegmentation* segment[2] =
219 {
9265505b 220 segmentation->GetMpSegmentation(detElemId,digit.Cathode()),
221 segmentation->GetMpSegmentation(detElemId,correspondingDigit->Cathode())
4ce497c4 222 };
223
224 AliMpPad pad[2] =
225 {
226 segment[0]->PadByIndices(AliMpIntPair(digit.PadX(),digit.PadY()),kTRUE),
227 segment[1]->PadByIndices(AliMpIntPair(correspondingDigit->PadX(),correspondingDigit->PadY()),kTRUE)
228 };
229
230 Int_t ix(0);
231 Int_t iy(1);
232
233 if (digit.Cathode()==0)
234 {
235 ix=1;
236 iy=0;
237 }
238
239 Float_t x = pad[ix].Position().X();
240 Float_t y = pad[iy].Position().Y();
241 if ( x==-1 && y==-1 )
242 {
243 x=-9999.;
244 y=-9999.;
245 AliError(Form("Got an unknown position for a digit in DE %d at (ix,iy)=(%d,%d)",
246 detElemId,pad[ix].GetIndices().GetFirst(),pad[iy].GetIndices().GetSecond()));
247 }
248 Float_t x0 = segment[0]->Dimensions().X();
249 Float_t y0 = segment[1]->Dimensions().Y();
250 TVector2 newCoord = fTriggerEfficiency->ChangeReferenceFrame(x, y, x0, y0);
251
252 Bool_t isTrig[2];
253 fTriggerEfficiency->IsTriggered(detElemId, newCoord.Px(), newCoord.Py(),
254 isTrig[0], isTrig[1]);
255
256 if (!isTrig[digit.Cathode()])
257 {
258 digit.SetSignal(0);
259 }
260
261 if ( &digit != correspondingDigit )
262 {
263 if (!isTrig[correspondingDigit->Cathode()])
264 {
265 correspondingDigit->SetSignal(0);
266 }
267 }
268}
269
270//_____________________________________________________________________________
92aeef15 271void
272AliMUONDigitizerV3::ApplyResponse()
273{
9265505b 274 /// Loop over all chamber digits, and apply the response to them
275 /// Note that this method may remove digits.
276
4ce497c4 277 const Bool_t kAddNoise = kTRUE;
278
92aeef15 279 for ( Int_t ich = 0; ich < AliMUONConstants::NCh(); ++ich )
4ce497c4 280 {
92aeef15 281 TClonesArray* digits = fOutputData->Digits(ich);
282 Int_t n = digits->GetEntriesFast();
4ce497c4 283 Bool_t trackingChamber = ( ich < AliMUONConstants::NTrackingCh() );
92aeef15 284 for ( Int_t i = 0; i < n; ++i )
285 {
286 AliMUONDigit* d = static_cast<AliMUONDigit*>(digits->UncheckedAt(i));
4ce497c4 287 if ( trackingChamber )
288 {
289 ApplyResponseToTrackerDigit(*d,kAddNoise);
290 }
291 else
292 {
293 ApplyResponseToTriggerDigit(*d,fOutputData);
294 }
92aeef15 295 if ( d->Signal() <= 0 )
296 {
297 digits->RemoveAt(i);
298 }
299 }
05314992 300 digits->Compress();
92aeef15 301 }
4ce497c4 302
303// The version below, using iterator, does not yet work (as the iterator
304// assumes it is reading digits from the tree, while in this case it's
305// writing...)
306//
307// AliMUONDigit* digit(0x0);
308//
309// // First loop on tracker digits
310// AliMUONDataIterator tracker(fOutputData,"D",AliMUONDataIterator::kTrackingChambers);
311//
312// while ( ( digit = static_cast<AliMUONDigit*>(tracker.Next()) ) )
313// {
314// ApplyResponseToTrackerDigit(*digit);
315// if ( digit->Signal() <= 0 )
316// {
317// tracker.Remove();
318// }
319//
320// }
321//
322// // Then loop on trigger digits
323// AliMUONDataIterator trigger(fOutputData,"D",AliMUONDataIterator::kTriggerChambers);
324//
325// while ( ( digit = static_cast<AliMUONDigit*>(trigger.Next()) ) )
326// {
327// ApplyResponseToTriggerDigit(*digit,fOutputData);
328// if ( digit->Signal() <= 0 )
329// {
330// trigger.Remove();
331// }
332// }
92aeef15 333}
334
335//_____________________________________________________________________________
336void
337AliMUONDigitizerV3::AddOrUpdateDigit(TClonesArray& array,
338 const AliMUONDigit& digit)
339{
9265505b 340 /// Add or update a digit, depending on whether there's already a digit
341 /// for the corresponding channel.
342
92aeef15 343 Int_t ix = FindDigitIndex(array,digit);
344
345 if (ix>=0)
346 {
347 AliMUONDigit* d = static_cast<AliMUONDigit*>(array.UncheckedAt(ix));
348 Bool_t ok = MergeDigits(digit,*d);
349 if (!ok)
350 {
351 AliError("Digits are not mergeable !");
352 }
92aeef15 353 }
354 else
355 {
356 ix = array.GetLast() + 1;
357 new(array[ix]) AliMUONDigit(digit);
358 }
359
360}
361
362//_____________________________________________________________________________
363void
364AliMUONDigitizerV3::Exec(Option_t*)
365{
9265505b 366 /// Main method.
367 /// We first loop over input files, and merge the sdigits we found there.
368 /// Second, we digitize all the resulting sdigits
369 /// Then we generate noise-only digits (for tracker only)
370 /// And we finally generate the trigger outputs.
4ce497c4 371
92aeef15 372 AliDebug(1, "Running digitizer.");
373
374 if ( fManager->GetNinputs() == 0 )
375 {
376 AliWarning("No input set. Nothing to do.");
377 return;
378 }
379
380 if ( !fIsInitialized )
381 {
382 AliError("Not initialized. Cannot perform the work. Sorry");
383 return;
384 }
385
4ce497c4 386 fExecTimer.Start(kFALSE);
387
92aeef15 388 Int_t nInputFiles = fManager->GetNinputs();
389
390 if ( fOutputData->TreeD() == 0x0 )
391 {
392 AliDebug(1,"Calling MakeDigitsContainer");
393 fOutputData->GetLoader()->MakeDigitsContainer();
394 }
395 fOutputData->MakeBranch("D,GLT");
396 fOutputData->SetTreeAddress("D,GLT");
397
398 // Loop over all the input files, and merge the sdigits found in those
399 // files.
400 for ( Int_t iFile = 0; iFile < nInputFiles; ++iFile )
401 {
402 AliMUONData* inputData = GetDataAccess(fManager->GetInputFolderName(iFile));
403 if (!inputData)
404 {
405 AliFatal(Form("Could not get access to input file #%d",iFile));
406 }
05314992 407
92aeef15 408 inputData->GetLoader()->LoadSDigits("READ");
92aeef15 409 inputData->SetTreeAddress("S");
410 inputData->GetSDigits();
05314992 411
412 MergeWithSDigits(*fOutputData,*inputData,fManager->GetMask(iFile));
413
92aeef15 414 inputData->ResetSDigits();
415 inputData->GetLoader()->UnloadSDigits();
416 delete inputData;
417 }
418
419 // At this point, we do have digit arrays (one per chamber) which contains
420 // the merging of all the sdigits of the input file(s).
421 // We now massage them to apply the detector response, i.e. this
422 // is here that we do the "digitization" work.
423
424 ApplyResponse();
4ce497c4 425
426 if ( fGenerateNoisyDigits )
427 {
428 // Generate noise-only digits for tracker.
429 GenerateNoisyDigits();
430 }
431
92aeef15 432 // We generate the global and local trigger decisions.
433 fTriggerProcessor->ExecuteTask();
434
435 // Fill the output treeD
436 fOutputData->Fill("D,GLT");
437
438 // Write to the output tree(D).
439 // Please note that as GlobalTrigger, LocalTrigger and Digits are in the same
440 // tree (=TreeD) in different branches, this WriteDigits in fact writes all of
441 // the 3 branches.
442 fOutputData->GetLoader()->WriteDigits("OVERWRITE");
443
444 // Finally, we clean up after ourselves.
445 fOutputData->ResetDigits();
446 fOutputData->ResetTrigger();
447 fOutputData->GetLoader()->UnloadDigits();
4ce497c4 448
449 fExecTimer.Stop();
92aeef15 450}
451
452//_____________________________________________________________________________
4ce497c4 453AliMUONDigit*
454AliMUONDigitizerV3::FindCorrespondingDigit(AliMUONDigit& digit,
85fec35d 455 AliMUONData* data) const
4ce497c4 456{
9265505b 457 /// \todo add comment
458
a0dc51a6 459 AliMUONDataIterator it(data,"D",AliMUONDataIterator::kTriggerChambers);
4ce497c4 460 AliMUONDigit* cd;
a0dc51a6 461
462 for(;;){
463 cd = static_cast<AliMUONDigit*>(it.Next());
464 if(!cd)continue;
465 if (cd->DetElemId() == digit.DetElemId() &&
466 cd->PadX() == digit.PadX() &&
467 cd->PadY() == digit.PadY() &&
468 cd->Cathode() == digit.Cathode()){
469 break;
470 }
471 }
472 //The corresponding digit is searched only forward in the AliMUONData.
473 //In this way when the first digit of the couple is given, the second digit is found and the efficiency is applied to both.
474 //Afterwards, when the second digit of the couple is given the first one is not found and hence efficiency is not applied again
4ce497c4 475
476 while ( ( cd = static_cast<AliMUONDigit*>(it.Next()) ) )
477 {
a0dc51a6 478 if(!cd)continue;//avoid problems when 1 digit is removed
4ce497c4 479 if ( cd->DetElemId() == digit.DetElemId() &&
480 cd->Hit() == digit.Hit() &&
481 cd->Cathode() != digit.Cathode() )
482 {
483 return cd;
484 }
485 }
486 return 0x0;
487}
488
489
490//_____________________________________________________________________________
92aeef15 491Int_t
4ce497c4 492AliMUONDigitizerV3::FindDigitIndex(TClonesArray& array,
493 const AliMUONDigit& digit) const
92aeef15 494{
9265505b 495 /// Return the index of digit within array, if that digit is there,
496 /// otherwise returns -1
497 ///
498 /// \todo FIXME: this is of course not the best implementation you can think of.
499 /// Reconsider the use of hit/digit map... ? (but be sure it's needed!)
4ce497c4 500
501 fFindDigitIndexTimer.Start(kFALSE);
502
92aeef15 503 Int_t n = array.GetEntriesFast();
504 for ( Int_t i = 0; i < n; ++i )
505 {
506 AliMUONDigit* d = static_cast<AliMUONDigit*>(array.UncheckedAt(i));
507 if ( d->DetElemId() == digit.DetElemId() &&
508 d->PadX() == digit.PadX() &&
509 d->PadY() == digit.PadY() &&
510 d->Cathode() == digit.Cathode() )
511 {
4ce497c4 512 fFindDigitIndexTimer.Stop();
92aeef15 513 return i;
514 }
515 }
4ce497c4 516 fFindDigitIndexTimer.Stop();
92aeef15 517 return -1;
518}
519
520//_____________________________________________________________________________
4ce497c4 521void
522AliMUONDigitizerV3::GenerateNoisyDigits()
523{
9265505b 524 /// According to a given probability, generate digits that
525 /// have a signal above the noise cut (ped+n*sigma_ped), i.e. digits
526 /// that are "only noise".
4ce497c4 527
528 if ( !fNoiseFunction )
529 {
530 fNoiseFunction = new TF1("AliMUONDigitizerV3::fNoiseFunction","gaus",
531 fgkNSigmas,fgkNSigmas*10);
532
533 fNoiseFunction->SetParameters(1,0,1);
534 }
535
536 fGenerateNoisyDigitsTimer.Start(kFALSE);
537
538 for ( Int_t i = 0; i < AliMUONConstants::NTrackingCh(); ++i )
539 {
540 AliMpDEIterator it;
541
542 it.First(i);
543
544 while ( !it.IsDone() )
545 {
546 for ( Int_t cathode = 0; cathode < 2; ++cathode )
547 {
548 GenerateNoisyDigitsForOneCathode(it.CurrentDE(),cathode);
549 }
550 it.Next();
551 }
552 }
553
554 fGenerateNoisyDigitsTimer.Stop();
555}
556
557//_____________________________________________________________________________
558void
559AliMUONDigitizerV3::GenerateNoisyDigitsForOneCathode(Int_t detElemId, Int_t cathode)
560{
9265505b 561 /// Generate noise-only digits for one cathode of one detection element.
562 /// Called by GenerateNoisyDigits()
4ce497c4 563
66f4c572 564 Int_t chamberId = AliMpDEManager::GetChamberId(detElemId);
565 TClonesArray* digits = fOutputData->Digits(chamberId);
4ce497c4 566
9265505b 567 const AliMpVSegmentation* seg
568 = AliMpSegmentation::Instance()->GetMpSegmentation(detElemId,cathode);
4ce497c4 569 Int_t nofPads = seg->NofPads();
570
571 Int_t maxIx = seg->MaxPadIndexX();
572 Int_t maxIy = seg->MaxPadIndexY();
573
cdea095e 574 static const Double_t kProbToBeOutsideNsigmas = TMath::Erfc(fgkNSigmas/TMath::Sqrt(2.0)) / 2. ;
4ce497c4 575
84aac932 576 Int_t nofNoisyPads = TMath::Nint(kProbToBeOutsideNsigmas*nofPads);
4ce497c4 577 if ( !nofNoisyPads ) return;
578
579 nofNoisyPads =
580 TMath::Nint(gRandom->Gaus(nofNoisyPads,
581 nofNoisyPads/TMath::Sqrt(nofNoisyPads)));
582
583 AliDebug(3,Form("DE %d cath %d nofNoisyPads %d",detElemId,cathode,nofNoisyPads));
584
585 for ( Int_t i = 0; i < nofNoisyPads; ++i )
586 {
587 Int_t ix(-1);
588 Int_t iy(-1);
589 do {
590 ix = gRandom->Integer(maxIx+1);
591 iy = gRandom->Integer(maxIy+1);
592 } while ( !seg->HasPad(AliMpIntPair(ix,iy)) );
593 AliMUONDigit d;
594 d.SetDetElemId(detElemId);
595 d.SetCathode(cathode);
596 d.SetPadX(ix);
597 d.SetPadY(iy);
598 if ( FindDigitIndex(*digits,d) >= 0 )
599 {
600 // this digit is already there, and not noise-only, we simply skip it
601 continue;
602 }
603 AliMpPad pad = seg->PadByIndices(AliMpIntPair(ix,iy));
604 Int_t manuId = pad.GetLocation().GetFirst();
605 Int_t manuChannel = pad.GetLocation().GetSecond();
606
607 d.SetElectronics(manuId,manuChannel);
608
609 AliMUONVCalibParam* pedestals = fCalibrationData->Pedestals(detElemId,manuId);
610
611 Float_t pedestalMean = pedestals->ValueAsFloat(manuChannel,0);
612 Float_t pedestalSigma = pedestals->ValueAsFloat(manuChannel,1);
613
614 Double_t ped = fNoiseFunction->GetRandom()*pedestalSigma;
59b91539 615
4ce497c4 616 d.SetSignal(TMath::Nint(ped+pedestalMean+0.5));
59b91539 617 d.SetPhysicsSignal(0);
4ce497c4 618 d.NoiseOnly(kTRUE);
619 AliDebug(3,Form("Adding a pure noise digit :"));
620 StdoutToAliDebug(3,cout << "Before Response: " << endl;
621 d.Print(););
622 ApplyResponseToTrackerDigit(d,kFALSE);
623 if ( d.Signal() > 0 )
624 {
625 AddOrUpdateDigit(*digits,d);
626 }
627 else
628 {
629 AliError("Pure noise below threshold. This should not happen. Not adding "
630 "this digit.");
631 }
632 StdoutToAliDebug(3,cout << "After Response: " << endl;
633 d.Print(););
634 }
635}
636
637//_____________________________________________________________________________
92aeef15 638AliMUONData*
639AliMUONDigitizerV3::GetDataAccess(const TString& folderName)
640{
9265505b 641 /// Create an AliMUONData to deal with data found in folderName.
642
92aeef15 643 AliDebug(1,Form("Getting access to folder %s",folderName.Data()));
644 AliRunLoader* runLoader = AliRunLoader::GetRunLoader(folderName);
645 if (!runLoader)
646 {
647 AliError(Form("Could not get RunLoader from folder %s",folderName.Data()));
648 return 0x0;
649 }
650 AliLoader* loader = static_cast<AliLoader*>(runLoader->GetLoader("MUONLoader"));
651 if (!loader)
652 {
653 AliError(Form("Could not get MuonLoader from folder %s",folderName.Data()));
654 return 0x0;
655 }
656 AliMUONData* data = new AliMUONData(loader,"MUON","MUONDataForDigitOutput");
657 AliDebug(1,Form("AliMUONData=%p loader=%p",data,loader));
658 return data;
659}
660
661//_____________________________________________________________________________
662Bool_t
663AliMUONDigitizerV3::Init()
664{
9265505b 665 /// Initialization of the TTask :
666 /// a) set the outputData pointer
667 /// b) create the calibrationData, according to run number
668 /// c) create the trigger processing task
669
92aeef15 670 AliDebug(1,"");
671
672 if ( fIsInitialized )
673 {
674 AliError("Object already initialized.");
675 return kFALSE;
676 }
677
678 if (!fManager)
679 {
680 AliError("fManager is null !");
681 return kFALSE;
682 }
683
684 fOutputData = GetDataAccess(fManager->GetOutputFolderName());
685 if (!fOutputData)
686 {
687 AliError("Can not perform digitization. I'm sorry");
688 return kFALSE;
689 }
690 AliDebug(1,Form("fOutputData=%p",fOutputData));
691
692 AliRunLoader* runLoader = fOutputData->GetLoader()->GetRunLoader();
693 AliRun* galice = runLoader->GetAliRun();
694 Int_t runnumber = galice->GetRunNumber();
695
696 fCalibrationData = new AliMUONCalibrationData(runnumber);
697
eb5d708c 698 fTriggerProcessor = new AliMUONTriggerElectronics(fOutputData,fCalibrationData);
4ce497c4 699
afb3ccf0 700 if ( muon()->GetTriggerEffCells() )
4ce497c4 701 {
702 fTriggerEfficiency = fCalibrationData->TriggerEfficiency();
703 if ( fTriggerEfficiency )
704 {
a7b01aa5 705 AliDebug(1, "Will apply trigger efficiency");
4ce497c4 706 }
707 else
708 {
709 AliError("I was requested to apply trigger efficiency, but I could "
710 "not get it !");
711 }
712 }
713
714 if ( fGenerateNoisyDigits )
715 {
a7b01aa5 716 AliDebug(1, "Will generate noise-only digits for tracker");
4ce497c4 717 }
718
92aeef15 719 fIsInitialized = kTRUE;
720 return kTRUE;
721}
722
723//_____________________________________________________________________________
724Bool_t
05314992 725AliMUONDigitizerV3::MergeDigits(const AliMUONDigit& src,
726 AliMUONDigit& srcAndDest)
92aeef15 727{
9265505b 728 /// Merge 2 digits (src and srcAndDest) into srcAndDest.
729
92aeef15 730 AliDebug(1,"Merging the following digits:");
05314992 731 StdoutToAliDebug(1,src.Print("tracks"););
732 StdoutToAliDebug(1,srcAndDest.Print("tracks"););
92aeef15 733
734 Bool_t check = ( src.DetElemId() == srcAndDest.DetElemId() &&
735 src.PadX() == srcAndDest.PadX() &&
736 src.PadY() == srcAndDest.PadY() &&
737 src.Cathode() == srcAndDest.Cathode() );
738 if (!check)
739 {
740 return kFALSE;
741 }
742
92aeef15 743 srcAndDest.AddSignal(src.Signal());
744 srcAndDest.AddPhysicsSignal(src.Physics());
05314992 745 for ( Int_t i = 0; i < src.Ntracks(); ++i )
746 {
747 srcAndDest.AddTrack(src.Track(i),src.TrackCharge(i));
748 }
749 StdoutToAliDebug(1,cout << "result:"; srcAndDest.Print("tracks"););
92aeef15 750 return kTRUE;
751}
752
753//_____________________________________________________________________________
754void
755AliMUONDigitizerV3::MergeWithSDigits(AliMUONData& outputData,
05314992 756 const AliMUONData& inputData, Int_t mask)
92aeef15 757{
9265505b 758 /// Merge the sdigits in inputData with the digits already present in outputData
759
92aeef15 760 AliDebug(1,"");
761
762 for ( Int_t ich = 0; ich < AliMUONConstants::NCh(); ++ich )
763 {
764 TClonesArray* iDigits = inputData.SDigits(ich);
765 TClonesArray* oDigits = outputData.Digits(ich);
766 if (!iDigits)
767 {
768 AliError(Form("Could not get sdigits for ich=%d",ich));
769 return;
770 }
771 Int_t nSDigits = iDigits->GetEntriesFast();
772 for ( Int_t k = 0; k < nSDigits; ++k )
773 {
774 AliMUONDigit* sdigit = static_cast<AliMUONDigit*>(iDigits->UncheckedAt(k));
92aeef15 775 if (!sdigit)
776 {
777 AliError(Form("Could not get sdigit for ich=%d and k=%d",ich,k));
778 }
779 else
780 {
05314992 781 // Update the track references using the mask.
782 // FIXME: this is dirty, for backward compatibility only.
783 // Should re-design all this way of keeping track of MC information...
784 if ( mask ) sdigit->PatchTracks(mask);
785 // Then add or update the digit to the output.
92aeef15 786 AddOrUpdateDigit(*oDigits,*sdigit);
787 }
9265505b 788 }
92aeef15 789 }
790}