Problem filling a tntuple object

Hi guys…
I did some simulation and saved the data in a .root file and I tried to use a TNtuple object to draw the charge distribution in space.

void readpc(TFile* file,std::string detector)
{
    //prepare to read tree from file
    TTree* PropagatedCharge_tree=static_cast<TTree*>(file->Get("PropagatedCharge"));
    TBranch* PropagatedCharge_branch=PropagatedCharge_tree->FindBranch(detector.c_str());
    std::vector<allpix::PropagatedCharge*> propagated_charges;
    PropagatedCharge_branch->SetObject(&propagated_charges);
    //get charge distribution information of propagated charges
    unsigned long pc_total_entry_count=0;
    for(int i=0;i<PropagatedCharge_tree->GetEntries();++i)
    {
	PropagatedCharge_tree->GetEntry(i);
	unsigned long pc_event_entry_count=0;
	for(auto& propagated_charge:propagated_charges)
        {
		pc_total_entry_count++;
		pc_event_entry_count++;
        }
	std::cout<<"The number of the propagated charge entries in event "<<i<<" is "<<pc_event_entry_count<<std::endl;
    }
    TCanvas* cpc=new TCanvas("cpc","",800,600);         
    TNtuple* npc=new TNtuple("n","n","x:y:z:charge");
    npc->SetMarkerStyle(20);
    npc->SetMarkerSize(0.5);
    unsigned long pc_total_charge_count=0;
    for(int i=0;i<PropagatedCharge_tree->GetEntries();++i)
    {
	PropagatedCharge_tree->GetEntry(i);
	float x,y,z;                      //local position
	unsigned long q;                  //charge
	unsigned long pc_event_charge_count=0;
	for(auto& propagated_charge:propagated_charges)
	{
		x=propagated_charge->getLocalPosition().X();
		y=propagated_charge->getLocalPosition().Y();
		z=propagated_charge->getLocalPosition().Z();
		q=propagated_charge->getCharge();
		npc->Fill(x,y,z,q);
		pc_total_charge_count+=q;
		pc_event_charge_count+=q;
	}
	std::cout<<"The number of the propagated charges in event "<<i<<" is "<<pc_event_charge_count<<std::endl;
    }
    std::cout<<"The total number of propageted charge entries is "<<pc_total_entry_count<<std::endl;
    std::cout<<"The total number of propageted charges is "<<pc_total_charge_count<<std::endl;
    cpc->cd();
    npc->Draw("x:y:z:charge");
    std::string pc="image/general/pc_distribution_"+std::to_string(PropagatedCharge_tree->GetEntries())+".png";
    cpc->SaveAs(pc.c_str());
    return;  
}

But an error occurrd:

Error in TBranch::TBranch::WriteBasketImpl: basket’s WriteBuffer failed.
Error in TBranch::TBranch::Fill: Failed to write out basket.
Error in TNtuple::Fill: Failed filling branch:n.charge, nbytes=-1, entry=7982
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(…)

I dont know what the problem is…Can someone offer some help?
Thanks~

_ROOT Version:_6.16.00
_Platform:_ubuntu 16.04
Compiler: Not Provided


1 Like

Right before TNtuple* npc=new TNtuple(...); try to add gROOT->cd();

BTW. When you post “source code” or “output” here, do remember to enclose them into two lines which contain just three characters ``` (see how your post has been edited above).

The problem is solved! Thank you~
BTW. I will remember to do that. :grinning: