TCanvas not displaying

Hello, I am trying to fill 3 histograms with the same entries number (7743).
Then, I would like to draw the histograms in a divided TCanvas. But, when I try, the TCanvas will not be displayed. If I reduce the entries number, by the way, it will display with no problems at all. It is not a matter of data, since I checked. Does anybody know how to fix this? I have also tried to use a simple (not splitted) Canvas. I am running ROOT 6.28/04 on macOS 13.5.1

Can you show the code that you use and doesn’t work, and the code that works? If possible, try to reduce the code to a minimum that still can be run and shows the problem.

1 Like

Sure, thank you! It is not so obvious, since it reads some txt file and extracts data from the, but I will try a simplified version.
The code which works (I fill just one histogram and I display it):

...{
...
 TGraph *g1;
    TH1D *hist1 = new TH1D("h1", "h1", 200, 0.02, 0.13);
    vector<double> vals1;
    for(int i = 0; i < 7744; i++){
        fileReader(file[i], g1); // filling the TGraph
        vals1 = function_1(g1); //returns a vector, I take just the first value
        hist1->Fill(vals1[0]); 
    }
    
    TCanvas *c = new TCanvas("canvas", "canvas");
    hist1->Draw("hist");
}

While if I try with the other 2 histograms:

...{
...
 TGraph *g1; TGraph *g2; TGraph *g3;
    TH1D *hist1 = new TH1D("h1", "h1", 200, 0.02, 0.13);
    TH1D *hist2 = new TH1D("h2", "h2", 200, -1.0, 1.0);
    TH1D *hist3 = new TH1D("h3", "h3", 200, -2.0, 1.0);
    vector<double> vals1; vector<double> vals2; vector<double> vals3;
    for(int i = 0; i < 7744; i++){
        fileReader(file[i], g1); // filling the TGraph
        fileReader(scint1[i], g2);
        fileReader(scint2[i], g3);
        vals1 = function_1(g1);
        hist1->Fill(vals1[0]);
        vals2 = function_2(g2);
        hist2->Fill(vals2[0]);
        vals3 = function_2(g3);
        hist3->Fill(vals3[0]);

        g2 = nullptr;
        delete g2;
        g3 = nullptr;
        delete g3;
    }
    
    TCanvas *c = new TCanvas("canvas", "canvas");
    c->Divide(1,3);
    c->cd(1);
    hist1->Draw("hist");
    c->cd(2);
    hist2->Draw("hist");
    c->cd(3);
    hist3->Draw("hist");
}

What am I doing wrong?

If I do this I get the 3 plots (obviously):

void annamarini()
{
//    TGraph *g1; TGraph *g2; TGraph *g3;
   TH1D *hist1 = new TH1D("h1", "h1", 200, 0.02, 0.13);
   TH1D *hist2 = new TH1D("h2", "h2", 200, -1.0, 1.0);
   TH1D *hist3 = new TH1D("h3", "h3", 200, -2.0, 1.0);
//    vector<double> vals1; vector<double> vals2; vector<double> vals3;
//    for(int i = 0; i < 7744; i++){
//       fileReader(file[i], g1); // filling the TGraph
//       fileReader(scint1[i], g2);
//       fileReader(scint2[i], g3);
//       vals1 = function_1(g1);
//       hist1->Fill(vals1[0]);
//       vals2 = function_2(g2);
//       hist2->Fill(vals2[0]);
//       vals3 = function_2(g3);
//       hist3->Fill(vals3[0]);
//
//       g2 = nullptr;
//       delete g2;
//       g3 = nullptr;
//       delete g3;
//    }

   TCanvas *c = new TCanvas("canvas", "canvas");
   c->Divide(1,3);
   c->cd(1);
   hist1->Draw("hist");
   c->cd(2);
   hist2->Draw("hist");
   c->cd(3);
   hist3->Draw("hist");
}

So the filling part does something wrong. do you get some error messages ? It is difficult for us to debug further since there is a lot information missing.

No error messages at all. I can paste the whole macro here, if it can be helpful

Yes, and also the messages you get during the execution (if there is some).

#include <fstream>
#include <string>
#include "TGraph.h"
#include "TCanvas.h"
#include <vector>

void fileReader(string fileName, TGraph* &gr){
    fstream myFile;
    myFile.open(fileName.c_str());
    string wordinStreamFile;
    double amplitude;
    double time;
    int fileExtension;
    while(myFile >> wordinStreamFile){
        if(wordinStreamFile == "SegmentSize"){
            myFile >> fileExtension; //with >> I go to the subsequent element in the stream
        }
        if(wordinStreamFile == "Ampl"){
            break;
        }
    }
    double *t = new double[fileExtension];
    double *A = new double[fileExtension];
    int i = 0;
    while(myFile >> time && myFile >> amplitude){
        t[i] = time;
        A[i] = amplitude;
        i = i+1;
    }
    gr = new TGraph(fileExtension, t, A);
}


