Hi,
I need your help to know if something is possible or not :
I want to open a compressed file using Boost C++ library.
To uncompress the entire file is too heavy.
In order to save memory, I do that :
ifstream file("ntrac.pin_detector.gz", std::ios_base::in | std::ios_base::binary);
boost::iostreams::filtering_streambuf<boost::iostreams::input> inbuf;
inbuf.push(boost::iostreams::gzip_decompressor());
inbuf.push(file);
istream instream(&inbuf); //Convert streambuf to istream
getline(instream, first_file_line);
After that, I use TTree.ReadStream()
to “put” my file in a TTree and I got that error :
Error in <TTree::ReadStream>: Error reading stream
Do you know if it is normal and how to make it work ?
Regards,
William.
PS : Someone told me that this error could happen because the stream is unseekable if I use Boost.
Maybe he’s right, what do you think ?
This is the full code :
#include "TTree.h"
#include "TH1F.h"
#include "Riostream.h"
#include "TDirectory.h"
#include <string>
#include <sstream>
#include <iostream>
#include <map>
#include <memory>
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/format.hpp>
using namespace std;
int main()
{
//init the map which contain the data type for each existing column on our file
std::map<string, string> type = {{"CAID", "/I"},
{"TIME", "/F"},
{"COOR_X", "/F"},
{"COOR_Y", "/F"},
{"COOR_Z", "/F"}};
//init of string variable
string first_file_line;
string file_head;
string str_temp;
ifstream file("ntrac.pin_detector.gz", std::ios_base::in | std::ios_base::binary);
boost::iostreams::filtering_streambuf<boost::iostreams::input> inbuf;
inbuf.push(boost::iostreams::gzip_decompressor());
inbuf.push(file);
istream instream(&inbuf); //Convert streambuf to istream
getline(instream, first_file_line);
istringstream first_line_stream;
first_line_stream.str(first_file_line);
first_line_stream >> str_temp;
//creating the branchDescriptor for the tree
file_head = file_head + str_temp + type[str_temp];
while(first_line_stream.good())
{
first_line_stream >> str_temp;
file_head = file_head + ":" + str_temp + type[str_temp];
}
//branchDescriptor is a const char*, not a string
const char* char_file_head = file_head.c_str();
cout << char_file_head << endl;
std::shared_ptr<TTree> mytree(new TTree("Data","This is my first ttree"));
mytree->ReadStream(instream, char_file_head, ' ');
return 0;
}
And the ten first file lines : ```
CAID TIME COOR_X COOR_Y COOR_Z
1 .869214 -8.7287829E-02 -1.9229909E-02 9.8033469E+01
1 1.570078 5.2402355E-01 -8.8949773E-02 1.3084988E+02
1 25.121292 6.3431012E-02 5.2537755E-01 7.2844195E+01
1 4.765265 3.0164797E-01 -1.3995741E-01 6.4161585E+01
1 55.628903 4.5086203E-01 2.9084185E-01 1.1436328E+02
1 9.530194 -3.3718074E-01 -1.1671732E-01 5.1681154E+01
1 .720152 -3.4568707E-01 -2.1176673E-01 9.7635021E+01
1 24.776037 4.7535869E-01 -9.5100680E+00 1.3371108E+02
1 23.394644 3.6313655E-02 4.4193817E-01 8.1504813E+01