Hi, You have several concept that are mis-used. If a branch is named 'NELE', it's name is a string that is only usable when going through the TTree interface. Naming a variable with the same name is insufficient to connect it to the data in the TTree. Also you never 'read' the TTree. You do not loop over the entries. TTree::Draw (and TTree::Process) handles this looping for you. If you need to call TH1F::Fill directly you have to do the looping. Your code could look like what I appended at the end. However there are simplier alternative (for the simple cases), for example read the documentation for TTree::Draw and you will see that you can fill an histogram that you have allocated your self by naming it in the Draw command. fChain = ... some initilization is missing here ... TFile f("filename.root","new"); TH1F *ne_pointer = new TH1F("ne", "Num electrons",80,0,20); fChain->Draw("NELE>>ne"); f.Write(); After writing the code below, I have a feeling that you actually extracted this code from your original code and that your code was extracted from a more complex and complete example. It looks like you extracted this code from the result of running MakeClass and or MakeSelector on your TTree. If this is the case, note that you have not extracted some essential part that do the proper definiton, setting and looping. In the future, please provide a full example so that we could be more helpful. Cheers, Philippe. // your code updated: { // create an empty root file first TFile f("filename.root","new"); // create a histogram and fill it from a tre TH1F *ne = new TH1F("ne", "Num electrons",80,0,20); TTree *fChain; //!pointer to the analyzed TTree or TChain Int_t fCurrent; //!current Tree number in a TChain fChain = ... something is missing here ... Int_t NELE_var; //define the type of NELE (the name of a leave in the tree) fChain->SetBranchAddress("NELE",&NELE_var); Int_t nentries = Int_t(fChain->GetEntriesFast()); for (Int_t entryInChain=0; entryInChain<nentries; entryInChain++) { Int_t entryInTree = LoadTree(entryInChain); if (entryInTree < 0) break; fChain->GetEntry(entryInChain); ne->Fill(NELE); //fill data from a tree to a histogram } ne->Write(); //write the histogram into the empty root file created } PS. I strongly recommend that you read the full User's Guide and take a look at the file in $ROOTSYS/tutorials. -----Original Message----- From: owner-roottalk@pcroot.cern.ch [mailto:owner-roottalk@pcroot.cern.ch]On Behalf Of tcli Sent: Tuesday, August 05, 2003 5:08 PM To: roottalk@pcroot.cern.ch Subject: [ROOT] how to create histograms using info from TTree Dear root users, The simplest way to histogram data from a tree is TTree::Draw. But I'd like to create histograms and then fill them with the leaves of a tree. I tried with the following macro: { // create an empty root file first TFile f("filename.root","new"); // create a histogram and fill it from a tree TH1F *ne = new TH1F("ne", "Num electrons",80,0,20); TTree *fChain; //!pointer to the analyzed TTree or TChain Int_t fCurrent; //!current Tree number in a TChain Int_t NELE; //define the type of NELE (the name of a leave in the tree) ne->Fill(NELE); //fill data from a tree to a histogram ne->Write(); //write the histogram into the empty root file created } However, the above commands give me the wrong histogram with only one entry (supposed to be 1000 entries) when I open the histogram with TBrowser. So is there any suggestion? Thnaks in advance! tcli ################################################################# ################################################################# ################################################################# ##### ##### ##### ################################################################# ################################################################# #################################################################
This archive was generated by hypermail 2b29 : Thu Jan 01 2004 - 17:50:14 MET