# Histogram operation optimization

**URL:** <https://root-forum.cern.ch/t/histogram-operation-optimization/54639>\
**Category:** Newbie\
**Created:** [April 26, 2023, 3:53am UTC](https://root-forum.cern.ch/t/histogram-operation-optimization/54639 "2023-04-26T03:53:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![meyerma](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/meyerma/32/29724_2.png) [@meyerma](https://root-forum.cern.ch/u/meyerma)\
**Post date:** [April 26, 2023, 3:53am UTC](https://root-forum.cern.ch/t/histogram-operation-optimization/54639/1 "2023-04-26T03:53:15Z")

</div>

Hi,

I have implemented some histogram operations (flip x axis, fourier transform, convolution,…)  
This is done bin by bin and I would like to speed up the process. How would you proceed ?

I was thinking about getting the bin content array and loop over vectors with some -O2 compilation optimizations, but this is a bit more complex with 2D histograms…

Sorry for the silly question and confusions 🙂

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [April 26, 2023, 5:05am UTC](https://root-forum.cern.ch/t/histogram-operation-optimization/54639/2 "2023-04-26T05:05:41Z")

</div>

This is not a silly question. Your message suggests you managed to do what you want for 1D histograms.  
Maybe you can post here what you did for 1D. In any case, the best person to help you is @moneta .

---

<div class="post-metadata">

**Author:** ![meyerma](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/meyerma/32/29724_2.png) [@meyerma](https://root-forum.cern.ch/u/meyerma)\
**Post date:** [April 26, 2023, 3:22pm UTC](https://root-forum.cern.ch/t/histogram-operation-optimization/54639/3 "2023-04-26T15:22:58Z")

</div>

Hi

Sure, here is a typical naive example that I would like to optimize:

```auto
TH1* Flip(TH1* h, Option_t* option)
{
        if( h == NULL) return NULL;

        TString opt = option;
        opt.ToLower();

        TString hname = (TString) h->GetName() + ":" + opt + "-flip";
        TH1 *h0 = (TH1*) h->Clone();

        TAxis *x1, *x2, *x3;
        if(opt == "x") {

                x3 = h->GetXaxis();
                x1 = h->GetYaxis();
                x2 = h->GetZaxis();

        } else if(opt == "y") {

                x3 = h->GetYaxis();
                x1 = h->GetXaxis();
                x2 = h->GetZaxis();

        } else if(opt == "z") {

                x3 = h->GetZaxis();
                x1 = h->GetXaxis();
                x2 = h->GetYaxis();

        } else {

                
                return NULL;
        }

        int N = h->GetEntries();
        for(int x1_i = 1, x1_N = x1->GetNbins(); x1_i <= x1_N; x1_i++) {

                for(int x2_i = 1, x2_N = x2->GetNbins(); x2_i <= x2_N; x2_i++) {

                        double c1 = 0, c2 = 0;
                        double c1_err = 0, c2_err = 0;

                        for(int x3_i = 1, x3_N = x3->GetNbins(); x3_i <= x3_N; x3_i++) {

                                if(opt == "x") {

                                        c1 = h->GetBinContent(x3_i, x1_i, x2_i);
                                        c1_err = h->GetBinError(x3_i, x1_i, x2_i);
                                        c2 = h->GetBinContent(x3->GetNbins()+1-x3_i, x1_i, x2_i);
                                        c2_err = h->GetBinError(x3->GetNbins()+1-x3_i, x1_i, x2_i);

                                        h0->SetBinContent(x3_i, x1_i, x2_i, c2);
                                        h0->SetBinError(x3_i, x1_i, x2_i, c2_err);
                                        h0->SetBinContent(x3->GetNbins()+1-x3_i, x1_i, x2_i, c1);
                                        h0->SetBinError(x3->GetNbins()+1-x3_i, x1_i, x2_i, c1_err);
                                        
                                } else if (opt == "y") {

                                        c1 = h->GetBinContent(x1_i, x3_i, x2_i);
                                        c1_err = h->GetBinError(x1_i, x3_i, x2_i);
                                        c2 = h->GetBinContent(x1_i, x3->GetNbins()+1-x3_i, x2_i);
                                        c2_err = h->GetBinError(x1_i, x3->GetNbins()+1-x3_i, x2_i);

                                        h0->SetBinContent(x1_i, x3_i, x2_i, c2);
                                        h0->SetBinError(x1_i, x3_i, x2_i, c2_err);
                                        h0->SetBinContent(x1_i, x3->GetNbins()+1-x3_i, x2_i, c1);
                                        h0->SetBinError(x1_i, x3->GetNbins()+1-x3_i, x2_i, c1_err);
                                        
                                } else if (opt == "z") {

                                        c1 = h->GetBinContent(x1_i, x2_i, x3_i);
                                        c1_err = h->GetBinError(x1_i, x2_i, x3_i);
                                        c2 = h->GetBinContent(x1_i, x2_i, x3->GetNbins()+1-x3_i);
                                        c2_err = h->GetBinError(x1_i, x2_i, x3->GetNbins()+1-x3_i);

                                        h0->SetBinContent(x1_i, x2_i, x3_i, c2);
                                        h0->SetBinError(x1_i, x2_i, x3_i, c2_err);
                                        h0->SetBinContent(x1_i, x2_i, x3->GetNbins()+1-x3_i, c1);
                                        h0->SetBinError(x1_i, x2_i, x3->GetNbins()+1-x3_i, c1_err);
                                }
                        }
                }
        }

        h0->SetEntries(N);
        
        return h0;
}

```

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [April 27, 2023, 7:41am UTC](https://root-forum.cern.ch/t/histogram-operation-optimization/54639/4 "2023-04-27T07:41:20Z")

</div>

So it seems you implement your code for all cases ? X Y and Z ? what is the issue with the example ?

---

<div class="post-metadata">

**Author:** ![meyerma](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/meyerma/32/29724_2.png) [@meyerma](https://root-forum.cern.ch/u/meyerma)\
**Post date:** [April 27, 2023, 8:54am UTC](https://root-forum.cern.ch/t/histogram-operation-optimization/54639/5 "2023-04-27T08:54:09Z")

</div>

This code is handling one by one the histogram bin flip.  
So I was wondering whether it is possible le to take advantage of compiler optimizations, vectorization or parallelization of loops if possible.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [April 27, 2023, 9:17am UTC](https://root-forum.cern.ch/t/histogram-operation-optimization/54639/6 "2023-04-27T09:17:15Z")

</div>

I am not sure if there is a better way or not. I let @moneta reply.

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [June 2, 2023, 3:43pm UTC](https://root-forum.cern.ch/t/histogram-operation-optimization/54639/7 "2023-06-02T15:43:39Z")

</div>

Hi,  
At the moment we don’t have implemented parallelisation of loops of histogram contents. For taking advantage of vectorisation you should probably return the content in a the histogram array.  
Otherwise for convenience, and faster than looping on bins, is using the class `THistRange` and `TBinIterator`, see [ROOT: THistRange Class Reference](https://root.cern.ch/doc/master/classTHistRange.html)

Lorenzo
