]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONDigitizerV3.cxx
No more misaligned_geometry
[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
19 #include "AliMUONDigitizerV3.h"
20
21 #include "AliCDBManager.h"
22 #include "AliLog.h"
23 #include "AliMUON.h"
24 #include "AliMUONCalibrationData.h"
25 #include "AliMUONConstants.h"
26 #include "AliMUONDigit.h"
27 #include "AliMUONLogger.h"
28 #include "AliMUONTriggerEfficiencyCells.h"
29 #include "AliMUONTriggerElectronics.h"
30 #include "AliMUONTriggerStoreV1.h"
31 #include "AliMUONVCalibParam.h"
32 #include "AliMUONVDigitStore.h"
33 #include "AliMpCathodType.h"
34 #include "AliMpConstants.h"
35 #include "AliMpDEIterator.h"
36 #include "AliMpDEManager.h"
37 #include "AliMpDEManager.h"
38 #include "AliMpIntPair.h"
39 #include "AliMpPad.h"
40 #include "AliMpSegmentation.h"
41 #include "AliMpStationType.h"
42 #include "AliMpVSegmentation.h"
43 #include "AliRun.h"
44 #include "AliRunDigitizer.h"
45 #include "AliRunLoader.h"
46 #include <Riostream.h>
47 #include <TF1.h>
48 #include <TFile.h>
49 #include <TMath.h>
50 #include <TRandom.h>
51 #include <TString.h>
52 #include <TSystem.h>
53
54 #include "AliMUONGeometryTransformer.h" //ADDED for trigger noise
55 ///
56 /// \class AliMUONDigitizerV3
57 /// The digitizer is performing the transformation to go from SDigits (digits
58 /// w/o any electronic noise) to Digits (w/ electronic noise, and decalibration)
59 /// 
60 /// The decalibration is performed by doing the reverse operation of the
61 /// calibration, that is we do (Signal+pedestal)/gain -> ADC
62 ///
63 /// Note also that the digitizer takes care of merging sdigits that belongs
64 /// to the same pad, either because we're merging several input sdigit files
65 /// or with a single file because the sdigitizer does not merge sdigits itself
66 /// (for performance reason mainly, and because anyway we know we have to do it
67 /// here, at the digitization level).
68 ///
69 /// \author Laurent Aphecetche
70
71 namespace
72 {
73   AliMUON* muon()
74   {
75     return static_cast<AliMUON*>(gAlice->GetModule("MUON"));
76   }
77
78   //ADDED for trigger noise
79   const AliMUONGeometryTransformer* GetTransformer()
80   {
81       return muon()->GetGeometryTransformer();
82   }
83 }
84
85 const Double_t AliMUONDigitizerV3::fgkNSigmas=3;
86
87 /// \cond CLASSIMP
88 ClassImp(AliMUONDigitizerV3)
89 /// \endcond
90
91 //_____________________________________________________________________________
92 AliMUONDigitizerV3::AliMUONDigitizerV3(AliRunDigitizer* manager, 
93                                        Int_t generateNoisyDigits)
94 : AliDigitizer(manager),
95 fIsInitialized(kFALSE),
96 fCalibrationData(0x0),
97 fTriggerProcessor(0x0),
98 fTriggerEfficiency(0x0),
99 fGenerateNoisyDigitsTimer(),
100 fExecTimer(),
101 fNoiseFunction(0x0),
102 fNoiseFunctionTrig(0x0),
103   fGenerateNoisyDigits(generateNoisyDigits),
104   fLogger(new AliMUONLogger(1000)),
105 fTriggerStore(new AliMUONTriggerStoreV1),
106 fDigitStore(0x0),
107 fOutputDigitStore(0x0)
108 {
109   /// Ctor.
110
111   AliDebug(1,Form("AliRunDigitizer=%p",fManager));
112   fGenerateNoisyDigitsTimer.Start(kTRUE); fGenerateNoisyDigitsTimer.Stop();
113   fExecTimer.Start(kTRUE); fExecTimer.Stop();
114 }
115
116 //_____________________________________________________________________________
117 AliMUONDigitizerV3::~AliMUONDigitizerV3()
118 {
119   /// Dtor. Note we're the owner of some pointers.
120
121   AliDebug(1,"dtor");
122
123   delete fCalibrationData;
124   delete fTriggerProcessor;
125   delete fNoiseFunction;
126   delete fNoiseFunctionTrig;
127   delete fTriggerStore;
128   delete fDigitStore;
129   delete fOutputDigitStore;
130   
131   if ( fGenerateNoisyDigits )
132   {
133     AliDebug(1, Form("Execution time for GenerateNoisyDigits() : R:%.2fs C:%.2fs",
134                  fGenerateNoisyDigitsTimer.RealTime(),
135                  fGenerateNoisyDigitsTimer.CpuTime()));
136   }
137   AliDebug(1, Form("Execution time for Exec() : R:%.2fs C:%.2fs",
138                fExecTimer.RealTime(),fExecTimer.CpuTime()));
139  
140   AliInfo("Summary of messages");
141   fLogger->Print();
142   
143   delete fLogger;
144 }
145
146 //_____________________________________________________________________________
147 void 
148 AliMUONDigitizerV3::ApplyResponseToTrackerDigit(AliMUONVDigit& digit, Bool_t addNoise)
149 {
150   /// For tracking digits, starting from an ideal digit's charge, we :
151   ///
152   /// - add some noise (thus leading to a realistic charge), if requested to do so
153   /// - "divide" by a gain (thus decalibrating the digit)
154   /// - add a pedestal (thus decalibrating the digit)
155   /// - sets the signal to zero if below 3*sigma of the noise
156
157   static const Int_t kMaxADC = (1<<12)-1; // We code the charge on a 12 bits ADC.
158   
159   Float_t charge = digit.Charge();
160   
161   // We set the charge to 0, as the only relevant piece of information
162   // after Digitization is the ADC value.  
163   digit.SetCharge(0);
164   
165   if ( !addNoise )
166   {
167     digit.SetADC(TMath::Min(kMaxADC,TMath::Nint(charge)));
168     return;
169   }
170   
171   Int_t detElemId = digit.DetElemId();
172   
173   Int_t manuId = digit.ManuId();
174   Int_t manuChannel = digit.ManuChannel();
175   
176   AliMUONVCalibParam* pedestal = fCalibrationData->Pedestals(detElemId,manuId);
177   if (!pedestal)
178   {
179     fLogger->Log(Form("%s:%d:Could not get pedestal for DE=%4d manuId=%4d. Disabling.",
180                       __FILE__,__LINE__,
181                       detElemId,manuId));
182     digit.SetADC(0);
183     return;    
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     fLogger->Log(Form("%s:%d:Could not get gain for DE=%4d manuId=%4d. Disabling.",
192                       __FILE__,__LINE__,
193                       detElemId,manuId));
194     digit.SetADC(0);
195     return;        
196   }    
197
198   Float_t a0 = gain->ValueAsFloat(manuChannel,0);
199   Float_t a1 = gain->ValueAsFloat(manuChannel,1);
200
201   Int_t thres = gain->ValueAsInt(manuChannel,2);
202   Float_t chargeThres = a0*thres;
203   
204   Float_t padc(0); // (adc - ped) value
205   
206   if ( charge <= chargeThres || TMath::Abs(a1) < 1E-12 ) 
207   {
208     // linear part only
209     
210     if ( TMath::Abs(a0) > 1E-12 ) 
211     {
212       padc = charge/a0;    
213     }
214   }
215   else // charge > chargeThres && a1 not zero
216   {
217     // parabolic part
218     padc = TMath::Sqrt((chargeThres-charge)/a1) + thres;
219   }
220
221   Float_t adcNoise = gRandom->Gaus(0.0,pedestalSigma);
222   Int_t adc(0);
223   
224   if ( padc > 0 ) 
225   {
226     adc = TMath::Nint(padc + pedestalMean + adcNoise);
227   }
228   
229   // be sure we stick to 12 bits.
230   if ( adc > kMaxADC )
231   {
232     adc = kMaxADC;
233   }
234   
235   AliDebug(3,Form("DE %4d Manu %4d Ch %2d Charge %e A0 %e A1 %e Thres %d padc %e ADC %4d",
236                   detElemId,manuId,manuChannel,charge,
237                   gain->ValueAsFloat(manuChannel,0),
238                   gain->ValueAsFloat(manuChannel,1),
239                   gain->ValueAsInt(manuChannel,2),
240                   padc,
241                   adc));
242                   
243   digit.SetADC(adc);
244 }
245
246 //_____________________________________________________________________________
247 void 
248 AliMUONDigitizerV3::ApplyResponseToTriggerDigit(const AliMUONVDigitStore& digitStore,
249                                                 AliMUONVDigit& digit)
250 {
251   /// \todo add comment
252
253   if ( !fTriggerEfficiency ) return;
254
255   if (digit.IsEfficiencyApplied()) return;
256
257   AliMUONVDigit* correspondingDigit = FindCorrespondingDigit(digitStore,digit);
258
259   if (!correspondingDigit) return; //reject bad correspondences
260
261   Int_t detElemId = digit.DetElemId();
262
263   AliMpSegmentation* segmentation = AliMpSegmentation::Instance();
264   const AliMpVSegmentation* segment[2] = 
265   {
266     segmentation->GetMpSegmentation(detElemId,AliMp::GetCathodType(digit.Cathode())), 
267     segmentation->GetMpSegmentation(detElemId,AliMp::GetCathodType(correspondingDigit->Cathode()))
268   };
269
270   AliMpPad pad[2] = 
271   {
272     segment[0]->PadByIndices(AliMpIntPair(digit.PadX(),digit.PadY()),kTRUE), 
273     segment[1]->PadByIndices(AliMpIntPair(correspondingDigit->PadX(),correspondingDigit->PadY()),kTRUE)
274   };
275
276   Int_t p0(1);
277   if (digit.Cathode()==0) p0=0;
278
279   AliMpIntPair location = pad[p0].GetLocation(0);
280   Int_t nboard = location.GetFirst();
281
282   Bool_t isTrig[2];
283
284   fTriggerEfficiency->IsTriggered(detElemId, nboard, 
285                                   isTrig[0], isTrig[1]);
286   digit.EfficiencyApplied(kTRUE);
287   correspondingDigit->EfficiencyApplied(kTRUE);
288
289   if (!isTrig[digit.Cathode()])
290   {
291           digit.SetCharge(0);
292   }
293   
294   if ( &digit != correspondingDigit )
295   {
296           if (!isTrig[correspondingDigit->Cathode()])
297     {
298       correspondingDigit->SetCharge(0);
299           }
300   }
301 }
302
303 //_____________________________________________________________________________
304 void
305 AliMUONDigitizerV3::ApplyResponse(const AliMUONVDigitStore& store,
306                                   AliMUONVDigitStore& filteredStore)
307 {
308   /// Loop over all chamber digits, and apply the response to them
309   /// Note that this method may remove digits.
310
311   filteredStore.Clear();
312   
313   const Bool_t kAddNoise = kTRUE;
314   
315   TIter next(store.CreateIterator());
316   AliMUONVDigit* digit;
317   
318   while ( ( digit = static_cast<AliMUONVDigit*>(next()) ) )
319   {
320     AliMp::StationType stationType = AliMpDEManager::GetStationType(digit->DetElemId());
321     
322     if ( stationType != AliMp::kStationTrigger )
323     {
324       ApplyResponseToTrackerDigit(*digit,kAddNoise);
325     }
326     else
327     {
328       ApplyResponseToTriggerDigit(store,*digit);
329     }
330     if ( digit->ADC() > 0  || digit->Charge() > 0 )
331     {
332       filteredStore.Add(*digit,AliMUONVDigitStore::kIgnore);
333     }
334   }
335 }    
336
337 //_____________________________________________________________________________
338 void
339 AliMUONDigitizerV3::Exec(Option_t*)
340 {
341   /// Main method.
342   /// We first loop over input files, and merge the sdigits we found there.
343   /// Second, we digitize all the resulting sdigits
344   /// Then we generate noise-only digits (for tracker only)
345   /// And we finally generate the trigger outputs.
346     
347   AliDebug(1, "Running digitizer.");
348   
349   if ( fManager->GetNinputs() == 0 )
350   {
351     AliWarning("No input set. Nothing to do.");
352     return;
353   }
354   
355   if ( !fIsInitialized )
356   {
357     AliError("Not initialized. Cannot perform the work. Sorry");
358     return;
359   }
360   
361   fExecTimer.Start(kFALSE);
362
363   Int_t nInputFiles = fManager->GetNinputs();
364   
365   AliLoader* outputLoader = GetLoader(fManager->GetOutputFolderName());
366   
367   outputLoader->MakeDigitsContainer();
368   
369   TTree* oTreeD = outputLoader->TreeD();
370   
371   if (!oTreeD) 
372   {
373     AliFatal("Cannot create output TreeD");
374   }
375
376   // Loop over all the input files, and merge the sdigits found in those
377   // files.
378   
379   for ( Int_t iFile = 0; iFile < nInputFiles; ++iFile )
380   {    
381     AliLoader* inputLoader = GetLoader(fManager->GetInputFolderName(iFile));
382
383     inputLoader->LoadSDigits("READ");
384
385     TTree* iTreeS = inputLoader->TreeS();
386     if (!iTreeS)
387     {
388       AliFatal(Form("Could not get access to input file #%d",iFile));
389     }
390     
391     AliMUONVDigitStore* inputStore = AliMUONVDigitStore::Create(*iTreeS);
392     inputStore->Connect(*iTreeS);
393     
394     iTreeS->GetEvent(0);
395     
396     MergeWithSDigits(fDigitStore,*inputStore,fManager->GetMask(iFile));
397
398     inputLoader->UnloadSDigits();
399     
400     delete inputStore;
401   }
402   
403   // At this point, we do have digit arrays (one per chamber) which contains 
404   // the merging of all the sdigits of the input file(s).
405   // We now massage them to apply the detector response, i.e. this
406   // is here that we do the "digitization" work.
407   
408   if (!fOutputDigitStore)
409   {
410     fOutputDigitStore = fDigitStore->Create();
411   }
412   
413   if ( fGenerateNoisyDigits>=2 )
414   {
415     // Generate noise-only digits for trigger.
416     GenerateNoisyDigitsForTrigger(*fDigitStore);
417   }
418
419   ApplyResponse(*fDigitStore,*fOutputDigitStore);
420   
421   if ( fGenerateNoisyDigits )
422   {
423     // Generate noise-only digits for tracker.
424     GenerateNoisyDigits(*fOutputDigitStore);
425   }
426   
427   // We generate the global and local trigger decisions.
428   fTriggerProcessor->Digits2Trigger(*fOutputDigitStore,*fTriggerStore);
429
430   // Prepare output tree
431   Bool_t okD = fOutputDigitStore->Connect(*oTreeD,kFALSE);
432   Bool_t okT = fTriggerStore->Connect(*oTreeD,kFALSE);
433   if (!okD || !okT)
434   {
435     AliError(Form("Could not make branch : Digit %d Trigger %d",okD,okT));
436     return;
437   }
438   
439   // Fill the output treeD
440   oTreeD->Fill();
441   
442   // Write to the output tree(D).
443   // Please note that as GlobalTrigger, LocalTrigger and Digits are in the same
444   // tree (=TreeD) in different branches, this WriteDigits in fact writes all of 
445   // the 3 branches.
446   outputLoader->WriteDigits("OVERWRITE");  
447   
448   outputLoader->UnloadDigits();
449   
450   // Finally, we clean up after ourselves.
451   fTriggerStore->Clear();
452   fDigitStore->Clear();
453   fOutputDigitStore->Clear();
454   fExecTimer.Stop();
455 }
456
457 //_____________________________________________________________________________
458 AliMUONVDigit* 
459 AliMUONDigitizerV3::FindCorrespondingDigit(const AliMUONVDigitStore& digitStore,
460                                            AliMUONVDigit& digit) const
461 {                                                
462   /// Find, if it exists, the digit corresponding to digit.Hit(), in the 
463   /// other cathode
464
465   TIter next(digitStore.CreateIterator());
466   AliMUONVDigit* d;
467   
468   while ( ( d = static_cast<AliMUONVDigit*>(next()) ) )
469   {
470     if ( d->DetElemId() == digit.DetElemId() &&
471          d->Hit() == digit.Hit() &&
472          d->Cathode() != digit.Cathode() )
473     {
474       return d;
475     }      
476   }    
477   return 0x0;
478 }
479
480
481 //_____________________________________________________________________________
482 void
483 AliMUONDigitizerV3::GenerateNoisyDigits(AliMUONVDigitStore& digitStore)
484 {
485   /// According to a given probability, generate digits that
486   /// have a signal above the noise cut (ped+n*sigma_ped), i.e. digits
487   /// that are "only noise".
488   
489   if ( !fNoiseFunction )
490   {
491     fNoiseFunction = new TF1("AliMUONDigitizerV3::fNoiseFunction","gaus",
492                              fgkNSigmas,fgkNSigmas*10);
493     
494     fNoiseFunction->SetParameters(1,0,1);
495   }
496   
497   fGenerateNoisyDigitsTimer.Start(kFALSE);
498   
499   for ( Int_t i = 0; i < AliMUONConstants::NTrackingCh(); ++i )
500   {
501     AliMpDEIterator it;
502   
503     it.First(i);
504   
505     while ( !it.IsDone() )
506     {
507       for ( Int_t cathode = 0; cathode < 2; ++cathode )
508       {
509         GenerateNoisyDigitsForOneCathode(digitStore,it.CurrentDEId(),cathode);
510       }
511       it.Next();
512     }
513   }
514   
515   fGenerateNoisyDigitsTimer.Stop();
516 }
517  
518 //_____________________________________________________________________________
519 void
520 AliMUONDigitizerV3::GenerateNoisyDigitsForOneCathode(AliMUONVDigitStore& digitStore,
521                                                      Int_t detElemId, Int_t cathode)
522 {
523   /// Generate noise-only digits for one cathode of one detection element.
524   /// Called by GenerateNoisyDigits()
525   
526   const AliMpVSegmentation* seg 
527     = AliMpSegmentation::Instance()->GetMpSegmentation(detElemId,AliMp::GetCathodType(cathode));
528   Int_t nofPads = seg->NofPads();
529   
530   Int_t maxIx = seg->MaxPadIndexX();
531   Int_t maxIy = seg->MaxPadIndexY();
532   
533   static const Double_t kProbToBeOutsideNsigmas = TMath::Erfc(fgkNSigmas/TMath::Sqrt(2.0)) / 2. ;
534   
535   Int_t nofNoisyPads = TMath::Nint(kProbToBeOutsideNsigmas*nofPads);
536   if ( !nofNoisyPads ) return;
537   
538   nofNoisyPads = 
539     TMath::Nint(gRandom->Gaus(nofNoisyPads,
540                               nofNoisyPads/TMath::Sqrt(nofNoisyPads)));
541   
542   AliDebug(3,Form("DE %d cath %d nofNoisyPads %d",detElemId,cathode,nofNoisyPads));
543   
544   for ( Int_t i = 0; i < nofNoisyPads; ++i ) 
545   {
546     Int_t ix(-1);
547     Int_t iy(-1);
548     AliMpPad pad;
549     
550     do {
551       ix = gRandom->Integer(maxIx+1);
552       iy = gRandom->Integer(maxIy+1);
553       pad = seg->PadByIndices(AliMpIntPair(ix,iy),kFALSE);
554     } while ( !pad.IsValid() );
555
556     Int_t manuId = pad.GetLocation().GetFirst();
557     Int_t manuChannel = pad.GetLocation().GetSecond();    
558
559     AliMUONVCalibParam* pedestals = fCalibrationData->Pedestals(detElemId,manuId);
560     
561     if (!pedestals) 
562     {
563       // no pedestal available for this channel, simply give up
564       continue;
565     }
566     
567     AliMUONVDigit* d = digitStore.CreateDigit(detElemId,manuId,manuChannel,cathode);
568     
569     d->SetPadXY(ix,iy);
570     
571     Float_t pedestalMean = pedestals->ValueAsFloat(manuChannel,0);
572     Float_t pedestalSigma = pedestals->ValueAsFloat(manuChannel,1);
573     
574     Double_t ped = fNoiseFunction->GetRandom()*pedestalSigma;
575
576     d->SetCharge(TMath::Nint(ped+pedestalMean+0.5));
577     d->NoiseOnly(kTRUE);
578     ApplyResponseToTrackerDigit(*d,kFALSE);
579     if ( d->ADC() > 0 )
580     {
581       Bool_t ok = digitStore.Add(*d,AliMUONVDigitStore::kDeny);
582       // this can happen (that we randomly chose a digit that is
583       // already there). We simply ignore this, but log the occurence
584       // to cross-check that it's not too frequent.
585       if (!ok)
586       {
587         fLogger->Log("Collision while adding noiseOnly digit");
588       }
589       else
590       {
591         fLogger->Log("Added noiseOnly digit");
592       }
593     }
594     else
595     {
596       AliError("Pure noise below threshold. This should not happen. Not adding "
597                "this digit.");
598     }
599     delete d;
600   }
601 }
602
603
604 //_____________________________________________________________________________
605 void
606 AliMUONDigitizerV3::GenerateNoisyDigitsForTrigger(AliMUONVDigitStore& digitStore)
607 {
608   /// Generate noise-only digits for one cathode of one detection element.
609   /// Called by GenerateNoisyDigits()
610
611   if ( !fNoiseFunctionTrig )
612   {
613     fNoiseFunctionTrig = new TF1("AliMUONDigitizerV3::fNoiseFunctionTrig","landau",
614                                  50.,270.);
615     
616     fNoiseFunctionTrig->SetParameters(3.91070e+02, 9.85026, 9.35881e-02);
617   }
618
619   AliMpPad pad[2];
620   AliMUONVDigit *d[2]={0x0};
621
622   for ( Int_t chamberId = AliMUONConstants::NTrackingCh(); chamberId < AliMUONConstants::NCh(); ++chamberId )
623   {
624   
625     Int_t nofNoisyPads = 50;
626
627     Float_t r=-1, fi = 0., gx, gy, x, y, z, xg01, yg01, zg, xg02, yg02;
628     AliMpDEIterator it;
629   
630     AliDebug(3,Form("Chamber %d nofNoisyPads %d",chamberId,nofNoisyPads));
631
632     for ( Int_t i = 0; i < nofNoisyPads; ++i )
633     {
634       //printf("Generating noise %i\n",i);
635         Int_t ix(-1);
636         Int_t iy(-1);
637         Bool_t isOk = kFALSE;
638         Int_t detElemId = -1;
639         do {
640           //r = gRandom->Landau(9.85026, 9.35881e-02);
641             r = fNoiseFunctionTrig->GetRandom();
642             fi = 2. * TMath::Pi() * gRandom->Rndm();
643             //printf("r = %f\tfi = %f\n", r, fi);
644             gx = r * TMath::Cos(fi);
645             gy = r * TMath::Sin(fi);
646
647             for ( it.First(chamberId); ! it.IsDone(); it.Next() ){
648                 Int_t currDetElemId = it.CurrentDEId();
649                 const AliMpVSegmentation* seg
650                     = AliMpSegmentation::Instance()->GetMpSegmentation(currDetElemId,AliMp::GetCathodType(0));
651                 if (!seg) continue;
652                 Float_t deltax = seg->Dimensions().X();
653                 Float_t deltay = seg->Dimensions().Y();
654                 GetTransformer()->Local2Global(currDetElemId, -deltax, -deltay, 0, xg01, yg01, zg);
655                 GetTransformer()->Local2Global(currDetElemId,  deltax,  deltay, 0, xg02, yg02, zg);
656                 Float_t xg1 = xg01, xg2 = xg02, yg1 = yg01, yg2 = yg02;
657                 if(xg01>xg02){
658                     xg1 = xg02;
659                     xg2 = xg01;
660                 }
661                 if(yg01>yg02){
662                     yg1 = yg02;
663                     yg2 = yg01;
664                 }
665                 if(gx>=xg1 && gx<=xg2 && gy>=yg1 && gy<=yg2){
666                     detElemId = currDetElemId;
667                     GetTransformer()->Global2Local(detElemId, gx, gy, 0, x, y, z);
668                     pad[0] = seg->PadByPosition(TVector2(x,y),kFALSE);
669                     if(!pad[0].IsValid()) continue;
670                     isOk = kTRUE;
671                     break;
672                 }
673             } // loop on slats
674         } while ( !isOk );
675
676         const AliMpVSegmentation* seg1
677             = AliMpSegmentation::Instance()->GetMpSegmentation(detElemId,AliMp::GetCathodType(1));
678         pad[1] = seg1->PadByPosition(TVector2(x,y),kFALSE);
679
680         for ( Int_t cathode = 0; cathode < 2; ++cathode ){
681           Int_t manuId = pad[cathode].GetLocation(0).GetFirst();
682           Int_t manuChannel = pad[cathode].GetLocation(0).GetSecond();    
683           d[cathode] = digitStore.CreateDigit(detElemId,manuId,manuChannel,cathode);
684           ix = pad[cathode].GetIndices().GetFirst();
685           iy = pad[cathode].GetIndices().GetSecond();
686           d[cathode]->SetPadXY(ix,iy);
687           //d[cathode].SetSignal(1);
688           //d[cathode].SetPhysicsSignal(0);
689           d[cathode]->SetCharge(1);
690           d[cathode]->NoiseOnly(kTRUE);
691           AliDebug(3,Form("Adding a pure noise digit :"));
692
693           Bool_t ok = digitStore.Add(*d[cathode],AliMUONVDigitStore::kDeny);
694           if (!ok)
695           {
696               fLogger->Log("Collision while adding TriggerNoise digit");
697           }
698           else
699           {
700               fLogger->Log("Added triggerNoise digit");
701           }
702         } //loop on cathodes
703     } // loop on noisy pads
704   } // loop on chambers
705 }
706
707
708 //_____________________________________________________________________________
709 AliLoader*
710 AliMUONDigitizerV3::GetLoader(const TString& folderName)
711 {
712   /// Get a MUON loader
713
714   AliDebug(2,Form("Getting access to folder %s",folderName.Data()));
715   AliLoader* loader = AliRunLoader::GetDetectorLoader("MUON",folderName.Data());
716   if (!loader)
717   {
718     AliError(Form("Could not get MuonLoader from folder %s",folderName.Data()));
719     return 0x0;
720   }
721   return loader;
722 }
723
724 //_____________________________________________________________________________
725 Bool_t
726 AliMUONDigitizerV3::Init()
727 {
728   /// Initialization of the TTask :
729   /// a) create the calibrationData, according to run number
730   /// b) create the trigger processing task
731
732   AliDebug(2,"");
733   
734   if ( fIsInitialized )
735   {
736     AliError("Object already initialized.");
737     return kFALSE;
738   }
739   
740   if (!fManager)
741   {
742     AliError("fManager is null !");
743     return kFALSE;
744   }
745   
746   Int_t runnumber = AliCDBManager::Instance()->GetRun();
747   
748   fCalibrationData = new AliMUONCalibrationData(runnumber);
749   if ( !fCalibrationData->Pedestals() )
750   {
751     AliFatal("Could not access pedestals from OCDB !");
752   }
753   if ( !fCalibrationData->Gains() )
754   {
755     AliFatal("Could not access gains from OCDB !");
756   }
757   fTriggerProcessor = new AliMUONTriggerElectronics(fCalibrationData);
758   
759   if ( muon()->GetTriggerEffCells() )
760   {
761     fTriggerEfficiency = fCalibrationData->TriggerEfficiency();
762     if ( fTriggerEfficiency )
763     {
764       AliDebug(1, "Will apply trigger efficiency");
765     }
766     else
767     {
768       AliFatal("I was requested to apply trigger efficiency, but I could "
769                "not get it !");
770     }
771   }
772   
773   AliDebug(1, Form("Will %s generate noise-only digits for tracker",
774                      (fGenerateNoisyDigits ? "":"NOT")));
775
776   fIsInitialized = kTRUE;
777   return kTRUE;
778 }
779
780 //_____________________________________________________________________________
781 void 
782 AliMUONDigitizerV3::MergeWithSDigits(AliMUONVDigitStore*& outputStore,
783                                      const AliMUONVDigitStore& input,
784                                      Int_t mask)
785 {
786   /// Merge the sdigits in inputData with the digits already present in outputData
787   
788   if ( !outputStore ) outputStore = input.Create();
789   
790   TIter next(input.CreateIterator());
791   AliMUONVDigit* sdigit;
792   
793   while ( ( sdigit = static_cast<AliMUONVDigit*>(next()) ) )
794   {
795     // Update the track references using the mask.
796     // FIXME: this is dirty, for backward compatibility only.
797     // Should re-design all this way of keeping track of MC information...
798     if ( mask ) sdigit->PatchTracks(mask);
799     // Then add or update the digit to the output.
800     AliMUONVDigit* added = outputStore->Add(*sdigit,AliMUONVDigitStore::kMerge);
801     if (!added)
802     {
803       AliError("Could not add digit in merge mode");
804     }
805   }
806 }