import FWCore.ParameterSet.Config as cms
from Configuration.StandardSequences.Eras import eras

process = cms.Process('RECO', eras.Run2_2018)

# import of standard configurations
process.load('Configuration.StandardSequences.Services_cff')
process.load('FWCore.MessageService.MessageLogger_cfi')
process.load('HeterogeneousCore.CUDACore.ProcessAcceleratorCUDA_cfi')
process.load('Configuration.StandardSequences.GeometryRecoDB_cff')
process.load('Configuration.StandardSequences.MagneticField_AutoFromDBCurrent_cff')
process.load('Configuration.StandardSequences.FrontierConditions_GlobalTag_cff')

# Other statements
from Configuration.AlCa.GlobalTag import GlobalTag
process.GlobalTag = GlobalTag(process.GlobalTag, '102X_dataRun2_HLT_v2', '')


process.maxEvents = cms.untracked.PSet(
    input = cms.untracked.int32(100)
)

# load data using the DAQ source
import sys, os, inspect
sys.path.append(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))))
process.load('sourceFromRawCmggpu_cff')

process.load('Configuration.StandardSequences.GeometryRecoDB_cff')
process.load("RecoLocalCalo.Configuration.hcalLocalReco_cff")
process.load("EventFilter.HcalRawToDigi.HcalRawToDigi_cfi")
process.load("EventFilter.EcalRawToDigi.EcalUnpackerData_cfi")
process.load("RecoLuminosity.LumiProducer.bunchSpacingProducer_cfi")

# load both cpu and gpu plugins
process.load("RecoLocalCalo.EcalRecProducers.ecalUncalibRecHitProducerGPU_cfi")
process.load("RecoLocalCalo.EcalRecProducers.ecalMultiFitUncalibRecHit_cfi")

# for validation of gpu multifit products
process.load("RecoLocalCalo.EcalRecProducers.ecalCPUUncalibRecHitProducer_cfi")
process.load("EventFilter.EcalRawToDigi.ecalCPUDigisProducer_cfi")

process.load("EventFilter.EcalRawToDigi.ecalRawToDigiGPU_cfi")
process.load("EventFilter.EcalRawToDigi.ecalElectronicsMappingGPUESProducer_cfi")

process.load("RecoLocalCalo.EcalRecProducers.ecalPedestalsGPUESProducer_cfi")
process.load("RecoLocalCalo.EcalRecProducers.ecalGainRatiosGPUESProducer_cfi")
process.load("RecoLocalCalo.EcalRecProducers.ecalPulseShapesGPUESProducer_cfi")
process.load("RecoLocalCalo.EcalRecProducers.ecalPulseCovariancesGPUESProducer_cfi")
process.load("RecoLocalCalo.EcalRecProducers.ecalSamplesCorrelationGPUESProducer_cfi")
process.load("RecoLocalCalo.EcalRecProducers.ecalTimeBiasCorrectionsGPUESProducer_cfi")
process.load("RecoLocalCalo.EcalRecProducers.ecalTimeCalibConstantsGPUESProducer_cfi")

process.load("RecoLocalCalo.EcalRecProducers.ecalRechitADCToGeVConstantGPUESProducer_cfi")
process.load("RecoLocalCalo.EcalRecProducers.ecalIntercalibConstantsGPUESProducer_cfi")
process.load("RecoLocalCalo.EcalRecProducers.ecalRechitChannelStatusGPUESProducer_cfi")

process.load("RecoLocalCalo.EcalRecProducers.ecalLaserAPDPNRatiosGPUESProducer_cfi")
process.load("RecoLocalCalo.EcalRecProducers.ecalLaserAPDPNRatiosRefGPUESProducer_cfi")
process.load("RecoLocalCalo.EcalRecProducers.ecalLaserAlphasGPUESProducer_cfi")
process.load("RecoLocalCalo.EcalRecProducers.ecalLinearCorrectionsGPUESProducer_cfi")

