9 #include "GaudiKernel/Incident.h"
10 #include "GaudiKernel/IIncidentSvc.h"
11 #include "GaudiKernel/GaudiException.h"
42 #include "GaudiKernel/ThreadLocalContext.h"
64 m_l1CaloTTIdTools(
"LVL1::L1CaloTTIdTools/L1CaloTTIdTools", this),
65 m_ttSvc(
"CaloTriggerTowerService/CaloTriggerTowerService", this),
66 m_mappingTool(
"", this),
67 m_dbFineTimeRefsTowers(0),
69 m_dynamicPedestalProvider(
"", this)
71 declareInterface<IL1TriggerTowerTool>(
this);
97 if(!
m_ttSvc.retrieve().isSuccess()) {
104 if (scID.isFailure()) {
115 SmartIF<IIncidentSvc> incSvc{service(
"IncidentSvc")};
117 incSvc->addListener(
this,
"BeginRun");
131 return StatusCode::SUCCESS;
138 return StatusCode::SUCCESS;
145 if (inc.type()==
"BeginRun") {
188 ATH_MSG_ERROR(
"non-Run2 no longer supported ... requires update to L1CaloCondAlg to create non-Run2 versions of containers");
189 return StatusCode::FAILURE;
191 return StatusCode::SUCCESS;
197 std::vector<int> &
et, std::vector<int> &bcidResults,
198 std::vector<int> &bcidDecisions,
bool useJepLut )
204 process(digits,
id,
et, bcidResults, bcidDecisions, useJepLut);
211 std::vector<int> &
et, std::vector<int> &bcidResults,
212 std::vector<int> &bcidDecisions,
bool useJepLut )
224 bcidDecisions.clear();
231 std::vector<int> lutInput;
242 std::vector<int> decisionRange;
249 std::vector<int> lutOutput;
268 template <
typename DST,
typename SRC>
269 std::vector<DST> convertVectorType(
const std::vector<SRC>&
s) {
270 std::vector<DST>
d(
s.size());
272 [](SRC
v){
return static_cast<DST
>(
v);});
283 unsigned int readoutConfigID = std::cbegin(*m_runParametersContainer)->readoutConfigID();
289 std::vector<uint16_t> digits40;
291 if(readoutConfigID == 5 or readoutConfigID == 6){
297 int nSlices =
tt.adc().size();
300 for (
int i=0 ;
i < (nSlices-1)/2 ;
i++ ){
301 digits40.push_back(
tt.adc().at(2*
i+1));
304 else if((nSlices%4)==1){
305 for (
int i=0 ;
i <= (nSlices-1)/2 ;
i++){
306 digits40.push_back(
tt.adc().at(2*
i));
318 const auto& digits = convertVectorType<int>(digits40);
333 bcidDecisions.clear();
346 const std::size_t nCorr =
tt.correctionEnabled().size();
347 const std::size_t filterOffset =
filter.size()/2 - nCorr/2;
348 for(std::size_t iCorr = 0; iCorr < nCorr; ++iCorr) {
349 filter[filterOffset + iCorr] -=
tt.correction()[iCorr] *
tt.correctionEnabled()[iCorr];
353 std::vector<int> lutInput;
364 std::vector<int> decisionRange;
371 std::vector<int> cpLutOutput, jepLutOutput;
391 std::vector<int> peak;
393 std::vector<int> sat;
397 output.reserve(sat.size());
401 for ( ; itpeak!=peak.end() && itsat!=sat.end(); ++itpeak, ++itsat ) {
402 output.push_back( (*itpeak<<2) + (*itsat<<1) );
415 unsigned int strategy,
int satLow,
int satHigh,
int satLevel, std::vector<int> &
output)
418 std::vector<int> peak;
420 std::vector<int> sat;
421 satBcid(digits, satLow, satHigh, satLevel, sat);
424 output.reserve(sat.size());
428 for ( ; itpeak!=peak.end() && itsat!=sat.end(); ++itpeak, ++itsat ) {
429 output.push_back( (*itpeak<<2) + (*itsat<<1) );
441 void L1TriggerTowerTool::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,
442 unsigned int strategy,
int satLow,
int satHigh,
int satLevel, std::vector<int> &
result, std::vector<int> &decision)
445 std::vector<int> peak;
447 std::vector<int> sat;
448 satBcid(digits, satLow, satHigh, satLevel, sat);
451 result.reserve(sat.size());
456 for ( ; itpeak!=peak.end() && itsat!=sat.end(); ++itpeak, ++itsat ) {
457 result.push_back( (*itpeak<<2) + (*itsat<<1) );
466 std::vector<int> decisionRange;
467 if (!(decisionSource&0
x1))
etRange(digits, energyLow, energyHigh, decisionRange);
468 else etRange(lutInput, energyLow, energyHigh, decisionRange);
479 const std::vector<short int>* getFirCoefficients(
unsigned int coolId, std::any& C) {
480 auto settings = std::any_cast<T*>(C)->pprConditions(coolId);
481 if(!settings)
return nullptr;
482 return &(settings->firCoefficients());
492 std::vector<int> firCoeffs;
494 const std::vector<short int>* hwCoeffs;
503 firCoeffs.reserve(hwCoeffs->size());
504 for (
int i = hwCoeffs->size()-1;
i >= 0; --
i) firCoeffs.push_back((*hwCoeffs)[
i]);
525 output.reserve(digits.size());
530 for (
unsigned int i = 0;
i < firCoeffs.size(); ++
i) {
531 if (firstFIR < 0 && firCoeffs[
i] != 0) firstFIR =
i;
532 if (firCoeffs[
i] != 0) lastFIR =
i;
534 if (firstFIR < 0) firstFIR = lastFIR + 1;
536 for (
int i = 0;
i < (
int)digits.size();
i++) {
539 if (
i >= 2-firstFIR &&
i < (
int)digits.size()+2-lastFIR) {
540 for (
int j = firstFIR; j <= lastFIR; ++j) {
541 sum += digits[
i+j-2]*firCoeffs[j];
556 unsigned int getStrategy(std::any& C) {
557 return std::any_cast<T*>(C)->peakFinderCond();
570 }
else ATH_MSG_WARNING(
"::peakBcid: No Conditions Container retrieved" );
583 for (
unsigned int i = 0;
i <
fir.size();
i++) {
586 if (
i > 0 &&
i <
fir.size()-1) {
605 std::tuple<bool, int, int, int> getSaturation(
unsigned int coolId, std::any& C) {
606 auto settings = std::any_cast<T*>(C)->pprConditions(coolId);
607 if(!settings)
return std::make_tuple(
false, 0, 0, 0);
608 return std::make_tuple(
true, settings->satBcidLevel(), settings->satBcidThreshLow(),
609 settings->satBcidThreshHigh());
620 bool available =
false;
625 if(!available)
ATH_MSG_WARNING(
"::satBcid: No L1CaloPprConditions found" );
626 }
else ATH_MSG_WARNING(
"::satBcid: No Conditions Container retrieved" );
629 <<
" satLow: " << satLow
630 <<
" satHigh: " << satHigh );
640 output.reserve(digits.size());
645 for (
unsigned int i = 0;
i<digits.size();
i++) {
651 if (digits[
i]>=satLevel) {
652 if (enabled &&
i>1) {
653 bool low = (digits[
i-2]>satLow);
654 bool high = (digits[
i-1]>satHigh);
684 unsigned int getDecisionSource(std::any& C) {
685 return std::any_cast<T*>(C)->decisionSource();
691 int decisionSource = 0;
696 }
else ATH_MSG_WARNING(
"::bcidDecisionRange: No Conditions Container retrieved" );
701 ATH_MSG_VERBOSE(
"::bcidDecisionRange: decisionSource: " << decisionSource);
711 std::tuple<unsigned int, unsigned int, unsigned int> getBcidDecision(std::any& C) {
712 auto CC = std::any_cast<T*>(C);
713 return std::make_tuple(
CC->bcidDecision1(),
CC->bcidDecision2(),
CC->bcidDecision3());
718 unsigned int decision1 = 0;
719 unsigned int decision2 = 0;
720 unsigned int decision3 = 0;
723 std::tie(decision1, decision2, decision3) = getBcidDecision<L1CaloPprConditionsContainerRun2>(
m_conditionsContainer);
725 std::tie(decision1, decision2, decision3) = getBcidDecision<L1CaloPprConditionsContainer>(
m_conditionsContainer);
726 }
else ATH_MSG_WARNING(
"::bcidDecision: No Conditions Container retrieved" );
729 std::vector<unsigned int>
mask = { decision3, decision2, decision1 };
732 << decision3 <<
" " << decision2 <<
" " << decision1 << MSG::dec );
742 output.reserve(bcidResults.size());
744 std::vector<int>::const_iterator itBcid = bcidResults.begin();
745 std::vector<int>::const_iterator itRange =
range.begin();
746 int nRange =
mask.size();
748 for ( ; itBcid != bcidResults.end() && itRange !=
range.end(); ++itBcid, ++itRange) {
749 if ((*itRange) < nRange && (
mask[*itRange]&(0
x1<<*itBcid)))
output.push_back(1);
788 unsigned int noiseCut = 0;
790 if (noiseCut > 0)
cut = noiseCut;
801 double offsetReal = 0;
804 unsigned short scale = 0;
808 const std::vector<short int>* hwCoeffs;
829 for (
unsigned int i = 0;
i < hwCoeffs->size();
i++){
830 hwCoeffSum += hwCoeffs->at(
i);
834 offsetReal = pedMean * hwCoeffSum /
pow(2.,startBit);
837 offsetReal = pedMean * hwCoeffSum * slope /
pow(2.,startBit) - slope/2.;
839 offset =
static_cast<unsigned short>( offsetReal < 0. ? 0 : offsetReal + 0.5 );
841 ATH_MSG_VERBOSE(
"::cpLut: Offset: offset/strategy/pedMean/firCoeffSum/startBit/slope: "
842 <<
offset <<
" " <<
strategy <<
" " <<
" " << pedMean <<
" " << hwCoeffSum <<
" " << startBit <<
" " << slope );
845 }
else ATH_MSG_WARNING(
"::cpLut: No Conditions Container retrieved" );
850 unsigned int noiseCut = 0;
852 if (noiseCut > 0)
cut = noiseCut;
866 double offsetReal = 0;
869 unsigned short scale_db = 0;
870 unsigned short scale_menu = 0;
874 const std::vector<short int>* hwCoeffs;
898 scale_menu = l1Menu->thrExtraInfo().JET().jetScale();
908 for (
unsigned int i = 0;
i < hwCoeffs->size();
i++){
909 hwCoeffSum += hwCoeffs->at(
i);
913 offsetReal = pedMean * hwCoeffSum /
pow(2.,startBit);
916 offsetReal = pedMean * hwCoeffSum * slope /
pow(2.,startBit) - slope/2.;
918 offset =
static_cast<unsigned short>( offsetReal < 0. ? 0 : offsetReal + 0.5 );
920 ATH_MSG_VERBOSE(
"::jepLut: Offset: offset/strategy/pedMean/firCoeffSum/startBit/slope: "
921 <<
offset <<
" " <<
strategy <<
" " <<
" " << pedMean <<
" " << hwCoeffSum <<
" " << startBit <<
" " << slope );
924 }
else ATH_MSG_WARNING(
"::jepLut: No Conditions Container retrieved" );
929 unsigned int noiseCut = 0;
931 if (noiseCut > 0)
cut = noiseCut;
934 nonLinearLut(
fir, slope,
offset,
cut, scale_db, par1, par2, par3, par4,
disabled,
output);
951 std::vector<int>::const_iterator
it =
fir.begin();
952 for ( ;
it !=
fir.end(); ++
it) {
972 void L1TriggerTowerTool::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)
977 std::vector<int>::const_iterator
it =
fir.begin();
978 for ( ;
it !=
fir.end(); ++
it) {
982 double nll_slope = 0.001 *
scale;
983 double nll_offset = 0.001 * par1;
984 double nll_ampl = 0.001 * par2;
985 double nll_expo = 0.;
987 nll_expo = -1. / (4096 * 0.001*par3);
991 double nll_noise = 0.001 * par4;
1018 std::vector<int>::const_iterator itlut =
lut.begin();
1019 std::vector<int>::const_iterator itrange =
range.begin();
1020 while ( itlut !=
lut.end() && itrange !=
range.end() ) {
1022 else output.push_back(*itlut);
1036 std::tuple<bool, int, int> getBcidEnergyRange(
unsigned int coolId, std::any& C) {
1037 auto settings = std::any_cast<T*>(C)->pprConditions(coolId);
1038 if(!settings)
return std::make_tuple(
false, 0, 0);
1039 return std::make_tuple(
true, settings->bcidEnergyRangeLow(), settings->bcidEnergyRangeHigh());
1048 bool available =
false;
1054 if(!available)
ATH_MSG_WARNING(
"::etRange: No L1CaloPprConditions found");
1055 }
else ATH_MSG_WARNING(
"::etRange: No Conditions Container retrieved");
1058 <<
" energyHigh: " << energyHigh);
1069 for (std::vector<int>::const_iterator
it =
et.begin();
it !=
et.end(); ++
it) {
1070 if ((*
it) <= energyLow)
output.push_back(0);
1071 else if ((*
it) <= energyHigh)
output.push_back(1);
1072 else output.push_back(2);
1084 std::tuple<bool, int> getFirStartBit(
unsigned int coolId, std::any& C) {
1085 auto settings = std::any_cast<T*>(C)->pprConditions(coolId);
1086 if(!settings)
return std::make_tuple(
false, 0);
1087 return std::make_tuple(
true, settings->firStartBit());
1093 unsigned int start = 0;
1095 bool available =
false;
1100 if(!available)
ATH_MSG_WARNING(
"::dropBits: No L1CaloPprConditions found" );
1101 }
else ATH_MSG_WARNING(
"::dropBits: No Conditions Container retrieved" );
1121 for (std::vector<int>::const_iterator
it =
fir.begin();
it !=
fir.end(); ++
it) {
1138 const std::vector<short int>* hwCoeffs =
nullptr;
1148 firCoeffs.reserve(hwCoeffs->size());
1149 for (
int i = hwCoeffs->size()-1;
i >= 0; --
i) firCoeffs.push_back((*hwCoeffs)[
i]);
1151 }
else ATH_MSG_WARNING(
"::firParams: No L1CaloPprConditions found" );
1152 }
else ATH_MSG_WARNING(
"::firParams: No Conditions Container retrieved" );
1163 unsigned int &peakFinderStrategy,
int &satLow,
int &satHigh,
int &satLevel)
1168 decisionConditions.clear();
1169 peakFinderStrategy = 0;
1176 std::tuple<unsigned int, unsigned int, unsigned int>
bcidDecision;
1177 std::tuple<bool, int, int> bcidEnergyRange;
1178 std::tuple<bool, int, int, int> saturation;
1198 if(get<0>(bcidEnergyRange)) {
1199 std::tie(
std::ignore, energyLow, energyHigh) = bcidEnergyRange;
1202 if(get<0>(saturation)) {
1203 std::tie(
std::ignore, satLevel, satLow, satHigh) = saturation;
1205 }
else ATH_MSG_WARNING(
"::bcid:Params No Conditions Container retrieved" );
1208 <<
" satLow: " << satLow <<
" satHigh: " << satHigh <<
endmsg
1209 <<
" energyLow: " << energyLow <<
" energyHigh: " << energyHigh <<
endmsg
1210 <<
" decisionSource: " << decisionSource <<
" peakFinderStrategy: "
1211 << peakFinderStrategy );
1243 pedMean = settings->
pedMean();
1244 }
else ATH_MSG_WARNING(
"::lutParams: No L1CaloPprConditions found" );
1245 }
else ATH_MSG_WARNING(
"::lutParams: No Conditions Container retrieved" );
1247 ATH_MSG_VERBOSE(
"::lutParams: LUT startBit/strategy/offset/slope/cut/pedValue/pedMean: "
1248 << startBit <<
" " <<
strategy <<
" " <<
offset <<
" " << slope <<
" " <<
cut <<
" " << pedValue <<
" " << pedMean );
1249 unsigned int noiseCut = 0;
1251 if (noiseCut > 0)
cut = noiseCut;
1259 double offsetReal = 0;
1266 const std::vector<short int>* hwCoeffs;
1283 pedMean = settings->
pedMean();
1286 for (
unsigned int i = 0;
i < hwCoeffs->size();
i++){
1287 hwCoeffSum += hwCoeffs->at(
i);
1291 offsetReal = pedMean * hwCoeffSum /
pow(2.,startBit);
1294 offsetReal = pedMean * hwCoeffSum * slope /
pow(2.,startBit) - slope/2.;
1296 offset =
static_cast<unsigned short>( offsetReal < 0. ? 0 : offsetReal + 0.5 );
1298 ATH_MSG_VERBOSE(
"::jepLutParams: Offset: offset/strategy/pedMean/firCoeffSum/startBit/slope: "
1299 <<
offset <<
" " <<
strategy <<
" " <<
" " << pedMean <<
" " << hwCoeffSum <<
" " << startBit <<
" " << slope );
1301 }
else ATH_MSG_WARNING(
"::cpLutParams: No L1CaloPprConditions found" );
1302 }
else ATH_MSG_WARNING(
"::cpLutParams: No Conditions Container retrieved" );
1304 ATH_MSG_VERBOSE(
"::cpLutParams: LUT startBit/strategy/offset/slope/cut/pedValue/pedMean: "
1305 << startBit <<
" " <<
strategy <<
" " <<
offset <<
" " << slope <<
" " <<
cut <<
" " << pedValue <<
" " << pedMean );
1306 unsigned int noiseCut = 0;
1308 if (noiseCut > 0)
cut = noiseCut;
1316 double offsetReal = 0;
1323 const std::vector<short int>* hwCoeffs;
1340 pedMean = settings->
pedMean();
1344 for (
unsigned int i = 0;
i < hwCoeffs->size();
i++){
1345 hwCoeffSum += hwCoeffs->at(
i);
1349 offsetReal = pedMean * hwCoeffSum /
pow(2.,startBit);
1352 offsetReal = pedMean * hwCoeffSum * slope /
pow(2.,startBit) - slope/2.;
1354 offset =
static_cast<unsigned short>( offsetReal < 0. ? 0 : offsetReal + 0.5 );
1356 ATH_MSG_VERBOSE(
"::jepLutParams: Offset: offset/strategy/pedMean/firCoeffSum/startBit/slope: "
1357 <<
offset <<
" " <<
strategy <<
" " <<
" " << pedMean <<
" " << hwCoeffSum <<
" " << startBit <<
" " << slope );
1359 }
else ATH_MSG_WARNING(
"::jepLutParams: No L1CaloPprConditions found" );
1360 }
else ATH_MSG_WARNING(
"::jepLutParams: No Conditions Container retrieved" );
1362 ATH_MSG_VERBOSE(
"::jepLutParams: LUT startBit/strategy/offset/slope/cut/pedValue/pedMean: "
1363 << startBit <<
" " <<
strategy <<
" " <<
offset <<
" " << slope <<
" " <<
cut <<
" " << pedValue <<
" " << pedMean );
1364 unsigned int noiseCut = 0;
1366 if (noiseCut > 0)
cut = noiseCut;
1391 try { hwId =
m_ttSvc->createTTChannelID(
id,
false); }
1410 const double absEta = fabs(eta);
1416 }
else if (
absEta < 3.2) {
1425 if (eta < 0.)
index += 1792;
1448 coolId =
m_ttSvc->createL1CoolChannelId(hwId);
1458 std::tuple<bool, bool, bool> getSatOverride(std::any& C) {
1459 auto CC = std::any_cast<T*>(C);
1460 return std::make_tuple(
CC->satOverride1(),
CC->satOverride2(),
CC->satOverride3());
1466 bool override =
false;
1477 }
else ATH_MSG_WARNING(
"::satOverride: No Conditions Container retrieved" );
1480 <<
" has saturation override flag " <<
override );
1489 unsigned int noiseCut = 0;
1496 bool isDisabled =
false;
1511 }
else isDisabled =
true;
1514 <<
"::disabledChannel: calibErrorCode: " << (disabledChan->
calibErrorCode()).errorCode()
1515 <<
" deadErrorCode: " << (disabledChan->
deadErrorCode()).errorCode()
1516 <<
" noiseCut: " << disabledChan->
noiseCut()
1521 ATH_MSG_VERBOSE(
"::disabledChannel: No L1CaloPprDisabledChannel found" );
1524 ATH_MSG_WARNING(
"::disabledChannel: No DisabledChannel Container retrieved" );
1544 float abseta = fabs(eta);
1545 if (abseta<3.2)
return eta;
1547 int sign = ((eta > 0) ? 1 : -1);
1548 if (abseta < 3.6) eta = 3.15 *
sign;
1549 else if (abseta < 4.0) eta = 3.33 *
sign;
1550 else if (abseta < 4.5) eta = 3.72 *
sign;
1551 else eta = 4.41 *
sign;
1554 if (abseta < 3.6) eta = 3.36;
1555 else if (abseta < 4.0) eta = 3.45;
1556 else if (abseta < 4.5) eta = 4.17;
1560 if (abseta < 3.6) eta = -3.45;
1561 else if (abseta < 4.0) eta = -3.36;
1562 else if (abseta < 4.5) eta = -4.19;
1575 throw GaudiException(
"No mapping tool configured",
1576 "L1TriggerTowerTool::FCalTTeta", StatusCode::FAILURE);
1582 unsigned int mcm =
channelId.subModule();
1599 template <
typename T>
1628 return StatusCode::SUCCESS;
1648 <<
"::refValues: errorCode: " << (ftref->
errorCode()).errorCode()
1649 << MSG::dec <<
" reference: " << ftref->
refValue() <<
" calib: " << ftref->
calibValue() );
1662 unsigned nFIR = firInOut.size();
1663 correctionOut.assign(nFIR, 0
u);
1668 for(
unsigned i = 0;
i != nFIR; ++
i) {
1670 firInOut[
i] -= correctionOut[
i];
1672 if(firInOut[
i] < 0) firInOut[
i] = 0;
1684 const EventContext& ctx = Gaudi::Hive::currentContext();
1685 if (ctx.eventID().run_number() >= 253377)
return true;