dsahoo
April 18, 2018, 5:21am
1
Dear Experts,
I want to plot several variables from tree by using array. Here, I am trying to plot two variables from a tree (Bmass, Mumumass) by using array. I am attaching my macro here. I have defined two arrays : one for variables(Bmass, Mumumass) and another for cuts(selbm0, selbm1,…etc). It is giving segmentation violation error. I also want to define different histograms for these two variables. Could you please tell me how to get rid of the error?
Thanks,
Deepakmultivar.cc (1.7 KB)
couet
April 18, 2018, 7:25am
2
Your code has several mistakes which I tried to fix in the following code. I used clout
instead of TTree:Draw because I do not have your data file. but putting back the
Draw` command should be fine.
{
const int n = 2;
const int m = 7;
string varmass[n] = {"Bmass","Mumumass"};
string varsel [m] = {"selbm0","selbm1","selbm2","selbm3","selbm4","selbm5","selbm6"};
for (int i = 0; i<n; i++){
for (int j = 0; j<m; j++){
cout << Form("%s",varsel[j].c_str()) <<endl;
}
}
}
dsahoo
April 18, 2018, 10:10am
3
Hi Couet,
I tried, but it is giving error. I am attaching the snapshot of error.
Thanks,
Deepak
dsahoo
April 18, 2018, 2:32pm
5
Hi,
It is showing segmentation violation.
Thanks,
Deepakmultivar.cc (2.2 KB)
couet
April 18, 2018, 2:41pm
6
{
const int n = 2;
const int m = 7;
TCut Q[7];
Q[0]= "Q2> 1.0 && Q2 < 4.30";
Q[1]= "Q2> 4.30 && Q2 < 8.68";
Q[2]= "Q2> 8.68 && Q2 < 10.09";
Q[3]= "Q2> 10.09 && Q2 < 12.86";
Q[4]= "Q2> 12.86 && Q2 < 14.18";
Q[5]= "Q2> 14.18 && Q2 < 16.0";
Q[6]= "Q2> 16.0 && Q2 < 19.0";
for (int i = 0; i<n; i++){
for (int j = 0; j<7; j++){
tr->Draw("Q2",Q[j]);
}
}
}
dsahoo
April 18, 2018, 2:54pm
7
Hi,
Still, it is giving segmentation violation error.
Thanks,
Deepak
couet
April 18, 2018, 2:56pm
8
can you post you macro and a data file so i can run the real macro … I am just guessing right now as I do not have your data…
dsahoo
April 18, 2018, 3:02pm
9
Here is the macro and data file.
Thanks,
Deepakmultivar.cc (2.2 KB)
sel_BsToPhiMuMu_2016H_cut1_data_s0.root (844.4 KB)
couet
April 18, 2018, 3:05pm
10
This one is working for me:
{
gStyle->SetOptStat("emr");
TChain *ch = new TChain("tree");
ch->Add("sel_BsToPhiMuMu_2016H_cut1_data_s0.root");
TTree *tr = ch;
double Bmass= 0;
double Mumumass = 0;
double CosThetaK = 0;
double CosThetaL = 0;
tr->SetBranchStatus("*",0);
tr->SetBranchStatus("Bmass" , 1);
tr->SetBranchStatus("Q2" , 1);
tr->SetBranchStatus("Mumumass" , 1);
tr->SetBranchStatus("Mumumasserr" , 1);
tr->SetBranchStatus("Triggers" , 1);
tr->SetBranchStatus("CosThetaL" ,1);
tr->SetBranchStatus("CosThetaL" ,1);
const int n = 2;
const int m = 7;
TCut Q[7];
Q[0]= "Q2> 1.0 && Q2 < 4.30";
Q[1]= "Q2> 4.30 && Q2 < 8.68";
Q[2]= "Q2> 8.68 && Q2 < 10.09";
Q[3]= "Q2> 10.09 && Q2 < 12.86";
Q[4]= "Q2> 12.86 && Q2 < 14.18";
Q[5]= "Q2> 14.18 && Q2 < 16.0";
Q[6]= "Q2> 16.0 && Q2 < 19.0";
string varmass[n] = {"Bmass","Mumumass"};
// string varsel [m] = {"selbm0","selbm1","selbm2","selbm3","selbm4","selbm5","selbm6"};
for (int i = 0; i<n; i++){
for (int j = 0; j<7; j++){
tr->Draw("Q2",Q[j]);
}
}
}
{
gStyle->SetOptStat("emr");
TChain *tr = new TChain("tree");
tr->Add("sel_BsToPhiMuMu_2016H_cut1_data_s0.root");
// tr->Print();
tr->SetBranchStatus("*", 0);
tr->SetBranchStatus("Bmass", 1);
tr->SetBranchStatus("Mumumass", 1);
tr->SetBranchStatus("Phimass", 1);
tr->SetBranchStatus("Q2", 1);
const char *varmass[] = {"Bmass", "Mumumass", "Phimass"};
Int_t n = sizeof(varmass) / sizeof(char*);
TCut Q[] = {"Q2 > 1.0 && Q2 < 4.30",
"Q2 > 4.30 && Q2 < 8.68",
"Q2 > 8.68 && Q2 < 10.09",
"Q2 > 10.09 && Q2 < 12.86",
"Q2 > 12.86 && Q2 < 14.18",
"Q2 > 14.18 && Q2 < 16.0",
"Q2 > 16.0 && Q2 < 19.0"}
Int_t m = sizeof(Q) / sizeof(TCut);
TCanvas *c = new TCanvas("c", "c");
c->Divide(m, n);
// c->DivideSquare(n * m);
Int_t p = 1;
for (Int_t i = 0; i < n; i++) {
for (Int_t j = 0; j < m; j++) {
c->cd(p++);
tr->Draw(varmass[i], Q[j]);
}
}
c->cd(0);
}
1 Like
dsahoo
April 23, 2018, 8:56am
12
Hi Cooyote,
Many thanks. Could you please tell me “Int_t n = sizeof(varmass) / sizeof(char*);”, what it means?
Thanks,
Deepak
couet
April 23, 2018, 9:03am
13
To compute automatically n and m
dsahoo
April 23, 2018, 9:09am
14
Hi couet,
If I want to plot each distribution in one canvas, how ?
Thanks,
Deepak
couet
April 23, 2018, 9:17am
15
Open a new canvas for each plot.
dsahoo
April 25, 2018, 5:06am
16
Hi Couet,
I want to set ranges for Bmass, Mumumass and Phimass histograms. Could you please tell me how to do it?
Thanks,
Deepak
Try something like:
// ...
TCut Qmass[] = {"Bmass > 0. && Bmass < 1.",
"Mumumass > 0. && Mumumass < 1.",
"Phimass > 0. && Phimass < 1."}
// ...
tr->Draw(varmass[i], Qmass[i] && Q[j]);
dsahoo
April 25, 2018, 6:31am
18
Hi Wile,
I don’t want to apply mass cut. I want to define histogram for each variable(Bmass, Mumumass, Phimass) and set ranges of histograms.
Thanks,
Deepak
Try: tr->Project("SomeHistoName", varmass[i], Q[j]);
See: TTree::Project
dsahoo
April 25, 2018, 7:02am
20
Hi Wile,
How to define histogram for all the three variables?
Thanks,
Deepak
You need to create as many histograms as you need (with different “HistoNames”) and then use their names in the loop.