Scatter plot

Dear experts,

I have 2 histograms (h1 and h2) where each bin represent a specific category. So for example, h1 and h2 has:
bin_1 (=category 1) which correspond to a number
bin_2 (=category 2) which correspond to another number

Now I want to draw a correlation plot to see h1 vs h2 for each category. Can you please confirm that TH2D is the good way to do that?

if yes, to set the bin content of the TH2D, which is different for h1 and h2, I saw this method:
SetBinContent(Int_t binx, Int_t biny, Int_t, Double_t content)

I as but no information is provide, I wonder what the 3rd parameter corresponds to? and if it is the good method to set the bin content for the TH2D for x and y independently?

well, if you wonder this, may be TH2 is not the right object in your case…

Dear Couet,

thank for your answer but do you know what would works for what I need?

Regards

I guess TH1 might be enough … ?

Dear Couet,

Sorry, I guess I should be more clear. For example if I want to know in which category each event fall at reconstructed (reco) and generated (gen) level, I fill a TH2D histo like:
TH2D->Fill( category_reco, category_gen); to see the correlation

and my THD2 X (reco) and Y (gen) axis title will be like this for each bin:
bin 1: category 1
bin 2: category 2

So basically I have a list of integer variable ( category_1_reco, category_2_reco, …, category_1_gen, category_2_gen… ) which is equal to 1 if the event fall in this category, and now I want to know how I can fill the TH2D histogram accordingly, or how to associate the integer variable to the bin axis?

Hi,

It is not very clear what you want to do exactly. You have two variables reco_category and gen_category.
But, is each event having only one reco_category and gen_category or it can have several of them ?
And what exactly do you want to plot ?

If you are not more specific and clear , we cannot really help you

Lorenzo

Hi Lorenzo,

I have a list of integer variables:

for the reco category
category_1_reco,
category_2_reco,

category_n_reco,

and for the gen category
category_1_gen,
category_2_gen,

category_n_gen,

Actually each variable can fall into several category at reco and gen. So for example:
event 1 = category_1_reco and category_2_reco, and category_1_gen.
event 2 = category_2_reco and category_n_reco, and category_2_gen and category_n_gen and.
event 3 = category_n_reco, and category_n_gen

Now I want to know how I can fill my TH2D histo for each event to see the reco vs gen correlation.
Actually I should use something like th2->Fill( cat_reco, cat_gen) but here I have mutiple possibility (not just cat_reco, cat_gen)?

Regards

Try this “pseudo-code”: [code]// assuming that "category_(i+1)[reco|gen] - category(i)_[reco|gen] = 1"
TH2D *MyTH2D = new TH2D(“MyTH2D”, “gen versus reco;reco cat.;gen cat.”,
number_of_reco_categories, category_1_reco - 0.5, category_n_reco + 0.5
number_of_gen_categories, category_1_gen - 0.5, category_n_gen + 0.5);

// loop over all events
Long64_t NOE = number_of_events;
for (Long64_t i = 0; i < NOE; i++) {
// loop over all found “reco” categories for an event
Long64_t NOFRC = event[i]->NumberOfFoundRecoCategories();
for (Long64_t j = 0; j < NOFRC; j++) {
MyTH2D->Fill(event[i]->FoundRecoCategory[j], // "x"
event[i]->GenCategory(), // "y"
1.0 / NOFRC); // “weight”
}
}[/code]

Dear Coyote,

thank you for the piece of code. It’s not too far from what I want to do. Bellow is my code:

#####################

// Define histogram
TH2D *hcorr = new TH2D("hcorr","",10,0,10,10,0,10 );
// Define histogram axis
TAxis* xAxis = hcorr->GetXaxis();
xAxis->SetBinLabel( 1, " category_1_reco ");
xAxis->SetBinLabel( 2, " category_2_reco ");
xAxis->SetBinLabel( n, " category_n_reco ");
TAxis* yAxis = hcorr->GetYaxis();
yAxis->SetBinLabel( 1, " category_1_gen ");
yAxis->SetBinLabel( 2, " category_2_gen ");
yAxis->SetBinLabel( 3, " category_1_gen ");

