About plotting multidimensional histogram

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:

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.

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:

/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

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

1 Like

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

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

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