TTree::Draw() and gPad issues

Hello all,
While I fear I may be overlooking some basic concept, I have a problem with the TGraph originating from tree.Draw() - see attached code for the basic structure of what I’m trying to do. The code worked fine at first… and then, without changing anything, it started not displaying the plots.

TTree *tree = new TTree("tree","tree");

std::ifstream file1;
string input="some_file.txt";,ios::in);

file1 >> branchName1 >> ... >> branchNameN;

TCanvas *c1 = new TCanvas("c1","c1",700,500);

// Plotting routines here are held in a dedicated plotting function, but it's a simple plotMe(double a, double b)

TGraph *graph = (TGraph*)gPad->GetPrimitive("Graph");
TH2F *htemp = (TH2F*)gPad->GetPrimitive("htemp");

// End of plotting routine
// End of main

The error that keeps appearing is related to the instructions I have inside my plotting function, which themselves relate to gPad->GetPrimitive. As far as I understand, to change the X/Y axis labels and the colors in the plot, I need to go use those functions if I’m creating the plots with TTree::Draw(). I’m guessing some kind of (memory?) problem comes from there, but I have run out of ideas on how to solve it. Here’s the actual error message:

Warning in <TCanvas::ResizePad>: c2 width changed from 0 to 10

Also, I don’t know if it’s a runaway consequence of the above, but: each time I run it afresh (it has some trivial iterative conditions to choose from different cases, but always with the same aforementioned structure - the point is I can run it more than once until I choose to exit it), it outputs before plotting: (void)1.

I tried it with ROOT v. 5.34/18 and 5.20/00.

I have seen a similar issue discussed back in 2002, and tried re-compiling ROOT. It worked (!) for a while, but now it has stopped working at all. Another way I “solved” it and then stopped working was changing the way I had defined the tree (TTree tree(“tree”,“tree”)->Gave problems; vs TTree *tree = new TTree(“tree”,“tree”)->Didn’t give problems) and calling it through tree->DoSomething instead of tree.DoSomething .

Thank you in advance for any help on this you might bring!

The piece of your source code, that you show, creates a “c1” canvas, but the error is related to another canvas named “c2”.

Yes, as I mention there are two different (trivial) if statements for different input cases. They both create canvases the same way and use the same plotting function, the only difference is that one is called c1 and the other c2, and I just chose the error message that I had at hand. It also happens with c1.

Before the first “gPad->GetPrimitive”, try to add:
gPad->Modified(); gPad->Update();

BTW. Check input data files for your “input cases” and make sure that there is no NaN case inside.

Thanks for your help! I still get the same result unfortunately…

Another sign of this problem being related somehow to memory allocation is that, this morning as I tried troubleshooting it again, the code worked for a while! Then it started showing the same errors as yesterday.

I think the root might be in the input part and not in gPad. The canvas errors are shown before entering the plotting routine. Further, I discovered that even entering non-existing input files does not trigger the if(!file) condition - indeed, the problem with plotting appears to be related to there not being data for the plots!

So, even if my problem morphed a bit, I’m including my input section… where can the problem lie? I tried toubleshooting, doing cout for the lines that it reads from the input text file… and after the first one, which it reads correctly, it just prints blanks (but goes through the while loop alright)!

#include <iostream>
#include <string>

int SlopeTemPlot(){

[Variable definitions, including TTree and TBranches]

string input="some_file.txt";
std::ifstream file1;,ios::in);
        if (!file1) {
              std::cout << "NO FILE AVAILABLE! ";
        file1 >> date >> time >> data1 >> ... >> data14;
        [If I include cout here, it just outputs the first line, and then blanks!]
        std::cout << "Current line: " << line << endl;

        [Things to do with the data before filling the tree...]
[Rest of the program, with plots etc]

The file is a simple .txt file with data from 14 sensors in each line, preceded by the date/time for each line. It doesn’t contain any other form of data.

std::getline (string)

I will try to troubleshoot with those tools, thanks. For starters I can think of a better way of handling a missing file.

However, why would this code scheme work sometimes, and then for some (memory?) reason, start not reading the file correctly?

Another perplexing datapoint:

While troubleshooting, I asked the tree to be written in a rootfile, to see what ended up being written. Also, I tried to cout each line that the while loop goes through in each iteration. And behold, the tree is filled alright (why not plotted?) but only a blank line is cout-ed except for the first one!

Ok, while I still don’t understand the problem, by chance I have discovered a way to make it work: if I open TBrowser first, and then run the code, all is well. :open_mouth:

I suppose some graphics routine gets saturated but gets “stabilized” with something simple as a TBrowser.

If someone has any ideas as to why this might be happening though, I’d be all ears… I don’t want a buggy code if I can avoid it :slight_smile: