Skip to content

Commit 67ffee3

Browse files
Merge pull request #99 from mach3-software/dbarrow257/feature/EvenMoreSplineFDCleanup
feat: Move FillSampleArray to splineFDBase
2 parents b6d3eb2 + 4410d28 commit 67ffee3

2 files changed

Lines changed: 23 additions & 151 deletions

File tree

splines/BinnedSplinesTutorial.cpp

Lines changed: 22 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -1,160 +1,33 @@
11
#include "BinnedSplinesTutorial.h"
2-
#include "manager/MaCh3Modes.h"
3-
#include "samplePDF/StructsTutorial.h"
4-
5-
6-
//MaCh3 core includes
7-
#include <samplePDF/Structs.h>
8-
#include <manager/MaCh3Logger.h>
9-
//****************************************
10-
11-
_MaCh3_Safe_Include_Start_ //{
12-
//ROOT include
13-
#include "TKey.h"
14-
#include "TROOT.h"
15-
_MaCh3_Safe_Include_End_ //}
162

173
BinnedSplineTutorial::BinnedSplineTutorial(covarianceXsec *xsec_cov, MaCh3Modes* Modes_) : splineFDBase(xsec_cov, Modes_) {
184
}
195

206
BinnedSplineTutorial::~BinnedSplineTutorial() {
217
}
228

