How to draw histo BEFORE return?

My problem is the following: I want to write a macro which draws an histogram for each event in a tree, and let hte user change it from time to time. Here’s the code:


#include "TGraphAsymmErrors.h"
#include "TString.h"
#include "TFile.h"
#include "TH1F.h"
#include "TRandom3.h"
#include "TCanvas.h"
#include "TMath.h"
//#include "Math/SpecFuncMath.h"



#include <iostream>
#include <fstream>
#include <sstream>
#include <cmath>
#include <math.h>


void DrawPads(const char * file_name){
    TH2I * mmq = new TH2I ("mmq","q in pads",35,-2,33,31,0,31);
    

    TFile * inFile =NULL;

    inFile=TFile::Open(file_name);
    if (inFile->IsOpen()){
        cout << "Processing: " << file_name << " ... " << endl;
    }
    else{
        cout<<"failed opening of" << file_name <<endl; 
        return;
    }


    TCanvas *cq = new TCanvas("cq"," charge in pads",1);
    TH2F * opening = new TH2F("histos","_histos",35,-1,34,31,0,31);   
    opening->Draw("");
    

    int padx[70][10];
    int pady[70][10];
    
    int x,y,t;
    int padq[70][10];
    int clusters;
    int multiplicity[70];
    
    TTree *intree = (TTree*)inFile->Get("outtree");
    intree->SetBranchAddress("pad_charge",&padq);
    intree->SetBranchAddress("pad_x",&padx);
    intree->SetBranchAddress("pad_y",&pady);
  
    intree->SetBranchAddress("rob_clusters",&clusters);
    intree->SetBranchAddress("multiplicity",&multiplicity);
    int i=0;
    int control=1;
    while(control){
        cout << "select event. -1 to exit\n"<<endl;
        cin >> i;
        if(i==-1){break;}
        intree->GetEntry(i);
        for(int j=0;j<clusters;j++){
            for(int k=0;k<multiplicity[j];k++){
                mmq->SetBinContent(padx[j][k],pady[j][k],padq[j][k]);
                
            }

        }
    
    

    cq->cd();
    mmq->Draw("colz");
    
    
    }

return ;

}

It all works well except that the histogram is drawn only when the “-1” command is given, that is when the user stops the program (only last event is shown) and return is read.
What am I doing wrong? thank you


Please read tips for efficient and successful posting and posting code

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


After “Draw”, try to add:
gPad->Modified(); gPad->Update(); // make sure it's really (re)drawn

In principle, instead of a simple “cin >> i;”, you need to use some “non-blocking I/O” loop (with “gSystem->ProcessEvents();” inside).

thanks, now the problem is that the histograms are “added” at every “i” given as input (entries increasing). How should I pass the “i” input using gSystem->ProcessEvents() ?

that the histograms are “added”

How are they “added”, can you describe or show what happens that you don’t want?

let’s say the first histogram is drawn and has 45 entries, if I then draw a second histo with 50 entries, i obtain a histo with 95 entries, instead I want ONLY the 50 entries of the second histogram.
Anyway I solved this problem (a little bit cumbersomely) by redefining (and thus subscribing) histos in each call of the loop.
I hope I made myself clear, please let me know…

Right before (or after) “GetEntry”, add: mmq->Reset("M");

That’s surprising - SetBinContent() should replace whatever has bin in that bin before.

Ah right - there’s an if, so indeed, some bins will keep their values from the previous TTree entry. What @Wile_E_Coyote says! :slight_smile:

Thanks. Now everything works fine.

1 Like