No dictionary for class TCanvas

Hello!

I have this message and can’t save canvas in file.
[attachment=3]Screenshot_1.png[/attachment]

Let’s save .root file (part1)

#include <iostream>

#include "TFile.h"
#include "TObjArray.h"
#include "TTree.h"
#include "TCanvas.h"
#include "TChain.h"
#include "TROOT.h"

using namespace std;

int main(int argc, char *argv[])
{
	//save
	TFile f_tree("D:\\Data_work\\test_folder\\test_save.root", "RECREATE");
	TTree tree("t1", "Parser tree save");
	TCanvas canv("c", "c", 0, 0, 1900, 1000);
	tree.Branch("canvas_tr", "TCanvas", &canv);
	tree.Fill();
	tree.Write();
	f_tree.Close();

	////read
	//TObjArray Hlist_gr(0);
	//Hlist_gr.SetOwner(kTRUE);
	//TCanvas* canv_read = 0;
	//TChain chain("t1");
	//chain.Add("D:\\Data_work\\test_folder\\test_save.root");
	//chain.SetBranchAddress("canvas_tr", &canv_read);
	//chain.GetEntry(0);
	//Hlist_gr.Add(canv_read->Clone());
	//TFile ofile_Hlist_gr("D:\\Data_work\\test_folder\\test_result.root", "RECREATE");
	//Hlist_gr.Write();
	//ofile_Hlist_gr.Close();


	system("pause");
	return 0;
}

Let’s read.root file and save canvas in another file (part2)

[code]#include

#include “TFile.h”
#include “TObjArray.h”
#include “TTree.h”
#include “TCanvas.h”
#include “TChain.h”
#include “TROOT.h”

using namespace std;

int main(int argc, char *argv[])
{
////save
//TFile f_tree(“D:\Data_work\test_folder\test_save.root”, “RECREATE”);
//TTree tree(“t1”, “Parser tree save”);
//TCanvas canv(“c”, “c”, 0, 0, 1900, 1000);
//tree.Branch(“canvas_tr”, “TCanvas”, &canv);
//tree.Fill();
//tree.Write();
//f_tree.Close();

//read
TObjArray Hlist_gr(0);
Hlist_gr.SetOwner(kTRUE);
TCanvas* canv_read = 0;
TChain chain("t1");
chain.Add("D:\\Data_work\\test_folder\\test_save.root");
chain.SetBranchAddress("canvas_tr", &canv_read);
chain.GetEntry(0);
Hlist_gr.Add(canv_read->Clone());
TFile ofile_Hlist_gr("D:\\Data_work\\test_folder\\test_result.root", "RECREATE");
Hlist_gr.Write();
ofile_Hlist_gr.Close();

system("pause");
return 0;

}
[/code]

Somethimes part2 works and somethimes does not.
I do not undestand why.
When I have warning message my file test_result.root is empty.

My VS:
[attachment=2]Screenshot_2.png[/attachment]

My root:

  *******************************************
  *                                         *
  *        W E L C O M E  to  R O O T       *
  *                                         *
  *   Version   5.34/36      5 April 2016   *
  *                                         *
  *  You are welcome to visit our Web site  *
  *          http://root.cern.ch            *
  *                                         *
  *******************************************

ROOT 5.34/36 (v5-34-36@v5-34-36, Apr 05 2016, 10:25:45 on win32)

CINT/ROOT C/C++ Interpreter version 5.18.00, July 2, 2010
Type ? for help. Commands must be C++ statements.
Enclose multiple statements between { }.

I installed root from this file root_v5.34.36.win32.vc12.debug.exe

I compile code in Debug mode:
[attachment=1]Screenshot_3.png[/attachment]

The VS project in attachment.

Could you help?

Best regards, Vladislav.

Hi Vladislav,

I really don’t understand what you’re trying to achieve, but anyway, I don’t have any problem running your example, after a few changes, as described below.

