Hi,
I have multiple graphs to fit, so I did a std::map<std::string, TF1*>
to store the fit functions,but when I try to access, I get the error:
/mnt/d/Sync1/Estudios/IFICCSIC/JAEICUIFIC2122/MadGraph/Project2/pp2ttbarllbar_SMEFTsim_analysis/xs_param.c:257:15: error: no viable overloaded operator[] for type 'std::vector<std::string>' (aka 'vector<basic_string<char> >')
f = eftList0[keys[i]];
~~~~~~~~^~~~~~~~
/usr/include/c++/10/bits/stl_vector.h:1043:7: note: candidate function not viable: no known conversion from '__gnu_cxx::__alloc_traits<std::allocator<std::__cxx11::basic_string<char> >, std::__cxx11::basic_string<char> >::value_type' (aka 'std::__cxx11::basic_string<char>') to 'std::vector::size_type' (aka 'unsigned long') for 1st argument
operator[](size_type __n) _GLIBCXX_NOEXCEPT
^
/usr/include/c++/10/bits/stl_vector.h:1061:7: note: candidate function not viable: no known conversion from '__gnu_cxx::__alloc_traits<std::allocator<std::__cxx11::basic_string<char> >, std::__cxx11::basic_string<char> >::value_type' (aka 'std::__cxx11::basic_string<char>') to 'std::vector::size_type' (aka 'unsigned long') for 1st argument
operator[](size_type __n) const _GLIBCXX_NOEXCEPT
I found some information here, but I don’t know how to solve.
This is the code for the std::map
creation (look at the two lines with comments):
auto param_all(std::map<std::string, std::vector<std::vector<double>>> data,
std::vector<std::string> eftList,
std::string path,
TString title){
std::string currKey;
std::vector<std::vector<double>> currPoints;
std::string name;
std::map<std::string, TF1*> fitFuncs = {}; // IMPORTANT
for(int i = 0; i < eftList.size(); ++i){
currKey = eftList[i];
TCanvas* c1 = new TCanvas();
TGraph* g1 = new TGraph();
currPoints = data[currKey];
for(int i = 0; i < currPoints.size(); ++i){
g1->AddPoint(currPoints[i][0], currPoints[i][1]);
}
g1->SetMarkerStyle(8);
g1->Fit("pol2", "SQ");
fitFuncs[currKey] = g1->GetFunction("pol2");
cout << fitFuncs[currKey]->GetParameter(0) << endl; // IMPORTANT THIS DOES NOT THROW THE ERROR
g1->Draw("AP");
g1->GetXaxis()->SetTitle(currKey.c_str());
g1->SetTitle(title);
g1->GetYaxis()->SetTitle("#sigma/#sigma_{SM}");
c1->Update();
std::string name = path + currKey + std::string(".png");
c1->Print(name.c_str());
}
return fitFuncs;
}
The error comes when I want to use the output (one line with comments):
void xs_param(){
TString paths[] = {
"run_01_files/run_01.root",
"run_02_files/run_02.root",
"run_03_files/run_03.root",
"run_05_files/run_05.root"
};
std::string pathsSaveSingle[] = {
"run_01_files/out/",
"run_02_files/out/",
"run_03_files/out/",
"run_05_files/out/"
};
TString mll_cuts[] = {
"#splitline{m_{ll} #in [101.0, 117.96] GeV}{#sigma_{SM}=(1876#pm8)#times10^{-6}pb}",
"#splitline{m_{ll} #in [117.96, 140.9] GeV}{#sigma_{SM}=(662#pm3)#times10^{-6}pb}",
"#splitline{m_{ll} #in [140.9, 181.36] GeV}{#sigma_{SM}=(446.0#pm1.5)#times10^{-6}pb}",
"#splitline{m_{ll} #in [181.36, +#infty] GeV}{#sigma_{SM}=(517.9#pm1.9)#times10^{-6}pb}"
};
Double_t xss_nominal[] = {
1876e-6,
662e-6,
446.0e-6,
517.9e-6
};
Double_t xss_error_nominal[] = {
8e-6,
3e-6,
1.5e-6,
1.9e-6
};
gROOT->SetBatch(kTRUE);
gStyle->SetOptStat(11);
gStyle->SetOptFit(1);
Int_t nbins = sizeof(paths)/sizeof(paths[0]);
auto [data0, eftList0] = read_root(paths[0], true);
auto fitFuncs0 = param_all(data0, eftList0, pathsSaveSingle[0], mll_cuts[0]);
auto [data1, eftList1] = read_root(paths[1], true);
auto fitFuncs1 = param_all(data1, eftList1, pathsSaveSingle[1], mll_cuts[1]);
auto [data2, eftList2] = read_root(paths[2], true);
auto fitFuncs2 = param_all(data2, eftList2, pathsSaveSingle[2], mll_cuts[2]);
auto [data3, eftList3] = read_root(paths[3], true);
auto fitFuncs3 = param_all(data3, eftList3, pathsSaveSingle[3], mll_cuts[3]);
std::vector<std::string> keys = {"cte11", "cte22", "cte33"};
TCanvas* c1 = new TCanvas();
c1->Divide(4, 1, 0, 0);
double minCoeff = -5;
double maxCoeff = 5;
double minXs = 2;
double maxXs = 0.7;
int N = 100;
std::vector<double> coeffs = {};
for(int i = 0; i < N; ++i){coeffs.push_back(minCoeff + i*2*maxCoeff/N);}
TH1F* frame;
TMultiGraph* mg;
TGraph* g;
TF1* f;
cout << "This part of the code is sensible" << endl;
double p0;
double p1;
double p2;
c1->cd(1);
frame = gPad->DrawFrame(0., 0., 1., 1.);
mg = new TMultiGraph();
std::string currKey;
for(int i = 0; i < keys.size(); ++i){
g = new TGraph();
f = eftList0[keys[i]]; // THE ERROR IS HERE
p0 = f->GetParameter(0);
}
}
_ROOT Version:ROOT 6.24/06
Platform: Debian, Windows Subsystem for Linux
Compiler: Not Provided