#include "../MyClass.h"
#include <string>
#include <iostream>
#include <fstream>
#include <cassert>
#include <vector>
#include "TFile.h"
#include "TH1F.h"
Go to the source code of this file.
Definition at line 27 of file test_MyClass.cxx.
28 const string myname =
"test_MyClass: ";
29 cout << myname <<
"Starting test" <<
endl;
31 cout << myname <<
"NDEBUG must be off." <<
endl;
34 string line =
"-----------------------------";
37 cout << myname << line <<
endl;
38 cout << myname <<
"Create object." <<
endl;
43 TH1F h0(
"histo",
"histo", 10, 0, 10);
44 h0.SetDirectory(
nullptr);
45 for (
int i=0; i<5; ++i ) h0.Fill(5+0.3*i);
46 obj1.
myobj = h0.Clone(
"myhisto");
47 TH1* ph1 =
dynamic_cast<TH1*
>(obj1.
myobj);
48 assert( ph1 !=
nullptr );
50 string rfnam =
"test_MyClass.root";
51 cout << myname << line <<
endl;
52 cout << myname <<
"Write object to " << rfnam <<
"." <<
endl;
53 TFile* prout = TFile::Open(rfnam.c_str(),
"RECREATE");
55 prout->WriteObject(&obj1,
"myobj1");
60 cout << myname << line <<
endl;
61 cout << myname <<
"Read object from " << rfnam <<
"." <<
endl;
62 TFile* prin = TFile::Open(rfnam.c_str(),
"READ");
64 prin->GetObject(
"myobj1", pobj2);
66 vector<string> msgs = {
"file open",
"file closed" };
67 for (
string msg : msgs ) {
68 cout << myname << line <<
endl;
69 cout << myname <<
"Check read object with input " <<
msg <<
endl;
70 cout <<
"Read count: " << pobj2->readCount <<
endl;
71 assert( pobj2->myint == obj1.
myint );
72 assert( pobj2->myfloat == obj1.
myfloat );
73 assert( pobj2->readCount == 1 );
74 cout <<
"TObject pointer: " << pobj2->myobj <<
endl;
75 assert( pobj2->myobj !=
nullptr );
76 pobj2->myobj->Print();
77 TH1* ph2 =
dynamic_cast<TH1*
>(pobj2->myobj);
78 assert( ph2 !=
nullptr );
80 assert( ph2->GetEntries() == ph1->GetEntries() );
81 if ( prin !=
nullptr ) {
82 cout << myname <<
"Closing input file." <<
endl;
88 cout << myname << line <<
endl;
89 cout << myname <<
"Deleting read object." <<
endl;
92 cout << myname << line <<
endl;
93 cout << myname <<
"Deleting original object." <<
endl;
96 cout << myname << line <<
endl;
97 string snam =
"test_MyClass.C";
98 cout << myname <<
"Creating test script " << snam <<
endl;
99 ofstream
fout(snam.c_str());
100 fout <<
"void test_MyClass() {" <<
endl;
101 fout <<
" TFile* pfin = TFile::Open(\"" << rfnam <<
"\");" <<
endl;
103 fout <<
" pfin->GetObject(\"myobj1\", pobj);" <<
endl;
104 fout <<
" cout << \" myint = \" << pobj->myint << endl;" <<
endl;
105 fout <<
" cout << \" myfloat = \" << pobj->myfloat << endl;" <<
endl;
108 cout <<
"Check root file with \"root.exe " << snam <<
"\"" <<
endl;
110 cout << myname << line <<
endl;
111 cout << myname <<
"Done." <<
endl;
void msg(const char *fmt,...)
void line(double t, double *p, double &x, double &y, double &z)
QTextStream & endl(QTextStream &s)