8 #include "GaudiKernel/Incident.h"
9 #include "GaudiKernel/IIncidentSvc.h"
10 #include "GaudiKernel/GaudiException.h"
23 #include "GaudiKernel/ThreadLocalContext.h"
27 #include <boost/range/adaptor/reversed.hpp>
28 using namespace boost::adaptors;
35 const int L1TriggerTowerToolRun3::s_saturationValue;
36 const int L1TriggerTowerToolRun3::s_maxTowers;
40 L1TriggerTowerToolRun3::L1TriggerTowerToolRun3(
const std::string&
t,
47 m_l1CaloTTIdTools(
"LVL1::L1CaloTTIdTools/L1CaloTTIdTools", this),
48 m_ttSvc(
"CaloTriggerTowerService/CaloTriggerTowerService", this),
49 m_mappingTool(
"", this),
51 m_dynamicPedestalProvider(
"", this)
53 declareInterface<IL1TriggerTowerToolRun3>(
this);
76 if(!
m_ttSvc.retrieve().isSuccess()) {
83 if (scID.isFailure()) {
94 SmartIF<IIncidentSvc> incSvc{service(
"IncidentSvc")};
96 incSvc->addListener(
this,
"BeginRun");
113 return StatusCode::SUCCESS;
120 return StatusCode::SUCCESS;
127 if (inc.type()==
"BeginRun") {
139 std::vector<int> &
et, std::vector<int> &bcidResults,
140 std::vector<int> &bcidDecisions,
bool useJepLut )
146 process(digits,
id,
et, bcidResults, bcidDecisions, useJepLut);
153 std::vector<int> &
et, std::vector<int> &bcidResults,
154 std::vector<int> &bcidDecisions,
bool useJepLut )
165 bcidDecisions.clear();
172 std::vector<int> lutInput;
183 std::vector<int> decisionRange;
185 bcidDecision(bcidResults, decisionRange, bcidDecisions);
190 std::vector<int> lutOutput;
206 template <
typename DST,
typename SRC>
207 std::vector<DST> convertVectorType(
const std::vector<SRC>&
s) {
208 std::vector<DST>
d(
s.size());
210 [](SRC
v){
return static_cast<DST
>(
v);});
222 ATH_MSG_DEBUG(
"RunParameters:: readoutConfigID " << readoutConfigID);
224 std::vector<uint16_t> digits40;
226 if(readoutConfigID == 5 or readoutConfigID == 6){
229 ATH_MSG_DEBUG(
"80 MHz readout detected, emulating 40 MHz samples");
231 int nSlices =
tt.adc().size();
234 for (
int i=0 ;
i < (nSlices-1)/2 ;
i++ ){
235 digits40.push_back(
tt.adc().at(2*
i+1));
238 else if((nSlices%4)==1){
239 for (
int i=0 ;
i <= (nSlices-1)/2 ;
i++){
240 digits40.push_back(
tt.adc().at(2*
i));
250 const auto& digits = convertVectorType<int>(digits40);
254 ATH_MSG_DEBUG(
"::simulateChannel: ==== Entered Process ====" );
263 bcidDecisions.clear();
272 ATH_MSG_DEBUG(
"::simulateChannel: ---- pedestalCorrection ----" );
275 const std::size_t nCorr =
tt.correctionEnabled().size();
276 const std::size_t filterOffset =
filter.size()/2 - nCorr/2;
277 for(std::size_t iCorr = 0; iCorr < nCorr; ++iCorr) {
278 filter[filterOffset + iCorr] -=
tt.correction()[iCorr] *
tt.correctionEnabled()[iCorr];
284 std::vector<int> lutInput;
287 ATH_MSG_DEBUG(
"::simulateChannel: ---- BCID algorithms ---- ");
294 std::vector<int> decisionRange;
296 bcidDecision(bcidResults, decisionRange, bcidDecisions);
298 ATH_MSG_DEBUG(
"::simulateChannel: bcidDecisionRange " << decisionRange);
299 ATH_MSG_DEBUG(
"::simulateChannel: bcidDecisions " << bcidDecisions);
303 ATH_MSG_DEBUG(
"::simulateChannel: ---- LUT ET calculation ----" );
306 std::vector<int> cpLutOutput, jepLutOutput;
311 ATH_MSG_DEBUG(
"::simulateChannel: jepLut " << jepLutOutput);
313 ATH_MSG_DEBUG(
"::simulateChannel: ---- use ET range ----" );
320 ATH_MSG_DEBUG(
"::simulateChannel: cpLut applyETRange " << outCpLut);
321 ATH_MSG_DEBUG(
"::simulateChannel: jepLut applyETRange " << outJepLut);
324 ATH_MSG_DEBUG(
"::simulateChannel: ==== Leaving Process ====" );
333 std::vector<int> peak;
335 std::vector<int> sat;
339 output.reserve(sat.size());
343 for ( ; itpeak!=peak.end() && itsat!=sat.end(); ++itpeak, ++itsat ) {
344 output.push_back( (*itpeak<<2) + (*itsat<<1) );
355 unsigned int strategy,
int satLow,
int satHigh,
int satLevel, std::vector<int> &
output)
const
358 std::vector<int> peak;
360 std::vector<int> sat;
361 satBcid(digits, satLow, satHigh, satLevel, sat);
364 output.reserve(sat.size());
368 for ( ; itpeak!=peak.end() && itsat!=sat.end(); ++itpeak, ++itsat ) {
369 output.push_back( (*itpeak<<2) + (*itsat<<1) );
378 void L1TriggerTowerToolRun3::bcid(
const std::vector<int> &
filter,
const std::vector<int> &lutInput,
const std::vector<int> &digits,
int energyLow,
int energyHigh,
int decisionSource, std::vector<unsigned int> &decisionConditions,
379 unsigned int strategy,
int satLow,
int satHigh,
int satLevel, std::vector<int> &
result, std::vector<int> &decision)
const
382 std::vector<int> peak;
384 std::vector<int> sat;
385 satBcid(digits, satLow, satHigh, satLevel, sat);
388 result.reserve(sat.size());
393 for ( ; itpeak!=peak.end() && itsat!=sat.end(); ++itpeak, ++itsat ) {
394 result.push_back( (*itpeak<<2) + (*itsat<<1) );
402 std::vector<int> decisionRange;
403 if (!(decisionSource&0
x1))
etRange(digits, energyLow, energyHigh, decisionRange);
404 else etRange(lutInput, energyLow, energyHigh, decisionRange);
414 if(!settings)
return nullptr;
415 return &(settings->firCoefficients());
428 std::vector<int> firCoeffs;
430 const std::vector<short int>* hwCoeffs;
431 hwCoeffs = getFirCoefficients<L1CaloPprConditionsContainerRun2>(
channelId.id(), pprConditionsRun2);
436 firCoeffs.reserve(hwCoeffs->size());
437 for (
auto &
i :
reverse(*hwCoeffs)) {
438 firCoeffs.push_back(
i);
456 output.reserve(digits.size());
461 for (
unsigned int i = 0;
i < firCoeffs.size(); ++
i) {
462 if (firstFIR < 0 && firCoeffs[
i] != 0) firstFIR =
i;
463 if (firCoeffs[
i] != 0) lastFIR =
i;
465 if (firstFIR < 0) firstFIR = lastFIR + 1;
467 for (
int i = 0;
i < (
int)digits.size();
i++) {
470 if (
i >= 2-firstFIR &&
i < (
int)digits.size()+2-lastFIR) {
471 for (
int j = firstFIR; j <= lastFIR; ++j) {
472 sum += digits[
i+j-2]*firCoeffs[j];
498 strategy = getStrategy<L1CaloPprConditionsContainerRun2>( pprConditionsRun2);
499 }
else ATH_MSG_WARNING(
"::peakBcid: No Conditions Container retrieved" );
512 for (
unsigned int i = 0;
i <
fir.size();
i++) {
515 if (
i > 0 &&
i <
fir.size()-1) {
535 if(!settings)
return std::make_tuple(
false, 0, 0, 0);
536 return std::make_tuple(
true, settings->satBcidLevel(), settings->satBcidThreshLow(),
537 settings->satBcidThreshHigh());
550 bool available =
false;
551 std::tie(available, satLevel, satLow, satHigh) = getSaturation<L1CaloPprConditionsContainerRun2>(
channelId.id(), pprConditionsRun2);
552 if(!available)
ATH_MSG_WARNING(
"::satBcid: No L1CaloPprConditions found" );
553 }
else ATH_MSG_WARNING(
"::satBcid: No Conditions Container retrieved" );
556 <<
" satLow: " << satLow
557 <<
" satHigh: " << satHigh );
567 output.reserve(digits.size());
572 for (
unsigned int i = 0;
i<digits.size();
i++) {
578 if (digits[
i]>=satLevel) {
579 if (enabled &&
i>1) {
580 bool low = (digits[
i-2]>satLow);
581 bool high = (digits[
i-1]>satHigh);
616 int decisionSource = 0;
620 decisionSource = getDecisionSource<L1CaloPprConditionsContainerRun2>(pprConditionsRun2);
622 }
else ATH_MSG_WARNING(
"::bcidDecisionRange: No Conditions Container retrieved" );
627 ATH_MSG_DEBUG(
"::bcidDecisionRange: decisionSource: " << decisionSource);
642 unsigned int decision1 = 0;
643 unsigned int decision2 = 0;
644 unsigned int decision3 = 0;
648 std::tie(decision1, decision2, decision3) = getBcidDecision<L1CaloPprConditionsContainerRun2>(pprConditionsRun2);
649 }
else ATH_MSG_WARNING(
"::bcidDecision: No Conditions Container retrieved" );
652 std::vector<unsigned int>
mask = { decision3, decision2, decision1 };
655 << decision3 <<
" " << decision2 <<
" " << decision1 << MSG::dec );
665 output.reserve(bcidResults.size());
667 std::vector<int>::const_iterator itBcid = bcidResults.begin();
668 std::vector<int>::const_iterator itRange =
range.begin();
669 int nRange =
mask.size();
671 for ( ; itBcid != bcidResults.end() && itRange !=
range.end(); ++itBcid, ++itRange) {
672 if ((*itRange) < nRange && (
mask[*itRange]&(0
x1<<*itBcid)))
output.push_back(1);
689 unsigned short scale_menu = 0;
693 const std::vector<short int>* hwCoeffs;
697 const EventContext& ctx = Gaudi::Hive::currentContext();
703 strategy = settings->lutCpStrategy();
704 slope = settings->lutCpSlope();
705 cut = settings->lutCpNoiseCut();
706 ped = settings->pedValue();
707 pedMean = settings->pedMean();
709 hwCoeffs = getFirCoefficients<L1CaloPprConditionsContainerRun2>(
channelId.id(), pprConditionsRun2);
712 scale_menu = l1Menu->thrExtraInfo().EM().emScale();
714 for(
auto &
coeffs : *hwCoeffs) {
720 ATH_MSG_DEBUG(
"::cpLut: Offset: offset/strategy/pedMean/firCoeffSum/startBit/slope: "
721 <<
offset <<
" " <<
strategy <<
" " <<
" " << pedMean <<
" " << hwCoeffSum <<
" " << startBit <<
" " << slope );
724 }
else ATH_MSG_WARNING(
"::cpLut: No Conditions Container retrieved" );
726 ATH_MSG_DEBUG(
"::cpLut: LUT strategy/offset/slope/cut/ped: "
729 unsigned int noiseCut = 0;
731 if (noiseCut > 0)
cut = noiseCut;
757 unsigned short scale_db = 0;
758 unsigned short scale_menu = 0;
762 const std::vector<short int>* hwCoeffs;
775 const EventContext& ctx = Gaudi::Hive::currentContext();
781 strategy = settings->lutJepStrategy();
782 slope = settings->lutJepSlope();
783 cut = settings->lutJepNoiseCut();
784 ped = settings->pedValue();
785 pedMean = settings->pedMean();
786 scale_db = settings->lutJepScale();
789 scale_menu = l1Menu->thrExtraInfo().JET().jetScale();
792 par1 = settings->lutJepPar1();
793 par2 = settings->lutJepPar2();
794 par3 = settings->lutJepPar3();
795 par4 = settings->lutJepPar4();
798 hwCoeffs = getFirCoefficients<L1CaloPprConditionsContainerRun2>(
channelId.id(), pprConditionsRun2);
800 for(
auto &
coeffs : *hwCoeffs) {
806 ATH_MSG_VERBOSE(
"::jepLut: Offset: offset/strategy/pedMean/firCoeffSum/startBit/slope: "
807 <<
offset <<
" " <<
strategy <<
" " <<
" " << pedMean <<
" " << hwCoeffSum <<
" " << startBit <<
" " << slope );
810 }
else ATH_MSG_WARNING(
"::jepLut: No Conditions Container retrieved" );
815 unsigned int noiseCut = 0;
817 if (noiseCut > 0)
cut = noiseCut;
820 nonLinearLut(
fir, slope,
offset,
cut, scale_db, par1, par2, par3, par4,
disabled,
output);
850 const int reScale = 2;
851 for(
auto it :
fir) {
862 out = (
it*slope*reScale -
offset*reScale + 2048)>>12;
874 void L1TriggerTowerToolRun3::nonLinearLut(
const std::vector<int> &fir,
int slope,
int offset,
int cut,
int scale,
short par1,
short par2,
short par3,
short par4,
bool disabled, std::vector<int> &
output)
const
880 for(
auto it :
fir) {
884 double nll_slope = 0.001 *
scale;
885 double nll_offset = 0.001 * par1;
886 double nll_ampl = 0.001 * par2;
887 double nll_expo = 0.;
889 nll_expo = -1. / (4096 * 0.001*par3);
893 double nll_noise = 0.001 * par4;
917 std::vector<int>::const_iterator itlut =
lut.begin();
918 std::vector<int>::const_iterator itrange =
range.begin();
919 while ( itlut !=
lut.end() && itrange !=
range.end() ) {
921 else output.push_back(*itlut);
935 if(!settings)
return std::make_tuple(
false, 0, 0);
936 return std::make_tuple(
true, settings->bcidEnergyRangeLow(), settings->bcidEnergyRangeHigh());
949 bool available =
false;
950 std::tie(available, energyLow, energyHigh) = getBcidEnergyRange<L1CaloPprConditionsContainerRun2>(
channelId.id(), pprConditionsRun2);
951 if(!available)
ATH_MSG_WARNING(
"::etRange: No L1CaloPprConditions found");
952 }
else ATH_MSG_WARNING(
"::etRange: No Conditions Container retrieved");
955 <<
" energyHigh: " << energyHigh);
967 if (
it <= energyLow)
output.push_back(0);
968 else if (
it <= energyHigh)
output.push_back(1);
982 if(!settings)
return std::make_tuple(
false, 0);
983 return std::make_tuple(
true, settings->firStartBit());
989 unsigned int start = 0;
993 bool available =
false;
994 std::tie(available,
start) = getFirStartBit<L1CaloPprConditionsContainerRun2>(
channelId.id(), pprConditionsRun2);
995 if(!available)
ATH_MSG_WARNING(
"::dropBits: No L1CaloPprConditions found" );
996 }
else ATH_MSG_WARNING(
"::dropBits: No Conditions Container retrieved" );
1016 for(
auto it :
fir) {
1031 const std::vector<short int>* hwCoeffs =
nullptr;
1032 hwCoeffs = getFirCoefficients<L1CaloPprConditionsContainerRun2>(
channelId.id(), pprConditionsRun2 );
1038 firCoeffs.reserve(hwCoeffs->size());
1039 for (
int i = hwCoeffs->size()-1;
i >= 0; --
i) firCoeffs.push_back((*hwCoeffs)[
i]);
1041 }
else ATH_MSG_WARNING(
"::firParams: No L1CaloPprConditions found" );
1042 }
else ATH_MSG_WARNING(
"::firParams: No Conditions Container retrieved" );
1051 unsigned int &peakFinderStrategy,
int &satLow,
int &satHigh,
int &satLevel)
const
1056 decisionConditions.clear();
1057 peakFinderStrategy = 0;
1066 std::tuple<unsigned int, unsigned int, unsigned int>
bcidDecision;
1067 std::tuple<bool, int, int> bcidEnergyRange;
1068 std::tuple<bool, int, int, int> saturation;
1071 bcidDecision = getBcidDecision<Cont>(pprConditionsRun2);
1072 peakFinderStrategy = getStrategy<Cont>(pprConditionsRun2);
1073 decisionSource = getDecisionSource<Cont>(pprConditionsRun2);
1074 bcidEnergyRange = getBcidEnergyRange<Cont>(
channelId.id(), pprConditionsRun2);
1075 saturation = getSaturation<Cont>(
channelId.id(), pprConditionsRun2);
1081 if(get<0>(bcidEnergyRange)) {
1082 std::tie(
std::ignore, energyLow, energyHigh) = bcidEnergyRange;
1085 if(get<0>(saturation)) {
1086 std::tie(
std::ignore, satLevel, satLow, satHigh) = saturation;
1088 }
else ATH_MSG_WARNING(
"::bcid:Params No Conditions Container retrieved" );
1091 <<
" satLow: " << satLow <<
" satHigh: " << satHigh <<
endmsg
1092 <<
" energyLow: " << energyLow <<
" energyHigh: " << energyHigh <<
endmsg
1093 <<
" decisionSource: " << decisionSource <<
" peakFinderStrategy: "
1094 << peakFinderStrategy );
1109 const std::vector<short int>* hwCoeffs;
1123 strategy = settings->lutCpStrategy();
1124 slope = settings->lutCpSlope();
1125 cut = settings->lutCpNoiseCut();
1126 pedValue = settings->pedValue();
1127 pedMean = settings->pedMean();
1129 hwCoeffs = getFirCoefficients<L1CaloPprConditionsContainerRun2>(
channelId.id(), pprConditionsRun2);
1131 for(
auto &
coeffs : *hwCoeffs) {
1137 ATH_MSG_VERBOSE(
"::jepLutParams: Offset: offset/strategy/pedMean/firCoeffSum/startBit/slope: "
1138 <<
offset <<
" " <<
strategy <<
" " <<
" " << pedMean <<
" " << hwCoeffSum <<
" " << startBit <<
" " << slope );
1140 }
else ATH_MSG_WARNING(
"::cpLutParams: No L1CaloPprConditions found" );
1141 }
else ATH_MSG_WARNING(
"::cpLutParams: No Conditions Container retrieved" );
1143 ATH_MSG_VERBOSE(
"::cpLutParams: LUT startBit/strategy/offset/slope/cut/pedValue/pedMean: "
1144 << startBit <<
" " <<
strategy <<
" " <<
offset <<
" " << slope <<
" " <<
cut <<
" " << pedValue <<
" " << pedMean );
1145 unsigned int noiseCut = 0;
1147 if (noiseCut > 0)
cut = noiseCut;
1161 const std::vector<short int>* hwCoeffs;
1173 strategy = settings->lutJepStrategy();
1174 slope = settings->lutJepSlope();
1175 cut = settings->lutJepNoiseCut();
1176 pedValue = settings->pedValue();
1177 pedMean = settings->pedMean();
1179 hwCoeffs = getFirCoefficients<L1CaloPprConditionsContainerRun2>(
channelId.id(),pprConditionsRun2);
1181 for(
auto &
coeffs : *hwCoeffs) {
1187 ATH_MSG_VERBOSE(
"::jepLutParams: Offset: offset/strategy/pedMean/firCoeffSum/startBit/slope: "
1188 <<
offset <<
" " <<
strategy <<
" " <<
" " << pedMean <<
" " << hwCoeffSum <<
" " << startBit <<
" " << slope );
1190 }
else ATH_MSG_WARNING(
"::jepLutParams: No L1CaloPprConditions found" );
1191 }
else ATH_MSG_WARNING(
"::jepLutParams: No Conditions Container retrieved" );
1193 ATH_MSG_VERBOSE(
"::jepLutParams: LUT startBit/strategy/offset/slope/cut/pedValue/pedMean: "
1194 << startBit <<
" " <<
strategy <<
" " <<
offset <<
" " << slope <<
" " <<
cut <<
" " << pedValue <<
" " << pedMean );
1195 unsigned int noiseCut = 0;
1197 if (noiseCut > 0)
cut = noiseCut;
1220 HWIdentifier hwId(0);
1222 try { hwId =
m_ttSvc->createTTChannelID(
id,
false); }
1241 const double absEta = std::abs(eta);
1247 }
else if (
absEta < 3.2) {
1256 if (eta < 0.)
index += 1792;
1279 coolId =
m_ttSvc->createL1CoolChannelId(hwId);
1299 bool override =
false;
1302 satOverride = getSatOverride<L1CaloPprConditionsContainerRun2>(pprConditionsRun2);
1307 }
else ATH_MSG_WARNING(
"::satOverride: No Conditions Container retrieved" );
1310 <<
" has saturation override flag " <<
override );
1319 unsigned int noiseCut = 0;
1328 bool isDisabled =
false;
1335 if (!disabledChan->disabledBits()) {
1339 if (deadError.
chanValid()) noiseCut = disabledChan->noiseCut();
1342 }
else isDisabled =
true;
1345 <<
"::disabledChannel: calibErrorCode: " << (disabledChan->calibErrorCode()).errorCode()
1346 <<
" deadErrorCode: " << (disabledChan->deadErrorCode()).errorCode()
1347 <<
" noiseCut: " << disabledChan->noiseCut()
1348 <<
" disabledBits: " << disabledChan->disabledBits()
1352 ATH_MSG_DEBUG(
"::disabledChannel: No L1CaloPprDisabledChannel found" );
1355 ATH_MSG_WARNING(
"::disabledChannel: No DisabledChannel Container retrieved" );
1357 if (isDisabled)
ATH_MSG_DEBUG(
"::disabledChannel: Channel is disabled" );
1369 float abseta = std::abs(eta);
1370 if (abseta<3.2)
return eta;
1372 int sign = ((eta > 0) ? 1 : -1);
1373 if (abseta < 3.6) eta = 3.15 *
sign;
1374 else if (abseta < 4.0) eta = 3.33 *
sign;
1375 else if (abseta < 4.5) eta = 3.72 *
sign;
1376 else eta = 4.41 *
sign;
1379 if (abseta < 3.6) eta = 3.36;
1380 else if (abseta < 4.0) eta = 3.45;
1381 else if (abseta < 4.5) eta = 4.17;
1385 if (abseta < 3.6) eta = -3.45;
1386 else if (abseta < 4.0) eta = -3.36;
1387 else if (abseta < 4.5) eta = -4.19;
1400 throw GaudiException(
"No mapping tool configured",
1401 "L1TriggerTowerToolRun3::FCalTTeta", StatusCode::FAILURE);
1407 unsigned int mcm =
channelId.subModule();
1443 <<
"::refValues: errorCode: " << (ftref->
errorCode()).errorCode()
1444 << MSG::dec <<
" reference: " << ftref->
refValue() <<
" calib: " << ftref->
calibValue() );
1457 unsigned nFIR = firInOut.size();
1458 correctionOut.assign(nFIR, 0
u);
1463 for(
unsigned i = 0;
i != nFIR; ++
i) {
1465 firInOut[
i] -= correctionOut[
i];
1467 if(firInOut[
i] < 0) firInOut[
i] = 0;
1470 ATH_MSG_DEBUG(
"::pedestalCorrection(BCID=" <<
bcid <<
", mu = " <<
mu <<
"): " << correctionOut);
1476 const EventContext& ctx = Gaudi::Hive::currentContext();
1477 if (ctx.eventID().run_number() >= 253377)
return true;
1484 unsigned int L1TriggerTowerToolRun3::getLutOffset(
const double &pedMean,
const unsigned int &firStartBit,
const std::vector<short int> &firCoeff,
const unsigned int &lutSlope,
const unsigned int &lutStrategy)
const
1486 unsigned int lutOffset = 0;
1488 long long int lutOffsetLong = 0;
1489 long long int lutSlopeLong = lutSlope;
1490 long long int firStartBitLong = firStartBit;
1491 long long int pedMeanLong = std::lround(pedMean * 10000.);
1492 long long int firCoeffSum = 0;
1494 for (
unsigned int i=0;
i<firCoeff.size();
i++) {
1495 firCoeffSum += firCoeff.at(
i);
1498 if ( lutStrategy == 0 ) {
1499 lutOffsetLong = ((pedMeanLong*firCoeffSum) >> firStartBitLong);
1502 lutOffsetLong = ((pedMeanLong*firCoeffSum*lutSlopeLong) >> firStartBitLong) - ((lutSlopeLong * 10000) >> 1);
1505 lutOffsetLong = (lutOffsetLong + (10000-1))/10000;
1506 lutOffset =
static_cast<unsigned int>( lutOffsetLong < 0 ? 0 : lutOffsetLong );
1514 if (
sc.isFailure()) {