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