Functions
test_MyClass.cxx File Reference
#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.

Functions

int test_MyClass ()
 
int main ()
 

Function Documentation

int main ( void  )

Definition at line 117 of file test_MyClass.cxx.

117  {
118  return test_MyClass();
119 }
int test_MyClass()
int test_MyClass ( )

Definition at line 27 of file test_MyClass.cxx.

27  {
28  const string myname = "test_MyClass: ";
29  cout << myname << "Starting test" << endl;
30 #ifdef NDEBUG
31  cout << myname << "NDEBUG must be off." << endl;
32  abort();
33 #endif
34  string line = "-----------------------------";
35  string scfg;
36 
37  cout << myname << line << endl;
38  cout << myname << "Create object." << endl;
39  MyClass* pobj1 = new MyClass;
40  MyClass& obj1 = *pobj1;
41  obj1.myint = 123;
42  obj1.myfloat = 246.78;
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 );
49 
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");
54  assert( obj1.readCount == 0 );
55  prout->WriteObject(&obj1, "myobj1");
56  assert( obj1.readCount == 0 );
57  prout->Write();
58  delete prout;
59 
60  cout << myname << line << endl;
61  cout << myname << "Read object from " << rfnam << "." << endl;
62  TFile* prin = TFile::Open(rfnam.c_str(), "READ");
63  MyClass* pobj2 = nullptr;
64  prin->GetObject("myobj1", pobj2);
65 
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 );
79  assert( ph2 != ph1 );
80  assert( ph2->GetEntries() == ph1->GetEntries() );
81  if ( prin != nullptr ) {
82  cout << myname << "Closing input file." << endl;
83  delete prin;
84  prin = 0;
85  }
86  }
87 
88  cout << myname << line << endl;
89  cout << myname << "Deleting read object." << endl;
90  delete pobj2;
91 
92  cout << myname << line << endl;
93  cout << myname << "Deleting original object." << endl;
94  delete pobj1;
95 
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;
102  fout << " MyClass* pobj;" << endl;
103  fout << " pfin->GetObject(\"myobj1\", pobj);" << endl;
104  fout << " cout << \" myint = \" << pobj->myint << endl;" << endl;
105  fout << " cout << \" myfloat = \" << pobj->myfloat << endl;" << endl;
106  fout << " delete pobj;" << endl;
107  fout << "}" << endl;
108  cout << "Check root file with \"root.exe " << snam << "\"" << endl;
109 
110  cout << myname << line << endl;
111  cout << myname << "Done." << endl;
112  return 0;
113 }
void msg(const char *fmt,...)
Definition: message.cpp:107
unsigned int readCount
Definition: MyClass.h:20
void line(double t, double *p, double &x, double &y, double &z)
int myint
Definition: MyClass.h:17
float myfloat
Definition: MyClass.h:18
TObject * myobj
Definition: MyClass.h:19
QTextStream & endl(QTextStream &s)