Problem with array of TLorentzVector in branch of TClonesArray

From: Anyes Taffard <anyes_at_fnal.gov>
Date: Mon, 02 May 2005 11:57:13 -0500


Hi,

I am trying to put into a TTree a TClonesArray of a class that contains an array of TLorentzVector.

The content of the array of TLorentzVector is fine when the TTree has a branch of the class.
With root version 3.05/07, if the branch is a TClonesArray of that class, the LorentzVector are mostly empty apart for the 1st one. With root version 4.00/08, it's working fine.

I attached the code to make such a TTree.

Is there a way that I can get this to work with root 3.05/07 ?

Thanks
Anyes

.x run.C
tree(0); //to make tree
tree(1); //to read tree

#include "TFile.h"
#include "TChain.h"
#include "fit.hh"

/*
0: write tree
1: read tree

*/
void tree(int opt=0)
{   

  /*
    Make a TTree with:
    2 branches for 2 instences of fit
    1 branche with a TClonesArray of fit. (This should contain fit1 & fit2, but it is not)

    to run do:
    .x run.C
    tree(0); //to make the TTree
    tree(1); //to read the TTree

  */

   TFile* ff;
  TTree* tt;   

  //2 instances of fit class
  fit* _fit1;
  fit* _fit2;

  //TClone of Fit class
  TClonesArray* _fitA;
  fit* _fit;
  int _nFit;  

  if(opt==0){//WRITE
    ff = new TFile("tree.root","RECREATE","TTree File", 5);     tt = new TTree("t","A TTree");        

    _fit1 = new fit;
    _fit2 = new fit;
    tt->Branch("fit1.","fit",&_fit1, 32000, 1);     tt->Branch("fit2.","fit",&_fit2, 32000, 1);

    _nFit=0;
    _fitA = new TClonesArray("fit");
    tt->Branch("fitA","TClonesArray",&_fitA,32000,2);     tt->Branch("nFit",&_nFit,"nFit/I");     

  }
  else if(opt==1){//READ
    ff = new TFile("tree.root");
    tt = (TTree*) ff->Get("t");

    _fitA = new TClonesArray("fit");
    _nFit=0;

    _fit1 = new fit;
    _fit2 = new fit;

    tt->SetBranchAddress("fit1.",&_fit1);     tt->SetBranchAddress("fit2.",&_fit2);

    tt->SetBranchAddress("fitA",&_fitA);     tt->SetBranchAddress("nFit",&_nFit);     

  }

  srand(2055704344.000);   

  Int_t nEntries=1;
  if(opt==1){
    nEntries = tt->GetEntries();
    printf("Reading tree with %i entries\n",nEntries);     tt->Print();
  }

  int lflag=0;
  int fCurrent=-999;

  //
  //FILL/READ THE TREE
  //

  for(int ev=0; ev<nEntries; ev++){
    printf("\n event %i \n",ev);     

    if(opt==0){

      for(int kk=0; kk<2; kk++){ //make 2 instance of fit
	_fit = new ((*_fitA)[_nFit++]) fit();
	_fit->clear();
	
	int X = ev+kk;
	_fit->x=X;
	
	if(kk==0) _fit1->x=X;
	if(kk==1) _fit2->x=X;
	
	for(int ii=0; ii<3; ii++){ //# of combinatorics
	  for(int jj=0; jj<4; jj++){ //4 jets in the event
	    float px,py,pz,e;
	    
	    px = float(rand())/RAND_MAX *100;
	    py = float(rand())/RAND_MAX *100;
	    pz = float(rand())/RAND_MAX *100;
	    e  = float(rand())/RAND_MAX *100;
	    
	    _fit->rawjets[ii][jj].SetPxPyPzE(px,py,pz,e);
	    
	    if(kk==0) _fit1->rawjets[ii][jj].SetPxPyPzE(px,py,pz,e);
	    if(kk==1) _fit2->rawjets[ii][jj].SetPxPyPzE(px,py,pz,e);
	    
	  }//4 jets
	}//3 combo
      }//2 fit blocks

    }//FILL          if(opt==1){//READ
      tt->GetEntry(ev);
    }          

    if(opt==0 || opt==1){//DUMP

      for(int kk=0; kk<2; kk++){
	_fit = (fit*) _fitA->At(kk);
	for(int ii=0; ii<3; ii++){
	  printf("==>Combo %i \n",ii);
	  for(int jj=0; jj<4; jj++)
	    printf("====> Jets %i px %2.3f py %2.3f pz %2.3f e %2.3f \n",
		   jj,_fit->rawjets[ii][jj].Px(),
		   _fit->rawjets[ii][jj].Py(),
		   _fit->rawjets[ii][jj].Pz(),
		   _fit->rawjets[ii][jj].E());
	}
      }
      
      for(int ii=0; ii<3; ii++){
	printf("==>Combo %i \n",ii);
	for(int jj=0; jj<4; jj++)
	  printf("====>Fit1 Jets %i px %2.3f py %2.3f pz %2.3f e %2.3f \n",
		 jj,_fit1->rawjets[ii][jj].Px(),
		 _fit1->rawjets[ii][jj].Py(),
		 _fit1->rawjets[ii][jj].Pz(),
		 _fit1->rawjets[ii][jj].E());
      }
      
      for(int ii=0; ii<3; ii++){
	printf("==>Combo %i \n",ii);
	for(int jj=0; jj<4; jj++)
	  printf("====>Fit2 Jets %i px %2.3f py %2.3f pz %2.3f e %2.3f \n",
		 jj,_fit2->rawjets[ii][jj].Px(),
		 _fit2->rawjets[ii][jj].Py(),
		 _fit2->rawjets[ii][jj].Pz(),
		 _fit2->rawjets[ii][jj].E());
      }

    }//DUMP     if(opt==0) tt->Fill();
  }//evt loop

  if(opt==0){
    ff->Write();
    tt->Print();
  }   

}

// Local version of HighLevelObjects
#include "fit.hh"

fit::fit()
{
  clear();
}

//______________________________________________________________________________
void fit::clear(){
  x=-999;

  for(int i=0; i<24; i++){
    for(int j=0; j<4; j++){
      rawjets[i][j].Clear();
    }
  }

}

ClassImp(fit)

#ifndef FIT_HH
#define FIT_HH

#include "TObject.h"
#include "TLorentzVector.h"
#include <iostream>

class fit : public TObject {
public:
  fit();
  ~fit() {};
  void clear();   

  int x;
  TLorentzVector rawjets[24][4]; //work when not using TCloneA of fit     

  ClassDef(fit,1)
};
#endif

{
#include <TObjectTable.h>

const char* exec_name = gApplication->Argv(0); // the line below tells ROOT script compiler // where to look for the include files  

gSystem->SetIncludePath(" -I./include -I$CDFSOFT2_DIR/include -D__STANDARD_CPLUSPLUS -DDEFECT_NO_EXCEPTIONS ");
gSystem->Load("$ROOTSYS/lib/libPhysics.so");
gSystem->Load("$ROOTSYS/lib/libEG.so");

gInterpreter->ProcessLine(".! ps | grep root");

gSystem->CompileMacro("fit.cc","k");
gSystem->CompileMacro("tree.C","k");

//tree(0); //make the ttree
//tree(1); //read it back

} Received on Mon May 02 2005 - 18:57:21 MEST

This archive was generated by hypermail 2.2.0 : Tue Jan 02 2007 - 14:45:07 MET