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