Re: easy histogram disabling

From: Philippe Canal <pcanal_at_fnal.gov>
Date: Fri, 1 Jul 2011 07:52:25 -0500


Hi Stefan,

 > - that is, functionality of (the objects of) given class that is not specific to any instantiated object, but is instead common to the class.
 > Constructors are an example of this, but what else?

Literally speaking, any of the member functions that are declared 'static' (and that's it).

 > I have used code like
 > MyObject::Bla() {
 >   if (!this) return;
 >   ...
 > }
 > before, but I guess it only worked back then, because the Bla() wasn't virtual.

Even in this case, it work only by (common) accident. Depending on the class hierarchy and there the function is declared, the value of 'this' might be offset compared to the value of the pointer you have. For example if you have a class derived from TObject and MyObject (in that order), and you call Blah on a pointer of that type (but whose value is zero) then when executing Blah, 'this' will not be zero but will be equal to sizeof(TObject) ....

 > So one can in principle imagine an implementation of the class which can handle such condition.  > But is something like that done ever in C++?

The only (legal) way is by introduce another layer of indirection (which can be very thin) which somehow holds on to the actual pointer and checks it before using it.

Cheers,
Philippe.

On 6/29/11 9:28 AM, Stefan Piperov wrote:
>
> Thiemo's question has some academic value: It is of the use of "Class methods" - that is, functionality of (the objects of) given
> class that is not specific to any instantiated object, but is instead common to the class.
>
> Constructors are an example of this, but what else?
>
> In his example, he wants to achieve functionality which is dependent upon the existance of the histogram object, through the use
> of a pointer. If the pointer is null, he wants one functionality; and if the pointer is not null - another. That seems like pretty
> reasonable expectation, given that the pointer is of a very well defined class. So one can in principle imagine an implementation
> of the class which can handle such condition.
>
> But is something like that done ever in C++?
>
> Stefan.
>
>
>
> On Mon, 27 Jun 2011, Philippe Canal wrote:
>
>> 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 Fri Jul 01 2011 - 14:54:08 CEST

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