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;

    if (inFile->IsOpen()){
        cout << "Processing: " << file_name << " ... " << endl;
        cout<<"failed opening of" << file_name <<endl; 

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

    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");
    int i=0;
    int control=1;
        cout << "select event. -1 to exit\n"<<endl;
        cin >> i;
        for(int j=0;j<clusters;j++){
            for(int k=0;k<multiplicity[j];k++){



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