# About plotting multidimensional histogram

**URL:** <https://root-forum.cern.ch/t/about-plotting-multidimensional-histogram/59188>\
**Category:** Newbie\
**Created:** [May 2, 2024, 3:32am UTC](https://root-forum.cern.ch/t/about-plotting-multidimensional-histogram/59188 "2024-05-02T03:32:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rana](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/rana/32/36242_2.png) [@Rana](https://root-forum.cern.ch/u/Rana)\
**Post date:** [May 2, 2024, 3:32am UTC](https://root-forum.cern.ch/t/about-plotting-multidimensional-histogram/59188/1 "2024-05-02T03:32:38Z")

</div>

Hello Experts,

I wanted to fill a histogram from a multidimensional array and I got a canvas with the histogram when I wrote this command in terminal:

```auto
root [1] TH2F *h12 = new TH2F(“h12”,“x vs z”,100,1000,2000,100, -400, 400)
(TH2F *) 0x4051e50
root [2] TTree->Draw(“HitPos[0][][0]:HitPos[0][][2]>>h12”,“EventNo==1322&&HitPos[0][][0]!=-999”)"
Info in <TCanvas::MakeDefCanvas>: created default TCanvas with name c1
(long long) 98

```

However I am trying to write the same in my macro with an if condition but it returns me an error. I am attaching the block of code and the error message below.

```auto
if (EventNo == 1322 && HitPos[0][][0] != -999) {
std::cout << "EventNo: " << EventNo << ",HitPos[0][0]: " << HitPos[0][0] << std::endl;

}
h2D->Fill(HitPos[0][0], HitPos[0][2]);
}

TCanvas* canvas = new TCanvas(“canvas”, “canvas”, 800, 600);
h2D->Draw(“colz”); // “colz” option for a 2D color plot

```

The error message:

```auto
/home/Analysis.C:56:39: error: expected expression
if (EventNo == 1322 && HitPos[0][0] != -999) {
^
/home/Analysis.C:58:94: error: expected expression
std::cout << "EventNo: " << EventNo << ", HitPos[0][0]: " <<HitPos[0][0] << std::endl;
^

```

I will be grateful if you can suggest me a fix.

Best,  
R

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [May 2, 2024, 4:18am UTC](https://root-forum.cern.ch/t/about-plotting-multidimensional-histogram/59188/2 "2024-05-02T04:18:04Z")

</div>

Dear Rana,

`TTree::Draw` supports a selection DSL which includes expressions such as `HitPos[0][][0]`. However, that is not valid C++, and that is what the error is highlighting.

Your code would need a small refactoring to become valid C++ after the initial exploratory phase driven by the usage of `TTree::Draw`.

Best,  
D

---

<div class="post-metadata">

**Author:** ![Rana](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/rana/32/36242_2.png) [@Rana](https://root-forum.cern.ch/u/Rana)\
**Post date:** [May 5, 2024, 6:13am UTC](https://root-forum.cern.ch/t/about-plotting-multidimensional-histogram/59188/3 "2024-05-05T06:13:41Z")

</div>

Hi Danilo,

I tried few things out but it didn’t work out for me. Perhaps, I missed your point. Can you explain a bit more ?  
If I were to correct my code, can you point me where to look at?

Best,  
R

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [May 5, 2024, 1:48pm UTC](https://root-forum.cern.ch/t/about-plotting-multidimensional-histogram/59188/4 "2024-05-05T13:48:01Z")

</div>

Hi Rana,

If I understand correctly, you need to loop over all values of the second index of HitPos, fixing the first and third to 0, perhaps?

D

---

<div class="post-metadata">

**Author:** ![Rana](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/rana/32/36242_2.png) [@Rana](https://root-forum.cern.ch/u/Rana)\
**Post date:** [May 6, 2024, 12:49am UTC](https://root-forum.cern.ch/t/about-plotting-multidimensional-histogram/59188/5 "2024-05-06T00:49:21Z")

</div>

HI Danilo,  
I am analyzing a different tree in the same root file and the terminal command I am using is(and this works fine like before):  
Line\_Candidates-\>Draw(“RecoHitPos[0]:RecoHitPos[2]\>\>h12”,“EventNo\>1100&&EventNo\<=2147&&RecoHitPos[0]!=-999”)

In the c++ macro I have implemented this:

if (fChain == 0) return;

```
Long64_t nentries = fChain->GetEntriesFast(); 
						
cout << "Total Entries in fChain: " << nentries << endl;

Long64_t nbytes = 0, nb = 0;	

for (Long64_t jentry=0; jentry<nentries;jentry++)

 {

Long64_t ientry = LoadTree(jentry);

if (ientry < 0) break;
  
nb = b_RecoHitPos->GetEntry(ientry);   
nbytes += nb; 

for (Long64_t nHit=0; nHit<nHits;nHit++)

{
if (EventNo <= 2342 && RecoHitPos[nHit][0] != -999) 
 {
        // Fill the histogram with data from this event
       h2D->Fill(RecoHitPos[nHit][0], RecoHitPos[nHit][2]);
      }
 }

}

```

is the correct way to loop over nHIt?

Please let me know about your thoughts on this.

Best,  
R
