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