my *.C and *.h file is generated by command MakeClass();
I added a read and write function (Write()) in it. I have tried using the MakeSelector method to generate the header and source files, unfortunately the error still appears when reading the Ntuple6.
this is my code.
I’m sorry I can’t provide the ROOT-data file as newbies can’t upload files
#ifndef Ntuple6_h
#define Ntuple6_h
#include <TROOT.h>
#include <TChain.h>
#include <TFile.h>
//Standard c++ header
#include <fstream>
#include <iostream>
using namespace std;
// Header file for the classes stored in the TTree if any.
#include "vector"
class Ntuple6 {
public :
TTree *fChain; //!pointer to the analyzed TTree or TChain
Int_t fCurrent; //!current Tree number in a TChain
fstream file1;
// Fixed size dimensions of array or collections stored in the TTree if any.
// Declaration of leaf types
Int_t EventID;
vector<double> *Px;
vector<double> *Py;
vector<double> *Pz;
// List of branches
TBranch *b_EventID; //!
TBranch *b_Px; //!
TBranch *b_Py; //!
TBranch *b_Pz; //!
Ntuple6(TTree *tree=0);
virtual ~Ntuple6();
virtual Int_t Cut(Long64_t entry);
virtual Int_t GetEntry(Long64_t entry);
virtual Long64_t LoadTree(Long64_t entry);
virtual void Init(TTree *tree);
virtual void Loop();
virtual Bool_t Notify();
virtual void Show(Long64_t entry = -1);
//
virtual Long64_t GetEntries();
virtual void Write();
};
#endif
#ifdef Ntuple6_cxx
Ntuple6::Ntuple6(TTree *tree) : fChain(0)
{
// if parameter tree is not specified (or zero), connect the file
// used to generate this class and read the Tree.
if (tree == 0) {
TFile *f = (TFile*)gROOT->GetListOfFiles()->FindObject("GGCM08.root");
if (!f || !f->IsOpen()) {
f = new TFile("GGCM08.root");
}
f->GetObject("Ntuple6",tree);
}
Init(tree);
}
Ntuple6::~Ntuple6()
{
if (!fChain) return;
delete fChain->GetCurrentFile();
}
Int_t Ntuple6::GetEntry(Long64_t entry)
{
// Read contents of entry.
if (!fChain) return 0;
return fChain->GetEntry(entry);
}
Long64_t Ntuple6::LoadTree(Long64_t entry)
{
// Set the environment to read one entry
if (!fChain) return -5;
Long64_t centry = fChain->LoadTree(entry);
if (centry < 0) return centry;
if (fChain->GetTreeNumber() != fCurrent) {
fCurrent = fChain->GetTreeNumber();
Notify();
}
return centry;
}
//
Long64_t Ntuple6::GetEntries()
{
Long64_t Nb;
if (!fChain) return 0;
Nb = fChain->GetEntriesFast();
return Nb;
}
void Ntuple6::Init(TTree *tree)
{
// The Init() function is called when the selector needs to initialize
// a new tree or chain. Typically here the branch addresses and branch
// pointers of the tree will be set.
// It is normally not necessary to make changes to the generated
// code, but the routine can be extended by the user if needed.
// Init() will be called many times when running on PROOF
// (once per file to be processed).
// Set object pointer
Px = 0;
Py = 0;
Pz = 0;
// Set branch addresses and branch pointers
if (!tree) return;
fChain = tree;
fCurrent = -1;
fChain->SetMakeClass(1);
fChain->SetBranchAddress("EventID", &EventID, &b_EventID);
fChain->SetBranchAddress("Px", &Px, &b_Px);
fChain->SetBranchAddress("Py", &Py, &b_Py);
fChain->SetBranchAddress("Pz", &Pz, &b_Pz);
Notify();
}
void Ntuple6::Write()
{
//write content of entry to .txt file.
// If entry is not specified, write current entry
if (!fChain) return;
//
file1.open("Ntuple6.txt");
cout<<"************---------open*****"<<endl;
Long64_t nentries = fChain->GetEntriesFast();
file1<<"EventID"<<"\t"<<"Px"<<"\t"<<"Py"<<"\t"<<"Pz"<<"\t"<<"Rxy"<<"\t"<<"x"<<"\t"<<"y"<<"\t"<<"z"<<"\t"<<"Rxyz"<<"\t"<<"Theta"<<"\t"<<"Phi"<<"\t"<<"LengthOfTrajectory"<<endl;
for (Long64_t jentry=0; jentry<nentries;jentry++)
{
fChain->GetEntry(jentry);
file1<<EventID<<endl;
double Length = 0;
Int_t NbofPoint = Px->size();
for(Int_t iPoint = 0; iPoint<NbofPoint;iPoint++)
{
if(iPoint==0)
{
double x,y,z;
x = Px->at(iPoint);
y = Py->at(iPoint);
z = Pz->at(iPoint);
file1<<" "<<iPoint<<"\t"<<x<<"\t"<<y<<"\t"<<z<<endl;
}
else
{
double x0,y0,z0,x1,y1,z1,Rxy,x,y,z,Rxyz,Theta,Phi;
x0 = Px->at(iPoint-1);
y0 = Py->at(iPoint-1);
z0 = Pz->at(iPoint-1);
x1 = Px->at(iPoint);
y1 = Py->at(iPoint);
z1 = Pz->at(iPoint);
Rxy = std::sqrt(x1*x1+y1*y1);
x = x1-x0;
y = y1-y0;
z = z1-z0;
Rxyz = std::sqrt(x*x+y*y+z*z);
Length+=Rxyz;
Theta = std::acos(z/Rxyz);
Phi = std::atan(y/x);
file1<<" "<<iPoint<<"\t"<<x1<<"\t"<<y1<<"\t"<<z1<<"\t"<<Rxy<<"\t"<<x<<"\t"<<y<<"\t"<<z<<"\t"<<Rxyz<<"\t"<<Theta<<"\t"<<Phi;
if(iPoint==(NbofPoint-1)) { file1<<"\t"<<Length<<endl; }
else{file1<<endl;}
}
}
}
cout<<"************---------complete*****"<<endl;
file1.close();
cout<<"************---------close*****"<<endl;
}
Bool_t Ntuple6::Notify()
{
// The Notify() function is called when a new file is opened. This
// can be either for a new TTree in a TChain or when when a new TTree
// is started when using PROOF. It is normally not necessary to make changes
// to the generated code, but the routine can be extended by the
// user if needed. The return value is currently not used.
return kTRUE;
}
void Ntuple6::Show(Long64_t entry)
{
// Print contents of entry.
// If entry is not specified, print current entry
if (!fChain) return;
fChain->Show(entry);
}
Int_t Ntuple6::Cut(Long64_t entry)
{
// This function may be called from Loop.
// returns 1 if entry is accepted.
// returns -1 otherwise.
return 1;
}
#endif // #ifdef Ntuple6_cxx
#define Ntuple6_cxx
#include "Ntuple6.h"
#include <TH2.h>
#include <TStyle.h>
#include <TCanvas.h>
void Ntuple6::Loop()
{
// In a ROOT session, you can do:
// root> .L Ntuple6.C
// root> Ntuple6 t
// root> t.GetEntry(12); // Fill t data members with entry number 12
// root> t.Show(); // Show values of entry 12
// root> t.Show(16); // Read and show values of entry 16
// root> t.Loop(); // Loop on all entries
//
// This is the loop skeleton where:
// jentry is the global entry number in the chain
// ientry is the entry number in the current Tree
// Note that the argument to GetEntry must be:
// jentry for TChain::GetEntry
// ientry for TTree::GetEntry and TBranch::GetEntry
//
// To read only selected branches, Insert statements like:
// METHOD1:
// fChain->SetBranchStatus("*",0); // disable all branches
// fChain->SetBranchStatus("branchname",1); // activate branchname
// METHOD2: replace line
// fChain->GetEntry(jentry); //read all branches
//by b_branchname->GetEntry(ientry); //read only this branch
if (fChain == 0) return;
Long64_t nentries = fChain->GetEntriesFast();
Long64_t nbytes = 0, nb = 0;
for (Long64_t jentry=0; jentry<nentries;jentry++) {
Long64_t ientry = LoadTree(jentry);
if (ientry < 0) break;
nb = fChain->GetEntry(jentry); nbytes += nb;
// if (Cut(ientry) < 0) continue;
}
}