# force HLT configuration for ecalMultiFitUncalibRecHit
process.ecalMultiFitUncalibRecHit.algoPSet = cms.PSet(
    EBtimeFitLimits_Upper = cms.double(1.4),
    EEtimeFitLimits_Lower = cms.double(0.2),
    timealgo = cms.string("None"),
    EBtimeNconst = cms.double(28.5),
    prefitMaxChiSqEE = cms.double(10.0),
    outOfTimeThresholdGain12mEB = cms.double(5.0),
    outOfTimeThresholdGain12mEE = cms.double(1000.0),
    EEtimeFitParameters = cms.vdouble(-2.390548, 3.553628, -17.62341, 67.67538, -133.213, 140.7432, -75.41106, 16.20277),
    prefitMaxChiSqEB = cms.double(25.0),
    simplifiedNoiseModelForGainSwitch = cms.bool(True),
    EBtimeFitParameters = cms.vdouble(-2.015452, 3.130702, -12.3473, 41.88921, -82.83944, 91.01147, -50.35761, 11.05621),
    selectiveBadSampleCriteriaEB = cms.bool(False),
    dynamicPedestalsEB = cms.bool(False),
    useLumiInfoRunHeader = cms.bool(False),
    EBamplitudeFitParameters = cms.vdouble(1.138, 1.652),
    doPrefitEE = cms.bool(False),
    dynamicPedestalsEE = cms.bool(False),
    selectiveBadSampleCriteriaEE = cms.bool(False),
    outOfTimeThresholdGain61pEE = cms.double(1000.0),
    outOfTimeThresholdGain61pEB = cms.double(5.0),
    activeBXs = cms.vint32(-5, -4, -3, -2, -1, 0, 1, 2, 3, 4),
    doPrefitEB = cms.bool(False),
    addPedestalUncertaintyEE = cms.double(0.0),
    addPedestalUncertaintyEB = cms.double(0.0),
    gainSwitchUseMaxSampleEB = cms.bool(True),
    EEtimeNconst = cms.double(31.8),
    EEamplitudeFitParameters = cms.vdouble(1.89, 1.4),
    outOfTimeThresholdGain12pEB = cms.double(5.0),
    gainSwitchUseMaxSampleEE = cms.bool(False),
    mitigateBadSamplesEB = cms.bool(False),
    outOfTimeThresholdGain12pEE = cms.double(1000.0),
    ampErrorCalculation = cms.bool(False),
    mitigateBadSamplesEE = cms.bool(False),
    amplitudeThresholdEB = cms.double(10.0),
    amplitudeThresholdEE = cms.double(10.0),
    EBtimeFitLimits_Lower = cms.double(0.2),
    EEtimeFitLimits_Upper = cms.double(1.4),
    outOfTimeThresholdGain61mEE = cms.double(1000.0),
    EEtimeConstantTerm = cms.double(1.0),
    EBtimeConstantTerm = cms.double(0.6),
    outOfTimeThresholdGain61mEB = cms.double(5.0)
)

process.ecalDigis = process.ecalEBunpacker.clone()
process.ecalDigis.InputLabel = cms.InputTag('rawDataCollector')

process.out = cms.OutputModule("PoolOutputModule",
    fileName = cms.untracked.string("test.root")
)

process.finalize = cms.EndPath(process.out)

process.bunchSpacing = cms.Path(
    process.bunchSpacingProducer
)

process.digiPath = cms.Path(
    process.ecalDigis +
    process.ecalRawToDigiGPU +
    process.ecalCPUDigisProducer
)

process.recoPath = cms.Path(
    process.ecalMultiFitUncalibRecHit +
    process.ecalUncalibRecHitProducerGPU +
    process.ecalCPUUncalibRecHitProducer
)

process.schedule = cms.Schedule(
    process.bunchSpacing,
    process.digiPath,
    process.recoPath,
    process.finalize
)

process.options = cms.untracked.PSet(
    numberOfThreads = cms.untracked.uint32(4),
    numberOfStreams = cms.untracked.uint32(4),
    SkipEvent = cms.untracked.vstring('ProductNotFound'),
    wantSummary = cms.untracked.bool(True)
)
