How can i draw scatter plot?

Hi experts!
I’m root beginner.
I try to draw crystal1:crystal2 scatter plot. However, it comes out as undeclared identifier ‘tree’ as shown in the picture below. What is the problem?

and this is the data structure. Branches are numbered from crystal1 to crystal8.


Please read tips for efficient and successful posting and posting code

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


// TFile *f = TFile::Open("mrgd_M001506.root.000");
// TTree *ntp; f->GetObject("ntp", ntp);
ntp->Draw("some_leaf : other_leaf");

Thanks for reply!
What is ntp?
And actually I want to draw a scatter plot of the coincident events of crystal 2_energy and crystal 1_energyD,crystal3_energyD,crystal4_energyD,crystal6_energyD,crystal7_energyD.
In other words, the x-axis is crystal 2_energy , and the y-axis is crystal 1, 3, 6, 7_energyD.
What should i do?

The name of your tree is “ntp”.
Attach the output of: ntp->Print();

There are so many branches, can I just pick one and show it? For crystal 1

*Br 12 :crystal1 : nc/I:t0/F:t1/F:rqc/F:rqcp/F:rqcn/F:qtail/F:qc/F:qc1/F: *

  •     | qc1_5/F:qc2/F:qc3/F:qc4/F:qc5/F:qc50/F:qc50_100/F:qc100_200/F:   *
    
  •     | qc200_400/F:qc400_1000/F:mt/F:x1/F:x2/F:tzero/F:nmt30/F:nmt300/F:*
    
  •     | nmt500/F:nmt1000/F:nmt1500/F:nmt2000/F:nq30/F:nq300/F:nq500/F:   *
    
  •     | nq1000/F:nq1500/F:nq2000/F:nx1/F:nx2/F:nx2_80/F:nx2_120/F:       *
    
  •     | cr_50/F:cr_100/F:cr_150/F:energy/F:energyD                       *
    

*Entries : 133495 : Total Size= 23504480 bytes File Size = 1432798 *
*Baskets : 59 : Basket Size= 6665216 bytes Compression= 16.40 *

Try maybe: ntp->Draw("crystal1.energyD : crystal2.energy");

1 Like

Thank you for reply !
So can i draw crystal1.energyD :crystal2.energy, crystal1.energyD :crystal3.energy, crystal1.energyD :crystal4.energy in one plot? I wanna see coincident event between crystal 1 and other crystals.

That will produce 3 scatter plots. If you what to have these 4 variable in one single plot you can do:

ntp->Draw("crystal1.energyD:crystal2.energy:crystal3.energy:crystal4.energy");

That will produce a 3D scatter for 3 variables and the 4th one will be map on the current colormap.

{
  // TFile *f = TFile::Open("mrgd_M001506.root.000");
  // TTree *ntp; f->GetObject("ntp", ntp);
  TCanvas *c = new TCanvas("c", "c", 900, 300);
  c->Divide(3, 1);
  c->cd(1);
  ntp->Draw("crystal1.energyD : crystal2.energy");
  c->cd(2);
  ntp->Draw("crystal1.energyD : crystal3.energy");
  c->cd(3);
  ntp->Draw("crystal1.energyD : crystal4.energy");
  c->cd(0);
}

Thanks for reply. But i want to overlap it one spectrum. With out dividing the canvas.

{
  // TFile *f = TFile::Open("mrgd_M001506.root.000");
  // TTree *ntp; f->GetObject("ntp", ntp);
  TCanvas *c = new TCanvas("c", "c", 900, 300);
  c->DrawFrame(x1,y1,x2,y2); // make sure x1,y1,x2,y2 are wide enough
  ntp->Draw("crystal1.energyD : crystal2.energy","","SAME");
  ntp->Draw("crystal1.energyD : crystal3.energy","","SAME");
  ntp->Draw("crystal1.energyD : crystal4.energy","","SAME");
}
{
  // TFile *f = TFile::Open("mrgd_M001506.root.000");
  // TTree *ntp; f->GetObject("ntp", ntp);
  Double_t xmin = ntp->GetMinimum("crystal1.energy");
  xmin = TMath::Min(xmin, ntp->GetMinimum("crystal2.energy"));
  xmin = TMath::Min(xmin, ntp->GetMinimum("crystal3.energy"));
  xmin = TMath::Min(xmin, ntp->GetMinimum("crystal4.energy"));
  Double_t xmax = ntp->GetMaximum("crystal1.energy");
  xmax = TMath::Max(xmax, ntp->GetMaximum("crystal2.energy"));
  xmax = TMath::Max(xmax, ntp->GetMaximum("crystal3.energy"));
  xmax = TMath::Max(xmax, ntp->GetMaximum("crystal4.energy"));
  Double_t ymin = ntp->GetMinimum("crystal1.energyD");
  Double_t ymax = ntp->GetMaximum("crystal1.energyD");
  gStyle->SetCanvasPreferGL(1); // needed for color transparency
  Double_t alpha = 0.25; // e.g. 0.25 or 0.33 or 0.5
  TCanvas *c = new TCanvas("c", "c");
  c->DrawFrame(xmin, ymin, xmax, ymax,
               "all crystals;crystal*.energy;crystal1.energyD");
  ntp->SetMarkerColorAlpha(kBlack, alpha);
  ntp->Draw("crystal1.energyD : crystal1.energy", "", "same");
  ntp->SetMarkerColorAlpha(kRed, alpha);
  ntp->Draw("crystal1.energyD : crystal2.energy", "", "same");
  ntp->SetMarkerColorAlpha(kGreen, alpha);
  ntp->Draw("crystal1.energyD : crystal3.energy", "", "same");
  ntp->SetMarkerColorAlpha(kBlue, alpha);
  ntp->Draw("crystal1.energyD : crystal4.energy", "", "same");
}
1 Like

