I am trying to write a text into a root file and read it. Below
is a short macro which is based on Example of macro to add histogram files
in ROOT page. It crashes in line 41 with a message illegal pointer to
class object obj 0x0 .... .
Am I doing something wrong or is it any other solution?
Thank you and regards,
Michal Lijowski
-------------------------------------------------------------------------
void test_TObjString()
{
// gROOT -> Reset();
Char_t Macro_name[80] = "test_TObjString";
Char_t infile[200], outfile[200];
// Create a new ROOT file to contain TObjString
sprintf(outfile, "%s.root", Macro_name);
printf("%s\n", outfile);
TFile *tfile = new TFile(outfile, "recreate");
Char_t large_str[10000];
TObjString Comment;
large_str[0] = '\0';
sprintf(large_str, "testing TObjString");
Comment -> SetString(large_str);
Comment -> Print();
Comment -> Write();
Comment -> Delete();
tfile -> Write("Comment");
tfile -> Close();
TObject *obj;
TKey *key;
sprintf(infile, "%s.root", Macro_name);
printf("%s\n", infile);
TFile *f = new TFile(infile, "read");
f -> ls();
// create an iterator to loop through all objects(keys) in the file
TIter nextkey(f -> GetListOfKeys());
while (key = (TKey*)nextkey()) {
obj = key -> Read();
if (obj -> IsA() -> InheritsFrom("TObjString") ) {
TObjString *Comm = (TObjString *) obj;
Comm -> Print();
}
}
f -> Close();
}