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