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