Number of Entries

I am writing a program on random numbers where I will plot them using AMPT model with number of entries in TTree to be 10000. I tried it without random numbers on other examples , then its working okay. but when i am using the code below , it shows
Entries in tree: 1

gROOT->Reset();

  TFile f("firsttree.root","RECREATE");
  TTree* myTree = new TTree("marooz", "firsttree");
 
   Double_t multiplicity;
   Double_t eta;
   Double_t phi;
      
	  myTree->Branch("m", &multiplicity, "m/D");
      myTree->Branch("e", &eta, "e/D");
      myTree->Branch("p", &phi, "p/D");

   Int_t n=10000;
   TRandom rGen(0);
 
 for (Int_t i=0; i < n; i++) 
 {
   multiplicity = rGen.Rndm() * (1000-20) + 20; 
   eta = rGen.Rndm() * 2 - 1;
   phi = rGen.Rndm() * 6;
      
	  myTree->Fill();
  }
  
  cout<<"Entries in tree: "<<myTree->GetEntries()<<endl;

I too got that:

Entries in tree: 1

it’s only when I wrote it like that (at the ROOT prompt):

for (Int_t i=0; i < n; i++) {
   multiplicity = rGen.Rndm() * (1000-20) + 20; 
   eta = rGen.Rndm() * 2 - 1;
   phi = rGen.Rndm() * 6;
      
	  myTree->Fill();
}

that it worked.

1 Like

Exact versions of your Operating Systems and your ROOTs are needed here, I think.

Ubuntu 14.04 and root 6.12 .
Is there any other way to write it ?

This is Ubuntu 14.04 / x86_64 (gcc 4.8.4) and CentOS 7 / x86_64 (gcc 4.8.5) and “v5-34-00-patches” and “6.12/06”. In all cases, I get 10000.

BTW. Do NOT execute gROOT->Reset(); unless you REALLY know what it does.

The solution to this problem is available here in this thread, Help me with TFile.

I suspect one gets the result the OP got in the first place when one is copy-pasting the original quoted code snippet in a ROOT prompt.

the { of the for is on the next line so it seems as for ROOT-Cling, it’s a for-loop with an empty body (probably a collateral damage of the automatic ; insertion ?) followed by a scope containing the tree fill (so executed only once.)

of course, it’s all just crystal-ball debugging… :slight_smile:

Hi,

yes. To have it working with a C/P on the prompt, the scope has to be opened on the same line of the for.

Cheers,
D

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.