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(
"::simulateChannel: 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;
692 const std::vector<short int>* hwCoeffs;
696 const EventContext& ctx = Gaudi::Hive::currentContext();
702 strategy = settings->lutCpStrategy();
703 slope = settings->lutCpSlope();
704 cut = settings->lutCpNoiseCut();
705 pedMean = settings->pedMean();
707 hwCoeffs = getFirCoefficients<L1CaloPprConditionsContainerRun2>(
channelId.id(), pprConditionsRun2);
710 scale_menu = l1Menu->thrExtraInfo().EM().emScale();
712 for(
auto &
coeffs : *hwCoeffs) {
719 }
else ATH_MSG_WARNING(
"::cpLut: No Conditions Container retrieved" );
721 ATH_MSG_DEBUG(
"::cpLut: strategy/scale/offset/slope/cut/pedMean/firCoeffSum/startBit: "
722 <<
strategy <<
"/" << scale_menu <<
"/" <<
offset <<
"/" << slope <<
"/" <<
cut <<
"/" << pedMean <<
"/" << hwCoeffSum <<
"/" << startBit );
724 unsigned int noiseCut = 0;
726 if (noiseCut > 0)
cut = noiseCut;
752 unsigned short scale_db = 0;
753 unsigned short scale_menu = 0;
756 const std::vector<short int>* hwCoeffs;
769 const EventContext& ctx = Gaudi::Hive::currentContext();
775 strategy = settings->lutJepStrategy();
776 slope = settings->lutJepSlope();
777 cut = settings->lutJepNoiseCut();
778 pedMean = settings->pedMean();
779 scale_db = settings->lutJepScale();
782 scale_menu = l1Menu->thrExtraInfo().JET().jetScale();
785 par1 = settings->lutJepPar1();
786 par2 = settings->lutJepPar2();
787 par3 = settings->lutJepPar3();
788 par4 = settings->lutJepPar4();
791 hwCoeffs = getFirCoefficients<L1CaloPprConditionsContainerRun2>(
channelId.id(), pprConditionsRun2);
793 for(
auto &
coeffs : *hwCoeffs) {
800 }
else ATH_MSG_WARNING(
"::jepLut: No Conditions Container retrieved" );
802 ATH_MSG_DEBUG(
"::jepLut: strategy/scale/offset/slope/cut/pedMean/firCoeffSum/startBit: "
803 <<
strategy <<
"/" << scale_menu <<
"/" <<
offset <<
"/" << slope <<
"/" <<
cut <<
"/" << pedMean <<
"/" << hwCoeffSum <<
"/" << startBit );
805 unsigned int noiseCut = 0;
807 if (noiseCut > 0)
cut = noiseCut;
810 nonLinearLut(
fir, slope,
offset,
cut, scale_db, par1, par2, par3, par4,
disabled,
output);
840 const int reScale = 2;
841 for(
auto it :
fir) {
852 out = (
it*slope*reScale -
offset*reScale + 2048)>>12;
864 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
870 for(
auto it :
fir) {
874 double nll_slope = 0.001 *
scale;
875 double nll_offset = 0.001 * par1;
876 double nll_ampl = 0.001 * par2;
877 double nll_expo = 0.;
879 nll_expo = -1. / (4096 * 0.001*par3);
883 double nll_noise = 0.001 * par4;
907 std::vector<int>::const_iterator itlut =
lut.begin();
908 std::vector<int>::const_iterator itrange =
range.begin();
909 while ( itlut !=
lut.end() && itrange !=
range.end() ) {
911 else output.push_back(*itlut);
925 if(!settings)
return std::make_tuple(
false, 0, 0);
926 return std::make_tuple(
true, settings->bcidEnergyRangeLow(), settings->bcidEnergyRangeHigh());
939 bool available =
false;
940 std::tie(available, energyLow, energyHigh) = getBcidEnergyRange<L1CaloPprConditionsContainerRun2>(
channelId.id(), pprConditionsRun2);
941 if(!available)
ATH_MSG_WARNING(
"::etRange: No L1CaloPprConditions found");
942 }
else ATH_MSG_WARNING(
"::etRange: No Conditions Container retrieved");
945 <<
" energyHigh: " << energyHigh);
957 if (
it <= energyLow)
output.push_back(0);
958 else if (
it <= energyHigh)
output.push_back(1);
972 if(!settings)
return std::make_tuple(
false, 0);
973 return std::make_tuple(
true, settings->firStartBit());
979 unsigned int start = 0;
983 bool available =
false;
984 std::tie(available,
start) = getFirStartBit<L1CaloPprConditionsContainerRun2>(
channelId.id(), pprConditionsRun2);
985 if(!available)
ATH_MSG_WARNING(
"::dropBits: No L1CaloPprConditions found" );
986 }
else ATH_MSG_WARNING(
"::dropBits: No Conditions Container retrieved" );
1006 for(
auto it :
fir) {
1021 const std::vector<short int>* hwCoeffs =
nullptr;
1022 hwCoeffs = getFirCoefficients<L1CaloPprConditionsContainerRun2>(
channelId.id(), pprConditionsRun2 );
1028 firCoeffs.reserve(hwCoeffs->size());
1029 for (
int i = hwCoeffs->size()-1;
i >= 0; --
i) firCoeffs.push_back((*hwCoeffs)[
i]);
1031 }
else ATH_MSG_WARNING(
"::firParams: No L1CaloPprConditions found" );
1032 }
else ATH_MSG_WARNING(
"::firParams: No Conditions Container retrieved" );
1041 unsigned int &peakFinderStrategy,
int &satLow,
int &satHigh,
int &satLevel)
const
1046 decisionConditions.clear();
1047 peakFinderStrategy = 0;
1056 std::tuple<unsigned int, unsigned int, unsigned int>
bcidDecision;
1057 std::tuple<bool, int, int> bcidEnergyRange;
1058 std::tuple<bool, int, int, int> saturation;
1061 bcidDecision = getBcidDecision<Cont>(pprConditionsRun2);
1062 peakFinderStrategy = getStrategy<Cont>(pprConditionsRun2);
1063 decisionSource = getDecisionSource<Cont>(pprConditionsRun2);
1064 bcidEnergyRange = getBcidEnergyRange<Cont>(
channelId.id(), pprConditionsRun2);
1065 saturation = getSaturation<Cont>(
channelId.id(), pprConditionsRun2);
1071 if(get<0>(bcidEnergyRange)) {
1072 std::tie(
std::ignore, energyLow, energyHigh) = bcidEnergyRange;
1075 if(get<0>(saturation)) {
1076 std::tie(
std::ignore, satLevel, satLow, satHigh) = saturation;
1078 }
else ATH_MSG_WARNING(
"::bcid:Params No Conditions Container retrieved" );
1081 <<
" satLow: " << satLow <<
" satHigh: " << satHigh <<
endmsg
1082 <<
" energyLow: " << energyLow <<
" energyHigh: " << energyHigh <<
endmsg
1083 <<
" decisionSource: " << decisionSource <<
" peakFinderStrategy: "
1084 << peakFinderStrategy );
1099 const std::vector<short int>* hwCoeffs;
1113 strategy = settings->lutCpStrategy();
1114 slope = settings->lutCpSlope();
1115 cut = settings->lutCpNoiseCut();
1116 pedValue = settings->pedValue();
1117 pedMean = settings->pedMean();
1119 hwCoeffs = getFirCoefficients<L1CaloPprConditionsContainerRun2>(
channelId.id(), pprConditionsRun2);
1121 for(
auto &
coeffs : *hwCoeffs) {
1127 ATH_MSG_VERBOSE(
"::jepLutParams: Offset: offset/strategy/pedMean/firCoeffSum/startBit/slope: "
1128 <<
offset <<
" " <<
strategy <<
" " <<
" " << pedMean <<
" " << hwCoeffSum <<
" " << startBit <<
" " << slope );
1130 }
else ATH_MSG_WARNING(
"::cpLutParams: No L1CaloPprConditions found" );
1131 }
else ATH_MSG_WARNING(
"::cpLutParams: No Conditions Container retrieved" );
1133 ATH_MSG_VERBOSE(
"::cpLutParams: LUT startBit/strategy/offset/slope/cut/pedValue/pedMean: "
1134 << startBit <<
" " <<
strategy <<
" " <<
offset <<
" " << slope <<
" " <<
cut <<
" " << pedValue <<
" " << pedMean );
1135 unsigned int noiseCut = 0;
1137 if (noiseCut > 0)
cut = noiseCut;
1151 const std::vector<short int>* hwCoeffs;
1163 strategy = settings->lutJepStrategy();
1164 slope = settings->lutJepSlope();
1165 cut = settings->lutJepNoiseCut();
1166 pedValue = settings->pedValue();
1167 pedMean = settings->pedMean();
1169 hwCoeffs = getFirCoefficients<L1CaloPprConditionsContainerRun2>(
channelId.id(),pprConditionsRun2);
1171 for(
auto &
coeffs : *hwCoeffs) {
1177 ATH_MSG_VERBOSE(
"::jepLutParams: Offset: offset/strategy/pedMean/firCoeffSum/startBit/slope: "
1178 <<
offset <<
" " <<
strategy <<
" " <<
" " << pedMean <<
" " << hwCoeffSum <<
" " << startBit <<
" " << slope );
1180 }
else ATH_MSG_WARNING(
"::jepLutParams: No L1CaloPprConditions found" );
1181 }
else ATH_MSG_WARNING(
"::jepLutParams: No Conditions Container retrieved" );
1183 ATH_MSG_VERBOSE(
"::jepLutParams: LUT startBit/strategy/offset/slope/cut/pedValue/pedMean: "
1184 << startBit <<
" " <<
strategy <<
" " <<
offset <<
" " << slope <<
" " <<
cut <<
" " << pedValue <<
" " << pedMean );
1185 unsigned int noiseCut = 0;
1187 if (noiseCut > 0)
cut = noiseCut;
1210 HWIdentifier hwId(0);
1212 try { hwId =
m_ttSvc->createTTChannelID(
id,
false); }
1231 const double absEta = std::abs(eta);
1237 }
else if (
absEta < 3.2) {
1246 if (eta < 0.)
index += 1792;
1269 coolId =
m_ttSvc->createL1CoolChannelId(hwId);
1289 bool override =
false;
1292 satOverride = getSatOverride<L1CaloPprConditionsContainerRun2>(pprConditionsRun2);
1297 }
else ATH_MSG_WARNING(
"::satOverride: No Conditions Container retrieved" );
1300 <<
" has saturation override flag " <<
override );
1309 unsigned int noiseCut = 0;
1318 bool isDisabled =
false;
1325 if (!disabledChan->disabledBits()) {
1329 if (deadError.
chanValid()) noiseCut = disabledChan->noiseCut();
1332 }
else isDisabled =
true;
1335 <<
"::disabledChannel: calibErrorCode: " << (disabledChan->calibErrorCode()).errorCode()
1336 <<
" deadErrorCode: " << (disabledChan->deadErrorCode()).errorCode()
1337 <<
" noiseCut: " << disabledChan->noiseCut()
1338 <<
" disabledBits: " << disabledChan->disabledBits()
1342 ATH_MSG_DEBUG(
"::disabledChannel: No L1CaloPprDisabledChannel found" );
1345 ATH_MSG_WARNING(
"::disabledChannel: No DisabledChannel Container retrieved" );
1347 if (isDisabled)
ATH_MSG_DEBUG(
"::disabledChannel: Channel is disabled" );
1359 float abseta = std::abs(eta);
1360 if (abseta<3.2)
return eta;
1362 int sign = ((eta > 0) ? 1 : -1);
1363 if (abseta < 3.6) eta = 3.15 *
sign;
1364 else if (abseta < 4.0) eta = 3.33 *
sign;
1365 else if (abseta < 4.5) eta = 3.72 *
sign;
1366 else eta = 4.41 *
sign;
1369 if (abseta < 3.6) eta = 3.36;
1370 else if (abseta < 4.0) eta = 3.45;
1371 else if (abseta < 4.5) eta = 4.17;
1375 if (abseta < 3.6) eta = -3.45;
1376 else if (abseta < 4.0) eta = -3.36;
1377 else if (abseta < 4.5) eta = -4.19;
1390 throw GaudiException(
"No mapping tool configured",
1391 "L1TriggerTowerToolRun3::FCalTTeta", StatusCode::FAILURE);
1397 unsigned int mcm =
channelId.subModule();
1433 <<
"::refValues: errorCode: " << (ftref->
errorCode()).errorCode()
1434 << MSG::dec <<
" reference: " << ftref->
refValue() <<
" calib: " << ftref->
calibValue() );
1447 unsigned nFIR = firInOut.size();
1448 correctionOut.assign(nFIR, 0
u);
1453 for(
unsigned i = 0;
i != nFIR; ++
i) {
1455 firInOut[
i] -= correctionOut[
i];
1457 if(firInOut[
i] < 0) firInOut[
i] = 0;
1460 ATH_MSG_DEBUG(
"::pedestalCorrection(BCID=" <<
bcid <<
", mu = " <<
mu <<
"): " << correctionOut);
1466 const EventContext& ctx = Gaudi::Hive::currentContext();
1467 if (ctx.eventID().run_number() >= 253377)
return true;
1474 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
1476 unsigned int lutOffset = 0;
1478 long long int lutOffsetLong = 0;
1479 long long int lutSlopeLong = lutSlope;
1480 long long int firStartBitLong = firStartBit;
1481 long long int pedMeanLong = std::lround(pedMean * 10000.);
1482 long long int firCoeffSum = 0;
1484 for (
unsigned int i=0;
i<firCoeff.size();
i++) {
1485 firCoeffSum += firCoeff.at(
i);
1488 if ( lutStrategy == 0 ) {
1489 lutOffsetLong = ((pedMeanLong*firCoeffSum) >> firStartBitLong);
1492 lutOffsetLong = ((pedMeanLong*firCoeffSum*lutSlopeLong) >> firStartBitLong) - ((lutSlopeLong * 10000) >> 1);
1495 lutOffsetLong = (lutOffsetLong + (10000-1))/10000;
1496 lutOffset =
static_cast<unsigned int>( lutOffsetLong < 0 ? 0 : lutOffsetLong );
1504 if (
sc.isFailure()) {