# Can't get the tree content from the root file

**URL:** <https://root-forum.cern.ch/t/cant-get-the-tree-content-from-the-root-file/36272>\
**Category:** Newbie\
**Created:** [October 9, 2019, 7:33am UTC](https://root-forum.cern.ch/t/cant-get-the-tree-content-from-the-root-file/36272 "2019-10-09T07:33:49Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ozgur\_Akcali](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ozgur_akcali/32/19101_2.png) [@Ozgur\_Akcali](https://root-forum.cern.ch/u/Ozgur_Akcali)\
**Post date:** [October 9, 2019, 7:33am UTC](https://root-forum.cern.ch/t/cant-get-the-tree-content-from-the-root-file/36272/1 "2019-10-09T07:33:49Z")

</div>

Hi,  
I’m trying to convert ORTEC-MAESTRO files to root files by adding required information on tree branches. When I check the root file via TBrowser, I get/see the stored values but in the batch mode nothing comes back.

\_ROOT Version: 5.34/38  
\_Platform: Win10  
\_Compiler: Cint

* * *

```auto
void okuw(){
	char mSPEC_ID[256], //specinfo
				mDATE_MEA[256]; //datetime
	Int_t mInitialCh,
				mFinalCh,
				*mData,
				mLiveTime, //MEAS_TIM
				mRealTime, 
				mDataSize;
	Float_t	mEnCal[3], 
				mShpCal[3];

	TH1F	*histo=new TH1F();
		histo->SetName("histo");
	TFile *mActiveFile = new TFile("MCA.root","RECREATE");
	TTree *mMCATree = new TTree("MCATree","Maestro File");
		mMCATree->Branch("mLiveTime",&mLiveTime,"mLiveTime/I");
		mMCATree->Branch("mRealTime",&mRealTime,"mRealTime/I");
		mMCATree->Branch("mDataSize",&mDataSize,"mDataSize/I");
		mMCATree->Branch("mEnCal",mEnCal,"mEnCal[3]/F");
		mMCATree->Branch("mShpCal",mShpCal,"mShpCal[3]/F");
		mMCATree->Branch("histo",histo,128000,0);
		mMCATree->Branch("mSPEC_ID",mSPEC_ID,"mSPEC_ID[256]/C");
		mMCATree->Branch("mDATE_MEA",mDATE_MEA,"mDATE_MEA[256]/C");

	Int_t i;
	char linex[256];
	ifstream	in;	
// https://drive.google.com/file/d/1cseGkus91ahrkL541Ixo8Z9c1vtvbqIf/view?usp=sharing
		in.open("Eu152_Pb_10.Spe"); 
	TString FLine(linex);
	while(1) {
		in.getline(linex,256);
		FLine=linex;

		if (FLine.Contains("$SPEC_ID:"))	{
			in.getline(mSPEC_ID,256);
			mSPEC_ID[strlen(mSPEC_ID)]='\0';
		}
		if (FLine.Contains("$DATE_MEA:")){
			in.getline(mDATE_MEA,256);
			mDATE_MEA[strlen(mDATE_MEA)]='\0';
		}
		if (FLine.Contains("$MEAS_TIM:"))	in >> mLiveTime >> mRealTime;
		if (FLine.Contains("$DATA:")) {
			in >> mInitialCh >> mFinalCh;
			mDataSize = (mFinalCh - mInitialCh) + 1;
			mData = new Int_t[mDataSize];
			for (i=mInitialCh; i<=mFinalCh; i++){ in >> mData[i];}
		}
		if (FLine.Contains("$MCA_CAL:")){	
			in.getline(linex, 256);
			in >> mEnCal[0] >> mEnCal[1] >> mEnCal[2] ;
		}
		if (FLine.Contains("$SHAPE_CAL:")){	
			in.getline(linex, 256);
			in >> mShpCal[0] >> mShpCal[1] >> mShpCal[2] ;
		}
		if (!in.good()) break;
	}
	
	cout << mSPEC_ID << endl
	     << mDATE_MEA << endl
		  << mInitialCh << endl
	     << mFinalCh << endl
	     << mLiveTime << endl
	     << mRealTime << endl
	     << mDataSize << endl
	     << mEnCal[0] << " " << mEnCal[1] << " " << mEnCal[2] << " " << endl
	     << mShpCal[0] << " " << mShpCal[1] << " " << mShpCal[2] << " " << endl;
	in.close();	
	
	histo->SetTitle(mSPEC_ID);
	histo->SetBins(mDataSize-1, (Float_t)mInitialCh+0.5, (Float_t)mFinalCh+0.5);
	histo->GetXaxis()->SetTitle("Channel (Ch)");
	histo->GetYaxis()->SetTitle("dN/dCh");
	for (i=mInitialCh;i<=mFinalCh;i++){
		histo->SetBinContent(i, (Float_t)mData[i]);
	}

	mMCATree->Fill();
	mMCATree->Print();
	cout << "#Branch : " << mMCATree->GetNbranches() << endl 
			<< "#Entries: " << mMCATree->GetEntries() << endl 	
			<< "#Bins : " << histo->GetEntries() << endl << endl; 
	mMCATree->Write();
	mActiveFile->Close();

}
void okur(){
	char rSPEC_ID[256], //specinfo
				rDATE_MEA[256]; //datetime
	Int_t rLiveTime, //MEAS_TIM
				rRealTime, 
				rDataSize;
	Float_t	rEnCal[3], 
				rShpCal[3];

	TH1F	*rhisto;
	TFile *rActiveFile = new TFile("MCA.root","READ");
	TTree *rMCATree = (TTree*)rActiveFile->Get("MCATree");
		rMCATree->SetBranchAddress("mLiveTime",&rLiveTime);
		rMCATree->SetBranchAddress("mRealTime",&rRealTime);
		rMCATree->SetBranchAddress("mDataSize",&rDataSize);
		rMCATree->SetBranchAddress("mEnCal",rEnCal);
		rMCATree->SetBranchAddress("mShpCal",rShpCal);
		rMCATree->SetBranchAddress("histo",&rhisto);
		rMCATree->SetBranchAddress("mSPEC_ID",rSPEC_ID);
		rMCATree->SetBranchAddress("mDATE_MEA",rDATE_MEA);
	rMCATree->GetEntry(1);
	rMCATree->Print();
	rMCATree->Show();

	cout << endl
			<< "#Branch : " << rMCATree->GetNbranches() << endl 
			<< "#Entries: " << rMCATree->GetEntries() << endl 	
			<< "#Bins : " << rhisto->GetEntries() << endl << endl; 

	cout << rSPEC_ID << endl
	     << rDATE_MEA << endl
	     << rLiveTime << endl
	     << rRealTime << endl
	     << rDataSize << endl
	     << rEnCal[0] << " " << rEnCal[1] << " " << rEnCal[2] << " " << endl
	     << rShpCal[0] << " " << rShpCal[1] << " " << rShpCal[2] << " " << endl;
	
	rActiveFile->Close();		
}

```

---

<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 9, 2019, 8:06am UTC](https://root-forum.cern.ch/t/cant-get-the-tree-content-from-the-root-file/36272/2 "2019-10-09T08:06:46Z")

</div>

At least (in “okur”): `TH1F *rhisto = 0;`

---

<div class="post-metadata">

**Author:** ![Ozgur\_Akcali](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ozgur_akcali/32/19101_2.png) [@Ozgur\_Akcali](https://root-forum.cern.ch/u/Ozgur_Akcali)\
**Post date:** [October 9, 2019, 8:09am UTC](https://root-forum.cern.ch/t/cant-get-the-tree-content-from-the-root-file/36272/3 "2019-10-09T08:09:03Z")

</div>

Thanks for reply. I’ve tried it but still nothing.

---

<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 9, 2019, 8:10am UTC](https://root-forum.cern.ch/t/cant-get-the-tree-content-from-the-root-file/36272/4 "2019-10-09T08:10:27Z")

</div>

I guess, you should have (in “okuw”):

```auto
mMCATree->Branch("mSPEC_ID", mSPEC_ID, "mSPEC_ID/C");
mMCATree->Branch("mDATE_MEA", mDATE_MEA, "mDATE_MEA/C");

```

---

<div class="post-metadata">

**Author:** ![Ozgur\_Akcali](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ozgur_akcali/32/19101_2.png) [@Ozgur\_Akcali](https://root-forum.cern.ch/u/Ozgur_Akcali)\
**Post date:** [October 9, 2019, 8:18am UTC](https://root-forum.cern.ch/t/cant-get-the-tree-content-from-the-root-file/36272/5 "2019-10-09T08:18:53Z")

</div>

The output of TTree::Show() for okuw()

======\> EVENT:-1  
mLiveTime = 1177  
mRealTime = 1200  
mDataSize = 2048  
mEnCal = -16.561,  
0.723679, 1.4497e-005  
mShpCal = 0,  
0, 0  
histo = (TH1F\*)0x2f6e7e0  
mSPEC\_ID = Eu152 Pb m=536.6 t=1  
mDATE\_MEA = 08/09/2019 14:15:12

and for okur()

======\> EVENT:-1  
mLiveTime = 0  
mRealTime = 0  
mDataSize = 0  
mEnCal = 2.6439e+020,  
4.51325e+027, 1.41531e-043  
mShpCal = 3.32031e+012,  
3285.95, 6.16993e-039  
histo = (TH1F\*)0x3305738  
mSPEC\_ID = pä0­┤ m=536.6 t=1  
mDATE\_MEA = Ém/­┤AL:

Dropping the array didn’t make any change.

---

<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 9, 2019, 8:21am UTC](https://root-forum.cern.ch/t/cant-get-the-tree-content-from-the-root-file/36272/6 "2019-10-09T08:21:55Z")

</div>

Try: `rMCATree->GetEntry(0);`

---

<div class="post-metadata">

**Author:** ![Ozgur\_Akcali](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ozgur_akcali/32/19101_2.png) [@Ozgur\_Akcali](https://root-forum.cern.ch/u/Ozgur_Akcali)\
**Post date:** [October 9, 2019, 8:25am UTC](https://root-forum.cern.ch/t/cant-get-the-tree-content-from-the-root-file/36272/7 "2019-10-09T08:25:29Z")

</div>

Solved, thanks for help.
