ATLAS Offline Software
Loading...
Searching...
No Matches
Tool_FeatureExtractor.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
10
11
13 return u->getBDTValue() > v->getBDTValue();
14}
15
16
18 return u->p4().Et() > v->p4().Et();
19}
20
21
23 asg::AsgTool(name)
24{}
25
26
28
29 ATH_MSG_INFO(" initialize()");
30 m_init=true;
31
34
35 ATH_CHECK( m_Tool_InformationStore->getInfo_String("FeatureExtractor_VarTypeName_varTypeName_Ratio", m_varTypeName_Ratio) );
36 ATH_CHECK( m_Tool_InformationStore->getInfo_String("FeatureExtractor_VarTypeName_varTypeName_StdDev", m_varTypeName_StdDev) );
37 ATH_CHECK( m_Tool_InformationStore->getInfo_String("FeatureExtractor_VarTypeName_varTypeName_HLV", m_varTypeName_HLV) );
38 ATH_CHECK( m_Tool_InformationStore->getInfo_String("FeatureExtractor_VarTypeName_varTypeName_JetMoment", m_varTypeName_JetMoment) );
39 ATH_CHECK( m_Tool_InformationStore->getInfo_String("FeatureExtractor_VarTypeName_varTypeName_Combined", m_varTypeName_Combined) );
40 ATH_CHECK( m_Tool_InformationStore->getInfo_String("FeatureExtractor_VarTypeName_varTypeName_Basic", m_varTypeName_Basic) );
41 ATH_CHECK( m_Tool_InformationStore->getInfo_String("FeatureExtractor_VarTypeName_varTypeName_PID", m_varTypeName_PID) );
42
43 ATH_CHECK( m_Tool_InformationStore->getInfo_Int("FeatureExtractor_UseEmptySeeds", m_Config_UseEmptySeeds) );
44
45 ATH_CHECK( m_Tool_InformationStore->getInfo_VecDouble("Common_BinEdges_Eta", m_Config_BinEdges_Eta) );
46 ATH_CHECK( m_Tool_InformationStore->getInfo_VecDouble("CellBased_EtaBinned_Pi0MVACut_1prong", m_Config_CellBased_EtaBinned_Pi0MVACut_1prong) );
47 ATH_CHECK( m_Tool_InformationStore->getInfo_VecDouble("CellBased_EtaBinned_Pi0MVACut_3prong", m_Config_CellBased_EtaBinned_Pi0MVACut_3prong) );
48
49 return StatusCode::SUCCESS;
50}
51
52
53void PanTau::Tool_FeatureExtractor::fillVariantsSeedEt(const std::vector<PanTau::TauConstituent*>& tauConstituents,
54 std::map<std::string, double>& variants_SeedEt) {
55
56 //use different approaches to calculate total energy of seed:
57 variants_SeedEt["EtAllConsts"] = 0.0;
58
59 //loop over all constituents in seed
60 for (unsigned int iConst = 0; iConst < tauConstituents.size(); iConst++) {
61
62 //get current constituents
63 PanTau::TauConstituent* curConstituent = tauConstituents.at(iConst);
64 double curEt = curConstituent->p4().Et();
65
66 //update the different Et definitions
67 if (curConstituent->isOfType(PanTau::TauConstituent::t_Charged) ||
69 variants_SeedEt["EtAllConsts"] += curEt;
70 }
71 }//end loop over constituents in seed
72}
73
74
76 const std::string& featName,
77 double numerator,
78 const std::map<std::string, double>& denominatorMap) {
79 std::map<std::string, double>::const_iterator it = denominatorMap.begin();
80 for(; it!=denominatorMap.end(); ++it) {
81 std::string FullName = featName + it->first;
82 float value = static_cast<float>(it->second);
83 if(value <= 0. || std::isnan(value) || std::isinf(value) ) continue;
84 targetMap->addFeature(FullName, numerator / it->second);
85 }
86}
87
88
90
93 bool noValidInputTau = inSeed->isOfTechnicalQuality(PanTau::PanTauSeed::t_NoValidInputTau);
94 bool isBadSeed = (noAnyConstituents || noSelConstituents || noValidInputTau);
95 if(static_cast<bool>(m_Config_UseEmptySeeds)) isBadSeed = noValidInputTau;
96
97 if (isBadSeed) {
98 ATH_MSG_DEBUG("Seed is not valid for feature extraction (no constituents or no valid input tau) - just fill isPanTauCandidate feature");
99 inSeed->getFeatures()->addFeature("CellBased_" + m_varTypeName_Basic + "_isPanTauCandidate", 0);
100 return StatusCode::SUCCESS;
101 }
102 inSeed->getFeatures()->addFeature("CellBased_" + m_varTypeName_Basic + "_isPanTauCandidate", 1);
103
104
106
108
109 // map containing different methods to calc seed et
110 std::map<std::string, double> variants_SeedEt;
111 // calculate the Et variants for the seed
112 fillVariantsSeedEt(inSeed->getConstituentsAsList_All(), variants_SeedEt);
113
114 //loop through all types of Constituents in tau and calculate type features for them
115 //baseline
116 ATH_CHECK( calculateFeatures(inSeed, PanTau::TauConstituent::t_NoType, variants_SeedEt) ); //=> all constituents
117 ATH_CHECK( calculateFeatures(inSeed, PanTau::TauConstituent::t_Charged, variants_SeedEt) ); //=> charged ones in core
118 ATH_CHECK( calculateFeatures(inSeed, PanTau::TauConstituent::t_Neutral, variants_SeedEt) ); //=> neutral ones in core
119 ATH_CHECK( calculateFeatures(inSeed, PanTau::TauConstituent::t_Pi0Neut, variants_SeedEt) ); //=> pi0 tagged ones in core
120
121 //fill the combined features
123
124 return StatusCode::SUCCESS;
125}
126
127
129
130 PanTau::TauFeature* featureMap = inSeed->getFeatures();
131
132 std::string featurePrefix = m_varTypeName_Basic;
133 std::string name = "CellBased_" + featurePrefix;
134
138 for(int iType=0; iType<PanTau::TauConstituent::t_nTypes; iType++) {
139
140 bool foundIt = false;
141 std::vector<TauConstituent*> curList = inSeed->getConstituentsOfType(iType, foundIt);
142 if(!foundIt) continue;
143
144 //store multiplicity of current type
146 unsigned int nConstituents = curList.size();
147 featureMap->addFeature(name + "_N" + typeName + "Consts", nConstituents);
148 }
149
150 return StatusCode::SUCCESS;
151}
152
153
155 TauFeature* tauFeatureMap = inSeed->getFeatures();
156 std::string prefixVARType = m_varTypeName_HLV;
157 for(int iType=0; iType<static_cast<int>(PanTau::TauConstituent::t_nTypes); iType++) {
158 bool isOK;
159 std::vector<PanTau::TauConstituent*> list_TypeConstituents = inSeed->getConstituentsOfType(iType, isOK); // list of constituents of current type
160 TLorentzVector tlv_TypeConstituents = inSeed->getSubsystemHLV(iType, isOK); // summed hlv of objects of current type
162 std::string name = "CellBased_" + curTypeName + "_" + prefixVARType;
163
164 if(!list_TypeConstituents.empty()) {
165 tauFeatureMap->addFeature(name + "_SumM", tlv_TypeConstituents.M());
166 }
167
168 } //end loop over constituent types
169
170 return StatusCode::SUCCESS;
171}
172
173
175 int tauConstituentType,
176 const std::map<std::string, double>& variants_SeedEt) const {
177
178 std::string curTypeName = PanTau::TauConstituent::getTypeName( (PanTau::TauConstituent::Type)tauConstituentType );
179 std::string curTypeName_All = PanTau::TauConstituent::AllConstituentsName();
180 PanTau::TauFeature* tauFeatureMap = inSeed->getFeatures();
181 TLorentzVector tlv_Reference = inSeed->getProtoMomentumCore();
182
183 std::vector<PanTau::TauConstituent*> list_AllConstituents = inSeed->getConstituentsAsList_Core();
184
185 bool foundIt = false;
186 std::vector<PanTau::TauConstituent*> list_TypeConstituents;
187 if (tauConstituentType != PanTau::TauConstituent::t_NoType) list_TypeConstituents = inSeed->getConstituentsOfType(tauConstituentType, foundIt);
188 if (tauConstituentType == PanTau::TauConstituent::t_NoType) list_TypeConstituents = list_AllConstituents;
189 if (!foundIt) return StatusCode::SUCCESS;
190
191 unsigned int n_Constituents_Type = list_TypeConstituents.size();
192
193 //sort the lists by Et
194 std::sort(list_AllConstituents.begin(), list_AllConstituents.end(), sortTauConstituentEt);
195 std::sort(list_TypeConstituents.begin(), list_TypeConstituents.end(), sortTauConstituentEt);
196
197 //make an additional list of constituents, but now ordered by BDT value
198 std::vector<PanTau::TauConstituent*> list_TypeConstituents_SortBDT = list_TypeConstituents;
199 std::sort(list_TypeConstituents_SortBDT.begin(), list_TypeConstituents_SortBDT.end(), sortTauConstituentMVA);
200
201 TLorentzVector tlv_1st_BDT;
202
203 if(!list_TypeConstituents_SortBDT.empty()) tlv_1st_BDT = list_TypeConstituents_SortBDT[0]->p4();
204
208
209 // ===> Sum of Et, Et^2, E and E^2
210 double sum_Et = 0;
211 double sum_Et2 = 0;
212 // ===> Sum of Et (and E) times DeltaR, DeltaR', Angle
213 double sum_EtxDR = 0;
214 // ===> Multiplicities
215 unsigned int num_EFOs = 0;
216
220 for(unsigned int iTypeConst=0; iTypeConst<list_TypeConstituents.size(); iTypeConst++) {
221
222 //get hep lorentz vector
223 TLorentzVector tlv_curConst = list_TypeConstituents.at(iTypeConst)->p4();
224
225 //final check (nan & inf)
226 if (std::isnan(tlv_curConst.Pt()) || std::isinf(tlv_curConst.Pt())) continue;
227
228 //ready to calc stuff
229 //basically update each of the prepared sum_* and num_* variables above,
230 // the sum HLV and the pointers to 1st, 2nd, 3rd leading constituents of current type
231
232 //helpers to reduce function calls
233 double hlp_Et = tlv_curConst.Et();
234 double hlp_Et2 = hlp_Et * hlp_Et;
235 double hlp_DeltaR = tlv_Reference.DeltaR(tlv_curConst);
236
237 // update Sum of Et, Et^2, E and E^2
238 sum_Et += hlp_Et;
239 sum_Et2 += hlp_Et2;
240 // update Sum of Et (and E) times DeltaR, DeltaR', Angle
241 sum_EtxDR += hlp_Et * hlp_DeltaR;
242 // update Multiplicities
243 num_EFOs++;
244 }//end loop over selected EFOs
245
246
247
251
252 // naming string
253 std::string prefixVARType = "";
254
256 //if there are no EFOs of this type in the seed, all other variables cannot be calculated to have any reasonable value
257 if( num_EFOs == 0 ) {
258 return StatusCode::SUCCESS;
259 }
260
262 prefixVARType = m_varTypeName_PID;
263
264 // Sorted by highest BDT score
265 for(unsigned int iTypeConst=0; iTypeConst<n_Constituents_Type; iTypeConst++) {
266
267 double value_BDT = list_TypeConstituents_SortBDT[iTypeConst]->getBDTValue();
268 if( std::isnan(value_BDT) || std::isinf(value_BDT) ) continue;
269
270 //correct BDT value based on BDT cut
271 if(tauConstituentType != PanTau::TauConstituent::t_Charged) {
272 double mvaCorrection = 0.0;
273 double etaCurConst = list_TypeConstituents_SortBDT[iTypeConst]->p4().Eta();
274 int etaBinIndex = m_HelperFunctions.getBinIndex(m_Config_BinEdges_Eta, std::abs(etaCurConst));
275 int numTrack = inSeed->getTauJet()->nTracks();
276 if(numTrack == 1) { mvaCorrection = m_Config_CellBased_EtaBinned_Pi0MVACut_1prong.at(etaBinIndex); }
277 else { mvaCorrection = m_Config_CellBased_EtaBinned_Pi0MVACut_3prong.at(etaBinIndex); }
278
279 value_BDT = value_BDT - mvaCorrection;
280 }
281
282 std::string iConst = m_HelperFunctions.convertNumberToString(static_cast<double>(iTypeConst+1));
283 tauFeatureMap->addFeature("CellBased_" + curTypeName + "_" + prefixVARType + "_BDTValues_BDTSort_" + iConst, value_BDT);
284
285 }
286
289 //only execute if the constituent type is neutral
290 if(PanTau::TauConstituent::isNeutralType(tauConstituentType)) {
291
292 unsigned int totalPhotonsInSeed = 0;
293
294 for(unsigned int iConst=0; iConst<list_TypeConstituents_SortBDT.size(); iConst++) {
295
296 PanTau::TauConstituent* curConst = list_TypeConstituents_SortBDT.at(iConst);
297 std::vector<PanTau::TauConstituent*> shotConstituents = curConst->getShots();
298 unsigned int nShots = shotConstituents.size();
299
300 unsigned int totalPhotonsInNeutral = 0;
301
302 for(unsigned int iShot=0; iShot<nShots; iShot++) {
303 PanTau::TauConstituent* curShot = shotConstituents.at(iShot);
304 totalPhotonsInNeutral += curShot->getNPhotonsInShot();
305 }//end loop over shots
306 totalPhotonsInSeed += totalPhotonsInNeutral;
307
308 }//end loop over constituents in tau
309
310 //number of shots in seed
311 tauFeatureMap->addFeature("CellBased_" + curTypeName + "_" + prefixVARType + "_NPhotonsInSeed", totalPhotonsInSeed);
312
313 }//end if check for shot info dumping
314
315
317 prefixVARType = m_varTypeName_Ratio;
318
319 if(curTypeName != curTypeName_All) addFeatureWrtSeedEnergy(tauFeatureMap, "CellBased_" + curTypeName + "_" + prefixVARType + "_EtOver", sum_Et, variants_SeedEt);
320
321 if(tlv_1st_BDT.Pt() != 0) addFeatureWrtSeedEnergy(tauFeatureMap, "CellBased_" + curTypeName + "_" + prefixVARType + "_1stBDTEtOver", tlv_1st_BDT.Et(), variants_SeedEt);
322
324 prefixVARType = m_varTypeName_StdDev;
325
326 double stddev_Et = m_HelperFunctions.stddev(sum_Et2, sum_Et, num_EFOs);
327
328 if(stddev_Et > 0.) addFeatureWrtSeedEnergy(tauFeatureMap, "CellBased_" + curTypeName + "_" + prefixVARType + "_Et_Wrt", stddev_Et, variants_SeedEt);
329
331 // Moment wrt X = Sum( Et * X ) / Sum(Et)
332 // ==> Named as Eflow_EFOType_JetMoment_EtX" when using Et as weight
333 // ==> Named as Eflow_EFOType_JetMoment_EX" when using E as weight
334 prefixVARType = m_varTypeName_JetMoment;
335
336 if(sum_Et > 0.) {
337 //Using transverse energy
338 tauFeatureMap->addFeature("CellBased_" + curTypeName + "_" + prefixVARType + "_EtDRxTotalEt", (sum_EtxDR / sum_Et) * variants_SeedEt.at("EtAllConsts"));
339 }
340
341 return StatusCode::SUCCESS;
342}
343
344
346
347 // Prepare some short notations for variables
348 PanTau::TauFeature* tauFeatures = inSeed->getFeatures();
349
350 // Prepare the list of names for EFO Types &
351 // the 4 momenta of the different sub systems
352 // (ie. charged, neutral subsystem, etc...)
353 TLorentzVector tlv_System[PanTau::TauConstituent::t_nTypes];
354 TLorentzVector tlv_1stEFO[PanTau::TauConstituent::t_nTypes];
355
356 // get input objects to calc combined features
357 bool tlv_Sys_OK[PanTau::TauConstituent::t_nTypes];
358 bool tlv_1st_OK[PanTau::TauConstituent::t_nTypes];
359
360 //initialize arrays with default values
361 for(unsigned int iType=0; iType<(unsigned int)PanTau::TauConstituent::t_nTypes; iType++) {
362 tlv_System[iType] = TLorentzVector();
363 tlv_1stEFO[iType] = TLorentzVector();
364 tlv_Sys_OK[iType] = false;
365 tlv_1st_OK[iType] = false;
366 }
367
368 for(int iType=0; iType<static_cast<int>(PanTau::TauConstituent::t_nTypes); iType++) {
369
370 tlv_System[iType] = inSeed->getSubsystemHLV(iType, tlv_Sys_OK[iType]);
371 if (!tlv_Sys_OK[iType]) continue;
372
373 std::vector<TauConstituent*> typeConstituents = inSeed->getConstituentsOfType(iType, tlv_Sys_OK[iType]);
374 if (typeConstituents.empty()) tlv_Sys_OK[iType] = false;
375 if (!tlv_Sys_OK[iType]) continue;
376
377 if (!typeConstituents.empty()) {
378 tlv_1stEFO[iType] = typeConstituents.at(0)->p4();
379 tlv_1st_OK[iType] = true;
380 } else {
381 tlv_1st_OK[iType] = false;
382 }
383 }
384
385 // From the extracted input, calc combined features
386 std::string prefixVARType = m_varTypeName_Combined;
387
388 // Combined Type-vs-Type Features
389 // Loop over all EFO types...
390 // for every type, loop over all other types (but neglect permutations, i.e. A,B is the same as B,A)
391 // calculate ratios, angles etc...
392
393 for(int iType=0; iType<PanTau::TauConstituent::t_nTypes; iType++) {
394
395 if(iType == static_cast<int>(PanTau::TauConstituent::t_NoType)) continue;
396 int type_Denom = iType;
397
398 for(int jType=0; jType<PanTau::TauConstituent::t_nTypes; jType++) {
399
400 if(jType == static_cast<int>(PanTau::TauConstituent::t_NoType)) continue;
401 int type_Nom = jType;
402
403 if(jType == iType) continue;
404
405 std::string typeName_Nom = PanTau::TauConstituent::getTypeName((PanTau::TauConstituent::Type)type_Nom);
406 std::string typeName_Denom = PanTau::TauConstituent::getTypeName((PanTau::TauConstituent::Type)type_Denom);
407
408 double sum_Et_Nom = 0.0;
409 double sum_Et_Denom = 0.0;
410 if(tlv_Sys_OK[type_Nom] && tlv_Sys_OK[type_Denom]) {
411 sum_Et_Nom = tlv_System[type_Nom].Et();
412 sum_Et_Denom = tlv_System[type_Denom].Et();
413 }
414
415 //following features symmetric in system A-B, hence skip multi calculation
416 if(jType <= iType) continue;
417
418 //Energy ratios between systems
419 if(sum_Et_Denom > 0. && sum_Et_Nom > 0.) {
420 tauFeatures->addFeature("CellBased_" + prefixVARType + "_Log" + typeName_Nom + "EtOver" + typeName_Denom + "Et", std::log10(sum_Et_Nom / sum_Et_Denom) );
421 }//end check for div by zero
422
423 if(tlv_1st_OK[type_Nom] && tlv_1st_OK[type_Denom]) {
424 //Delta R between 1st and 1st EFO
425 tauFeatures->addFeature("CellBased_" + prefixVARType + "_DeltaR1st" + typeName_Nom + "To1st" + typeName_Denom, tlv_1stEFO[type_Nom].DeltaR( tlv_1stEFO[type_Denom] ) );
426
427 } //end check for valid leading efo
428
429 }//end loop over system B
430 }//end loop over system A
431
432 return StatusCode::SUCCESS;
433}
434
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_INFO(x)
#define ATH_MSG_DEBUG(x)
bool sortTauConstituentMVA(const PanTau::TauConstituent *u, const PanTau::TauConstituent *v)
bool sortTauConstituentEt(const PanTau::TauConstituent *u, const PanTau::TauConstituent *v)
static StatusCode bindToolHandle(ToolHandle< T > &, std::string)
std::vector< PanTau::TauConstituent * > getConstituentsOfType(int tauConstituent_Type, bool &foundit)
TLorentzVector getProtoMomentumCore() const
Definition PanTauSeed.h:219
const std::vector< PanTau::TauConstituent * > & getConstituentsAsList_Core() const
Definition PanTauSeed.h:221
bool isOfTechnicalQuality(int pantauSeed_TechnicalQuality) const
const xAOD::TauJet * getTauJet() const
Definition PanTauSeed.h:215
const std::vector< PanTau::TauConstituent * > & getConstituentsAsList_All() const
Definition PanTauSeed.h:222
TLorentzVector getSubsystemHLV(int tauConstituent_Type, bool &foundit)
const PanTau::TauFeature * getFeatures() const
Definition PanTauSeed.h:217
Input object for PanTau.
std::vector< std::string > getTypeName() const
const std::vector< TauConstituent * > & getShots() const
static bool isNeutralType(int tauConstituentType)
virtual FourMom_t p4() const
The full 4-momentum of the particle as a TLorentzVector.
static std::string AllConstituentsName()
bool isOfType(TauConstituent::Type aType) const
Type
Type enumeration to specify type of this tau constituent.
Class containing features of a tau seed.
Definition TauFeature.h:19
bool addFeature(const std::string &name, const double value)
adds a new feature
std::vector< double > m_Config_BinEdges_Eta
Helper members.
static const std::string varTypeName_Shots()
Gaudi::Property< std::string > m_Tool_InformationStoreName
Tool_FeatureExtractor(const std::string &name)
StatusCode addConstituentMomenta(PanTau::PanTauSeed *inSeed) const
ToolHandle< PanTau::ITool_InformationStore > m_Tool_InformationStore
StatusCode calculateFeatures(PanTau::PanTauSeed *inSeed, int tauConstituentType, const std::map< std::string, double > &variants_SeedEt) const
std::vector< double > m_Config_CellBased_EtaBinned_Pi0MVACut_3prong
std::vector< double > m_Config_CellBased_EtaBinned_Pi0MVACut_1prong
static void addFeatureWrtSeedEnergy(PanTau::TauFeature *targetMap, const std::string &featName, double numerator, const std::map< std::string, double > &denominatorMap)
StatusCode addCombinedFeatures(PanTau::PanTauSeed *inSeed) const
virtual StatusCode initialize() override
Dummy implementation of the initialisation function.
PanTau::HelperFunctions m_HelperFunctions
virtual StatusCode execute(PanTau::PanTauSeed *inSeed) const override
StatusCode calculateBasicFeatures(PanTau::PanTauSeed *inSeed) const
static void fillVariantsSeedEt(const std::vector< PanTau::TauConstituent * > &tauConstituents, std::map< std::string, double > &variants_SeedEt)
AsgTool(const std::string &name)
Constructor specifying the tool instance's name.
Definition AsgTool.cxx:58
size_t nTracks(TauJetParameters::TauTrackFlag flag=TauJetParameters::TauTrackFlag::classifiedCharged) const
void sort(typename DataModel_detail::iterator< DVL > beg, typename DataModel_detail::iterator< DVL > end)
Specialization of sort for DataVector/List.