TObjArray with canvases seems problematics

Hi,

I have just modified a C++ code handling and writing on a root file tens of histograms (a list of TObjArray) into the same code handling and writing the same histograms but now included in canvases. The goal of this change is to handle also TPaveLabels over these histograms.
Vi operation in the source code (cfr. beneath): :1,$s/Add(h)/Add(canvas)

[ul]When I have one TCanvas in the TObjArray; there is no problem. Browsing (TBrowse) the root file containing the TObjArray shows the right lonely canvas.[/ul]
[ul]When I have two TCanvas in the TObjArray; this error appears on the console:

Error in <TCanvasImp::Streamer>: version id <=0 in ClassDef, dummy Streamer() called

[list]When I want to browse the root file generated (TBrowse) a strange frame (frame94;1) appear in this root file coming with this console error:

Error in <TCanvasImp::Streamer>: version id <=0 in ClassDef, dummy Streamer() called Warning in <TClass::TClass>: no dictionary for class TRootContainer is available
[/ul]
[ul]And when I click the frame, some other strange stuffes appears coming with this console error:

*** Break *** segmentation violation Generating stack trace... 0x011d38e8 in TRootContainer::HandleConfigureNotify(Event_t*) + 0x2e from /user/adeprell/root//lib/libGui.so 0x01140b9a in TGFrame::HandleEvent(Event_t*) + 0xb0 from /user/adeprell/root//lib/libGui.so 0x0111e50b in TGClient::HandleEvent(Event_t*) + 0xd1 from /user/adeprell/root//lib/libGui.so 0x0111e189 in TGClient::ProcessOneEvent() + 0x71 from /user/adeprell/root//lib/libGui.so 0x0111e291 in TGClient::HandleInput() + 0x2d from /user/adeprell/root//lib/libGui.so 0x0111c966 in TGInputHandler::Notify() + 0x1e from /user/adeprell/root//lib/libGui.so 0x00aeb24e in TUnixSystem::DispatchOneEvent(bool) + 0x44 from /user/adeprell/root/lib/libCore.so 0x00a219f8 in TSystem::InnerLoop() + 0x18 from /user/adeprell/root/lib/libCore.so 0x00a2199d in TSystem::Run() + 0x6f from /user/adeprell/root/lib/libCore.so 0x009aa036 in TApplication::Run(bool) + 0x32 from /user/adeprell/root/lib/libCore.so 0x0804988a in TFile::TFile(char const*, char const*, char const*, int) + 0x1e6 from bin/analyse 0x0079add8 in <unknown> from /lib/tls/libpthread.so.0 0x0799cd2a in __clone + 0x5a from /lib/tls/libc.so.6
[/ul]

[/list:u]
[ul]When I have three of more TCanvas in the TObjArray; these errors appears on the console:

[code]Error in TCanvasImp::Streamer: version id <=0 in ClassDef, dummy Streamer() called

*** Break *** segmentation violation
Generating stack trace…
0x00e080ef in TCollection::Write(char const*, int, int) + 0x1b from /user/adeprell/root/lib/libCore.so
0x0804aa0c in from bin/analyse
0x08049c76 in TFile::TFile(char const*, char const*, char const*, int) + 0x5d2 from bin/analyse
0x02a5679a in __libc_start_main + 0xda from /lib/tls/libc.so.6
0x080496e5 in TFile::TFile(char const*, char const*, char const*, int) + 0x41 from bin/analyse
Abort (core dumped)[/code][/ul]

Here is the source code:

