Skip to content

Commit 42b2f89

Browse files
INFNuserINFNuser
authored andcommitted
Inclusion of AE model inference
1 parent 554ef0a commit 42b2f89

3 files changed

Lines changed: 104 additions & 94 deletions

File tree

PWGHF/Core/HfAeToMseXicToPKPi.h

Lines changed: 79 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -16,102 +16,111 @@
1616
#ifndef PWGHF_CORE_HFAETOMSEXICTOPKPI_H_
1717
#define PWGHF_CORE_HFAETOMSEXICTOPKPI_H_
1818

19-
#include <vector>
20-
2119
#include "PWGHF/Core/HfMlResponse.h"
20+
21+
#include <vector>
2222
namespace o2::analysis
2323
{
24-
template <typename TypeOutputScore = float>
25-
class HfAeToMseXicToPKPi : public HfMlResponse<TypeOutputScore>
26-
{
27-
public:
28-
/// Default constructor
29-
HfAeToMseXicToPKPi() = default;
30-
/// Default destructor
31-
virtual ~HfAeToMseXicToPKPi() = default;
24+
template <typename TypeOutputScore = float>
25+
class HfAeToMseXicToPKPi : public HfMlResponse<TypeOutputScore>
26+
{
27+
public:
28+
/// Default constructor
29+
HfAeToMseXicToPKPi() = default;
30+
/// Default destructor
31+
virtual ~HfAeToMseXicToPKPi() = default;
3232

33-
std::vector<float> yScaled, yOutRescaled;
34-
//private :
35-
void setMinMaxScaling(std::vector<float>& yOut, std::vector<float> yIn, std::vector<float> scaleMin, std::vector<float> scaleMax)
36-
{ yOut.clear(); // initial clear to avoid multiple filling if setMinMax o setScaling are called more than once
37-
for (size_t j = 0; j < yIn.size(); ++j)
38-
{ // loop for over the features
39-
// MinMax scaling of the input features
40-
LOG(debug)<<"--------------> MinMax scaling Debug \t"<<scaleMin.at(j)<<"\t"<<scaleMax.at(j);
41-
yOut.push_back((yIn.at(j) - scaleMin.at(j))/(scaleMax.at(j)- scaleMin.at(j)));
42-
LOG(debug)<<"Feature = "<<j<<" ----> input = "<<yIn.at(j)<<" scaled feature = "<< yOut.at(j);
43-
}
44-
}
45-
// ---- External preprocessing scaling
46-
void setScaling(bool scaleFlag, int scaleType, /* input features of a candidate */ std::vector<float> yIn, std::vector<float> scaleMin, std::vector<float> scaleMax)
47-
{ // it takes the bool flag and scaling parameters configurables in taskXic
48-
yScaled.clear();
49-
if (scaleFlag == false) {
50-
LOG(debug)<<"No external preprocessing transformation will be applied";
51-
yScaled.assign(yIn.begin(), yIn.end());
52-
} else {
53-
if (scaleType == 1) {
54-
LOG(debug)<<"MinMax scaling will be applied";
55-
setMinMaxScaling(yScaled, yIn, scaleMin, scaleMax);
56-
} // ... with scaleType > 1 we could add other preprocessing trasformations
57-
}
58-
}
59-
std::vector<float> getPreprocessedFeatures(){
60-
for (size_t j = 0; j < yScaled.size(); ++j) LOG(debug)<<"Global scaled feature = "<< yScaled.at(j);
61-
return yScaled;
62-
}
63-
// Reverse preprocessing - output postprocessing
64-
void unsetMinMaxScaling(std::vector<float>& yOut, std::vector<float> yIn, std::vector<float> scaleMin, std::vector<float> scaleMax)
65-
{ yOut.clear(); // initial clear to avoid multiple filling if setMinMax o setScaling are called more than once
66-
for (size_t j = 0; j < yIn.size(); ++j)
67-
{ // loop for over the features
33+
std::vector<float> yScaled, yOutRescaled;
34+
//private :
35+
void setMinMaxScaling(std::vector<float>& yOut, std::vector<float> yIn, std::vector<float> scaleMin, std::vector<float> scaleMax)
36+
{
37+
yOut.clear(); // initial clear to avoid multiple filling if setMinMax o setScaling are called more than once
38+
for (size_t j = 0; j < yIn.size(); ++j){ // loop for over the features
39+
// MinMax scaling of the input features
40+
LOG(debug) << "--------------> MinMax scaling Debug \t" << scaleMin.at(j) << "\t" << scaleMax.at(j);
41+
yOut.push_back((yIn.at(j) - scaleMin.at(j))/(scaleMax.at(j)- scaleMin.at(j)));
42+
LOG(debug) << "Feature = " << j << " ----> input = " << yIn.at(j) << " scaled feature = " << yOut.at(j);
43+
}
44+
}
45+
// ---- External preprocessing scaling
46+
void setScaling(bool scaleFlag, int scaleType, /* input features of a candidate */ std::vector<float> yIn, std::vector<float> scaleMin, std::vector<float> scaleMax)
47+
{ // it takes the bool flag and scaling parameters configurables in taskXic
48+
yScaled.clear();
49+
if (scaleFlag == false) {
50+
LOG(debug) << "No external preprocessing transformation will be applied";
51+
yScaled.assign(yIn.begin(), yIn.end());
52+
} else {
53+
if (scaleType == 1) {
54+
LOG(debug) << "MinMax scaling will be applied";
55+
setMinMaxScaling(yScaled, yIn, scaleMin, scaleMax);
56+
} // ... with scaleType > 1 we could add other preprocessing trasformations
57+
}
58+
}
59+
std::vector<float> getPreprocessedFeatures()
60+
{
61+
for (size_t j = 0; j < yScaled.size(); ++j)
62+
LOG(debug) << "Global scaled feature = " << yScaled.at(j);
63+
return yScaled;
64+
}
65+
// Reverse preprocessing - output postprocessing
66+
void unsetMinMaxScaling(std::vector<float>& yOut, std::vector<float> yIn, std::vector<float> scaleMin, std::vector<float> scaleMax)
67+
{
68+
yOut.clear(); // initial clear to avoid multiple filling if setMinMax o setScaling are called more than once
69+
for (size_t j = 0; j < yIn.size(); ++j){ // loop for over the features
6870
// MinMax scaling of the input features
69-
LOG(debug)<<"--------------> MinMax unscaling Debug \t"<<scaleMin.at(j)<<"\t"<<scaleMax.at(j);
71+
LOG(debug) << "--------------> MinMax unscaling Debug \t" << scaleMin.at(j) << "\t" << scaleMax.at(j);
7072
yOut.push_back(yIn.at(j)*(scaleMax.at(j)- scaleMin.at(j))+ scaleMin.at(j));
71-
LOG(debug)<<"Unscaling output = "<<j<<" ----> input = "<<yIn.at(j)<<" rescaled output = "<< yOut.at(j);
73+
LOG(debug) << "Unscaling output = " << j << " ----> input = " << yIn.at(j) << " rescaled output = " << yOut.at(j);
7274
}
7375
}
7476

7577
void unsetScaling(bool scaleFlag, int scaleType, /*AE output*/ std::vector<float> yIn, std::vector<float> scaleMin, std::vector<float> scaleMax)
7678
{ // it takes the bool flag and scaling parameters configurables in taskXic
7779
yOutRescaled.clear();
7880
if (scaleFlag == false) {
79-
LOG(debug)<<"No external preprocessing transformation will be applied";
81+
LOG(debug) << "No external preprocessing transformation will be applied";
8082
yOutRescaled.assign(yIn.begin(), yIn.end());
8183
} else {
82-
if (scaleType == 1) {
83-
LOG(debug)<<"MinMax unscaling will be applied";
84-
unsetMinMaxScaling(yOutRescaled, yIn, scaleMin, scaleMax);
85-
} //... with scaleType > 1 we could add other preprocessing trasformations
86-
}
84+
if (scaleType == 1) {
85+
LOG(debug) << "MinMax unscaling will be applied";
86+
unsetMinMaxScaling(yOutRescaled, yIn, scaleMin, scaleMax);
87+
} //... with scaleType > 1 we could add other preprocessing trasformations
88+
}
8789
}
8890

89-
std::vector<float> getPostprocessedOutput() {
90-
for (size_t j = 0; j < yOutRescaled.size(); ++j) LOG(debug)<<"Global rescaled AE output = "<< yOutRescaled.at(j);
91-
return yOutRescaled;
91+
std::vector<float> getPostprocessedOutput()
92+
{
93+
for (size_t j = 0; j < yOutRescaled.size(); ++j)
94+
LOG(debug)<<"Global rescaled AE output = "<< yOutRescaled.at(j);
95+
return yOutRescaled;
9296
}
9397
//---- MSE function
94-
float getMse(std::vector<float> yTrue, std::vector<float> yPred){
95-
LOG(debug)<<"Inside getMse sizes "<<yTrue.size()<<"\t"<<yPred.size();
98+
float getMse(std::vector<float> yTrue, std::vector<float> yPred)
99+
{
100+
LOG(debug) << "Inside getMse sizes " << yTrue.size() << "\t" << yPred.size();
96101
float mse= 0.0f;
97102
float sum = 0.0f;
98-
for (size_t j = 0; j < yTrue.size(); ++j) LOG(debug)<<"Local Feature = "<<j<<" ----> input = "<<yTrue.at(j)<<" scaled feature = "<< yPred.at(j);
103+
for (size_t j = 0; j < yTrue.size(); ++j)
104+
LOG(debug) << "Local Feature = " << j << " ----> input = " << yTrue.at(j) << " scaled feature = " << yPred.at(j);
99105
std::vector<float> yTrueScaled = getPreprocessedFeatures(); // to make the input features adimensional
100-
i f(yTrue.size() != yPred.size()){
101-
LOG(debug)<< "size of input vector ="<<yTrue.size();
102-
LOG(debug)<< "size of AE output vector ="<< yPred.size();
106+
if (yTrue.size() != yPred.size())
107+
{
108+
LOG(debug) << "size of input vector =" << yTrue.size();
109+
LOG(debug) << "size of AE output vector =" << yPred.size();
103110
LOG(fatal) << "vectors of input and predictions don't have the same size";
104-
} else { //MSE
105-
for (size_t j = 0; j < yPred.size(); ++j) {
106-
sum += std::pow(((yTrueScaled).at(j) - (yPred).at(j)), 2); //AE model gives adimensional predictions by design choice
107-
LOG(debug)<<"getMse Local feature = "<<j<<" ----> input = "<<yTrueScaled.at(j)<<" AE prediction = "<< yPred.at(j);
108-
}
109-
mse = sum/yPred.size(); // MSE of a candidate
110-
LOG(debug)<<"Local mse "<<mse;
111+
}
112+
else
113+
{ //MSE
114+
for (size_t j = 0; j < yPred.size(); ++j) {
115+
sum += std::pow(((yTrueScaled).at(j) - (yPred).at(j)), 2); //AE model gives adimensional predictions by design choice
116+
LOG(debug) << "getMse Local feature = " << j << " ----> input = " << yTrueScaled.at(j) << " AE prediction = " << yPred.at(j);
117+
}
118+
mse = sum/yPred.size(); // MSE of a candidate
119+
LOG(debug) << "Local mse " << mse;
111120
}
112121
return mse;
113122
}
114-
}; // end of the class
123+
}; // end of the class
115124

116125
} // namespace o2::analysis
117126
#endif // PWGHF_CORE_HFAETOMSEXICTOPKPI_H_

PWGHF/D2H/Tasks/taskXic.cxx

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ struct HfTaskXic {
8181
ConfigurableAxis thnConfigAxisBdtScoreBkg{"thnConfigAxisBdtScoreBkg", {100, 0., 1.}, ""};
8282
ConfigurableAxis thnConfigAxisBdtScoreSignal{"thnConfigAxisBdtScoreSignal", {100, 0., 1.}, ""};
8383
ConfigurableAxis thnConfigAxisYMC{"thnConfigAxisYMC", {100, -2., 2.}, ""};
84-
ConfigurableAxis thnConfigAxisMseXic{"thnConfigAxisMseXic", {502, -0.0002, 1}, ""}; // MSE axis
84+
ConfigurableAxis thnConfigAxisMseXic{"thnConfigAxisMseXic", {502, -0.0002, 1}, ""}; // MSE axis
8585
ConfigurableAxis thnConfigAxisAeOutputXic{"thnConfigAxisAeOutputXic", {20, 0.8, 1},""}; // an AE output axis
8686
//
8787

@@ -383,10 +383,10 @@ struct HfTaskXic {
383383
/// Fill the ML outputScores and variables of candidate Xic
384384
registry.get<THnSparse>(HIST("hnXicVarsWithBdt"))->Fill(massXic, ptCandidate, outputBkg, outputPrompt, outputFD, false);
385385
} else {
386-
outputAE = candidate.aeOutputXicToPKPi()[0]; /// AE output of feature 0
387-
outputMSE = candidate.mseXicToPKPi()[0]; /// MSE
388-
LOG(debug)<<"Global mse in taskXic for PKPi Data "<<outputMSE;
389-
registry.get<THnSparse>(HIST("hnXicVarsWithMse"))->Fill(massXic, ptCandidate, candidate.decayLength(), candidate.cpa(), outputAE, 0, 0.0, 0.0, false, outputMSE);
386+
outputAE = candidate.aeOutputXicToPKPi()[0]; /// AE output of feature 0
387+
outputMSE = candidate.mseXicToPKPi()[0]; /// MSE
388+
LOG(debug) << "Global mse in taskXic for PKPi Data " << outputMSE;
389+
registry.get<THnSparse>(HIST("hnXicVarsWithMse"))->Fill(massXic, ptCandidate, candidate.decayLength(), candidate.cpa(), outputAE, false, outputMSE);
390390
}
391391
} else {
392392
registry.get<THnSparse>(HIST("hnXicVars"))->Fill(massXic, ptCandidate, candidate.chi2PCA(), candidate.decayLength(), candidate.decayLengthXY(), candidate.cpa(), false);
@@ -402,9 +402,9 @@ struct HfTaskXic {
402402
/// Fill the ML outputScores and variables of candidate
403403
registry.get<THnSparse>(HIST("hnXicVarsWithBdt"))->Fill(massXic, ptCandidate, outputBkg, outputPrompt, outputFD, false);
404404
} else {
405-
outputAE = candidate.aeOutputXicToPiKP()[0]; /// AE output of feature 0
406-
outputMSE = candidate.mseXicToPiKP()[0]; /// MSE
407-
LOG(debug)<<"Global mse in taskXic for PiKP Data "<<outputMSE;
405+
outputAE = candidate.aeOutputXicToPiKP()[0]; /// AE output of feature 0
406+
outputMSE = candidate.mseXicToPiKP()[0]; /// MSE
407+
LOG(debug) << "Global mse in taskXic for PiKP Data " << outputMSE;
408408
registry.get<THnSparse>(HIST("hnXicVarsWithMse"))->Fill(massXic, ptCandidate, candidate.decayLength(), candidate.cpa(), outputAE, false, outputMSE);
409409
}
410410
} else {
@@ -529,9 +529,9 @@ struct HfTaskXic {
529529
/// Fill the ML outputScores and variables of candidate (todo: add multiplicity)
530530
registry.get<THnSparse>(HIST("hnXicVarsWithBdt"))->Fill(massXicToPKPi, ptCandidate, outputBkg, outputPrompt, outputFD, origin);
531531
} else {
532-
outputAE = candidate.aeOutputXicToPKPi()[0]; /// AE output of feature 0
533-
outputMSE = candidate.mseXicToPKPi()[0]; /// MSE
534-
registry.get<THnSparse>(HIST("hnXicVarsWithMse"))->Fill(massXicToPKPi, ptCandidate, candidate.decayLength(), candidate.cpa(), outputAE, origin, outputMSE);
532+
outputAE = candidate.aeOutputXicToPKPi()[0]; /// AE output of feature 0
533+
outputMSE = candidate.mseXicToPKPi()[0]; /// MSE
534+
registry.get<THnSparse>(HIST("hnXicVarsWithMse"))->Fill(massXicToPKPi, ptCandidate, candidate.decayLength(), candidate.cpa(), outputAE, origin, outputMSE);
535535
}
536536
} else {
537537
registry.get<THnSparse>(HIST("hnXicVars"))->Fill(massXicToPKPi, ptCandidate, candidate.chi2PCA(), candidate.decayLength(), candidate.decayLengthXY(), candidate.cpa(), origin);
@@ -547,9 +547,9 @@ struct HfTaskXic {
547547
// add here the pT_Mother, y_Mother, level (reco, Gen, Gen + Acc)
548548
registry.get<THnSparse>(HIST("hnXicVarsWithBdt"))->Fill(massXicToPiKP, ptCandidate, outputBkg, outputPrompt, outputFD, origin);
549549
} else {
550-
outputAE = candidate.aeOutputXicToPiKP()[0]; /// AE output of feature 0
551-
outputMSE = candidate.mseXicToPiKP()[0]; /// MSE
552-
registry.get<THnSparse>(HIST("hnXicVarsWithMse"))->Fill(massXicToPiKP, ptCandidate, candidate.decayLength(), candidate.cpa(), outputAE, origin, outputMSE);
550+
outputAE = candidate.aeOutputXicToPiKP()[0]; /// AE output of feature 0
551+
outputMSE = candidate.mseXicToPiKP()[0]; /// MSE
552+
registry.get<THnSparse>(HIST("hnXicVarsWithMse"))->Fill(massXicToPiKP, ptCandidate, candidate.decayLength(), candidate.cpa(), outputAE, origin, outputMSE);
553553
}
554554
} else {
555555
registry.get<THnSparse>(HIST("hnXicVars"))->Fill(massXicToPiKP, ptCandidate, candidate.chi2PCA(), candidate.decayLength(), candidate.decayLengthXY(), candidate.cpa(), origin);

PWGHF/TableProducer/candidateSelectorXicToPKPi.cxx

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ struct HfCandidateSelectorXicToPKPi {
8787
Configurable<bool> applyMSE{"applyMSE", false, "Flag to calculate MSE for autoencoders"};
8888
Configurable<bool> applyMinMax{"applyMinMax", false, "Flag to MinMax feature preprocessing"};
8989
/// Parameter vectors for feature preprocessing - external scaling
90-
Configurable<std::vector<float>> scaleMin{"scaleMin", {0.,0.,0.}, "vector of scaling parameter min"};
91-
Configurable<std::vector<float>> scaleMax{"scaleMax", {1.,1.,1.}, "vector of scaling parameter max"};
90+
Configurable<std::vector<float>> scaleMin{"scaleMin", {0., 0., 0.}, "vector of scaling parameter min"};
91+
Configurable<std::vector<float>> scaleMax{"scaleMax", {1., 1., 1.}, "vector of scaling parameter max"};
9292
// CCDB configuration
9393
Configurable<std::string> ccdbUrl{"ccdbUrl", "http://alice-ccdb.cern.ch", "url of the ccdb repository"};
9494
Configurable<std::vector<std::string>> modelPathsCCDB{"modelPathsCCDB", std::vector<std::string>{"EventFiltering/PWGHF/BDTXic"}, "Paths of models on CCDB"};
@@ -155,10 +155,11 @@ struct HfCandidateSelectorXicToPKPi {
155155
hfMlResponse.cacheInputFeaturesIndices(namesInputFeatures);
156156
hfMlResponse.init();
157157
/// AE feature preprocessing - MinMax scaling initialization
158-
if(applyMinMax == 1) {
158+
if (applyMinMax == 1) {
159159
LOG(info)<<"MinMax scaling will be applied";
160160
scaleType = 1;
161-
} else LOG(info)<<"No external preprocessing transformation will be applied";
161+
} else
162+
LOG(info) << "No external preprocessing transformation will be applied";
162163
}
163164
}
164165

@@ -280,7 +281,7 @@ struct HfCandidateSelectorXicToPKPi {
280281
if (applyMl) {
281282
hfMlXicToPKPiCandidate(outputMlXicToPKPi, outputMlXicToPiKP);
282283
hfMseXicToPKPiCandidate(outputMseXicToPKPi, outputMseXicToPiKP); // MSE
283-
hfAeXicToPKPiCandidate(outputAeXicToPKPi, outputAeXicToPiKP); // autoencoder outputs
284+
hfAeXicToPKPiCandidate(outputAeXicToPKPi, outputAeXicToPiKP); // autoencoder outputs
284285
}
285286
if (activateQA) {
286287
registry.fill(HIST("hSelections"), 1, ptCand);
@@ -305,7 +306,7 @@ struct HfCandidateSelectorXicToPKPi {
305306
if (applyMl) {
306307
hfMlXicToPKPiCandidate(outputMlXicToPKPi, outputMlXicToPiKP);
307308
hfMseXicToPKPiCandidate(outputMseXicToPKPi, outputMseXicToPiKP); // MSE
308-
hfAeXicToPKPiCandidate(outputAeXicToPKPi, outputAeXicToPiKP); // autoencoder outputs
309+
hfAeXicToPKPiCandidate(outputAeXicToPKPi, outputAeXicToPiKP); // autoencoder outputs
309310
}
310311
continue;
311312
}
@@ -320,7 +321,7 @@ struct HfCandidateSelectorXicToPKPi {
320321
if (applyMl) {
321322
hfMlXicToPKPiCandidate(outputMlXicToPKPi, outputMlXicToPiKP);
322323
hfMseXicToPKPiCandidate(outputMseXicToPKPi, outputMseXicToPiKP); // MSE
323-
hfAeXicToPKPiCandidate(outputAeXicToPKPi, outputAeXicToPiKP); // autoencoder outputs
324+
hfAeXicToPKPiCandidate(outputAeXicToPKPi, outputAeXicToPiKP); // autoencoder outputs
324325
}
325326
continue;
326327
}
@@ -389,7 +390,7 @@ struct HfCandidateSelectorXicToPKPi {
389390
if (applyMl) {
390391
hfMlXicToPKPiCandidate(outputMlXicToPKPi, outputMlXicToPiKP);
391392
hfMseXicToPKPiCandidate(outputMseXicToPKPi, outputMseXicToPiKP); // MSE
392-
hfAeXicToPKPiCandidate(outputAeXicToPKPi, outputAeXicToPiKP); // autoencoder outputs
393+
hfAeXicToPKPiCandidate(outputAeXicToPKPi, outputAeXicToPiKP); // autoencoder outputs
393394
}
394395
continue;
395396
}
@@ -412,7 +413,7 @@ struct HfCandidateSelectorXicToPKPi {
412413
if (topolXicToPKPi && pidXicToPKPi) {
413414
std::vector<float> inputFeaturesXicToPKPi = hfMlResponse.getInputFeatures(candidate, true);
414415
isSelectedMlXicToPKPi = hfMlResponse.isSelectedMl(inputFeaturesXicToPKPi, ptCand, outputMlXicToPKPi);
415-
if(applyMSE){
416+
if (applyMSE) {
416417
/// fill outputAeXicToPKPi with rescaled AE output since ML output is automatically scaled
417418
hfAeResponse.unsetScaling(applyMSE, scaleType, outputMlXicToPKPi, scaleMin, scaleMax);
418419
outputAeXicToPKPi = hfAeResponse.getPostprocessedOutput();
@@ -425,7 +426,7 @@ struct HfCandidateSelectorXicToPKPi {
425426
if (topolXicToPiKP && pidXicToPiKP) {
426427
std::vector<float> inputFeaturesXicToPiKP = hfMlResponse.getInputFeatures(candidate, false);
427428
isSelectedMlXicToPiKP = hfMlResponse.isSelectedMl(inputFeaturesXicToPiKP, ptCand, outputMlXicToPiKP);
428-
if(applyMSE){
429+
if (applyMSE) {
429430
/// fill outputAeXicToPiKP with rescaled AE output since ML output is automatically scaled
430431
hfAeResponse.unsetScaling(applyMSE, scaleType, outputMlXicToPiKP, scaleMin, scaleMax);
431432
outputAeXicToPiKP = hfAeResponse.getPostprocessedOutput();

0 commit comments

Comments
 (0)