#include <string>
#include <iostream>
#include <TTree.h>
#include <TFile.h>


int EventBuilderVFAT(const char* rawfilename,
		     const char* rootfilename, 
		     const int readmaxevent);

using namespace std;

void Builder(string rawfile_address){
 
  size_t spos;
  size_t substring_lenght;
  string slash = "/";
  spos = rawfile_address.rfind(slash);
  substring_lenght = 7;

  // Extracts the string "Run####" from the rawfile name
  string rawfile_name = rawfile_address.substr(spos+5,substring_lenght);
  // Sets the output file name
  string rootfile_address = "../RootData/" + rawfile_name + ".root";
  // Builds a .root file from a rawfile
  EventBuilderVFAT(rawfile_address.c_str(),rootfile_address.c_str(),100000000);
  // Sets the reco file name
  string recofile_address = "../RootData/" + rawfile_name + "_reco.root";
  // Creates a reco file using Offset_Settings.txt
  TFile file0(rootfile_address.c_str());
  TTree* t = dynamic_cast<TTree*>(file0.Get("rd51tb"));
  t->Process("Reco2d_ps.C+",recofile_address.c_str());

}