23-
/*! ETA - moved this into experiment specific code since it relies on experimental MC format and also the experiment spline name mapping
24-
* Performs two jobs
25-
* 1. Fills indexing/each sample
26-
* 2. Creates the big spline vector
27-
*/
28-
void BinnedSplineTutorial::FillSampleArray(std::string SampleName, std::vector<std::string> OscChanFileNames)
29-
{
30-
int iSample = getSampleIndex(SampleName);
31-
32-
int nOscChannels = nOscChans[iSample];
9+
std::vector<std::string> BinnedSplineTutorial::GetTokensFromSplineName(std::string FullSplineName) {
10+
std::vector<std::string> ReturnVec(TokenOrdering::kNTokens);
11+
12+
TObjArray *tokens = TString(FullSplineName).Tokenize(".");
13+
/*
14+
A little hacky but lets us grab both old + new splines
15+
*/
16+
if(tokens->GetEntries()!=7){
17+
delete tokens;
18+
tokens = TString(FullSplineName).Tokenize("_");
19+
}
3320

34-
for (int iOscChan = 0; iOscChan < nOscChannels; iOscChan++) {
35-
MACH3LOG_INFO("Processing: {}", OscChanFileNames[iOscChan]);
36-
TSpline3* mySpline = nullptr;
37-
TSpline3_red* Spline = nullptr;
38-
TString Syst, Mode;
39-
int nKnots, SystNum, ModeNum, Var1Bin, Var2Bin, Var3Bin = M3::_BAD_INT_;
40-
double x,y, Eval = M3::_BAD_DOUBLE_;
41-
bool isFlat = true;
42-
43-
auto File = std::unique_ptr<TFile>(TFile::Open(OscChanFileNames[iOscChan].c_str()));
44-
45-
if (!File || File->IsZombie()) {
46-
MACH3LOG_ERROR("File {} not found", OscChanFileNames[iOscChan]);
47-
throw MaCh3Exception(__FILE__, __LINE__);
48-
}
49-
50-
//This is the MC specific part of the code
51-
//i.e. we always assume that the splines are just store in single TDirectory and they're all in there as single objects
52-
for (auto k : *File->GetListOfKeys()) {
53-
auto Key = static_cast<TKey*>(k);
54-
TClass *Class = gROOT->GetClass(Key->GetClassName(), false);
55-
if(!Class->InheritsFrom("TSpline3")) {
56-
continue;
57-
}
58-
59-
TString FullSplineName = static_cast<TString>(Key->GetName());
60-
MACH3LOG_DEBUG("FillSplineName is {}", FullSplineName);
61-
// First We split into binning and spline name
62-
TObjArray *tokens = FullSplineName.Tokenize(".");
63-
MACH3LOG_DEBUG("Number of tokens is {}", tokens->GetEntries());
64-
/*
65-
A little hacky but lets us grab both old + new splines
66-
*/
67-
if(tokens->GetEntries()!=7){
68-
delete tokens;
69-
tokens = FullSplineName.Tokenize("_");
70-
}
71-
72-
Syst = static_cast<TObjString*>(tokens->At(1))->GetString();
73-
Mode = static_cast<TObjString*>(tokens->At(2))->GetString();
74-
// Skip 3 because it's "sp"
75-
Var1Bin = static_cast<TObjString*>(tokens->At(4))->GetString().Atoi();
76-
Var2Bin = static_cast<TObjString*>(tokens->At(5))->GetString().Atoi();
77-
Var3Bin = 0;
78-
79-
if (tokens->GetEntries() == 7)
80-
{
81-
Var3Bin = static_cast<TObjString*>(tokens->At(6))->GetString().Atoi();
82-
}
83-
84-
SystNum = -1;
85-
for (unsigned iSyst = 0; iSyst < SplineFileParPrefixNames[iSample].size(); iSyst++) {
86-
if (strcmp(Syst, SplineFileParPrefixNames[iSample][iSyst].c_str()) == 0) {
87-
SystNum = iSyst;
88-
break;
89-
}
90-
}
91-
92-
// If the syst doesn't match any of the spline names then skip it
93-
if (SystNum == -1){
94-
MACH3LOG_WARN("Couldn't match!!");
95-
MACH3LOG_DEBUG("Couldn't Match any systematic name in xsec yaml with spline name: {}" , FullSplineName.Data());
96-
continue;
97-
}
98-
99-
ModeNum = -1;
100-
for (unsigned int iMode = 0; iMode < SplineModeVecs[iSample][SystNum].size(); iMode++) {
101-
if (strcmp(Mode, Modes->GetSplineSuffixFromMaCh3Mode(SplineModeVecs[iSample][SystNum][iMode]).c_str()) == 0) {
102-
ModeNum = iMode;
103-
break;
104-
}
105-
}
106-
107-
if (ModeNum == -1) {
108-
MACH3LOG_ERROR("Couldn't find mode for {} in {}. Problem Spline is : {} ", Mode, Syst, FullSplineName);
109-
throw;
110-
}
111-
112-
113-
mySpline = Key->ReadObject<TSpline3>();
114-
115-
if (isValidSplineIndex(SampleName, iOscChan, SystNum, ModeNum, Var1Bin, Var2Bin, Var3Bin))
116-
{ // loop over all the spline knots and check their value
117-
MACH3LOG_DEBUG("Pushed back monolith for spline {}", FullSplineName);
118-
// if the value is 1 then set the flat bool to false
119-
nKnots = mySpline->GetNp();
120-
isFlat = true;
121-
for (int iKnot = 0; iKnot < nKnots; iKnot++)
122-
{
123-
mySpline->GetKnot(iKnot, x, y);
124-
125-
Eval = mySpline->Eval(x);
126-
if (Eval < 0.99999 || Eval > 1.00001)
127-
{
128-
isFlat = false;
129-
break;
130-
}
131-
}
132-
133-
//Rather than keeping a mega vector of splines then converting, this should just keep everything nice in memory!
134-
indexvec[iSample][iOscChan][SystNum][ModeNum][Var1Bin][Var2Bin][Var3Bin]=MonolithIndex;
135-
coeffindexvec.push_back(CoeffIndex);
136-
// Should save memory rather saving [x_i_0 ,... x_i_maxknots] for every spline!
137-
if (isFlat) {
138-
splinevec_Monolith.push_back(nullptr);
139-
delete mySpline;
140-
} else {
141-
Spline = new TSpline3_red(mySpline, SplineInterpolationTypes[iSample][SystNum]);
142-
delete mySpline;
143-
144-
splinevec_Monolith.push_back(Spline);
145-
uniquecoeffindices.push_back(MonolithIndex); //So we can get the unique coefficients and skip flat splines later on!
146-
CoeffIndex+=nKnots;
147-
}
148-
//Incrementing MonolithIndex to keep track of number of valid spline indices
149-
MonolithIndex+=1;
150-
} else {
151-
//Potentially you are not a valid spline index
152-
delete mySpline;
153-
}
154-
}//End of loop over all TKeys in file
21+
ReturnVec[TokenOrdering::kSystToken] = (static_cast<TObjString*>(tokens->At(1)))->GetString();
22+
ReturnVec[TokenOrdering::kModeToken] = (static_cast<TObjString*>(tokens->At(2)))->GetString();
23+
// Skip 3 because it's "sp"
24+
ReturnVec[TokenOrdering::kVar1BinToken] = (static_cast<TObjString*>(tokens->At(4)))->GetString();
25+
ReturnVec[TokenOrdering::kVar2BinToken] = (static_cast<TObjString*>(tokens->At(5)))->GetString();
26+
ReturnVec[TokenOrdering::kVar3BinToken] = "0";
27+
28+
if (tokens->GetEntries() == 7) {
29+
ReturnVec[TokenOrdering::kVar3BinToken] = (static_cast<TObjString*>(tokens->At(6)))->GetString();
30+
}
15531

156-
//A bit of clean up
157-
File->Delete("*");
158-
File->Close();
159-
} //End of oscillation channel loop
32+
return ReturnVec;
16033
}

splines/BinnedSplinesTutorial.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22

33
//MaCh3 includes
44
#include "splines/splineFDBase.h"
5-
#include "manager/MaCh3Modes.h"
65

76
class BinnedSplineTutorial : public splineFDBase
87
{
98
public:
109
BinnedSplineTutorial(covarianceXsec *xsec_cov, MaCh3Modes *Modes);
1110
virtual ~BinnedSplineTutorial();
1211

13-
void FillSampleArray(std::string SampleName, std::vector<std::string> OscChanFileNames);
12+
std::vector<std::string> GetTokensFromSplineName(std::string FullSplineName);
1413
};

0 commit comments

Comments
 (0)