Re: [ROOT] Problem with Destructor

From: Rene Brun (Rene.Brun@cern.ch)
Date: Thu Nov 06 2003 - 21:32:10 MET


Hi Attilio,

You can store your variable k1 as a member of your class.
Another alternative, if all your Trees  are created in the
same file, is to call
   file->Write() in your destructor. This will take care
of saving all objects associated with this file 
to the file. In this case you do not need k1 to be a class member.
Note that your logic in the destructor (the way you open the file)
is wrong. If the file is already open, you should not open it again.

Rene Brun

On 
Thu, 
6 Nov 
2003, Attilio Santocchia wrote:

> Hello Everybodys,
> I have a problem with the analysis software in CMS: I'm writing some 
> events variable in a rootpla and to do this I wrote a new class called 
> MyRootTree. This is a semplified version of this class:
> 
> class MyRootTree : public TObject {
> public:
>   MyRootTree(int k1);
>   ~MyRootTree();
>    void FillMyRootupla(int ia, vector<RawJet>&);
> private:
> ...
> };
> 
> As you can see the creator of the class has a parameter which is used to 
> create k1 different tree. This creator is something like this:
> 
> MyRootTree::MyRootTree(int k1){
>   for (int i=0;i<k1;i++) { 
>           t[i] = new TTree(Form("t%d",i),Form("MC-Rec Jets %d",i));        
>           t[i]->Branch("irun",&irun,"irun/I");
>   ...}
> }
> 
> The main analysis is done using a class with a constructor, a 
> UserAnaysis called for each event and a destructor.
> What I'm doing now is create in the analysis constructor an object for  
> MyRootTree:
> int NumTree=5
> theRootupla = new MyRootTree(NumTree)
>  
> Then I'm using the method FillMyRootupla to fill all the branches for 
> each tree for each event I'm processing...
> Finally in the destructor of the Main Anlysis class I call the destrucor 
> of the object theRootupla
> delete theRootupla;
> 
> And the destructor of the class MyRootTree is something like this:
> 
> MyRootTree::~MyRootTree() {
>   int k1 = 5;
>   TFile file1;
>   file1.Open(...);
>   for (int i=0;i<k1;i++) {  t[i][j][k]->Write(); }
>   file1.Close();
> }
> 
> My problem is how to tell to the destructor how many trees have to be 
> written in the file... the actual situation is that each time I manually 
> define k1= the exact number of the tree created before but I really 
> would like to have an automatic way to pass to the destructor the 
> correct parameter numTree defined above... but it's not possible to pass 
> parameters do destracutors... am I right? Do you have any suggestion on 
> how to do it or a more elegant way to build the class to store the 
> output of the analysis?
>  
> Thank you very much.
> 
> Attilio
>  
> 
> 



This archive was generated by hypermail 2b29 : Thu Jan 01 2004 - 17:50:16 MET