Std::map with TF1: error: no viable overloaded operator[] for type 'std::vector<std::string>'

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


The keys[i] returns cte11 for i=0. What is the meaning of the f=eftList0["cte11"] then?

@yus I’m just getting the element with key “cte11”. If I add this on the function param_all where the std::map is created:

		cout << currKey << endl;
		cout << fitFuncs[currKey]->GetParameter(0) << endl;

I get

cte11
1

and the other keys. That is not the problem.

Something important I have missed to say: If I run the full program I get this output

root [1] .x xs_param.c
In file included from input_line_95:1:
/mnt/d/Sync1/Estudios/IFICCSIC/JAEICUIFIC2122/MadGraph/Project2/pp2ttbarllbar_SMEFTsim_analysis/xs_param.c:214:7: warning: decomposition declarations are a C++17 extension [-Wc++17-extensions]
        auto [data0, eftList0] = read_root(paths[0], true);
             ^~~~~~~~~~~~~~~~~
/mnt/d/Sync1/Estudios/IFICCSIC/JAEICUIFIC2122/MadGraph/Project2/pp2ttbarllbar_SMEFTsim_analysis/xs_param.c:217:7: warning: decomposition declarations are a C++17 extension [-Wc++17-extensions]
        auto [data1, eftList1] = read_root(paths[1], true);
             ^~~~~~~~~~~~~~~~~
/mnt/d/Sync1/Estudios/IFICCSIC/JAEICUIFIC2122/MadGraph/Project2/pp2ttbarllbar_SMEFTsim_analysis/xs_param.c:220:7: warning: decomposition declarations are a C++17 extension [-Wc++17-extensions]
        auto [data2, eftList2] = read_root(paths[2], true);
             ^~~~~~~~~~~~~~~~~
/mnt/d/Sync1/Estudios/IFICCSIC/JAEICUIFIC2122/MadGraph/Project2/pp2ttbarllbar_SMEFTsim_analysis/xs_param.c:223:7: warning: decomposition declarations are a C++17 extension [-Wc++17-extensions]
        auto [data3, eftList3] = read_root(paths[3], true);
             ^~~~~~~~~~~~~~~~~
/mnt/d/Sync1/Estudios/IFICCSIC/JAEICUIFIC2122/MadGraph/Project2/pp2ttbarllbar_SMEFTsim_analysis/xs_param.c:258: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
      ^
root [2]

What I want to say here is that at param_all there are some prints and are not being shown, that means that the error comes before code execution.

And if you look at the link I shared at the beggining, you know that this is not related to the keys, but to the elements of the std::map

I try

auto generate(){
	
	TGraph* g1 = new TGraph();
	g1->AddPoint(0,0);
	g1->AddPoint(1,1);
	g1->Fit("pol1");
	
	std::map<char, TF1*> out = {};
	out['a'] = g1->GetFunction("pol1");
	
	return out;
}

auto test_tf1(){
	
	auto dict = generate();
	TF1* fa1 = generate()['a'];
}

And this works, but this does not explain why my original code does not work.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.