Problem in filling

Dear Developers,

Kindly check this macro,it is not getting entries from tree to plot a simple variable met_pt. I is giving a warning
" Warning: Automatic variable W_mass is allocated missing.cc:39:
Error in TTree::Fill: Failed filling branch:AnaTree.tzq_w, nbytes=-1, entry=430521
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(…)
"
and donot get enteries.

The macro is

#include <TROOT.h>
#include <TH1.h>
#include <TH2.h>
#include <TSystem.h>
#include <vector>
#include <TLorentzVector.h>
#include <math.h>
#include "TMath.h"
#include <iostream>
#include <fstream>
#include <algorithm>
#include <map>
#include <TFileMerger.h>

void missing()

{

   TFile *f = new TFile("May26_2.root");
   TTree *t = (TTree*)f->Get("demo/AnaTree");
   TH1F *h4 = new TH1F("h1","pt",100,0,200);
using namespace std;
Float_t  met_pt, met_phi;

          t->SetBranchAddress("met_pt",met_pt);
          t->SetBranchAddress("met_phi",met_phi);

TLorentzVector tmp4(0,0,0,0);

 for (int i=0;i<10000;i++)   {

 TLorentzVector tmp4;
         t->GetEntry(i);
//float met_pt=0;
tmp4.SetPtEtaPhiM(met_pt, 0, met_phi, 0);
//tmp4.SetE(tmp4.Pt());
       W_mass = tmp4.M();
t->Fill();
h4->Fill(W_mass);

}
   TCanvas *c1 = new TCanvas("c1","c1",800,600) ;
h4->Draw();
}

Root file path is /afs/cern.ch/user/n/nmajeed/public

Cheers,
Nab

Hi,

you are opening a file in read mode and taking a tree out of it to then try to fill it. This is ill defined. If you want to write a new tree, open the file in write or update mode: https://root.cern.ch/doc/master/classTFile.html#aadd8e58e4d010c80b728bc909ac86760

Cheers,
D

But this code is working. I used the same method

lorentz32.cc (3.3 KB)

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