Skip to content

Commit 9bbe904

Browse files
INFNuserINFNuser
authored andcommitted
Code review 2 of AE inclusion
1 parent fa068eb commit 9bbe904

4 files changed

Lines changed: 129 additions & 125 deletions

File tree

PWGHF/Core/HfAeToMseXicToPKPi.h

Lines changed: 90 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -21,93 +21,97 @@
2121
#include "PWGHF/Core/HfMlResponse.h"
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;
32-
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-
{ //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){ //it takes the bool flag and scaling parameters configurables in taskXic
47-
yScaled.clear();
48-
if( scaleFlag == false){ LOG(debug)<<"No external preprocessing transformation will be applied";
49-
yScaled.assign(yIn.begin(), yIn.end());
50-
} else{
51-
if(scaleType == 1){
52-
LOG(debug)<<"MinMax scaling will be applied";
53-
setMinMaxScaling(yScaled, yIn, scaleMin, scaleMax);
54-
}//... with scaleType > 1 we could add other preprocessing trasformations
55-
}
56-
}
57-
std::vector<float> getPreprocessedFeatures(){
58-
for (size_t j = 0; j < yScaled.size(); ++j) LOG(debug)<<"Global scaled feature = "<< yScaled.at(j);
59-
return yScaled;
60-
}
61-
//Reverse preprocessing - output postprocessing
62-
void unsetMinMaxScaling(std::vector<float>& yOut, std::vector<float> yIn, std::vector<float> scaleMin, std::vector<float> scaleMax)
63-
{ yOut.clear();//initial clear to avoid multiple filling if setMinMax o setScaling are called more than once
64-
for (size_t j = 0; j < yIn.size(); ++j)
65-
{ //for over the features
66-
//MinMax scaling of the input features
67-
LOG(debug)<<"--------------> MinMax unscaling Debug \t"<<scaleMin.at(j)<<"\t"<<scaleMax.at(j);
68-
yOut.push_back(yIn.at(j)*(scaleMax.at(j)- scaleMin.at(j))+ scaleMin.at(j));
69-
LOG(debug)<<"Unscaling output = "<<j<<" ----> input = "<<yIn.at(j)<<" rescaled output = "<< yOut.at(j);
70-
}
71-
}
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;
7232

73-
void unsetScaling(bool scaleFlag, int scaleType, /*AE output*/ std::vector<float> yIn, std::vector<float> scaleMin, std::vector<float> scaleMax){ //it takes the bool flag and scaling parameters configurables in taskXic
74-
yOutRescaled.clear();
75-
if( scaleFlag == false){ LOG(debug)<<"No external preprocessing transformation will be applied";
76-
yOutRescaled.assign(yIn.begin(), yIn.end());
77-
} else{
78-
if(scaleType == 1){
79-
LOG(debug)<<"MinMax unscaling will be applied";
80-
unsetMinMaxScaling(yOutRescaled, yIn, scaleMin, scaleMax);
81-
}//... with scaleType > 1 we could add other preprocessing trasformations
82-
}
83-
}
84-
std::vector<float> getPostprocessedOutput(){
85-
for (size_t j = 0; j < yOutRescaled.size(); ++j) LOG(debug)<<"Global rescaled AE output = "<< yOutRescaled.at(j);
86-
return yOutRescaled;
87-
}
88-
//---- MSE function
89-
float getMse(std::vector<float> yTrue, std::vector<float> yPred){
90-
LOG(debug)<<"Inside getMse sizes "<<yTrue.size()<<"\t"<<yPred.size();
91-
float mse= 0.0f;
92-
float sum = 0.0f;
93-
for (size_t j = 0; j < yTrue.size(); ++j) LOG(debug)<<"Local Feature = "<<j<<" ----> input = "<<yTrue.at(j)<<" scaled feature = "<< yPred.at(j);
94-
std::vector<float> yTrueScaled = getPreprocessedFeatures();
95-
if( yTrue.size() != yPred.size()){
96-
LOG(debug)<< "size of input vector ="<<yTrue.size();
97-
LOG(debug)<< "size of AE output vector ="<< yPred.size();
98-
LOG(fatal) << "vectors of input and predictions don't have the same size";
99-
}
100-
else{//MSE
101-
for (size_t j = 0; j < yPred.size(); ++j) { //for over the features
102-
sum += std::pow(((yTrueScaled).at(j) - (yPred).at(j)), 2); //has dimensions
103-
LOG(debug)<<"getMse Local feature = "<<j<<" ----> input = "<<yTrueScaled.at(j)<<" AE prediction = "<< yPred.at(j);
104-
}
105-
mse = sum/yPred.size(); //MSE of a candidate
106-
LOG(debug)<<"Local mse "<<mse;
107-
}
108-
return mse;
109-
}
110-
}; //end of the class
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
68+
// MinMax scaling of the input features
69+
LOG(debug)<<"--------------> MinMax unscaling Debug \t"<<scaleMin.at(j)<<"\t"<<scaleMax.at(j);
70+
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);
72+
}
73+
}
74+
75+
void unsetScaling(bool scaleFlag, int scaleType, /*AE output*/ std::vector<float> yIn, std::vector<float> scaleMin, std::vector<float> scaleMax)
76+
{ // it takes the bool flag and scaling parameters configurables in taskXic
77+
yOutRescaled.clear();
78+
if( scaleFlag == false){
79+
LOG(debug)<<"No external preprocessing transformation will be applied";
80+
yOutRescaled.assign(yIn.begin(), yIn.end());
81+
} 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+
}
87+
}
88+
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;
92+
}
93+
//---- MSE function
94+
float getMse(std::vector<float> yTrue, std::vector<float> yPred){
95+
LOG(debug)<<"Inside getMse sizes "<<yTrue.size()<<"\t"<<yPred.size();
96+
float mse= 0.0f;
97+
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);
99+
std::vector<float> yTrueScaled = getPreprocessedFeatures(); // to make the input features adimensional
100+
if( yTrue.size() != yPred.size()){
101+
LOG(debug)<< "size of input vector ="<<yTrue.size();
102+
LOG(debug)<< "size of AE output vector ="<< yPred.size();
103+
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+
return mse;
113+
}
114+
}; // end of the class
111115