I don’t know why you use this code

Double_t xmin = ntp->GetMinimum("crystal1.energy");
  xmin = TMath::Min(xmin, ntp->GetMinimum("crystal2.energy"));
  xmin = TMath::Min(xmin, ntp->GetMinimum("crystal3.energy"));
  xmin = TMath::Min(xmin, ntp->GetMinimum("crystal4.energy"));
  Double_t xmax = ntp->GetMaximum("crystal1.energy");
  xmax = TMath::Max(xmax, ntp->GetMaximum("crystal2.energy"));
  xmax = TMath::Max(xmax, ntp->GetMaximum("crystal3.energy"));
  xmax = TMath::Max(xmax, ntp->GetMaximum("crystal4.energy"));
  Double_t ymin = ntp->GetMinimum("crystal1.energyD");
  Double_t ymax = ntp->GetMaximum("crystal1.energyD");

To compute the DrawFrame's parameters.

Is this a necessary process? Can’t we just use the range when drawing on the canvas?

I follow the code but it shows
Error in TCanvas::Range: illegal world coordinates range: x1=0.000000, y1=-0.125000, x2
=0.000000, y2=1.125000
Error in TCanvas::RangeAxis: illegal axis coordinates range: xmin=0.000000, ymin=0.0000
00, xmax=0.000000, ymax=1.000000
JIT has not been linked in.

{
gROOT->SetStyle(“Plain”);
TChain *ch = new TChain(“ntp”);
char filename[300];
for (int i=1; i<=578; i++){
TString datadir = “/data/COSINE/MRGD/phys/V00-04-04/”;
sprintf(filename,“mrgd_M001544.root.%.3d”,i);
ch->Add(datadir+filename);
}
Double_t xmin = ch->GetMinimum(“crystal2.energyD”);
xmin = TMath::Min(xmin, ch->GetMinimum(“crystal1.energyD”));
xmin = TMath::Min(xmin, ch->GetMinimum(“crystal3.energyD”));
xmin = TMath::Min(xmin, ch->GetMinimum(“crystal4.energyD”));
xmin = TMath::Min(xmin, ch->GetMinimum(“crsytal6.energyD”));
xmin = TMath::Min(xmin, ch->GetMinimum(“crystal7.energyD”));
Double_t xmax = ch->GetMaximum(“crystal2.energyD”);
xmax = TMath::Max(xmax, ch->GetMaximum(“crystal1.energyD”));
xmax = TMath::Max(xmax, ch->GetMaximum(“crystal3.energyD”));
xmax = TMath::Max(xmax, ch->GetMaximum(“crystal4.energyD”));
xmax = TMath::Max(xmax, ch->GetMaximum(“crystal6.energyD”));
xmax = TMath::Max(xmax, ch->GetMaximum(“crystal7.energyD”));
Double_t ymin = ch->GetMinimum(“crystal2.energy”);
Double_t ymax = ch->GetMaximum(“crystal2.energy”);
gStyle->SetCanvasPreferGL(1);
TCanvas *c = new TCanvas(“c”,“c”);
c->DrawFrame(xmin, ymin, xmax, ymax, “Coincident event”);

ch->Draw(“crystal1.energyD:crystal2.energy”,"",“same”);
ch->Draw(“crystal3.energyD:crystal2.energy”,"",“same”);
ch->Draw(“crystal4.energyD:crystal2.energy”,"",“same”);
ch->Draw(“crystal6.energyD:crystal2.energy”,"",“same”);
ch->Draw(“crystal7.energyD:crystal2.energy”,"",“same”);

TFile o(“C2_Coincidentevent_1544.root”,“RECREATE”);
}
This is the code i wrote.

What are the values of xmin, ymin, xmax, ymax just before calling DrawFrame ?

You mean the values of energy? I couldn’t understand your question.

No, I mean check the values you pass to DrawFrame. They seems not correct.
Put the following line just before DrawFrame to check:

printf("xmin = %g, ymin = %g, xmax = %g, ymax = %g",xmin, ymin, xmax, ymax);