# Problem filling a tntuple object

**URL:** <https://root-forum.cern.ch/t/problem-filling-a-tntuple-object/36326>\
**Category:** Newbie\
**Created:** [October 12, 2019, 4:06am UTC](https://root-forum.cern.ch/t/problem-filling-a-tntuple-object/36326 "2019-10-12T04:06:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![thuwang-xz18](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/e274bd/32.png) [@thuwang-xz18](https://root-forum.cern.ch/u/thuwang-xz18)\
**Post date:** [October 12, 2019, 4:06am UTC](https://root-forum.cern.ch/t/problem-filling-a-tntuple-object/36326/1 "2019-10-12T04:06:55Z")

</div>

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.

```auto
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:

```auto
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

* * *

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [October 12, 2019, 7:15am UTC](https://root-forum.cern.ch/t/problem-filling-a-tntuple-object/36326/2 "2019-10-12T07:15:46Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![thuwang-xz18](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/e274bd/32.png) [@thuwang-xz18](https://root-forum.cern.ch/u/thuwang-xz18)\
**Post date:** [October 12, 2019, 7:57am UTC](https://root-forum.cern.ch/t/problem-filling-a-tntuple-object/36326/4 "2019-10-12T07:57:12Z")

</div>

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