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