Plotting several variables by using array

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)

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 theDraw` 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;
      }
   }
}

Hi Couet,

I tried, but it is giving error. I am attaching the snapshot of error.

Thanks,
Deepak

Hi,

It is showing segmentation violation.

Thanks,
Deepakmultivar.cc (2.2 KB)

{
   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]);
    }
   }
}

Hi,

Still, it is giving segmentation violation error.

Thanks,
Deepak

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…

Here is the macro and data file.

Thanks,
Deepakmultivar.cc (2.2 KB)
sel_BsToPhiMuMu_2016H_cut1_data_s0.root (844.4 KB)

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

Hi Cooyote,

Many thanks. Could you please tell me “Int_t n = sizeof(varmass) / sizeof(char*);”, what it means?

Thanks,
Deepak

To compute automatically n and m

Hi couet,

If I want to plot each distribution in one canvas, how ?

Thanks,
Deepak

Open a new canvas for each plot.

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]);

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

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.