Hi all
I have a root file with a tree, I want to clone that tree, use some of the variables stored there, calculate something and then save a new root file with the new tree including the new variables.
I think I managed to do it local, but when I tried to run in a batch system I am getting or an empty new file, or different sets of errors (mostly regarding the stored of the branch).
Here is a simplify version of my code:
#include <iostream>
#include <limits>
#include <sys/time.h>
#include "TTree.h"
#include "TChain.h"
#include <TROOT.h>
#include <TFile.h>
#include <TTreeReader.h>
#include <TTreeReaderValue.h>
#include <TTreeReaderArray.h>
using namespace std;
typedef vector<TLorentzVector> TLorentzVectors;
typedef vector<double> doubles;
const TLorentzVector makeVector(double pt, double eta, double phi, double mass) {
TLorentzVector lv;
lv.SetPtEtaPhiM(pt, eta, phi, mass);
return lv;
}
int main(int argc, char *argv[]) {
auto oldFile = TFile::Open( "inNameFile.root" );
TTree *newtree = (TTree*)oldFile->Get("tree");
TTreeReader newreader( "tree", oldFile );
TFile *newfile = new TFile("outNameFile.root","recreate");
TTreeReaderValue<Int_t> nleps = {newreader, "nleps"};
TTreeReaderArray<Float_t> leps_pt = {newreader, "leps_pt"};
TTreeReaderArray<Float_t> leps_eta = {newreader, "leps_eta"};
TTreeReaderArray<Float_t> leps_phi = {newreader, "leps_phi"};
TTreeReaderArray<Float_t> leps_mass = {newreader, "leps_mass"};
double newVar;
newtree->Branch("newVar", &newVar, "newVar/F");
TH1F hnewVar("newVar", "newVar", 10, 0, 1);
int dummy=0;
while (newreader.Next()) {
if ( *nleps == 1 ) {
if ((++dummy)==100) break;
TLorentzVectors leptons;
TLorentzVector lepton = makeVector( leps_pt[0], leps_eta[0], leps_phi[0], leps_mass[0] );
newVar = lepton.M(); /// for instance, the variable is not important
hnewVar.Fill( lepton.M() );
} else {
newVar = -999;
}
newtree->Fill();
}
newfile->cd();
newtree->Write();
hnewVar.Write();
newfile->Close();
// return 0;
}
Is it something that I am doing wrong? I found it weird that running local it works fine.
This is the error that I am getting now:
Error in <TBranch::TBranch::WriteBasketImpl>: basket's WriteBuffer failed.
Error in <TBranch::TBranch::Fill>: Failed to write out basket.
Error in <TTree::Fill>: Failed filling branch:tree.jets_pt, nbytes=-1, entry=1938
This error is symptomatic of a Tree created as a memory-resident Tree
Instead of doing:
TTree *T = new TTree(...)
TFile *f = new TFile(...)
you should do:
TFile *f = new TFile(...)
TTree *T = new TTree(...)
Error in <TBranch::TBranch::WriteBasketImpl>: basket's WriteBuffer failed.
cheers,
ROOT Version: 6.10/09
Platform: Linux
Compiler: Not Provided