112116

113117
} // namespace o2::analysis

PWGHF/D2H/Tasks/taskXic.cxx

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ struct HfTaskXic {
255255

256256
if (doprocessDataWithMl || doprocessMcWithMl) { // with ML
257257
registry.add("hnXicVarsWithBdt", "THn for Xic candidates with BDT scores", HistType::kTHnSparseF, {thnAxisMass, thnAxisPt, thnAxisBdtScoreXicBkg, thnAxisBdtScoreXicPrompt, thnAxisBdtScoreXicNonPrompt, thnAxisMcOrigin});
258-
registry.add("hnXicVarsWithMse", "THn for Xic candidates with MSE using AD", HistType::kTHnSparseF, {thnAxisMass, thnAxisPt, thnAxisDecLength, thnAxisCPA, thnAxisAeOutputXic, thnAxisMcOrigin, thnAxisMseXic});
258+
registry.add("hnXicVarsWithMse", "THn for Xic candidates with MSE using AD", HistType::kTHnSparseF, {thnAxisMass, thnAxisPt, thnAxisDecLength, thnAxisCPA, thnAxisAeOutputXic, thnAxisMcOrigin, thnAxisMseXic});
259259
} else {
260260
registry.add("hnXicVars", "THn for Xic candidates", HistType::kTHnSparseF, {thnAxisMass, thnAxisPt, thnAxisChi2PCA, thnAxisDecLength, thnAxisDecLengthXY, thnAxisCPA, thnAxisMcOrigin});
261261
}
@@ -387,7 +387,7 @@ struct HfTaskXic {
387387
outputMSE = candidate.mseXicToPKPi()[0]; /// MSE
388388
LOG(debug)<<"Global mse in taskXic for PKPi Data "<<outputMSE;
389389
registry.get<THnSparse>(HIST("hnXicVarsWithMse"))->Fill(massXic, ptCandidate, candidate.decayLength(), candidate.cpa(), outputAE, 0, 0.0, 0.0, false, outputMSE);
390-
}
390+
}
391391
} else {
392392
registry.get<THnSparse>(HIST("hnXicVars"))->Fill(massXic, ptCandidate, candidate.chi2PCA(), candidate.decayLength(), candidate.decayLengthXY(), candidate.cpa(), false);
393393
}
@@ -406,7 +406,7 @@ struct HfTaskXic {
406406
outputMSE = candidate.mseXicToPiKP()[0]; /// MSE
407407
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);
409-
}
409+
}
410410
} else {
411411
registry.get<THnSparse>(HIST("hnXicVars"))->Fill(massXic, ptCandidate, candidate.chi2PCA(), candidate.decayLength(), candidate.decayLengthXY(), candidate.cpa(), false);
412412
}
@@ -529,10 +529,10 @@ 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);
535-
}
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);
535+
}
536536
} else {
537537
registry.get<THnSparse>(HIST("hnXicVars"))->Fill(massXicToPKPi, ptCandidate, candidate.chi2PCA(), candidate.decayLength(), candidate.decayLengthXY(), candidate.cpa(), origin);
538538
}
@@ -547,10 +547,10 @@ 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);
553-
}
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);
553+
}
554554
} else {
555555
registry.get<THnSparse>(HIST("hnXicVars"))->Fill(massXicToPiKP, ptCandidate, candidate.chi2PCA(), candidate.decayLength(), candidate.decayLengthXY(), candidate.cpa(), origin);
556556
}

PWGHF/DataModel/CandidateSelectionTables.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,9 +336,9 @@ DECLARE_SOA_TABLE(HfSelXicToPKPi, "AOD", "HFSELXIC", //!
336336
DECLARE_SOA_TABLE(HfMlXicToPKPi, "AOD", "HFMLXIC", //!
337337
hf_sel_candidate_xic::MlProbXicToPKPi, hf_sel_candidate_xic::MlProbXicToPiKP);
338338
DECLARE_SOA_TABLE(HfMseXicToPKPi, "AOD", "HFMSEXIC", //! new table for MSE
339-
hf_sel_candidate_xic::MseXicToPKPi, hf_sel_candidate_xic::MseXicToPiKP);
339+
hf_sel_candidate_xic::MseXicToPKPi, hf_sel_candidate_xic::MseXicToPiKP);
340340
DECLARE_SOA_TABLE(HfAeOutXicToPKPi, "AOD", "HFAEXIC", //! new table for AE output
341-
hf_sel_candidate_xic::AeOutputXicToPKPi, hf_sel_candidate_xic::AeOutputXicToPiKP);
341+
hf_sel_candidate_xic::AeOutputXicToPKPi, hf_sel_candidate_xic::AeOutputXicToPiKP);
342342
// XicPlus to Xi Pi Pi
343343
DECLARE_SOA_TABLE(HfSelXicToXiPiPi, "AOD", "HFSELXICTOXI2PI", //!
344344
hf_sel_candidate_xic::IsSelXicToXiPiPi);

0 commit comments

Comments
 (0)