I got a problem doing histogram tutorials

Hi experts!
I am a root beginner. I am working on a histogram tutorial. To solve the example called hbars.c, I type TBrowser b("cernstaff.C); to open the cernstaff.C file, and then "Error in : Trying to dereference null pointer or trying to call routine taking non-null arguments.

Execution of your code was aborted.
In file included from input_line_50:1:
/home/hyungjoe/6.22.06/tutorials/tree/cernstaff.C:24:4: warning: null passed to a callee that requires a non-null argument [-Wnonnull]
    T->Draw("Nation:Division>>hN","","text");"

What is wrong?

Thanks!


Please read tips for efficient and successful posting and posting code

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


TBrowser open a file browser. It does not execute .C macros.
To execute cernstaff.C do:

root [] .x cernstaff.C

Thanks for reply!
But i still get the same error message.

Can you copy/paste here what you typed and what output error you get ?

root [0] .x cernstaff.C
Error in TApplication::ExecuteFile: macro cernbuild.C not found in path /snap/root-framework/146/usr/local/tutorials/tree/cernstaff.C
Error in TFile::TFile: file /home/hyungjoe/6.22.06/tutorials/tree/cernstaff.root does not exist
Generating stack trace…
0x00007fa33f77145b in
0x00007fa33f775053 in
0x00007fa33a245028 in cling::IncrementalExecutor::executeWrapper(llvm::StringRef, cling::Value*) const + 0x148 from /snap/root-framework/146/usr/local/lib/libCling.so
0x00007fa33a1ad5f9 in cling::Interpreter::RunFunction(clang::FunctionDecl const*, cling::Value*) + 0xc9 from /snap/root-framework/146/usr/local/lib/libCling.so
0x00007fa33a1af049 in cling::Interpreter::EvaluateInternal(std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, cling::CompilationOptions, cling::Value*, cling::Transaction**, unsigned long) + 0x209 from /snap/root-framework/146/usr/local/lib/libCling.so
0x00007fa33a2bc245 in cling::MetaSema::actOnxCommand(llvm::StringRef, llvm::StringRef, cling::Value*) + 0x445 from /snap/root-framework/146/usr/local/lib/libCling.so
0x00007fa33a2ca275 in cling::MetaParser::isXCommand(cling::MetaSema::ActionResult&, cling::Value*) + 0x1b5 from /snap/root-framework/146/usr/local/lib/libCling.so
0x00007fa33a2cb76e in cling::MetaParser::isCommand(cling::MetaSema::ActionResult&, cling::Value*) + 0x9e from /snap/root-framework/146/usr/local/lib/libCling.so
0x00007fa33a2b51d9 in cling::MetaProcessor::process(llvm::StringRef, cling::Interpreter::CompilationResult&, cling::Value*, bool) + 0x189 from /snap/root-framework/146/usr/local/lib/libCling.so
0x00007fa339fc37e5 in from /snap/root-framework/146/usr/local/lib/libCling.so
0x00007fa339fc4278 in from /snap/root-framework/146/usr/local/lib/libCling.so
0x00007fa339fc7dde in from /snap/root-framework/146/usr/local/lib/libCling.so
0x00007fa33fa6d6c2 in TApplication::ExecuteFile(char const*, int*, bool) at TApplication.cxx:? from /snap/root-framework/146/usr/local/lib/libCore.so
0x00007fa33fa6ce20 in TApplication::ProcessFile(char const*, int*, bool) + 0x34 from /snap/root-framework/146/usr/local/lib/libCore.so
0x00007fa33fa6cc0b in TApplication::ProcessLine(char const*, bool, int*) + 0xda9 from /snap/root-framework/146/usr/local/lib/libCore.so
0x00007fa33fe2c8fb in TRint::ProcessLineNr(char const*, char const*, int*) + 0x24d from /snap/root-framework/146/usr/local/lib/libRint.so
0x00007fa33fe2c022 in TRint::HandleTermInput() + 0x344 from /snap/root-framework/146/usr/local/lib/libRint.so
0x00007fa33fe298b9 in TTermInputHandler::Notify() + 0x35 from /snap/root-framework/146/usr/local/lib/libRint.so
0x00007fa33fe2e1bf in TTermInputHandler::ReadNotify() + 0x29 from /snap/root-framework/146/usr/local/lib/libRint.so
0x00007fa33fc0def7 in TUnixSystem::CheckDescriptors() + 0x121 from /snap/root-framework/146/usr/local/lib/libCore.so
0x00007fa33fc0d154 in TUnixSystem::DispatchOneEvent(bool) + 0x178 from /snap/root-framework/146/usr/local/lib/libCore.so
0x00007fa33fae701b in TSystem::InnerLoop() + 0x45 from /snap/root-framework/146/usr/local/lib/libCore.so
0x00007fa33fae6d8c in TSystem::Run() + 0xf4 from /snap/root-framework/146/usr/local/lib/libCore.so
0x00007fa33fa6d886 in TApplication::Run(bool) + 0x54 from /snap/root-framework/146/usr/local/lib/libCore.so
0x00007fa33fe2b332 in TRint::Run(bool) + 0x8da from /snap/root-framework/146/usr/local/lib/libRint.so
0x0000564ea8fcd241 in from /snap/root-framework/146/usr/local/bin/root.exe
0x00007fa33f3a80b3 in __libc_start_main + 0xf3 from /lib/x86_64-linux-gnu/libc.so.6
0x0000564ea8fcd10e in _start + 0x2e from /snap/root-framework/146/usr/local/bin/root.exe
Error in : Trying to dereference null pointer or trying to call routine taking non-null arguments.
Execution of your code was aborted.
In file included from input_line_8:1:
/home/hyungjoe/6.22.06/tutorials/tree/cernstaff.C:24:4: warning: null passed to a callee that requires a non-null argument [-Wnonnull]
T->Draw(“Nation:Division>>hN”,"",“text”);
^
root [1]
root [1] TCanvas *hbars() {
root (cont’ed, cancel with .@) [2] // Try to open first the file cernstaff.root in tutorials/tree directory
root (cont’ed, cancel with .@) [3] TString filedir = gROOT->GetTutorialDir();
root (cont’ed, cancel with .@) [4] filedir += TString("/tree/");
root (cont’ed, cancel with .@) [5] TString filename = “cernstaff.root”;
root (cont’ed, cancel with .@) [6] bool fileNotFound = gSystem->AccessPathName(filename); // note opposite return code
root (cont’ed, cancel with .@) [7]
root (cont’ed, cancel with .@) [7] // If file is not found try to generate it uing the macro tree/cernbuild.C
root (cont’ed, cancel with .@) [8] if (fileNotFound) {
root (cont’ed, cancel with .@) [9] TString macroName = filedir + “cernbuild.C”;
root (cont’ed, cancel with .@) [10] if (!gInterpreter->IsLoaded(macroName)) gInterpreter->LoadMacro(macroName);
root (cont’ed, cancel with .@) [11] gROOT->ProcessLineFast(“cernbuild()”);
root (cont’ed, cancel with .@) [12] }
root (cont’ed, cancel with .@) [13] TFile * f = TFile::Open(filename);
root (cont’ed, cancel with .@) [14] if (!f) {
root (cont’ed, cancel with .@) [15] Error(“hbars”,“file cernstaff.root not found”);
root (cont’ed, cancel with .@) [16] return 0;
root (cont’ed, cancel with .@) [17] }
root (cont’ed, cancel with .@) [18] TTree T = (TTree)f->Get(“T”);
root (cont’ed, cancel with .@) [19] if (!T) {
root (cont’ed, cancel with .@) [20] Error(“hbars”,“Tree T is not present in file %s”,f->GetName() );
root (cont’ed, cancel with .@) [21] return 0;
root (cont’ed, cancel with .@) [22] }
root (cont’ed, cancel with .@) [23] T->SetFillColor(45);
root (cont’ed, cancel with .@) [24] TCanvas *c1 = new TCanvas(“c1”,“histograms with bars”,700,800);
root (cont’ed, cancel with .@) [25] c1->SetFillColor(42);
root (cont’ed, cancel with .@) [26] c1->Divide(1,2);
root (cont’ed, cancel with .@) [27]
root (cont’ed, cancel with .@) [27] // Horizontal bar chart
root (cont’ed, cancel with .@) [28] c1->cd(1); gPad->SetGrid(); gPad->SetLogx(); gPad->SetFrameFillColor(33);
root (cont’ed, cancel with .@) [29] T->Draw(“Nation”,"",“hbar2”);
root (cont’ed, cancel with .@) [30]
root (cont’ed, cancel with .@) [30] // Vertical bar chart
root (cont’ed, cancel with .@) [31] c1->cd(2); gPad->SetGrid(); gPad->SetFrameFillColor(33);
root (cont’ed, cancel with .@) [32] T->Draw(“Division>>hDiv”,"",“goff”);
root (cont’ed, cancel with .@) [33] TH1F hDiv = (TH1F)gDirectory->Get(“hDiv”);
root (cont’ed, cancel with .@) [34] hDiv->SetStats(0);
root (cont’ed, cancel with .@) [35] TH1F hDivFR = (TH1F)hDiv->Clone(“hDivFR”);
root (cont’ed, cancel with .@) [36] T->Draw(“Division>>hDivFR”,“Nation==“FR””,“goff”);
root (cont’ed, cancel with .@) [37] hDiv->SetBarWidth(0.45);
root (cont’ed, cancel with .@) [38] hDiv->SetBarOffset(0.1);
root (cont’ed, cancel with .@) [39] hDiv->SetFillColor(49);
root (cont’ed, cancel with .@) [40] TH1 *h1 = hDiv->DrawCopy(“bar2”);
root (cont’ed, cancel with .@) [41] hDivFR->SetBarWidth(0.4);
root (cont’ed, cancel with .@) [42] hDivFR->SetBarOffset(0.55);
root (cont’ed, cancel with .@) [43] hDivFR->SetFillColor(50);
root (cont’ed, cancel with .@) [44] TH1 *h2 = hDivFR->DrawCopy(“bar2,same”);
root (cont’ed, cancel with .@) [45]
root (cont’ed, cancel with .@) [45] TLegend *legend = new TLegend(0.55,0.65,0.76,0.82);
root (cont’ed, cancel with .@) [46] legend->AddEntry(h1,“All nations”,“f”);
root (cont’ed, cancel with .@) [47] legend->AddEntry(h2,“French only”,“f”);
root (cont’ed, cancel with .@) [48] legend->Draw();
root (cont’ed, cancel with .@) [49]
root (cont’ed, cancel with .@) [49] c1->cd();
root (cont’ed, cancel with .@) [50] delete f;
root (cont’ed, cancel with .@) [51] return c1;
root (cont’ed, cancel with .@) [52]}

The error message tell you what is wrong:

root [0] .x cernstaff.C
Error in TApplication::ExecuteFile: macro cernbuild.C not found in path /snap/root-framework/146/usr/local/tutorials/tree/cernstaff.C

The first thing cernstaff.C does is executing the macro cernbuild.C so, cernbuild.C should be next to cernstaff.C. In the same folder. And notice that cernbuild.C needs cernstaff.dat so you need to have also cernstaff.dat in the same folder.

There is a file in a different path, can’t I change the path to connect to?

You can change whatever you what, I just explained how the current version is working.

How can i change the path?? Thanks to reply again!

in the macro you can change the file name to the full path name;

/.../..../.../cernstaff.dat

What do you want to do exactly ? if you just want to execute cernstaff.C simply do:

$ cd $ROOTSYS/tutorials/tree/

and execute the macro from there … all is in place in that folder.

I just wanna solve this example.

TCanvas *hbars() {

// Try to open first the file cernstaff.root in tutorials/tree directory

TString filedir = gROOT->GetTutorialDir();

filedir += TString("/tree/");

TString filename = “cernstaff.root”;

bool fileNotFound = gSystem → AccessPathName(filename); // note opposite return code

// If file is not found try to generate it uing the macro tree/cernbuild.C

if (fileNotFound) {

TString macroName = filedir + “cernbuild.C”;

if (gInterpreter->IsLoaded(macroName)) gInterpreter->LoadMacro(macroName);

gROOT->ProcessLineFast(“cernbuild()”);

}

TFile * f = TFile::Open(filename);

if (f) {

Error(“hbars”,“file cernstaff.root not found”);

return 0;

}

TTree T = (TTree)f->Get(“T”);

if (T) {

Error(“hbars”,“Tree T is not present in file %s”,f->GetName() );

return 0;

}

T->SetFillColor(45);

TCanvas *c1 = new TCanvas(“c1”,“histograms with bars”,700,800);

c1->SetFillColor(42);

c1->Divide(1,2);

// Horizontal bar chart

c1->cd(1); gPad->SetGrid(); gPad->SetLogx(); gPad->SetFrameFillColor(33);

T->Draw(“Nation”,"",“hbar2”);

// Vertical bar chart

c1->cd(2); gPad->SetGrid(); gPad->SetFrameFillColor(33);

T->Draw(“Division>>hDiv”,"",“goff”);

TH1F hDiv = (TH1F)gDirectory->Get(“hDiv”);

hDiv->SetStats(0);

TH1F hDivFR = (TH1F)hDiv->Clone(“hDivFR”);

T->Draw(“Division>>hDivFR”,“Nation==“FR””,“goff”);

hDiv->SetBarWidth(0.45);

hDiv->SetBarOffset(0.1);

hDiv->SetFillColor(49);

TH1 *h1 = hDiv->DrawCopy(“bar2”);

hDivFR->SetBarWidth(0.4);

hDivFR->SetBarOffset(0.55);

hDivFR->SetFillColor(50);

TH1 *h2 = hDivFR->DrawCopy(“bar2,same”);

TLegend *legend = new TLegend(0.55,0.65,0.76,0.82);

legend->AddEntry(h1,“All nations”,“f”);

legend->AddEntry(h2,“French only”,“f”);

legend->Draw();

c1->cd();

delete f;

return c1;

}

But there is error as i said
“Error in TApplication::ExecuteFile: macro cernbuild.C not found in path /snap/root-framework/146/usr/local/tutorials/home/6.22.06/tutorials/tree/cernstaff.C
Error in TFile::TFile: file /home/hyungjoe/6.22.06/tutorials/tree/cernstaff.root does not exist”

See what you should do/get:

% cd $ROOTSYS/tutorials/hist
% root hbars.C 
   ------------------------------------------------------------------
  | Welcome to ROOT 6.25/01                        https://root.cern |
  | (c) 1995-2021, The ROOT Team; conception: R. Brun, F. Rademakers |
  | Built for macosx64 on Apr 20 2021, 08:52:14                      |
  | From heads/master@v6-25-01-662-ga0ce07bfed                       |
  | With Apple clang version 12.0.0 (clang-1200.0.32.29)             |
  | Try '.help', '.demo', '.license', '.credits', '.quit'/'.q'       |
   ------------------------------------------------------------------

root [0] 
Processing hbars.C...
(TCanvas *) 0x7f8e70431040
root [1] 

Big thanks to help. I finally solve this problem!!
Have a nice day!

1 Like

Oh unfortunately, i’m in another problem…

hyungjoe@hyungjoe-MINIPC:~/root/tutorials/hist$ root hbars.C
   ------------------------------------------------------------------
  | Welcome to ROOT 6.22/06                        https://root.cern |
  | (c) 1995-2020, The ROOT Team; conception: R. Brun, F. Rademakers |
  | Built for linuxx8664gcc on Nov 27 2020, 15:14:08                 |
  | From tags/v6-22-06@v6-22-06                                      |
  | Try '.help', '.demo', '.license', '.credits', '.quit'/'.q'       |
   ------------------------------------------------------------------

root [0] 
Processing hbars.C...
you must run the script in a directory with write access
Error in <TFile::TFile>: file cernstaff.root does not exist
Error in <hbars>: file cernstaff.root not found
(TCanvas *) nullptr
root [1] 

In this case, am i paste the cernstaff.root to /root/tutorials/hist ?
cernstaff.root is located in /root/tutorials/tree.

It crashes because your are running hbars.C in a directory where you do not have right access.

SO what should i do??

Copy the directory $ROOTSYS/tutorials in a place where you have right access or change the right accesses of $ROOTSYS/tutorials.

I wanna change the right accesses. How can i change the right accesses?

That’s a linux question. You can find the answer on the web for instance this one : How to Manage File and Folder Permissions in Linux - Linux.com

1 Like

Thank you very much! Finally i solve this problem!