// Define categories == 0 or 1
int category_1_reco, category_2_reco, category_n_reco, category_1_gen, category_2_gen, category_n_gen;

// loop over all events
for (int i = 0; i < nb_event; i++) {

  category_1_reco = event.catReco1();
  category_2_reco = event.catReco2();
  category_n_reco = event.catRecon();
  category_1_gen = event.catGen1();
  category_2_gen = event.catGen2();
  category_n_gen = event.catGenn();

  // Arrays of 0 and/or 1
  int array_reco[n] = { category_1_reco, category_2_reco, category_n_reco };
  int array_gen[n]  = { category_1_gen, category_2_gen, category_n_gen };

  // loop over all found "reco" categories for an event
   for (int j = 0; j < n; j++) {

      if ( array_reco[i] == 0 ) continue;

     // loop over all found "gen" categories for an event
      for (int k = 0; k < n; k++) {

        if ( array_gen[k] == 0 ) continue;

        hcorr->Fill( array_reco[i], array_gen[k]);  // ***
      }
    }
}

But at (*) I still do not see how to map the variable category to the hcorr x-y-axis?
I saw the Int_t Fill(const char
namex, const char
namey, Double_t w) method
but how can I encode the TH2D histogram bin into a char so that I can use this Fill method?

For example I could fill the array with string and use a sort of:
hcorr->Fill( array_reco[i].c_str(), array_gen[k].c_str() ); … to increase the corresponding x-y-bins

Regards

Dear experts,

I figured out how to do that, thanks for your clue.

Regards

Hello calbet, could you described a little bit your solution? It could help other users in the future. Thanks in advance.

Dear Pamputt,

sure, from

    #####################

    // Define histogram
    TH2D *hcorr = new TH2D("hcorr","",10,0,10,10,0,10 );
    // Define histogram axis
    TAxis* xAxis = hcorr->GetXaxis();
    xAxis->SetBinLabel( 1, " category_1_reco ");
    xAxis->SetBinLabel( 2, " category_2_reco ");
    xAxis->SetBinLabel( n, " category_n_reco ");
    TAxis* yAxis = hcorr->GetYaxis();
    yAxis->SetBinLabel( 1, " category_1_gen ");
    yAxis->SetBinLabel( 2, " category_2_gen ");
    yAxis->SetBinLabel( 3, " category_1_gen ");

    // Define categories == 0 or 1
    int category_1_reco, category_2_reco, category_n_reco, category_1_gen, category_2_gen, category_n_gen;

    // loop over all events
    for (int i = 0; i < nb_event; i++) {

      category_1_reco = event.catReco1();
      category_2_reco = event.catReco2();
      category_n_reco = event.catRecon();
      category_1_gen = event.catGen1();
      category_2_gen = event.catGen2();
      category_n_gen = event.catGenn();

      // Arrays of 0 and/or 1
      int array_reco[n] = { category_1_reco, category_2_reco, category_n_reco };
      int array_gen[n]  = { category_1_gen, category_2_gen, category_n_gen };
      char array_creco[n] = { "category_1_reco", "category_2_reco", "category_n_reco" };
      char array_cgen[n]  = { "category_1_gen", "category_2_gen", "category_n_gen" };
  
      // loop over all found "reco" categories for an event
       for (int j = 0; j < n; j++) {

          if ( array_reco[j] == 0 ) continue;

         // loop over all found "gen" categories for an event
          for (int k = 0; k < n; k++) {

            if ( array_gen[k] == 0 ) continue;

            hcorr->Fill( array_creco[j], array_cgen[k]);  // ***
          }
        }
    }

*** I used the Fill with char method to increment the appropriate bin: root.cern.ch/root/html/TH2.html#TH2:Fill@6

Regards