]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONDigitizerV3.cxx
Intrinsic chamber efficiency calculation performed during reconstruction (Diego)
[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 signal = digit.Charge();
160   
161   if ( !addNoise )
162   {
163     digit.SetADC(TMath::Min(kMaxADC,TMath::Nint(signal)));
164     return;
165   }
166   
167   Int_t detElemId = digit.DetElemId();
168   
169   Int_t manuId = digit.ManuId();
170   Int_t manuChannel = digit.ManuChannel();
171   
172   AliMUONVCalibParam* pedestal = fCalibrationData->Pedestals(detElemId,manuId);
173   if (!pedestal)
174   {
175     fLogger->Log(Form("%s:%d:Could not get pedestal for DE=%4d manuId=%4d. Disabling.",
176                       __FILE__,__LINE__,
177                       detElemId,manuId));
178     digit.SetCharge(0);
179     digit.SetADC(0);
180     return;    
181   }
182   Float_t pedestalMean = pedestal->ValueAsFloat(manuChannel,0);
183   Float_t pedestalSigma = pedestal->ValueAsFloat(manuChannel,1);
184   
185   AliMUONVCalibParam* gain = fCalibrationData->Gains(detElemId,manuId);
186   if (!gain)
187   {
188     fLogger->Log(Form("%s:%d:Could not get gain for DE=%4d manuId=%4d. Disabling.",
189                       __FILE__,__LINE__,
190                       detElemId,manuId));
191     digit.SetCharge(0);
192     digit.SetADC(0);
193     return;        
194   }    
195   Float_t gainMean = gain->ValueAsFloat(manuChannel,0);
196   
197   Float_t adcNoise = gRandom->Gaus(0.0,pedestalSigma);
198   
199   Int_t adc;
200   
201   if ( gainMean < 1E-6 )
202   {
203     AliError(Form("Got a too small gain %e for DE=%d manuId=%d manuChannel=%d. "
204                   "Setting signal to 0.",
205                   gainMean,detElemId,manuId,manuChannel));
206     adc = 0;
207   }
208   else
209   {
210     adc = TMath::Nint( signal / gainMean + pedestalMean + adcNoise);///
211     
212     if ( adc <= pedestalMean + fgkNSigmas*pedestalSigma ) 
213     {
214       adc = 0;
215     }
216   }
217   
218   // be sure we stick to 12 bits.
219   if ( adc > kMaxADC )
220   {
221     adc = kMaxADC;
222   }
223   
224   digit.SetCharge(adc);
225   digit.SetADC(adc);
226 }
227
228 //_____________________________________________________________________________
229 void 
230 AliMUONDigitizerV3::ApplyResponseToTriggerDigit(const AliMUONVDigitStore& digitStore,
231                                                 AliMUONVDigit& digit)
232 {
233   /// \todo add comment
234
235   if ( !fTriggerEfficiency ) return;
236
237   if (digit.IsEfficiencyApplied()) return;
238
239   AliMUONVDigit* correspondingDigit = FindCorrespondingDigit(digitStore,digit);
240
241   if (!correspondingDigit) return; //reject bad correspondences
242
243   Int_t detElemId = digit.DetElemId();
244
245   AliMpSegmentation* segmentation = AliMpSegmentation::Instance();
246   const AliMpVSegmentation* segment[2] = 
247   {
248     segmentation->GetMpSegmentation(detElemId,AliMp::GetCathodType(digit.Cathode())), 
249     segmentation->GetMpSegmentation(detElemId,AliMp::GetCathodType(correspondingDigit->Cathode()))
250   };
251
252   AliMpPad pad[2] = 
253   {
254     segment[0]->PadByIndices(AliMpIntPair(digit.PadX(),digit.PadY()),kTRUE), 
255     segment[1]->PadByIndices(AliMpIntPair(correspondingDigit->PadX(),correspondingDigit->PadY()),kTRUE)
256   };
257
258   Int_t p0(1);
259   if (digit.Cathode()==0) p0=0;
260
261   AliMpIntPair location = pad[p0].GetLocation(0);
262   Int_t nboard = location.GetFirst();
263
264   Bool_t isTrig[2];
265
266   fTriggerEfficiency->IsTriggered(detElemId, nboard, 
267                                   isTrig[0], isTrig[1]);
268   digit.EfficiencyApplied(kTRUE);
269   correspondingDigit->EfficiencyApplied(kTRUE);
270
271   if (!isTrig[digit.Cathode()])
272   {
273           digit.SetCharge(0);
274   }
275   
276   if ( &digit != correspondingDigit )
277   {
278           if (!isTrig[correspondingDigit->Cathode()])
279     {
280       correspondingDigit->SetCharge(0);
281           }
282   }
283 }
284
285 //_____________________________________________________________________________
286 void
287 AliMUONDigitizerV3::ApplyResponse(const AliMUONVDigitStore& store,
288                                   AliMUONVDigitStore& filteredStore)
289 {
290   /// Loop over all chamber digits, and apply the response to them
291   /// Note that this method may remove digits.
292
293   filteredStore.Clear();
294   
295   const Bool_t kAddNoise = kTRUE;
296   
297   TIter next(store.CreateIterator());
298   AliMUONVDigit* digit;
299   
300   while ( ( digit = static_cast<AliMUONVDigit*>(next()) ) )
301   {
302     AliMp::StationType stationType = AliMpDEManager::GetStationType(digit->DetElemId());
303     
304     if ( stationType != AliMp::kStationTrigger )
305     {
306       ApplyResponseToTrackerDigit(*digit,kAddNoise);
307     }
308     else
309     {
310       ApplyResponseToTriggerDigit(store,*digit);
311     }
312     if ( digit->Charge() > 0  )
313     {
314       filteredStore.Add(*digit,AliMUONVDigitStore::kIgnore);
315     }
316   }
317 }    
318
319 //_____________________________________________________________________________
320 void
321 AliMUONDigitizerV3::Exec(Option_t*)
322 {
323   /// Main method.
324   /// We first loop over input files, and merge the sdigits we found there.
325   /// Second, we digitize all the resulting sdigits
326   /// Then we generate noise-only digits (for tracker only)
327   /// And we finally generate the trigger outputs.
328     
329   AliDebug(1, "Running digitizer.");
330   
331   if ( fManager->GetNinputs() == 0 )
332   {
333     AliWarning("No input set. Nothing to do.");
334     return;
335   }
336   
337   if ( !fIsInitialized )
338   {
339     AliError("Not initialized. Cannot perform the work. Sorry");
340     return;
341   }
342   
343   fExecTimer.Start(kFALSE);
344
345   Int_t nInputFiles = fManager->GetNinputs();
346   
347   AliLoader* outputLoader = GetLoader(fManager->GetOutputFolderName());
348   
349   outputLoader->MakeDigitsContainer();
350   
351   TTree* oTreeD = outputLoader->TreeD();
352   
353   if (!oTreeD) 
354   {
355     AliFatal("Cannot create output TreeD");
356   }
357
358   // Loop over all the input files, and merge the sdigits found in those
359   // files.
360   
361   for ( Int_t iFile = 0; iFile < nInputFiles; ++iFile )
362   {    
363     AliLoader* inputLoader = GetLoader(fManager->GetInputFolderName(iFile));
364
365     inputLoader->LoadSDigits("READ");
366
367     TTree* iTreeS = inputLoader->TreeS();
368     if (!iTreeS)
369     {
370       AliFatal(Form("Could not get access to input file #%d",iFile));
371     }
372     
373     AliMUONVDigitStore* inputStore = AliMUONVDigitStore::Create(*iTreeS);
374     inputStore->Connect(*iTreeS);
375     
376     iTreeS->GetEvent(0);
377     
378     MergeWithSDigits(fDigitStore,*inputStore,fManager->GetMask(iFile));
379
380     inputLoader->UnloadSDigits();
381     
382     delete inputStore;
383   }
384   
385   // At this point, we do have digit arrays (one per chamber) which contains 
386   // the merging of all the sdigits of the input file(s).
387   // We now massage them to apply the detector response, i.e. this
388   // is here that we do the "digitization" work.
389   
390   if (!fOutputDigitStore)
391   {
392     fOutputDigitStore = fDigitStore->Create();
393   }
394   
395   if ( fGenerateNoisyDigits>=2 )
396   {
397     // Generate noise-only digits for trigger.
398     GenerateNoisyDigitsForTrigger(*fDigitStore);
399   }
400
401   ApplyResponse(*fDigitStore,*fOutputDigitStore);
402   
403   if ( fGenerateNoisyDigits )
404   {
405     // Generate noise-only digits for tracker.
406     GenerateNoisyDigits(*fOutputDigitStore);
407   }
408   
409   // We generate the global and local trigger decisions.
410   fTriggerProcessor->Digits2Trigger(*fOutputDigitStore,*fTriggerStore);
411
412   // Prepare output tree
413   Bool_t okD = fOutputDigitStore->Connect(*oTreeD,kFALSE);
414   Bool_t okT = fTriggerStore->Connect(*oTreeD,kFALSE);
415   if (!okD || !okT)
416   {
417     AliError(Form("Could not make branch : Digit %d Trigger %d",okD,okT));
418     return;
419   }
420   
421   // Fill the output treeD
422   oTreeD->Fill();
423   
424   // Write to the output tree(D).
425   // Please note that as GlobalTrigger, LocalTrigger and Digits are in the same
426   // tree (=TreeD) in different branches, this WriteDigits in fact writes all of 
427   // the 3 branches.
428   outputLoader->WriteDigits("OVERWRITE");  
429   
430   outputLoader->UnloadDigits();
431   
432   // Finally, we clean up after ourselves.
433   fTriggerStore->Clear();
434   fDigitStore->Clear();
435   fOutputDigitStore->Clear();
436   fExecTimer.Stop();
437 }
438
439 //_____________________________________________________________________________
440 AliMUONVDigit* 
441 AliMUONDigitizerV3::FindCorrespondingDigit(const AliMUONVDigitStore& digitStore,
442                                            AliMUONVDigit& digit) const
443 {                                                
444   /// Find, if it exists, the digit corresponding to digit.Hit(), in the 
445   /// other cathode
446
447   TIter next(digitStore.CreateIterator());
448   AliMUONVDigit* d;
449   
450   while ( ( d = static_cast<AliMUONVDigit*>(next()) ) )
451   {
452     if ( d->DetElemId() == digit.DetElemId() &&
453          d->Hit() == digit.Hit() &&
454          d->Cathode() != digit.Cathode() )
455     {
456       return d;
457     }      
458   }    
459   return 0x0;
460 }
461
462
463 //_____________________________________________________________________________
464 void
465 AliMUONDigitizerV3::GenerateNoisyDigits(AliMUONVDigitStore& digitStore)
466 {
467   /// According to a given probability, generate digits that
468   /// have a signal above the noise cut (ped+n*sigma_ped), i.e. digits
469   /// that are "only noise".
470   
471   if ( !fNoiseFunction )
472   {
473     fNoiseFunction = new TF1("AliMUONDigitizerV3::fNoiseFunction","gaus",
474                              fgkNSigmas,fgkNSigmas*10);
475     
476     fNoiseFunction->SetParameters(1,0,1);
477   }
478   
479   fGenerateNoisyDigitsTimer.Start(kFALSE);
480   
481   for ( Int_t i = 0; i < AliMUONConstants::NTrackingCh(); ++i )
482   {
483     AliMpDEIterator it;
484   
485     it.First(i);
486   
487     while ( !it.IsDone() )
488     {
489       for ( Int_t cathode = 0; cathode < 2; ++cathode )
490       {
491         GenerateNoisyDigitsForOneCathode(digitStore,it.CurrentDEId(),cathode);
492       }
493       it.Next();
494     }
495   }
496   
497   fGenerateNoisyDigitsTimer.Stop();
498 }
499  
500 //_____________________________________________________________________________
501 void
502 AliMUONDigitizerV3::GenerateNoisyDigitsForOneCathode(AliMUONVDigitStore& digitStore,
503                                                      Int_t detElemId, Int_t cathode)
504 {
505   /// Generate noise-only digits for one cathode of one detection element.
506   /// Called by GenerateNoisyDigits()
507   
508   const AliMpVSegmentation* seg 
509     = AliMpSegmentation::Instance()->GetMpSegmentation(detElemId,AliMp::GetCathodType(cathode));
510   Int_t nofPads = seg->NofPads();
511   
512   Int_t maxIx = seg->MaxPadIndexX();
513   Int_t maxIy = seg->MaxPadIndexY();
514   
515   static const Double_t kProbToBeOutsideNsigmas = TMath::Erfc(fgkNSigmas/TMath::Sqrt(2.0)) / 2. ;
516   
517   Int_t nofNoisyPads = TMath::Nint(kProbToBeOutsideNsigmas*nofPads);
518   if ( !nofNoisyPads ) return;
519   
520   nofNoisyPads = 
521     TMath::Nint(gRandom->Gaus(nofNoisyPads,
522                               nofNoisyPads/TMath::Sqrt(nofNoisyPads)));
523   
524   AliDebug(3,Form("DE %d cath %d nofNoisyPads %d",detElemId,cathode,nofNoisyPads));
525   
526   for ( Int_t i = 0; i < nofNoisyPads; ++i ) 
527   {
528     Int_t ix(-1);
529     Int_t iy(-1);
530     AliMpPad pad;
531     
532     do {
533       ix = gRandom->Integer(maxIx+1);
534       iy = gRandom->Integer(maxIy+1);
535       pad = seg->PadByIndices(AliMpIntPair(ix,iy),kFALSE);
536     } while ( !pad.IsValid() );
537
538     Int_t manuId = pad.GetLocation().GetFirst();
539     Int_t manuChannel = pad.GetLocation().GetSecond();    
540
541     AliMUONVCalibParam* pedestals = fCalibrationData->Pedestals(detElemId,manuId);
542     
543     if (!pedestals) 
544     {
545       // no pedestal available for this channel, simply give up
546       continue;
547     }
548     
549     AliMUONVDigit* d = digitStore.CreateDigit(detElemId,manuId,manuChannel,cathode);
550     
551     d->SetPadXY(ix,iy);
552     
553     Float_t pedestalMean = pedestals->ValueAsFloat(manuChannel,0);
554     Float_t pedestalSigma = pedestals->ValueAsFloat(manuChannel,1);
555     
556     Double_t ped = fNoiseFunction->GetRandom()*pedestalSigma;
557
558     d->SetCharge(TMath::Nint(ped+pedestalMean+0.5));
559     d->NoiseOnly(kTRUE);
560     ApplyResponseToTrackerDigit(*d,kFALSE);
561     if ( d->Charge() > 0 )
562     {
563       Bool_t ok = digitStore.Add(*d,AliMUONVDigitStore::kDeny);
564       // this can happen (that we randomly chose a digit that is
565       // already there). We simply ignore this, but log the occurence
566       // to cross-check that it's not too frequent.
567       if (!ok)
568       {
569         fLogger->Log("Collision while adding noiseOnly digit");
570       }
571       else
572       {
573         fLogger->Log("Added noiseOnly digit");
574       }
575     }
576     else
577     {
578       AliError("Pure noise below threshold. This should not happen. Not adding "
579                "this digit.");
580     }
581     delete d;
582   }
583 }
584
585
586 //_____________________________________________________________________________
587 void
588 AliMUONDigitizerV3::GenerateNoisyDigitsForTrigger(AliMUONVDigitStore& digitStore)
589 {
590   /// Generate noise-only digits for one cathode of one detection element.
591   /// Called by GenerateNoisyDigits()
592
593   if ( !fNoiseFunctionTrig )
594   {
595     fNoiseFunctionTrig = new TF1("AliMUONDigitizerV3::fNoiseFunctionTrig","landau",
596                                  50.,270.);
597     
598     fNoiseFunctionTrig->SetParameters(3.91070e+02, 9.85026, 9.35881e-02);
599   }
600
601   AliMpPad pad[2];
602   AliMUONVDigit *d[2]={0x0};
603
604   for ( Int_t chamberId = AliMUONConstants::NTrackingCh(); chamberId < AliMUONConstants::NCh(); ++chamberId )
605   {
606   
607     Int_t nofNoisyPads = 50;
608
609     Float_t r=-1, fi = 0., gx, gy, x, y, z, xg01, yg01, zg, xg02, yg02;
610     AliMpDEIterator it;
611   
612     AliDebug(3,Form("Chamber %d nofNoisyPads %d",chamberId,nofNoisyPads));
613
614     for ( Int_t i = 0; i < nofNoisyPads; ++i )
615     {
616       //printf("Generating noise %i\n",i);
617         Int_t ix(-1);
618         Int_t iy(-1);
619         Bool_t isOk = kFALSE;
620         Int_t detElemId = -1;
621         do {
622           //r = gRandom->Landau(9.85026, 9.35881e-02);
623             r = fNoiseFunctionTrig->GetRandom();
624             fi = 2. * TMath::Pi() * gRandom->Rndm();
625             //printf("r = %f\tfi = %f\n", r, fi);
626             gx = r * TMath::Cos(fi);
627             gy = r * TMath::Sin(fi);
628
629             for ( it.First(chamberId); ! it.IsDone(); it.Next() ){
630                 Int_t currDetElemId = it.CurrentDEId();
631                 const AliMpVSegmentation* seg
632                     = AliMpSegmentation::Instance()->GetMpSegmentation(currDetElemId,AliMp::GetCathodType(0));
633                 if (!seg) continue;
634                 Float_t deltax = seg->Dimensions().X();
635                 Float_t deltay = seg->Dimensions().Y();
636                 GetTransformer()->Local2Global(currDetElemId, -deltax, -deltay, 0, xg01, yg01, zg);
637                 GetTransformer()->Local2Global(currDetElemId,  deltax,  deltay, 0, xg02, yg02, zg);
638                 Float_t xg1 = xg01, xg2 = xg02, yg1 = yg01, yg2 = yg02;
639                 if(xg01>xg02){
640                     xg1 = xg02;
641                     xg2 = xg01;
642                 }
643                 if(yg01>yg02){
644                     yg1 = yg02;
645                     yg2 = yg01;
646                 }
647                 if(gx>=xg1 && gx<=xg2 && gy>=yg1 && gy<=yg2){
648                     detElemId = currDetElemId;
649                     GetTransformer()->Global2Local(detElemId, gx, gy, 0, x, y, z);
650                     pad[0] = seg->PadByPosition(TVector2(x,y),kFALSE);
651                     if(!pad[0].IsValid()) continue;
652                     isOk = kTRUE;
653                     break;
654                 }
655             } // loop on slats
656         } while ( !isOk );
657
658         const AliMpVSegmentation* seg1
659             = AliMpSegmentation::Instance()->GetMpSegmentation(detElemId,AliMp::GetCathodType(1));
660         pad[1] = seg1->PadByPosition(TVector2(x,y),kFALSE);
661
662         for ( Int_t cathode = 0; cathode < 2; ++cathode ){
663           Int_t manuId = pad[cathode].GetLocation(0).GetFirst();
664           Int_t manuChannel = pad[cathode].GetLocation(0).GetSecond();    
665           d[cathode] = digitStore.CreateDigit(detElemId,manuId,manuChannel,cathode);
666           ix = pad[cathode].GetIndices().GetFirst();
667           iy = pad[cathode].GetIndices().GetSecond();
668           d[cathode]->SetPadXY(ix,iy);
669           //d[cathode].SetSignal(1);
670           //d[cathode].SetPhysicsSignal(0);
671           d[cathode]->SetCharge(1);
672           d[cathode]->NoiseOnly(kTRUE);
673           AliDebug(3,Form("Adding a pure noise digit :"));
674
675           Bool_t ok = digitStore.Add(*d[cathode],AliMUONVDigitStore::kDeny);
676           if (!ok)
677           {
678               fLogger->Log("Collision while adding TriggerNoise digit");
679           }
680           else
681           {
682               fLogger->Log("Added triggerNoise digit");
683           }
684         } //loop on cathodes
685     } // loop on noisy pads
686   } // loop on chambers
687 }
688
689
690 //_____________________________________________________________________________
691 AliLoader*
692 AliMUONDigitizerV3::GetLoader(const TString& folderName)
693 {
694   /// Get a MUON loader
695
696   AliDebug(2,Form("Getting access to folder %s",folderName.Data()));
697   AliLoader* loader = AliRunLoader::GetDetectorLoader("MUON",folderName.Data());
698   if (!loader)
699   {
700     AliError(Form("Could not get MuonLoader from folder %s",folderName.Data()));
701     return 0x0;
702   }
703   return loader;
704 }
705
706 //_____________________________________________________________________________
707 Bool_t
708 AliMUONDigitizerV3::Init()
709 {
710   /// Initialization of the TTask :
711   /// a) create the calibrationData, according to run number
712   /// b) create the trigger processing task
713
714   AliDebug(2,"");
715   
716   if ( fIsInitialized )
717   {
718     AliError("Object already initialized.");
719     return kFALSE;
720   }
721   
722   if (!fManager)
723   {
724     AliError("fManager is null !");
725     return kFALSE;
726   }
727   
728   Int_t runnumber = AliCDBManager::Instance()->GetRun();
729   
730   fCalibrationData = new AliMUONCalibrationData(runnumber);
731   if ( !fCalibrationData->Pedestals() )
732   {
733     AliFatal("Could not access pedestals from OCDB !");
734   }
735   if ( !fCalibrationData->Gains() )
736   {
737     AliFatal("Could not access gains from OCDB !");
738   }
739   fTriggerProcessor = new AliMUONTriggerElectronics(fCalibrationData);
740   
741   if ( muon()->GetTriggerEffCells() )
742   {
743     fTriggerEfficiency = fCalibrationData->TriggerEfficiency();
744     if ( fTriggerEfficiency )
745     {
746       AliDebug(1, "Will apply trigger efficiency");
747     }
748     else
749     {
750       AliFatal("I was requested to apply trigger efficiency, but I could "
751                "not get it !");
752     }
753   }
754   
755   AliDebug(1, Form("Will %s generate noise-only digits for tracker",
756                      (fGenerateNoisyDigits ? "":"NOT")));
757
758   fIsInitialized = kTRUE;
759   return kTRUE;
760 }
761
762 //_____________________________________________________________________________
763 void 
764 AliMUONDigitizerV3::MergeWithSDigits(AliMUONVDigitStore*& outputStore,
765                                      const AliMUONVDigitStore& input,
766                                      Int_t mask)
767 {
768   /// Merge the sdigits in inputData with the digits already present in outputData
769   
770   if ( !outputStore ) outputStore = input.Create();
771   
772   TIter next(input.CreateIterator());
773   AliMUONVDigit* sdigit;
774   
775   while ( ( sdigit = static_cast<AliMUONVDigit*>(next()) ) )
776   {
777     // Update the track references using the mask.
778     // FIXME: this is dirty, for backward compatibility only.
779     // Should re-design all this way of keeping track of MC information...
780     if ( mask ) sdigit->PatchTracks(mask);
781     // Then add or update the digit to the output.
782     AliMUONVDigit* added = outputStore->Add(*sdigit,AliMUONVDigitStore::kMerge);
783     if (!added)
784     {
785       AliError("Could not add digit in merge mode");
786     }
787   }
788 }