Re: easy histogram disabling

From: Philippe Canal <pcanal_at_fnal.gov>
Date: Mon, 27 Jun 2011 11:39:25 -0500


Hi Thiemo,

 >    if (!this) return;
 > would allow much cleaner code:
 >    h1->Fill(...);

For better or worse, this is not (and can not be) valid C++ code. In order to make the call to the Fill function (in particular because it is a virtual function), C++ requires dereference the pointer _before_ calling the function (because without this dereferencing it has _no_ way of knowing which function to call).

In order to support the coding style you are looking at, you will need to introduce one layer of indirection to handle the conditional statement. For example you could create a small class that wraps the pointer to the histogram and contain/factor out the conditional statement.

Cheers,
Philippe.

template <class HistClass> class DebugHisto { private:

      HistClass *fPointer;
public:

      DebugHisto(HistClass *hist) : fPointer(hist) {}
      ~DebugHisto() { delete fPointer; }

      Int_t Fill(Double value) { if (fPointer) fPointer->Fill(value): }
      etc..

};

On 6/27/11 7:54 AM, Thiemo Nagel wrote:
> Dear ROOTers,
>
> when debugging software, I often come across the issue, that I'd like to make extensive use of histograms to understand some
> problem in detail, however I'd like to be able to turn off these histograms (because of memory and speed issues) when the same
> code is used in production.
>
> The obvious solution is to write code along the lines of:
>
> TH3D *h1;
> if (debug)
> h1 = new TH3D(...);
> else
> h1 = NULL;
>
> ...
>
> if (debug1)
> h1->Fill(...);
> if (debug2)
> h2->Fill(...);
> if (debug3)
> h3->Fill(...);
>
> ...
>
> However, this impairs the readability of the code, especially when large numbers of histograms and different levels of debugging
> are used.
>
> To my mind, there would be a simple way to improve this. Adding to the top of the THxx::Fill() methods
>
> if (!this) return;
>
> would allow much cleaner code:
>
> h1->Fill(...);
> h2->Fill(...);
> h3->Fill(...);
>
> What do you think? Would you accept a patch?
>
> Cheers,
> Thiemo
>
Received on Mon Jun 27 2011 - 18:39:31 CEST

This archive was generated by hypermail 2.2.0 : Fri Jul 01 2011 - 17:50:01 CEST