vector<double> function_1(TGraph* &gr){
    double amplSum;
    double *FirstValsAmpl = new double [4000];
    for (int i = 0; i < 4000; i++){
        FirstValsAmpl[i] = gr->GetPointY(i);
        amplSum = amplSum + gr->GetPointY(i);
    }
    double pedestal = amplSum/4000;
    
    double *peakTrig = new double[2000];
    int counter = 0;
    for(int i=4000; i < 6000; i++){
        peakTrig[counter] = gr->GetPointY(i);
        counter = counter + 1;
    }
    
    double maxVal = -99999.9;
    for(int i = 0; i < 2000; i++){
        if(peakTrig[i] > maxVal){
            maxVal = peakTrig[i];
        }
    }
    double sigmas;
    for (int i = 0; i < 4000; i++){
        sigmas += pow(FirstValsAmpl[i] - pedestal, 2);
    }
    
    double sDev = sqrt(sigmas/4000);
    double AmplSub = maxVal - pedestal;
    
    return {AmplSub, sDev, pedestal};
}

vector<double> function_2(TGraph* &gr){
    double amplSum;
    double *FirstValsAmpl = new double [4000];
    for (int i = 0; i < 4000; i++){
        FirstValsAmpl[i] = gr->GetPointY(i);
        amplSum = amplSum + gr->GetPointY(i);
    }
    double pedestal = amplSum/4000;
    
    double *peakTrig = new double[2000];
    int counter = 0;
    for(int i=4000; i < 6000; i++){
        peakTrig[counter] = gr->GetPointY(i);
        counter = counter + 1;
    }
    
    double minVal = 99999.9;
    for(int i = 0; i < 2000; i++){
        if(peakTrig[i] < minVal){
            minVal = peakTrig[i];
        }
    }
    double sigmas;
    for (int i = 0; i < 4000; i++){
        sigmas += pow(FirstValsAmpl[i] - pedestal, 2);
    }
    
    double sDev = sqrt(sigmas/4000);
    double AmplSub = minVal - pedestal;
    
    return {AmplSub, sDev, pedestal};
}





void waveforms(){
    string path = "my/path/to/file";
    fstream myIndexFile;
    string fileName = "indexFile.txt";
    int fileExtensionMeas3 = 7744;
    myIndexFile.open(fileName.c_str());
    string word1; string word2; string word3;
    string *file1 = new string[fileExtensionMeas3];
    string *file2 = new string[fileExtensionMeas3];
    string *file3 = new string[fileExtensionMeas3];
    int i = 0;
    while(myIndexFile >> word1 && myIndexFile >> word2 && myIndexFile >> word3){
        file1[i] = path+word1;
        file2[i] = path+word2;
        file3[i] = path+word3;
        i = i+1;
    }
    
    TGraph *g1; TGraph *g2; TGraph *g3;
    TH1D *hist1 = new TH1D("h1", "h1", 200, 0.02, 0.13);
    TH1D *hist2 = new TH1D("h2", "h2", 200, -1.0, 1.0);
    TH1D *hist3 = new TH1D("h3", "h3", 200, -2.0, 1.0);
    vector<double> vals1; vector<double> vals2; vector<double> vals3;
    vector<string> maybeAfterPulseHere;
    for(int i = 0; i < fileExtensionMeas3; i++){
        fileReader(file1[i], g1); // filling the TGraph
        fileReader(file2[i], g2);
        fileReader(file3[i], g3);
        vals1 = function_1(g1);
        hist1->Fill(vals1[0]);
        vals2 = function_2(g2);
        hist2->Fill(vals2[0]);
        vals3 = function_2(g3);
        hist3->Fill(vals3[0]);
        int fileLen = g1->GetN();
        double *allAmpl = new double[fileLen];
        bool flag = false;
        for(int j = 6000; j < fileLen; j ++){
            allAmpl[j] = g1->GetPointY(j);
            if(allAmpl[j] - vals1[2] > 5*vals1[1] && allAmpl[j-1] - vals1[2] > 5*vals1[1]){
                flag = true;

             }
        }
        g2 = nullptr;
        delete g2;
        g3 = nullptr;
        delete g3;
        
        if(flag){
            maybeAfterPulseHere.push_back(file1[i]);
        }
    }
    
    TCanvas *c = new TCanvas("canvas", "canvas");
    c->Divide(1,3);
    c->cd(1);
    hist1->Draw("hist");
    c->cd(2);
    hist2->Draw("hist");
    c->cd(3);
    hist3->Draw("hist");
//
//
    TFile newFile;
    newFile.Open("maybeAfterPulseHere.root", "RECREATE");
    TGraph *g4; TGraph *g5; TGraph *g6;
    for(int i = 100; i < maybeAfterPulseHere.size(); i++){
        int pos = maybeAfterPulseHere[i].find("measure3");
        string eventNumber = maybeAfterPulseHere[i].substr(pos+9,8);
        string scint1 = path+"C2_"+eventNumber+".txt";
        string scint2 = path+"C3_"+eventNumber+".txt";
        fileReader(maybeAfterPulseHere[i], g4);
        string grName = eventNumber+"_1";
        g4->SetName(grName.c_str());
        g4->SetTitle(grName.c_str());
        grName = eventNumber+"_2";
        fileReader(scint1, g5);
        g5->SetName(grName.c_str());
        g5->SetTitle(grName.c_str());
        grName = eventNumber+"_3";
        fileReader(scint2, g6);
        g6->SetName(grName.c_str());
        g6->SetTitle(grName.c_str());
        g4->Write();
        g5->Write();
        g6->Write();

    }
}

