Can't GetEntries() from histogram

Hello,

I created a script (extract.C) that until yesterday was running well on lxplus, and today I get strange identification error:

**error:** **use of undeclared identifier 'h_output'**
f_entries1 = h_output->GetEntries();

This is a simplified version of it (if needed I will attach the full one):

void extract()
{
    string file_name = "file.root";
    TFile *_file0 = new TFile(file_name.c_str());
    TCanvas *c_output = new TCanvas();
    c_output->cd();
    TTree *t  = new TTree ();
    _file0->GetObject("mytree", t);
    t->Draw("branch>>h_output(100,0,1)");
    c_output->SaveAs("newFile.root");
    FILE* f_out = fopen(("h_output.csv").c_str() , "w+");
    float f_entries1 = 0.0;
    f_entries1 = h_output->GetEntries();
   fprintf(f_out,"%f,\n",f_entries1);
    gROOT->ProcessLine(".q");
}

I am not sure what I changed, but I tried to recover from afs the script that ran yesterday well, and I still get errors.

Any advise will be super helpful :slight_smile: And apologies if the mistake is silly.
Many thanks

Try to add

   TH1F *h_output = (TH1F *)gDirectory->Get("h_output");

before

   f_entries1 = h_output->GetEntries();

Many thanks for the suggestion :slight_smile:
Unfortunately adding this made the code crash (the hints for the crash below).

I also tried to add the following at the beginning of the code, but it didn’t help:
#include <TF1.h>
#include <TMath.h>
#include <TFile.h>
#include <stdio.h>

Looking forward for more ideas :pray: :blush:

— crash details —
#5 0x00007f41ce962560 in _nl_C_LC_MEASUREMENT () from /lib64/libc.so.6
#6 0x00007f41cf804168 in ?? ()
#7 0x0000000001fe0b00 in ?? ()
#8 0x00007f41cf6fcdee in _dl_fixup () from /lib64/ld-linux-x86-64.so.2
#9 0x00007f41cf7049ea in _dl_runtime_resolve_xsave () from /lib64/ld-linux-x86-64.so.2
#10 0x00007f41cee873f8 in std::string::_Rep::_S_empty_rep_storage () from /lib64/libstdc++.so.6
#11 0x00000000030a3ec8 in ?? ()
#12 0x0000000002db00b0 in ?? ()
#13 0x00007fff0d5ebcf0 in ?? ()
#14 0x00007f41cec3dcd9 in std::string::_Rep::_S_create(unsigned long, unsigned long, std::allocator const&) () from /lib64/libstdc++.so.6
#15 0x00007f41cf0da4fe in char* std::string::_S_construct<char const*>(char const*, char const*, std::allocator const&, std::forward_iterator_tag) () from /usr/lib64/root/libCore.so.6.22
#16 0x0000000002be5bc8 in ?? ()
#17 0x0000000001f94600 in ?? ()
#18 0xf6f4b02152133200 in ?? ()
#19 0x000000000222d8a0 in ?? ()
#20 0x0000000002cfc188 in ?? ()
#21 0x00007fff0d5ebd20 in ?? ()
#22 0x00007f41cf8020ba in ?? ()
#23 0x0000000000000020 in ?? ()
#24 0x0000000001f8d690 in ?? ()
#25 0x0000000002ed8040 in ?? ()
#26 0x00007f41c923fedc in void std::vector<TCling::MutexStateAndRecurseCount, std::allocatorTCling::MutexStateAndRecurseCount >::_M_emplace_back_aux<>() () from /usr/lib64/root/libCling.so

#27 0x00007f41c920ba90 in TCling::RewindInterpreterMutex() () from /usr/lib64/root/libCling.so
#28 0x00007f41c9286627 in cling::MultiplexInterpreterCallbacks::EnteringUserCode() () from /usr/lib64/root/libCling.so
#29 0x00007f41c92e3ecb in cling::IncrementalExecutor::executeWrapper(llvm::StringRef, cling::Value*) const () from /usr/lib64/root/libCling.so
#30 0x00007f41c928798c in cling::Interpreter::RunFunction(clang::FunctionDecl const*, cling::Value*) () from /usr/lib64/root/libCling.so
#31 0x00007f41c928873e in cling::Interpreter::EvaluateInternal(std::string const&, cling::CompilationOptions, cling::Value*, cling::Transaction**, unsigned long) () from /usr/lib64/root/libCling.so
#32 0x00007f41c9288d32 in cling::Interpreter::echo(std::string const&, cling::Value*) () from /usr/lib64/root/libCling.so
#33 0x00007f41c932a7e9 in cling::MetaSema::actOnxCommand(llvm::StringRef, llvm::StringRef, cling::Value*) () from /usr/lib64/root/libCling.so
#34 0x00007f41c93371c2 in cling::MetaParser::isXCommand(cling::MetaSema::ActionResult&, cling::Value*) () from /usr/lib64/root/libCling.so
#35 0x00007f41c933758e in cling::MetaParser::isCommand(cling::MetaSema::ActionResult&, cling::Value*) () from /usr/lib64/root/libCling.so
#36 0x00007f41c9324841 in cling::MetaProcessor::process(llvm::StringRef, cling::Interpreter::CompilationResult&, cling::Value*, bool) () from /usr/lib64/root/libCling.so
#37 0x00007f41c91fe2ea in HandleInterpreterException(cling::MetaProcessor*, char const*, cling::Interpreter::CompilationResult&, cling::Value*) () from /usr/lib64/root/libCling.so

Just to report that the issue has been solved.
In case this helps someone, this is the final code:

#include <TMath.h>
#include <TFile.h>
#include <stdio.h>
void extract()
{
    string file_name = "file.root";
    TFile *_file0 = new TFile(file_name.c_str());
    TH1F* h_nn_output=new TH1F("h_nn_output","h_nn_output",100,0,1);
     float f_entries1 = 0.0;
     TTree *t  = new TTree ();
    _file0->GetObject("mytree", t);
    TCanvas *c_output = new TCanvas();
    c_output->cd();
    t->Draw("branch>>h_output");
    f_entries1 = h_output->GetEntries();
    fprintf(f_out,"%f,\n",f_entries1);
    gROOT->ProcessLine(".q");
}
1 Like

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