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