No messages at all. It just does not complete the for loop “for(int i = 0; i < fileExtensionMeas3; i++)”

Ah ok, so the problem is there, that’s not a plotting issue. The plotting part is simply never reached because the loop because the loop before never finishes. Once again it is is difficult to debug without running it. try to understand (with printouts for instance) where you have an infinite loop …

The loop actually stops without reaching the end, it stops at different points each time. Printouts are confirming it too. One thing I recently noticed is that the loops completes (and the histograms get plotted) if I comment out some sections, as it follows:

void waveforms(){
    string path = "path";
    fstream myIndexFile;
    string fileName = "measure3Files.txt";
    int fileExtensionMeas3 = 7744;
    myIndexFile.open(fileName.c_str());
    string word1; string word2; string word3;
    string *file1 = new string[fileExtensionMeas3];
    string *file2 = new string[fileExtensionMeas3];
    string *file3 = new string[fileExtensionMeas3];
    int i = 0;
    while(myIndexFile >> word1 && myIndexFile >> word2 && myIndexFile >> word3){
        file1[i] = path+word1;
        file2[i] = path+word2;
        file3[i] = path+word3;
        i = i+1;
    }
    
    TGraph *g1; TGraph *g2; TGraph *g3;
    TH1D *hist1 = new TH1D("h1", "h1", 200, 0.02, 0.13);
    TH1D *hist2 = new TH1D("h2", "h2", 200, -1.0, 1.0);
    TH1D *hist3 = new TH1D("h3", "h3", 200, -2.0, 1.0);
    vector<double> vals1; vector<double> vals2; vector<double> vals3;
    vector<string> maybeAfterPulseHere;
    for(int i = 0; i < fileExtensionMeas3; i++){
        fileReader(file1[i], g1); // filling the TGraph
        fileReader(file2[i], g2);
        fileReader(file3[i], g3);
        vals1 = function_1(g1);
        hist1->Fill(vals1[0]);
        vals2 = function_2(g2);
        hist2->Fill(vals2[0]);
        vals3 = function_2(g3);
        hist3->Fill(vals3[0]);
        cout << file1[i] << " " << vals1[0] << " " << vals2[0] << " " << vals3[0] << endl;
        int fileLen = g1->GetN();
        double *allAmpl = new double[fileLen];
        bool flag = false;
        for(int j = 6000; j < fileLen; j ++){
            allAmpl[j] = g1->GetPointY(j);
            if(allAmpl[j] - vals1[2] > 5*vals1[1] && allAmpl[j-1] - vals1[2] > 5*vals1[1]){
                flag = true;

             }
        }
        g2 = nullptr;
        delete g2;
        g3 = nullptr;
        delete g3;
        
//        if(flag){
//            maybeAfterPulseHere.push_back(file1[i]);
//        }
    }
    
    TCanvas *c = new TCanvas("canvas", "canvas");
    c->Divide(1,3);
    c->cd(1);
    hist1->Draw("hist");
    c->cd(2);
    hist2->Draw("hist");
    c->cd(3);
    hist3->Draw("hist");
//
//
//    TFile newFile;
//    newFile.Open("maybeAfterPulseHere.root", "RECREATE");
//    TGraph *g4; TGraph *g5; TGraph *g6;
//    for(int i = 100; i < maybeAfterPulseHere.size(); i++){
//        int pos = maybeAfterPulseHere[i].find("measure3");
//        string eventNumber = maybeAfterPulseHere[i].substr(pos+9,8);
//        string scint1 = path+"C2_"+eventNumber+".txt";
//        string scint2 = path+"C3_"+eventNumber+".txt";
//        fileReader(maybeAfterPulseHere[i], g4);
//        string grName = eventNumber+"_1";
//        g4->SetName(grName.c_str());
//        g4->SetTitle(grName.c_str());
//        grName = eventNumber+"_2";
//        fileReader(scint1, g5);
//        g5->SetName(grName.c_str());
//        g5->SetTitle(grName.c_str());
//        grName = eventNumber+"_3";
//        fileReader(scint2, g6);
//        g6->SetName(grName.c_str());
//        g6->SetTitle(grName.c_str());
//        g4->Write();
//        g5->Write();
//        g6->Write();

//    }
}

But I cannot see why.

so it looks likes these 3 line make le loop stop and end the program ? that’s weird, even more if you do not get any errors. Once more, we cannot help you further without something we can run. Sorry for that.

One thing to try, invert the order of these:

to

        delete g2;
        g2 = nullptr;
        delete g3;
        g3 = nullptr;
2 Likes

That actually worked. Thank you so much

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.