Hi, I’m just beginning to learn ROOT and I’m having a lot of trouble with my second root macro. The code looks like this:
[code]#include “Riostream.h” #include "make_tree.cc"
using namespace std;
void make_hist() {
//Enumerate the data files
string files[] = {"ddx"}; //ddx", "dxd","uux", "uxu"};
int n_files = (sizeof(files)/sizeof(*files));
string infile, outfile;
// // process the text files by converting them to TTrees
// cout << "Processing " << n_files << " files..." << endl;
// for (int i=0;i<n_files;i++) {
// infile = "weights_" + files[i];
// outfile = files[i] + ".root";
// make_tree(infile.c_str(), outfile.c_str());
// }
//Start building a histogram
TH1D::SetDefaultSumw2(kTRUE);
TH1D *h1 = new TH1D("h1", "Title", 170, 0, 1000);
h1->GetYaxis()->SetTitle("a. u.");
h1->SetMarkerColor(kBlack + 1);
h1->SetLineColor(kBlack);
h1->SetFillColor(kBlack);
h1->SetLineWidth(2);
h1->GetYaxis()->SetTitleSize(23);
h1->GetYaxis()->SetTitleFont(43);
h1->GetYaxis()->SetTitleOffset(1.1);
h1->GetYaxis()->SetLabelFont(43); // Absolute font size in pixel (precision 3)
h1->GetYaxis()->SetLabelSize(21);
//Loop through each of the TTrees and add it's data to the histogram
TFile *f; TTree *T_p4Hmumu; TTree *T_weight0; TLorentzVector *p4Hmumul; Double_t weight0;
cout << "Finished building TTrees. Getting 4vectors..." << endl;
for (int j=0;j<n_files;j++) {
outfile = files[j] + ".root";
cout << "Reading from " << outfile << endl;
f = TFile::Open(outfile.c_str(),"READ");
f->GetObject("T",T_p4Hmumu);
p4Hmumu = new TLorentzVector();
T_p4Hmumu->SetBranchAddress("p4Hmumu", &p4Hmumu);
f->GetObject("T",T_weight0);
weight0 = 0;
T_weight0->SetBranchAddress("events", &weight0);
Int_t nevent = T_p4Hmumu->GetEntries();
cout<<nevent<<endl;
for (Int_t i=0;i<nevent;i++) {
T_p4Hmumu->GetEvent(i);
T_weight0->GetEvent(i);
cout << "Mass was: " << p4Hmumu.M() << " and weight was: " << weight0 << endl;
h1->Fill(p4Hmumu.M(), weight0);
}
}
cout << "No problem yet..." << endl;
h1->Draw("hist");
cout << "Still no problem..." << endl;
} [/code]
When I run this code from ROOT command line the first time, it sometimes works fine. Usually by the second time, though, it fails with a Segmentation violation at the line where I’m drawing h1. I’ve worked very hard to fix it but have had no success. Am I missing something obvious?
Sorry about that! I should have included the stack trace in my initial post. Here it is along with the commands I made in root:
root [0] .x make_hist.cc
Finished building TTrees. Getting 4vectors...
Reading from test.root
4
Warning: wrong member access operator '.' make_hist.cc:59:
Still in file loop...
No problem yet...
Still no problem...
root [1] .x make_hist.cc
*** Break *** segmentation violation
Generating stack trace...
0x000000010e235592 in G__unloadfile (in libCint.so) + 434
0x000000010e31bb30 in G__process_cmd (in libCint.so) + 12720
0x000000010de3d3eb in TCint::ProcessLine(char const*, TInterpreter::EErrorCode*) (in libCore.so) + 859
0x000000010de3d779 in TCint::ProcessLineSynch(char const*, TInterpreter::EErrorCode*) (in libCore.so) + 121
0x000000010de89ec6 in TApplication::ExecuteFile(char const*, int*, bool) (in libCore.so) + 2310
0x000000010ebac516 in TRint::HandleTermInput() (in libRint.so) + 678
0x000000010de75ca9 in TUnixSystem::CheckDescriptors() (in libCore.so) + 313
0x000000010de7e96d in TMacOSXSystem::DispatchOneEvent(bool) (in libCore.so) + 429
0x000000010dee735a in TSystem::InnerLoop() (in libCore.so) + 26
0x000000010dee71ab in TSystem::Run() (in libCore.so) + 203
0x000000010de8a0e4 in TApplication::Run(bool) (in libCore.so) + 36
0x000000010ebabe67 in TRint::Run(bool) (in libRint.so) + 1431
0x000000010db0be8f in main (in root.exe) + 79
0x00007fff82f7d5c9 in start (in libdyld.dylib) + 1
Root > !!!Dictionary position not recovered because G__unloadfile() is used in a macro!!!
I’ve attached the two macros I wrote as well as a test datafile I’ve been using for debugging. These should allow you to reproduce my error.
Best,
Sam weights_test.txt (1.91 KB) make_hist.cc (1.99 KB) make_tree.cc (3.52 KB)
In file included from /Users/couet/Downloads/make_hist.cc:2:
/Users/couet/Downloads/make_tree.cc:53:42: warning: missing terminating '"' character [-Winvalid-pp-token]
tree->Branch("events",&events.weight0,"weight0/D:weight1/D:
^
/Users/couet/Downloads/make_tree.cc:53:42: error: expected expression
/Users/couet/Downloads/make_tree.cc:56:38: warning: missing terminating '"' character [-Winvalid-pp-token]
p4H_0:p4H_1:p4H_2:p4H_3:ibody/I");
^
In file included from input_line_10:1:
/Users/couet/Downloads/make_hist.cc:5:18: error: function definition is not allowed here
void make_hist() {
^
-:1:1: error: expected '}'
^
/Users/couet/Downloads/make_tree.cc:9:47: note: to match this '{'
void make_tree(char * infile, char * outfile) {
^
you should not cut the text string on several lines I guess.
Yes I do the same but with ROOT 6.
ROOt 5 goes a bit further (I suggest you fixe the error anyway, try with AClic)
root [0]
Processing make_hist.cc...
Finished building TTrees. Getting 4vectors...
Reading from test.root
Error in <TFile::TFile>: file test.root does not exist
Error: illegal pointer to class object f 0x0 5 make_hist.cc:34:
*** Interpreter error recovered ***
root [1]
Is the make_tree.cc not generating a root file? It should produce a test.root file. I’m not familiar with AClic - Would you mind telling me the command I should use to compile this project?
I execute it from within make_hist.cc. In the main function, I first loop through four different text files and convert their values into TLorentzVectors and Double_t values, which I store in a TTree and save to a .root file. Then, I’m trying to read each of these root files, one after the other, and use all of their values to fill a single histogram in the code for make_hist.cc
I figured out the compilation; thanks (though now I’m getting new errors that I need to fix)