int main(int argc, char *argv[]) { std::string what(argv[1]); if (what == "save" || what == "write") { ////save TFile f_tree("D:\\Data_work\\test_folder\\test_save.root", "RECREATE"); TTree tree("t1", "Parser tree save"); TCanvas canv("c", "c", 0, 0, 1900, 1000); tree.Branch("canvas_tr", "TCanvas", &canv); tree.Fill(); tree.Write(); f_tree.Close(); } else if (what == "copy" || what == "read") { //read TObjArray Hlist_gr(0); Hlist_gr.SetOwner(kTRUE); TCanvas* canv_read = 0; TChain chain("t1"); chain.Add("D:\\Data_work\\test_folder\\test_save.root"); chain.SetBranchAddress("canvas_tr", &canv_read); chain.GetEntry(0); Hlist_gr.Add(canv_read->Clone()); TFile ofile_Hlist_gr("D:\\Data_work\\test_folder\\test_result.root", "RECREATE"); Hlist_gr.Write(); ofile_Hlist_gr.Close(); } else { std::cout << "invalid option!" << std::endl; } system("pause"); return 0; }
It is also sufficient to link against these libraries:<AdditionalDependencies>libCint.lib;libCore.lib;libGpad.lib;libGraf.lib;libGui.lib;libHist.lib;libMathCore.lib;libRint.lib;libRIO.lib;libTree.lib;%(AdditionalDependencies)</AdditionalDependencies>
Then, running it as:

[code]C:\Users\bellenot\rootdev\Forum\Vladislav\Parser>Debug\Parser.exe save
Press any key to continue . . .

C:\Users\bellenot\rootdev\Forum\Vladislav\Parser>Debug\Parser.exe copy
Press any key to continue . . .[/code]
And it produces these files: [attachment=0]test_save.root[/attachment][attachment=1]test_result.root[/attachment]
Cheers, Bertrand.

Hello!

I do not have problems with you code.
But could you run my code?
I.e. run Save.cpp and then Read.cpp.
This strange error appears somethimes in my project and I have to know the reason.
I wrote this code to demonstrate error.

Save.cpp

#include <iostream>

#include "TFile.h"
#include "TObjArray.h"
#include "TTree.h"
#include "TCanvas.h"
#include "TChain.h"
#include "TROOT.h"

using namespace std;

int main(int argc, char *argv[])
{
	//save
	TFile f_tree("D:\\Data_work\\test_folder\\test_save.root", "RECREATE");
	TTree tree("t1", "Parser tree save");
	TCanvas canv("c", "c", 0, 0, 1900, 1000);
	tree.Branch("canvas_tr", "TCanvas", &canv);
	tree.Fill();
	tree.Write();
	f_tree.Close();

	system("pause");
	return 0;
}

Read.cpp

#include <iostream>

#include "TFile.h"
#include "TObjArray.h"
#include "TTree.h"
#include "TCanvas.h"
#include "TChain.h"
#include "TROOT.h"

using namespace std;

int main(int argc, char *argv[])
{
	//read
	TObjArray Hlist_gr(0);
	Hlist_gr.SetOwner(kTRUE);
	TCanvas* canv_read = 0;
	TChain chain("t1");
	chain.Add("D:\\Data_work\\test_folder\\test_save.root");
	chain.SetBranchAddress("canvas_tr", &canv_read);
	chain.GetEntry(0);
	Hlist_gr.Add(canv_read->Clone());
	TFile ofile_Hlist_gr("D:\\Data_work\\test_folder\\test_result.root", "RECREATE");
	Hlist_gr.Write();
	ofile_Hlist_gr.Close();

	system("pause");
	return 0;
}

I see warnings



and test_result.root does not have expected canvas.

AdditionalDependencies:

libCint.lib
libCore.lib
libGpad.lib
libGraf.lib
libGui.lib
libHist.lib
libMathCore.lib
libRint.lib
libRIO.lib
libTree.lib

link to the whole project
drive.google.com/open?id=0B_9W0 … FVKWldSTVU

Best regards, Vladislav.
test_result.root (572 Bytes)
test_save.root (24.2 KB)

Hi Vladislav,

You should create a TApplication instance. E.g.:

[code]
#include

#include “TApplication.h”
#include “TFile.h”
#include “TObjArray.h”
#include “TTree.h”
#include “TCanvas.h”
#include “TChain.h”
#include “TROOT.h”

using namespace std;

int main(int argc, char argv[])
{
//read
TApplication theApp(“theApp”, &argc, argv);
TObjArray Hlist_gr(0);
Hlist_gr.SetOwner(kTRUE);
TCanvas
canv_read = 0;
TChain chain(“t1”);
chain.Add(“D:\Data_work\test_folder\test_save.root”);
chain.SetBranchAddress(“canvas_tr”, &canv_read);
chain.GetEntry(0);
Hlist_gr.Add(canv_read->Clone());
TFile ofile_Hlist_gr(“D:\Data_work\test_folder\test_result.root”, “RECREATE”);
Hlist_gr.Write();
ofile_Hlist_gr.Close();

system("pause");
theApp.Terminate();
theApp.Run();
return 0;

}[/code]This should properly initialize the environment for your application(s).

Cheers, Bertrand.

Bertrand, many thanks!
Now it works! :slight_smile: