X-Git-Url: http://git.uio.no/git/?a=blobdiff_plain;f=MUON%2FAliMUONDigitizerV3.cxx;h=91fb1e8d2cd0edc6de9c2cbc9333a981b369f8bd;hb=6265ad5557e30302fab3e1a3e35bf51bda1ac0e2;hp=be55b17fc2d06f1a9c6bfc448ae0bee6009edc3b;hpb=884a73f1d555fcad7e1d027ae330127d1ff97565;p=u%2Fmrichter%2FAliRoot.git diff --git a/MUON/AliMUONDigitizerV3.cxx b/MUON/AliMUONDigitizerV3.cxx index be55b17fc2d..91fb1e8d2cd 100644 --- a/MUON/AliMUONDigitizerV3.cxx +++ b/MUON/AliMUONDigitizerV3.cxx @@ -15,223 +15,510 @@ // $Id$ + #include "AliMUONDigitizerV3.h" -#include "AliLog.h" +#include "AliMUON.h" #include "AliMUONCalibrationData.h" -#include "AliMUONChamber.h" #include "AliMUONConstants.h" -#include "AliMUONData.h" #include "AliMUONDigit.h" -#include "AliMUONTriggerDecisionV1.h" +#include "AliMUONLogger.h" #include "AliMUONTriggerElectronics.h" +#include "AliMUONTriggerStoreV1.h" #include "AliMUONVCalibParam.h" +#include "AliMUONVDigitStore.h" +#include "AliMUONGeometryTransformer.h" //ADDED for trigger noise +#include "AliMUONRecoParam.h" +#include "AliMUONTriggerChamberEfficiency.h" +#include "AliMUONTriggerUtilities.h" + +#include "AliMpCDB.h" +#include "AliMpSegmentation.h" +#include "AliMpCathodType.h" +#include "AliMpConstants.h" +#include "AliMpDEIterator.h" #include "AliMpDEManager.h" +#include "AliMpPad.h" #include "AliMpStationType.h" +#include "AliMpVSegmentation.h" +#include "AliMpDDLStore.h" + +#include "AliCDBManager.h" +#include "AliCodeTimer.h" +#include "AliLog.h" #include "AliRun.h" -#include "AliRunDigitizer.h" +#include "AliDigitizationInput.h" +#include "AliLoader.h" #include "AliRunLoader.h" -#include "Riostream.h" -#include "TRandom.h" -#include "TString.h" +#include +#include +#include +#include +#include +#include +#include +#include + +//----------------------------------------------------------------------------- +/// \class AliMUONDigitizerV3 +/// +/// The digitizer is performing the transformation to go from SDigits (digits +/// w/o any electronic noise) to Digits (w/ electronic noise, and decalibration) +/// +/// The decalibration is performed by doing the reverse operation of the +/// calibration, that is we do (Signal+pedestal)/gain -> ADC +/// +/// Note also that the digitizer takes care of merging sdigits that belongs +/// to the same pad, either because we're merging several input sdigit files +/// or with a single file because the sdigitizer does not merge sdigits itself +/// (for performance reason mainly, and because anyway we know we have to do it +/// here, at the digitization level). +/// +/// August 2011. In order to remove the need for specific MC OCDB storages, +/// we're introducing a dependence of simulation on AliMUONRecoParam (stored +/// in MUON/Calib/RecoParam in OCDB), which is normally (or conceptually, if +/// you will) only a reconstruction object. That's not a pretty solution, but, +/// well, we have to do it... +/// This dependence comes from the fact that we must know how to decalibrate +/// the digits, so that the decalibration (done here) - calibration (done during +/// reco) process is (as much as possible) neutral. +/// +/// +/// \author Laurent Aphecetche +/// +//----------------------------------------------------------------------------- + +namespace +{ + AliMUON* muon() + { + return static_cast(gAlice->GetModule("MUON")); + } + + //ADDED for trigger noise + const AliMUONGeometryTransformer* GetTransformer() + { + return muon()->GetGeometryTransformer(); + } +} + +Double_t AliMUONDigitizerV3::fgNSigmas = 4.0; + +/// \cond CLASSIMP ClassImp(AliMUONDigitizerV3) +/// \endcond //_____________________________________________________________________________ -AliMUONDigitizerV3::AliMUONDigitizerV3(AliRunDigitizer* manager, - ETriggerCodeVersion triggerCodeVersion) -: AliDigitizer(manager), +AliMUONDigitizerV3::AliMUONDigitizerV3(AliDigitizationInput* digInput, + Int_t generateNoisyDigits) +: AliDigitizer(digInput), fIsInitialized(kFALSE), -fOutputData(0x0), fCalibrationData(0x0), fTriggerProcessor(0x0), -fTriggerCodeVersion(triggerCodeVersion) +fNoiseFunctionTrig(0x0), +fGenerateNoisyDigits(generateNoisyDigits), +fLogger(new AliMUONLogger(4207)), /* 4207 = 25% of the 16828 MCH manus */ +fTriggerStore(new AliMUONTriggerStoreV1), +fDigitStore(0x0), +fOutputDigitStore(0x0), +fInputDigitStores(0x0), +fRecoParam(0x0), +fTriggerEfficiency(0x0), +fTriggerUtilities(0x0), +fEfficiencyResponse(2*AliMUONConstants::NTriggerCh()*AliMUONConstants::NTriggerCircuit()) { - // - // Ctor. - // - AliDebug(1,Form("AliRunDigitizer=%p",fManager)); -} + /// Ctor. -//______________________________________________________________________________ -AliMUONDigitizerV3::AliMUONDigitizerV3(const AliMUONDigitizerV3& right) - : AliDigitizer(right) -{ -/// Protected copy constructor (not implemented) + AliDebug(1,Form("AliDigitizationInput=%p",fDigInput)); - AliFatal("Copy constructor not provided."); } //_____________________________________________________________________________ AliMUONDigitizerV3::~AliMUONDigitizerV3() { - // - // Dtor. Note we're the owner of some pointers. - // + /// Dtor. Note we're the owner of some pointers. + AliDebug(1,"dtor"); - delete fOutputData; - delete fCalibrationData; + + // delete fCalibrationData; delete fTriggerProcessor; + delete fNoiseFunctionTrig; + delete fTriggerStore; + delete fDigitStore; + delete fOutputDigitStore; + delete fInputDigitStores; + delete fTriggerUtilities; + + AliInfo("Summary of messages"); + fLogger->Print(); + + delete fLogger; } -//______________________________________________________________________________ -AliMUONDigitizerV3& -AliMUONDigitizerV3::operator=(const AliMUONDigitizerV3& right) +//_____________________________________________________________________________ +void +AliMUONDigitizerV3::ApplyResponseToTrackerDigit(AliMUONVDigit& digit, Bool_t addNoise) { -/// Protected assignement operator (not implemented) + /// For tracking digits, starting from an ideal digit's charge, we : + /// + /// - "divide" by a gain (thus decalibrating the digit) + /// - add a pedestal (thus decalibrating the digit) + /// - add some electronics noise (thus leading to a realistic adc), if requested to do so + /// - sets the signal to zero if below 3*sigma of the noise - // check assignement to self - if (this == &right) return *this; + Float_t charge = digit.Charge(); + + if (!digit.IsChargeInFC()) + { + charge *= AliMUONConstants::DefaultADC2MV()*AliMUONConstants::DefaultA0()*AliMUONConstants::DefaultCapa(); + fLogger->Log("CHECK ME ! WAS NOT SUPPOSED TO BE HERE !!! ARE YOU RECONSTRUCTING OLD SIMULATIONS ? "); + AliError("CHECK ME ! WAS NOT SUPPOSED TO BE HERE !!! ARE YOU RECONSTRUCTING OLD SIMULATIONS ? "); + } + + // We set the charge to 0, as the only relevant piece of information + // after Digitization is the ADC value. + digit.SetCharge(0); + + Int_t detElemId = digit.DetElemId(); + Int_t manuId = digit.ManuId(); + + AliMUONVCalibParam* pedestal = fCalibrationData->Pedestals(detElemId,manuId); + if (!pedestal) + { + fLogger->Log(Form("%s:%d:Could not get pedestal for DE=%4d manuId=%4d. Disabling.", + __FILE__,__LINE__, + detElemId,manuId)); + digit.SetADC(0); + return; + } + + Int_t manuChannel = digit.ManuChannel(); + + if ( pedestal->ValueAsFloat(manuChannel,0) == AliMUONVCalibParam::InvalidFloatValue() || + pedestal->ValueAsFloat(manuChannel,1) == AliMUONVCalibParam::InvalidFloatValue() ) + { + // protection against invalid pedestal value + digit.SetADC(0); + return; + } - AliFatal("Assignement operator not provided."); + TString calibrationMode(fRecoParam->GetCalibrationMode()); + calibrationMode.ToUpper(); + + AliMUONVCalibParam* gain = fCalibrationData->Gains(detElemId,manuId); + if (!gain) + { - return *this; -} + if (!calibrationMode.Contains("NOGAIN") ) + { + fLogger->Log(Form("%s:%d:Could not get gain for DE=%4d manuId=%4d. Disabling.", + __FILE__,__LINE__, + detElemId,manuId)); + digit.SetADC(0); + return; + } + } + + Int_t adc = DecalibrateTrackerDigit(*pedestal,gain,manuChannel,charge,addNoise, + digit.IsNoiseOnly(), + calibrationMode); + + digit.SetADC(adc); +} + //_____________________________________________________________________________ void -AliMUONDigitizerV3::ApplyResponseToDigit(AliMUONDigit& digit) +AliMUONDigitizerV3::ApplyResponseToTriggerDigit(AliMUONVDigit& digit) { - // For trigger digits, simply does nothing. - // - // For tracking digits, starting from an ideal digit's charge, we : - // - // - add some noise (thus leading to a realistic charge) - // - divide by a gain (thus decalibrating the digit) - // - add a pedestal (thus decalibrating the digit) - // - sets the signal to zero if below 3*sigma of the noise - // - - static const Int_t MAXADC = (1<<12)-1; // We code the charge on a 12 bits ADC. - + /// For trigger digits, starting from an ideal digit, we : + /// + /// - apply efficiency (on demand) + /// - apply trigger masks + Int_t detElemId = digit.DetElemId(); - AliMpStationType stationType = AliMpDEManager::GetStationType(detElemId); - if ( stationType == kStationTrigger ) - { - return; + Int_t localCircuit = digit.ManuId(); + Int_t strip = digit.ManuChannel(); + Int_t cathode = digit.Cathode(); + Int_t trigCh = detElemId/100 - 11; + + // Masked channels + Bool_t isMasked = fTriggerUtilities->IsMasked(digit); + AliDebug(1,Form("detElemId %i cath %i board %i strip %i is masked %i\n", detElemId, cathode, localCircuit, strip, isMasked)); + if ( isMasked ) { + digit.SetCharge(0); + digit.SetADC(0); + //AliDebug(1,Form("ch %i cath %i board %i strip %i masked\n", trigCh, cathode, localCircuit, strip)); + return; } - // The following is for tracking digits only. - Int_t manuId = digit.ManuId(); - Int_t manuChannel = digit.ManuChannel(); + Int_t arrayIndex = GetArrayIndex(cathode, trigCh, localCircuit); - AliMUONVCalibParam* pedestal = fCalibrationData->Pedestal(detElemId,manuId); - if (!pedestal) + // Trigger chamber efficiency + if ( fTriggerEfficiency ) { + if ( fEfficiencyResponse[arrayIndex] < 0 ) { + Bool_t isTrig[2] = {kTRUE, kTRUE}; + fTriggerEfficiency->IsTriggered(detElemId, localCircuit, isTrig[0], isTrig[1]); + Int_t arrayIndexBend = GetArrayIndex(0, trigCh, localCircuit); + Int_t arrayIndexNonBend = GetArrayIndex(1, trigCh, localCircuit); + fEfficiencyResponse[arrayIndexBend] = isTrig[0]; + fEfficiencyResponse[arrayIndexNonBend] = isTrig[1]; + } + AliDebug(1,Form("detElemId %i cath %i board %i strip %i efficiency %i\n", detElemId, cathode, localCircuit, strip, fEfficiencyResponse[arrayIndex])); + if ( fEfficiencyResponse[arrayIndex] == 0 ) { + digit.SetCharge(0); + digit.SetADC(0); + //AliDebug(1,Form("ch %i cath %i board %i strip %i NOT efficient\n", trigCh, cathode, localCircuit, strip)); + return; + } + } +} + + + +//_____________________________________________________________________________ +void +AliMUONDigitizerV3::ApplyResponse(const AliMUONVDigitStore& store, + AliMUONVDigitStore& filteredStore) +{ + /// Loop over all chamber digits, and apply the response to them + /// Note that this method may remove digits. + + filteredStore.Clear(); + + const Bool_t kAddNoise = kTRUE; + + TIter next(store.CreateIterator()); + AliMUONVDigit* digit; + + if ( fTriggerEfficiency ) fEfficiencyResponse.Reset(-1); + + while ( ( digit = static_cast(next()) ) ) { - AliFatal(Form("Could not get pedestal for DE=%d manuId=%d", - detElemId,manuId)); + AliMp::StationType stationType = AliMpDEManager::GetStationType(digit->DetElemId()); + + if ( stationType != AliMp::kStationTrigger ) + { + Bool_t addNoise = kAddNoise; + if (digit->IsConverted()) addNoise = kFALSE; // No need to add extra noise to a converted real digit + ApplyResponseToTrackerDigit(*digit,addNoise); + } + else { + ApplyResponseToTriggerDigit(*digit); + } + + if ( digit->ADC() > 0 || digit->Charge() > 0 ) + { + filteredStore.Add(*digit,AliMUONVDigitStore::kIgnore); + } } - Float_t pedestalMean = pedestal->ValueAsFloat(manuChannel,0); - Float_t pedestalSigma = pedestal->ValueAsFloat(manuChannel,1); +} + +//_____________________________________________________________________________ +Int_t +AliMUONDigitizerV3::DecalibrateTrackerDigit(const AliMUONVCalibParam& pedestals, + const AliMUONVCalibParam* gains, + Int_t channel, + Float_t charge, + Bool_t addNoise, + Bool_t noiseOnly, + const TString& calibrationMode) +{ + /// Decalibrate (i.e. go from charge to adc) a tracker digit, given its + /// pedestal and gain parameters. + /// Must insure before calling that channel is valid (i.e. between 0 and + /// pedestals or gains->GetSize()-1, but also corresponding to a valid channel + /// otherwise results are not predictible...) + /// + /// This method is completely tied to what happens in its sister method : + /// AliMUONDigitCalibrator::CalibrateDigit, which is doing the reverse work... + /// - Float_t adc_noise = gRandom->Gaus(0.0,pedestalSigma); + static const Int_t kMaxADC = (1<<12)-1; // We code the charge on a 12 bits ADC. - AliMUONVCalibParam* gain = fCalibrationData->Gain(detElemId,manuId); - if (!gain) + Bool_t nogain = calibrationMode.Contains("NOGAIN"); + + Float_t a1(0.0); + Int_t thres(4095); + Int_t qual(0xF); + Float_t capa(AliMUONConstants::DefaultCapa()); // capa = 0.2 and a0 = 1.25 + Float_t a0(AliMUONConstants::DefaultA0()); // is equivalent to gain = 4 mV/fC + Float_t adc2mv(AliMUONConstants::DefaultADC2MV()); // 1 ADC channel = 0.61 mV + + if ( ! nogain ) { - AliFatal(Form("Could not get gain for DE=%d manuId=%d", - detElemId,manuId)); + if (!gains) + { + AliFatalClass("Cannot make gain decalibration without gain values !"); + } + a0 = gains->ValueAsFloat(channel,0); + a1 = gains->ValueAsFloat(channel,1); + thres = gains->ValueAsInt(channel,2); + qual = gains->ValueAsInt(channel,3); } - Float_t gainMean = gain->ValueAsFloat(manuChannel,0); - Float_t signal_noise = adc_noise*gainMean; + Float_t pedestalMean = pedestals.ValueAsFloat(channel,0); + Float_t pedestalSigma = pedestals.ValueAsFloat(channel,1); + + AliDebugClass(2,Form("DE %04d MANU %04d CH %02d PEDMEAN %7.2f PEDSIGMA %7.2f", + pedestals.ID0(),pedestals.ID1(),channel,pedestalMean,pedestalSigma)); + + if ( qual <= 0 ) return 0; - Float_t signal = digit.Signal() + signal_noise; - Int_t adc; + Float_t chargeThres = a0*thres; - if ( gainMean < 1E-6 ) + Float_t padc(0); // (adc - ped) value + + if ( nogain || charge <= chargeThres || TMath::Abs(a1) < 1E-12 ) { - AliError(Form("Got a too small gain %e for DE=%d manuId=%d manuChannel=%d. " - "Setting signal to 0.", - gainMean,detElemId,manuId,manuChannel)); - adc = 0; + // linear part only + + if ( TMath::Abs(a0) > 1E-12 ) + { + padc = charge/a0; + } } - else + else { - adc = TMath::Nint( signal / gainMean + pedestalMean ); + // FIXME: when we'll use capacitances and real gains, revise this part + // to take capa properly into account... + + AliWarningClass("YOU PROBABLY NEED TO REVISE THIS PART OF CODE !!!"); - if ( adc <= pedestalMean + 3.0*pedestalSigma ) + // linear + parabolic part + Double_t qt = chargeThres - charge; + Double_t delta = a0*a0-4*a1*qt; + if ( delta < 0 ) { - adc = 0; + AliErrorClass(Form("delta=%e DE %d Manu %d Channel %d " + " charge %e a0 %e a1 %e thres %d ped %e pedsig %e", + delta,pedestals.ID0(),pedestals.ID1(), + channel, charge, a0, a1, thres, pedestalMean, + pedestalSigma)); + } + else + { + delta = TMath::Sqrt(delta); + + padc = ( ( -a0 + delta ) > 0 ? ( -a0 + delta ) : ( -a0 - delta ) ); + + padc /= 2*a1; + + if ( padc < 0 ) + { + if ( TMath::Abs(padc) > 1E-3) + { + // this is more than a precision problem : let's signal it ! + AliErrorClass(Form("padc=%e DE %d Manu %d Channel %d " + " charge %e a0 %e a1 %e thres %d ped %e pedsig %e delta %e", + padc,pedestals.ID0(),pedestals.ID1(), + channel, charge, a0, a1, thres, pedestalMean, + pedestalSigma,delta)); + } + + // ok. consider we're just at thres, let it be zero. + padc = 0; + } + + padc += thres; + } } - // be sure we stick to 12 bits. - if ( adc > MAXADC ) + + padc /= capa*adc2mv; + + Int_t adc(0); + + Float_t adcNoise = 0.0; + + if ( addNoise ) { - adc = MAXADC; + if ( noiseOnly ) + { + adcNoise = NoiseFunction()->GetRandom()*pedestalSigma; + } + else + { + adcNoise = gRandom->Gaus(0.0,pedestalSigma); + } } - digit.SetPhysicsSignal(TMath::Nint(signal)); - digit.SetSignal(adc); - digit.SetADC(adc); -} - -//_____________________________________________________________________________ -void -AliMUONDigitizerV3::ApplyResponse() -{ - // - // Loop over all chamber digits, and apply the response to them - // Note that this method may remove digits. - // - for ( Int_t ich = 0; ich < AliMUONConstants::NCh(); ++ich ) - { - TClonesArray* digits = fOutputData->Digits(ich); - Int_t n = digits->GetEntriesFast(); - for ( Int_t i = 0; i < n; ++i ) + + adc = TMath::Nint(padc + pedestalMean + adcNoise + 0.5); + + if ( adc < TMath::Nint(pedestalMean + fgNSigmas*pedestalSigma + 0.5) ) + { + // this is an error only in specific cases + if ( !addNoise || (addNoise && noiseOnly) ) { - AliMUONDigit* d = static_cast(digits->UncheckedAt(i)); - ApplyResponseToDigit(*d); - if ( d->Signal() <= 0 ) - { - digits->RemoveAt(i); - } + AliDebugClass(1,Form(" DE %04d Manu %04d Channel %02d " + " a0 %7.2f a1 %7.2f thres %04d ped %7.2f pedsig %7.2f adcNoise %7.2f " + " charge=%7.2f padc=%7.2f adc=%04d ZS=%04d fgNSigmas=%e addNoise %d noiseOnly %d ", + pedestals.ID0(),pedestals.ID1(),channel, + a0, a1, thres, pedestalMean, pedestalSigma, adcNoise, + charge, padc, adc, + TMath::Nint(pedestalMean + fgNSigmas*pedestalSigma + 0.5), + fgNSigmas,addNoise,noiseOnly)); } - digits->Compress(); - } + + adc = 0; + } + + // be sure we stick to 12 bits. + if ( adc > kMaxADC ) + { + adc = kMaxADC; + } + + return adc; } //_____________________________________________________________________________ void -AliMUONDigitizerV3::AddOrUpdateDigit(TClonesArray& array, - const AliMUONDigit& digit) +AliMUONDigitizerV3::CreateInputDigitStores() { - // - // Add or update a digit, depending on whether there's already a digit - // for the corresponding channel. - // - Int_t ix = FindDigitIndex(array,digit); + /// Create input digit stores + /// - if (ix>=0) + if (fInputDigitStores) { - AliMUONDigit* d = static_cast(array.UncheckedAt(ix)); - Bool_t ok = MergeDigits(digit,*d); - if (!ok) + AliFatal("Should be called only once !"); + } + + fInputDigitStores = new TObjArray; + + fInputDigitStores->SetOwner(kTRUE); + + for ( Int_t iFile = 0; iFile < fDigInput->GetNinputs(); ++iFile ) + { + AliLoader* inputLoader = GetLoader(fDigInput->GetInputFolderName(iFile)); + + inputLoader->LoadSDigits("READ"); + + TTree* iTreeS = inputLoader->TreeS(); + if (!iTreeS) { - AliError("Digits are not mergeable !"); + AliFatal(Form("Could not get access to input file #%d",iFile)); } + + fInputDigitStores->AddAt(AliMUONVDigitStore::Create(*iTreeS),iFile); } - else - { - ix = array.GetLast() + 1; - new(array[ix]) AliMUONDigit(digit); - } - } //_____________________________________________________________________________ void -AliMUONDigitizerV3::Exec(Option_t*) +AliMUONDigitizerV3::Digitize(Option_t*) { - // - // Main method. - // We first loop over input files, and merge the sdigits we found there. - // We then digitize all the resulting sdigits - // And we finally generate the trigger outputs. - // - AliDebug(1, "Running digitizer."); + /// Main method. + /// We first loop over input files, and merge the sdigits we found there. + /// Second, we digitize all the resulting sdigits + /// Then we generate noise-only digits (for tracker only) + /// And we finally generate the trigger outputs. + + AliCodeTimerAuto("",0) - if ( fManager->GetNinputs() == 0 ) + if ( fDigInput->GetNinputs() == 0 ) { AliWarning("No input set. Nothing to do."); return; @@ -243,124 +530,338 @@ AliMUONDigitizerV3::Exec(Option_t*) return; } - Int_t nInputFiles = fManager->GetNinputs(); + Int_t nInputFiles = fDigInput->GetNinputs(); + + AliLoader* outputLoader = GetLoader(fDigInput->GetOutputFolderName()); + + outputLoader->MakeDigitsContainer(); - if ( fOutputData->TreeD() == 0x0 ) + TTree* oTreeD = outputLoader->TreeD(); + + if (!oTreeD) { - AliDebug(1,"Calling MakeDigitsContainer"); - fOutputData->GetLoader()->MakeDigitsContainer(); + AliFatal("Cannot create output TreeD"); } - fOutputData->MakeBranch("D,GLT"); - fOutputData->SetTreeAddress("D,GLT"); - + // Loop over all the input files, and merge the sdigits found in those // files. + for ( Int_t iFile = 0; iFile < nInputFiles; ++iFile ) - { - AliMUONData* inputData = GetDataAccess(fManager->GetInputFolderName(iFile)); - if (!inputData) + { + AliLoader* inputLoader = GetLoader(fDigInput->GetInputFolderName(iFile)); + + inputLoader->LoadSDigits("READ"); + + TTree* iTreeS = inputLoader->TreeS(); + if (!iTreeS) { AliFatal(Form("Could not get access to input file #%d",iFile)); } - inputData->GetLoader()->LoadSDigits("READ"); - inputData->SetTreeAddress("S"); - inputData->GetSDigits(); + if (!fInputDigitStores) + { + CreateInputDigitStores(); + } + + AliMUONVDigitStore* dstore = static_cast(fInputDigitStores->At(iFile)); + + dstore->Connect(*iTreeS); + + iTreeS->GetEvent(0); + + MergeWithSDigits(fDigitStore,*dstore,fDigInput->GetMask(iFile)); - MergeWithSDigits(*fOutputData,*inputData,fManager->GetMask(iFile)); + inputLoader->UnloadSDigits(); - inputData->ResetSDigits(); - inputData->GetLoader()->UnloadSDigits(); - delete inputData; + dstore->Clear(); } + // At this point, we do have digit arrays (one per chamber) which contains // the merging of all the sdigits of the input file(s). // We now massage them to apply the detector response, i.e. this // is here that we do the "digitization" work. - ApplyResponse(); + if (!fOutputDigitStore) + { + fOutputDigitStore = fDigitStore->Create(); + } + + if ( fGenerateNoisyDigits>=2 ) + { + // Generate noise-only digits for trigger. + GenerateNoisyDigitsForTrigger(*fDigitStore); + } + ApplyResponse(*fDigitStore,*fOutputDigitStore); + if ( fGenerateNoisyDigits ) + { + // Generate noise-only digits for tracker. + GenerateNoisyDigits(*fOutputDigitStore); + } + // We generate the global and local trigger decisions. - fTriggerProcessor->ExecuteTask(); + fTriggerProcessor->Digits2Trigger(*fOutputDigitStore,*fTriggerStore); + + // Prepare output tree + Bool_t okD = fOutputDigitStore->Connect(*oTreeD,kFALSE); + Bool_t okT = fTriggerStore->Connect(*oTreeD,kFALSE); + if (!okD || !okT) + { + AliError(Form("Could not make branch : Digit %d Trigger %d",okD,okT)); + return; + } // Fill the output treeD - fOutputData->Fill("D,GLT"); + oTreeD->Fill(); // Write to the output tree(D). // Please note that as GlobalTrigger, LocalTrigger and Digits are in the same // tree (=TreeD) in different branches, this WriteDigits in fact writes all of // the 3 branches. - fOutputData->GetLoader()->WriteDigits("OVERWRITE"); + outputLoader->WriteDigits("OVERWRITE"); + + outputLoader->UnloadDigits(); // Finally, we clean up after ourselves. - fOutputData->ResetDigits(); - fOutputData->ResetTrigger(); - fOutputData->GetLoader()->UnloadDigits(); + fTriggerStore->Clear(); + fDigitStore->Clear(); + fOutputDigitStore->Clear(); } + //_____________________________________________________________________________ -Int_t -AliMUONDigitizerV3::FindDigitIndex(TClonesArray& array, const AliMUONDigit& digit) +void +AliMUONDigitizerV3::GenerateNoisyDigits(AliMUONVDigitStore& digitStore) { - // - // Return the index of digit within array, if that digit is there, - // otherwise returns -1 - // - // FIXME: this is of course not the best implementation you can think of. - // Reconsider the use of hit/digit map... ? (but be sure it's needed!) - // - Int_t n = array.GetEntriesFast(); - for ( Int_t i = 0; i < n; ++i ) - { - AliMUONDigit* d = static_cast(array.UncheckedAt(i)); - if ( d->DetElemId() == digit.DetElemId() && - d->PadX() == digit.PadX() && - d->PadY() == digit.PadY() && - d->Cathode() == digit.Cathode() ) + /// According to a given probability, generate digits that + /// have a signal above the noise cut (ped+n*sigma_ped), i.e. digits + /// that are "only noise". + + AliCodeTimerAuto("",0) + + for ( Int_t i = 0; i < AliMUONConstants::NTrackingCh(); ++i ) + { + AliMpDEIterator it; + + it.First(i); + + while ( !it.IsDone() ) { - return i; + for ( Int_t cathode = 0; cathode < 2; ++cathode ) + { + GenerateNoisyDigitsForOneCathode(digitStore,it.CurrentDEId(),cathode); + } + it.Next(); } } - return -1; } + +//_____________________________________________________________________________ +void +AliMUONDigitizerV3::GenerateNoisyDigitsForOneCathode(AliMUONVDigitStore& digitStore, + Int_t detElemId, Int_t cathode) +{ + /// Generate noise-only digits for one cathode of one detection element. + /// Called by GenerateNoisyDigits() + + const AliMpVSegmentation* seg + = AliMpSegmentation::Instance()->GetMpSegmentation(detElemId,AliMp::GetCathodType(cathode)); + Int_t nofPads = seg->NofPads(); + + Int_t maxIx = seg->MaxPadIndexX(); + Int_t maxIy = seg->MaxPadIndexY(); + + static const Double_t kProbToBeOutsideNsigmas = TMath::Erfc(fgNSigmas/TMath::Sqrt(2.0)) / 2. ; + + Int_t nofNoisyPads = TMath::Nint(kProbToBeOutsideNsigmas*nofPads); + if ( !nofNoisyPads ) return; + + nofNoisyPads = + TMath::Nint(gRandom->Gaus(nofNoisyPads, + nofNoisyPads/TMath::Sqrt(nofNoisyPads))); + + AliDebug(3,Form("DE %d cath %d nofNoisyPads %d",detElemId,cathode,nofNoisyPads)); + + for ( Int_t i = 0; i < nofNoisyPads; ++i ) + { + Int_t ix(-1); + Int_t iy(-1); + AliMpPad pad; + + do { + ix = gRandom->Integer(maxIx+1); + iy = gRandom->Integer(maxIy+1); + pad = seg->PadByIndices(ix,iy,kFALSE); + } while ( !pad.IsValid() ); + + Int_t manuId = pad.GetManuId(); + Int_t manuChannel = pad.GetManuChannel(); + + AliMUONVCalibParam* pedestals = fCalibrationData->Pedestals(detElemId,manuId); + + if (!pedestals) + { + // no pedestal available for this channel, simply give up + continue; + } + + AliMUONVDigit* d = digitStore.CreateDigit(detElemId,manuId,manuChannel,cathode); + + d->SetPadXY(ix,iy); + + d->SetCharge(0.0); // charge is zero, the ApplyResponseToTrackerDigit will add the noise + d->NoiseOnly(kTRUE); + ApplyResponseToTrackerDigit(*d,kTRUE); + if ( d->ADC() > 0 ) + { + Bool_t ok = digitStore.Add(*d,AliMUONVDigitStore::kDeny); + // this can happen (that we randomly chose a digit that is + // already there). We simply ignore this, but log the occurence + // to cross-check that it's not too frequent. + if (!ok) + { + fLogger->Log("Collision while adding noiseOnly digit"); + } + else + { + fLogger->Log("Added noiseOnly digit"); + } + } + delete d; + } +} + //_____________________________________________________________________________ -AliMUONData* -AliMUONDigitizerV3::GetDataAccess(const TString& folderName) +void +AliMUONDigitizerV3::GenerateNoisyDigitsForTrigger(AliMUONVDigitStore& digitStore) { - // - // Create an AliMUONData to deal with data found in folderName. - // - AliDebug(1,Form("Getting access to folder %s",folderName.Data())); - AliRunLoader* runLoader = AliRunLoader::GetRunLoader(folderName); - if (!runLoader) - { - AliError(Form("Could not get RunLoader from folder %s",folderName.Data())); - return 0x0; + /// Generate noise-only digits for one cathode of one detection element. + /// Called by GenerateNoisyDigits() + + if ( !fNoiseFunctionTrig ) + { + fNoiseFunctionTrig = new TF1("AliMUONDigitizerV3::fNoiseFunctionTrig","landau", + 50.,270.); + + fNoiseFunctionTrig->SetParameters(3.91070e+02, 9.85026, 9.35881e-02); } - AliLoader* loader = static_cast(runLoader->GetLoader("MUONLoader")); + + AliMpPad pad[2]; + AliMUONVDigit *d[2]={0x0}; + + for ( Int_t chamberId = AliMUONConstants::NTrackingCh(); chamberId < AliMUONConstants::NCh(); ++chamberId ) + { + + Int_t nofNoisyPads = 50; + + Float_t r=-1, fi = 0., gx, gy, x, y, z, xg01, yg01, zg, xg02, yg02; + AliMpDEIterator it; + + AliDebug(3,Form("Chamber %d nofNoisyPads %d",chamberId,nofNoisyPads)); + + for ( Int_t i = 0; i < nofNoisyPads; ++i ) + { + //printf("Generating noise %i\n",i); + Int_t ix(-1); + Int_t iy(-1); + Bool_t isOk = kFALSE; + Int_t detElemId = -1; + do { + //r = gRandom->Landau(9.85026, 9.35881e-02); + r = fNoiseFunctionTrig->GetRandom(); + fi = 2. * TMath::Pi() * gRandom->Rndm(); + //printf("r = %f\tfi = %f\n", r, fi); + gx = r * TMath::Cos(fi); + gy = r * TMath::Sin(fi); + + for ( it.First(chamberId); ! it.IsDone(); it.Next() ){ + Int_t currDetElemId = it.CurrentDEId(); + const AliMpVSegmentation* seg + = AliMpSegmentation::Instance()->GetMpSegmentation(currDetElemId,AliMp::GetCathodType(0)); + if (!seg) continue; + Float_t deltax = seg->GetDimensionX(); + Float_t deltay = seg->GetDimensionY(); + GetTransformer()->Local2Global(currDetElemId, -deltax, -deltay, 0, xg01, yg01, zg); + GetTransformer()->Local2Global(currDetElemId, deltax, deltay, 0, xg02, yg02, zg); + Float_t xg1 = xg01, xg2 = xg02, yg1 = yg01, yg2 = yg02; + if(xg01>xg02){ + xg1 = xg02; + xg2 = xg01; + } + if(yg01>yg02){ + yg1 = yg02; + yg2 = yg01; + } + if(gx>=xg1 && gx<=xg2 && gy>=yg1 && gy<=yg2){ + detElemId = currDetElemId; + GetTransformer()->Global2Local(detElemId, gx, gy, 0, x, y, z); + pad[0] = seg->PadByPosition(x,y,kFALSE); + if(!pad[0].IsValid()) continue; + isOk = kTRUE; + break; + } + } // loop on slats + } while ( !isOk ); + + const AliMpVSegmentation* seg1 + = AliMpSegmentation::Instance()->GetMpSegmentation(detElemId,AliMp::GetCathodType(1)); + pad[1] = seg1->PadByPosition(x,y,kFALSE); + + for ( Int_t cathode = 0; cathode < 2; ++cathode ){ + Int_t manuId = pad[cathode].GetLocalBoardId(0); + Int_t manuChannel = pad[cathode].GetLocalBoardChannel(0); + d[cathode] = digitStore.CreateDigit(detElemId,manuId,manuChannel,cathode); + ix = pad[cathode].GetIx(); + iy = pad[cathode].GetIy(); + d[cathode]->SetPadXY(ix,iy); + //d[cathode].SetSignal(1); + //d[cathode].SetPhysicsSignal(0); + d[cathode]->SetCharge(1); + d[cathode]->NoiseOnly(kTRUE); + AliDebug(3,Form("Adding a pure noise digit :")); + + Bool_t ok = digitStore.Add(*d[cathode],AliMUONVDigitStore::kDeny); + if (!ok) + { + fLogger->Log("Collision while adding TriggerNoise digit"); + } + else + { + fLogger->Log("Added triggerNoise digit"); + } + } //loop on cathodes + } // loop on noisy pads + } // loop on chambers +} + + +//_____________________________________________________________________________ +AliLoader* +AliMUONDigitizerV3::GetLoader(const TString& folderName) +{ + /// Get a MUON loader + + AliDebug(2,Form("Getting access to folder %s",folderName.Data())); + AliLoader* loader = AliRunLoader::GetDetectorLoader("MUON",folderName.Data()); if (!loader) { AliError(Form("Could not get MuonLoader from folder %s",folderName.Data())); return 0x0; } - AliMUONData* data = new AliMUONData(loader,"MUON","MUONDataForDigitOutput"); - AliDebug(1,Form("AliMUONData=%p loader=%p",data,loader)); - return data; + return loader; } //_____________________________________________________________________________ Bool_t AliMUONDigitizerV3::Init() { - // - // Initialization of the TTask : - // a) set the outputData pointer - // b) create the calibrationData, according to run number - // c) create the trigger processing task - // - AliDebug(1,""); + /// Initialization of the digitization : + /// a) create the calibrationData, according to run number + /// b) create the trigger processing task + + AliDebug(2,""); if ( fIsInitialized ) { @@ -368,111 +869,111 @@ AliMUONDigitizerV3::Init() return kFALSE; } - if (!fManager) + if (!fDigInput) { - AliError("fManager is null !"); + AliError("fDigInput is null !"); return kFALSE; } - fOutputData = GetDataAccess(fManager->GetOutputFolderName()); - if (!fOutputData) + // Load mapping + if ( ! AliMpCDB::LoadDDLStore() ) { + AliFatal("Could not access mapping from OCDB !"); + } + + if (!fCalibrationData) + AliFatal("Calibration data object not defined"); + + if ( !fCalibrationData->Pedestals() ) { - AliError("Can not perform digitization. I'm sorry"); - return kFALSE; + AliFatal("Could not access pedestals from OCDB !"); + } + if ( !fCalibrationData->Gains() ) + { + AliFatal("Could not access gains from OCDB !"); } - AliDebug(1,Form("fOutputData=%p",fOutputData)); - AliRunLoader* runLoader = fOutputData->GetLoader()->GetRunLoader(); - AliRun* galice = runLoader->GetAliRun(); - Int_t runnumber = galice->GetRunNumber(); - fCalibrationData = new AliMUONCalibrationData(runnumber); + AliInfo("Using trigger configuration from CDB"); - switch (fTriggerCodeVersion) - { - case kTriggerDecision: - fTriggerProcessor = new AliMUONTriggerDecisionV1(fOutputData); - break; - case kTriggerElectronics: - fTriggerProcessor = new AliMUONTriggerElectronics(fOutputData); - break; - default: - AliFatal("Unknown trigger processor type"); - break; + fTriggerProcessor = new AliMUONTriggerElectronics(fCalibrationData); + + AliDebug(1, Form("Will %s generate noise-only digits for tracker", + (fGenerateNoisyDigits ? "":"NOT"))); + + fTriggerUtilities = new AliMUONTriggerUtilities(fCalibrationData); + + if ( muon()->GetTriggerEffCells() ) { + // Apply trigger efficiency + AliDebug(1, "Will apply trigger efficiency"); + fTriggerEfficiency = new AliMUONTriggerChamberEfficiency(fCalibrationData->TriggerEfficiency()); } - AliDebug(1,Form("Using the following trigger code %s - %s", - fTriggerProcessor->GetName(),fTriggerProcessor->GetTitle())); + fIsInitialized = kTRUE; return kTRUE; } + //_____________________________________________________________________________ -Bool_t -AliMUONDigitizerV3::MergeDigits(const AliMUONDigit& src, - AliMUONDigit& srcAndDest) +Int_t AliMUONDigitizerV3::GetArrayIndex(Int_t cathode, Int_t trigCh, Int_t localCircuit) +{ + /// Get index of array with trigger status map or efficiency + return + AliMUONConstants::NTriggerCircuit() * AliMUONConstants::NTriggerCh() * cathode + + AliMUONConstants::NTriggerCircuit() * trigCh + localCircuit-1; +} + + +//_____________________________________________________________________________ +void +AliMUONDigitizerV3::MergeWithSDigits(AliMUONVDigitStore*& outputStore, + const AliMUONVDigitStore& input, + Int_t mask) { - // - // Merge 2 digits (src and srcAndDest) into srcAndDest. - // - AliDebug(1,"Merging the following digits:"); - StdoutToAliDebug(1,src.Print("tracks");); - StdoutToAliDebug(1,srcAndDest.Print("tracks");); - - Bool_t check = ( src.DetElemId() == srcAndDest.DetElemId() && - src.PadX() == srcAndDest.PadX() && - src.PadY() == srcAndDest.PadY() && - src.Cathode() == srcAndDest.Cathode() ); - if (!check) + /// Merge the sdigits in inputData with the digits already present in outputData + + if ( !outputStore ) outputStore = input.Create(); + + TIter next(input.CreateIterator()); + AliMUONVDigit* sdigit; + + while ( ( sdigit = static_cast(next()) ) ) { - return kFALSE; + // Update the track references using the mask. + // FIXME: this is dirty, for backward compatibility only. + // Should re-design all this way of keeping track of MC information... + if ( mask ) sdigit->PatchTracks(mask); + // Then add or update the digit to the output. + AliMUONVDigit* added = outputStore->Add(*sdigit,AliMUONVDigitStore::kMerge); + if (!added) + { + AliError("Could not add digit in merge mode"); + } } - - srcAndDest.AddSignal(src.Signal()); - srcAndDest.AddPhysicsSignal(src.Physics()); - for ( Int_t i = 0; i < src.Ntracks(); ++i ) +} + +//_____________________________________________________________________________ +TF1* +AliMUONDigitizerV3::NoiseFunction() +{ + /// Return noise function + static TF1* f = 0x0; + if (!f) { - srcAndDest.AddTrack(src.Track(i),src.TrackCharge(i)); + f = new TF1("AliMUONDigitizerV3::NoiseFunction","gaus",fgNSigmas,fgNSigmas*10); + f->SetParameters(1,0,1); } - StdoutToAliDebug(1,cout << "result:"; srcAndDest.Print("tracks");); - return kTRUE; + return f; } //_____________________________________________________________________________ -void -AliMUONDigitizerV3::MergeWithSDigits(AliMUONData& outputData, - const AliMUONData& inputData, Int_t mask) +void AliMUONDigitizerV3::SetCalibrationData(AliMUONCalibrationData* calibrationData, + AliMUONRecoParam* recoParam) { - // - // Merge the sdigits in inputData with the digits already present in outputData - // - AliDebug(1,""); - - for ( Int_t ich = 0; ich < AliMUONConstants::NCh(); ++ich ) - { - TClonesArray* iDigits = inputData.SDigits(ich); - TClonesArray* oDigits = outputData.Digits(ich); - if (!iDigits) - { - AliError(Form("Could not get sdigits for ich=%d",ich)); - return; - } - Int_t nSDigits = iDigits->GetEntriesFast(); - for ( Int_t k = 0; k < nSDigits; ++k ) - { - AliMUONDigit* sdigit = static_cast(iDigits->UncheckedAt(k)); - if (!sdigit) - { - AliError(Form("Could not get sdigit for ich=%d and k=%d",ich,k)); - } - else - { - // Update the track references using the mask. - // FIXME: this is dirty, for backward compatibility only. - // Should re-design all this way of keeping track of MC information... - if ( mask ) sdigit->PatchTracks(mask); - // Then add or update the digit to the output. - AddOrUpdateDigit(*oDigits,*sdigit); - } - } + fCalibrationData = calibrationData; + fRecoParam = recoParam; + if (!fRecoParam) + { + AliError("Cannot work (e.g. decalibrate) without recoparams !"); } } +