1 #include <analyse.h> 2 3 void histos_sat(const char *path) 4 { 5 unsigned short flag = 0; 6 long int i = 0, id = 1, n_min = 10000, n=0; 7 char h_name[32] = "h1", 8 c_name[32] = "c1", 9 h_title[32] = "histo nr: 1", 10 c_title[32] = "canvas nr:1", 11 *output; 12 const char *ext = ".histos_sat.root"; 13 DAQMess_t dm[2][2], d1, d2; 14 char content[2][2][32]; 15 16 TObjArray Clist(0); 17 TCanvas *canvas = new TCanvas(c_name, c_name); 18 TH1I *h = new TH1I(h_name, h_title, 100, -50, 50); 19 TPaveText *TPT = new TPaveText(0.8, 0.6, 0.98, 0.8, "NDC"); 20 TFile *f = new TFile((const char *) path, "READ"); 21 TTree *t = (TTree *) f->Get("T"); 22 23 long int nentries = t->GetEntries(); 24 25 t->SetBranchAddress("Detector1", &d1); 26 t->SetBranchAddress("Detector2", &d2); 27 28 t->GetEntry(0); 29 30 dm[0][0] = d1; 31 dm[1][0] = d2; 32 33 h->Fill(diff_daq_mess(dm[0][0], dm[1][0])); 34 35 for (i = 1; i < nentries; i++) { 36 t->GetEntry(i); 37 dm[0][1] = d1; 38 dm[1][1] = d2; 39 40 if (((dm[0][1].svid_h == dm[0][0].svid_h) && 41 (dm[0][1].svid_l == dm[0][0].svid_l) && 42 (dm[1][1].svid_h == dm[1][0].svid_h) && 43 (dm[1][1].svid_l == dm[1][0].svid_l)) || flag) { 44 if (flag) 45 flag = 0; 46 h->Fill(diff_daq_mess(d1, d2)); 47 n++; 48 dm[0][0] = dm[0][1]; 49 dm[1][0] = dm[1][1]; 50 } 51 52 else { 53 if (n >= n_min) { 54 sprintf (content[0][0],"%s %i", "svid_h1: ", (int)d1.svid_h); 55 sprintf (content[0][1],"%s %i", "svid_l1: ", (int)d1.svid_l); 56 sprintf (content[1][0],"%s %i", "svid_h2: ", (int)d2.svid_h); 57 sprintf (content[1][1],"%s %i", "svid_l2: ", (int)d2.svid_l); 58 TPT -> AddText((const char*) "Satellites infos:"); 59 TPT -> AddText((const char*)content[0][0]); 60 TPT -> AddText((const char*)content[0][1]); 61 TPT -> AddText((const char*)content[1][0]); 62 TPT -> AddText((const char*)content[1][1]); 63 TPT -> SetTextAlign(12); 64 h->SetFillColor(42); 65 h->Fit("gaus"); 66 Clist.Add(canvas); 67 id++; 68 sprintf(h_name, "h%li", id); 69 sprintf(h_name, "c%li", id); 70 sprintf(h_title, "histo nr: %li", id); 71 sprintf(h_title, "canvas nr: %li", id); 72 } 73 else { 74 delete h; 75 delete canvas; 76 } 77 78 canvas = new TCanvas(c_name, c_title); 79 h = new TH1I(h_name, h_title, 100, -50, 50); 80 flag = 1; n = 0; 81 } 82 } 83 h->SetFillColor(42); 84 h->Fit("gaus"); 85 TPT -> Draw(); 86 if(n >= n_min) 87 Clist.Add(canvas); 88 output = 89 (char *) malloc((strlen(path) - strlen(".root") + strlen(ext)) * 90 sizeof(char)); 91 strcpy(output, path); 92 output[strlen(path) - strlen(".root")] = '\0'; 93 strcat(output, ext); 94 TFile f2(output, "recreate"); 95 Clist.Write(); 96 f->Close(); 97 f2.Close(); 98 printf("Done\n"); 99 } 100

Thank you very much for those who did read this post 'til the end and for the one who could and would help me.

Best regards,

Arnaud de Prelle.

Hi,

In order for your code to work properly you have to change the name fo the canvas each time you create one (if the name is not unique the previous one might get deleted).

Cheers,
Philippe

Hello pcanal,

You’re right, I just remark I did a stupid copy-paste error :angry:

68 sprintf(h_name, "h%li", id); 69 sprintf(h_name, "c%li", id); 70 sprintf(h_title, "histo nr: %li", id); 71 sprintf(h_title, "canvas nr: %li", id);

becomes:

68 sprintf(h_name, "h%li", id); 69 sprintf(c_name, "c%li", id); 70 sprintf(h_title, "histo nr: %li", id); 71 sprintf(c_title, "canvas nr: %li", id);

Thank you very much.

PS: I have also corrected some other errors not linked with the problem in this code.