DrawCopy crashes program

Hello Rooters,

I am trying to draw several fifferent histograms in a while loop.
Since I modify the original histogram in each iteration I use ‘DrawCopy’ to draw the histograms.
I already did exactly the same with 1-dim histograms and it worked fine but with the 2-dim histograms the program crashes at the ‘DrawCopy’ command (note that eg. ‘Draw’ doesn’t crash the program).
I tried different ideas but nothing helped. Do you have any suggestion what could be the problem?

I should add that I am using VC++6.0 and ROOT 4.0.

Thanks a lot for any reply!
Gerolf

here is the piece of code:

int ichan;
while(ichan<=20) {
sprintf(tmp,“pmt==%d”,ichan);
TCut c1 = tmp;
c->GetCanvas()->cd(ichan);
tree->Draw(“s0:event>>bufhist2”,c1);
TH2F bufhist2 = (TH2F)gPad->GetPrimitive(“bufhist2”);
bufhist2->DrawCopy();
}

I cannot reproduce this problem. Could you send a very short script?

Rene

Hello Rene,
here is the script that I use!
It crashes at the ‘DrawCopy’ commmand.

Cheers,
Gerolf

int pedestal(TRootEmbeddedCanvas *c)
{
TH2F *h2 = new TH2F(“h2”,“title”,100,0,50,100,0,300);
char line[40];
struct event_t {
int evt;
int pmt;
int gain;
int s0;
int s1;
int s2;
int s3;
int s4;
int s5;
int s6;
};
event_t event;

FILE *fp = fopen(“test2.dat”,“r”);
TFile *f = new TFile(“test.root”,“RECREATE”);
TTree *tree = new TTree(“tree”,“pedestals”);
tree->Branch(“event”,&event.evt,“evt/I:pmt:gain:s0:s1:s2:s3:s4:s5:s6”);
while (fgets(line,40,fp)) {
sscanf(line," %d %d %d,%d,%d,%d,%d,%d,%d,%d\n",&event.evt,&event.pmt,&event.gain,&event.s0,&event.s1,&event.s2,&event.s3,&event.s4,&event.s5,&event.s6);
tree->Fill();
}

while(ichan<=20) {
sprintf(tmp,“pmt==%d”,ichan);
TCut c1 = tmp;
c->GetCanvas()->cd(ichan);
tree->Draw(“s0:event>>h2”,c1);
h2 = (TH2F*)gPad->GetPrimitive(“h2”);
h2->DrawCopy();
ichan++;
}

fclose(fp);
f->Write();
c->GetCanvas()->Modified();
c->GetCanvas()->Update();
c->GetCanvas()->Print(“testgraph.ps)”);
return 0;
}

please send the data files

Rene

Hello rene,

here is the data file.
I had to change the ending to ‘.txt’ because ‘.dat’ files were not allowed.

cheers,
Gerolf
test2.txt (553 KB)

Your script cannot be executed as such. It contains undefined
variables (eg tmp, ichan).
If it crashes when calling DrawCopy, it means that your pointer h2 is null.
ie, 0 events pass the cut.
If you do not find your problem, please send the shortest self-contained script reproducing the problem.

Rene

Hello Rene,

we continued trying to find the root of problem and arrived at the following piece of code. With tree->Draw(“s0:event>>h2”,c1,“N”) it works but without …,“N”) it does not work (see code).
Can you please tell us why?

Thanks,
Gerolf

PS.: Code and Data file are attached
PPS.: I hope this time the code is really ‘standalone’

#include
#include
#include
#include <Riostream.h>
#include <TSystem.h>
#include <stdio.h>
#include <TROOT.h>
#include <TRootApplication.h>
#include <TRootEmbeddedCanvas.h>
#include <TH2F.h>
#include <TFile.h>
#include <TCut.h>
#include <TTree.h>
#include <TCanvas.h>
#include <TApplication.h>
#include <TQObject.h>
#include <TVirtualX.h>

int main(int argc, char ** argv)
{

char line[40]; 

char tmp[70];
int ichan=1;
TH2F *h2 = 0;

struct event_t {
int evt;
int pmt;
int gain;
int s0;
int s1;
int s2;
int s3;
int s4;
int s5;
int s6;
};
event_t event;
//TApplication app(“test”,&argc,argv); // For compiling in Visual C++

TCanvas *c = new TCanvas("Canvas");
FILE *fp = fopen("test2.txt","r"); 
TFile *f = new TFile("test.root","RECREATE"); 
TTree *tree = new TTree("tree","pedestals"); 
tree->Branch("event",&event.evt,"evt/I:pmt:gain:s0:s1:s2:s3:s4:s5:s6"); 

while (fgets(line,40,fp)) { 
	sscanf(line," %d %d %d,%d,%d,%d,%d,%d,%d,%d\n",&event.evt,&event.pmt,&event.gain,&event.s0,&event.s1,&event.s2,&event.s3,&event.s4,&event.s5,&event.s6); 
	tree->Fill(); 
} 
c->GetCanvas()->Divide(2,2);
while(ichan<=4) { 
	sprintf(tmp,"pmt==%d",ichan); 
	TCut c1 = tmp; 
	c->GetCanvas()->cd(ichan); 
	if (h2) {
		delete h2;
		h2=0;
	}

	tree->Draw("s0:event>>h2",c1,"N");		// works  !!!!!!!!
	//tree->Draw("s0:event>>h2",c1);	// doesn't work  !!!!!!!!!

	h2 = (TH2F*)gPad->GetPrimitive("h2");
	gPad->Update();
	if (h2 != (TH2F*)gDirectory->Get("h2")) {
		cout << "Different "  << hex <<
			(int) h2 << " " << (int) (TH2F*)gDirectory->Get("h2") << dec << endl;
	}
	gSystem->Sleep(500);
	gPad->Clear();
	h2->Print();
	h2->DrawCopy();
	gPad->Update();
	ichan++; 
} 
fclose(fp); 

//app.Run(kTRUE); // For compiling in Visual C++
return 0; 

}
test2.txt (553 KB)

Your macro runs correctly with versions 3.10/07
and the current version 4.00/06.
I get the 4 scatter-plots (one per pad)

TH1.Print Name= h2, Entries= 400, Total sum= 400
TH1.Print Name= h2, Entries= 400, Total sum= 400
TH1.Print Name= h2, Entries= 400, Total sum= 400
TH1.Print Name= h2, Entries= 400, Total sum= 400

Rene