What I want to do, is to read a text file which includes x-secs for some processes and then and fill a tree . What I do is this
Double_t x_sec;
tree->Branch("x_sec",&x_sec,"x_sec/D");
#include <fstream>
#include <iostream>
string filename = "MyTextFile.txt";
vector<string> input_files;
ifstream textfile;
textfile.open(filename.c_str());
if( !textfile.good() ) {
cerr << "Cannot open the file: \"" << filename+"\"!"<<endl;
abort();
}
// OK we have the file!
string line("");
while( !textfile.eof() ) {
line="";
textfile >> line;
// Do your stuff here! maybe:
if (line=="") continue;
if (line== "#") continue;
input_files.push_back(line);
}
How to take now the string from input_files which of type char and make it of type Double_t in order to write to tree ? (ie pass to and then write it to the x_sec ? )
You will need to either use 2 distinct TTree or copy/paste and adapt the code in ReadFile. i.e. ReadFile assumes you are reading all the branches of the TTree.
I am not sure what you tried nor exactly what you mean but the following might be what you are looking for:void readOneVar(const char *txtfile, const char *varname) {
TString filename; filename.Form( "../results/Tree%s.root", varname );
TFile *savefile = new TFile (filename);
TTree *tree = new TTree("tree","");
tree->ReadFile(txtfile,varname);
tree->GetCurrentFile()->Write();
}