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