TNtupleD.cxx

Go to the documentation of this file.
00001 // @(#)root/tree:$Id: TNtupleD.cxx 35940 2010-09-30 15:52:18Z brun $
00002 // Author: Rene Brun   12/08/2001
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 //////////////////////////////////////////////////////////////////////////
00013 //                                                                      //
00014 // TNtupleD                                                             //
00015 //                                                                      //
00016 // A simple tree restricted to a list of double variables only.         //
00017 //                                                                      //
00018 // Each variable goes to a separate branch.                             //
00019 //                                                                      //
00020 //  A Ntuple is created via                                             //
00021 //     TNtupleD(name,title,varlist,bufsize)                             //
00022 //  It is filled via:                                                   //
00023 //     TNtupleD::Fill(*x)  or                                           //
00024 //     TNtupleD::Fill(v1,v2,v3.....)                                    //
00025 //                                                                      //
00026 //////////////////////////////////////////////////////////////////////////
00027 
00028 #include "TNtupleD.h"
00029 #include "TTree.h"
00030 #include "TBranch.h"
00031 #include "TLeaf.h"
00032 #include "TBrowser.h"
00033 #include "Riostream.h"
00034 #include "TClass.h"
00035 
00036 ClassImp(TNtupleD)
00037 
00038 //______________________________________________________________________________
00039 TNtupleD::TNtupleD(): TTree()
00040 {
00041 //*-*-*-*-*-*Default constructor for Ntuple*-*-*-*-*-*-*-*-*-*-*-*-*-*
00042 //*-*        ==============================
00043 
00044    fNvar = 0;
00045    fArgs = 0;
00046 }
00047 
00048 //______________________________________________________________________________
00049 TNtupleD::TNtupleD(const char *name, const char *title, const char *varlist, Int_t bufsize)
00050        :TTree(name,title)
00051 {
00052 //*-*-*-*-*-*-*-*-*-*-*-*-*Create an Ntuple*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
00053 //*-*                      ================
00054 //       The parameter varlist describes the list of the ntuple variables
00055 //       separated by a colon:
00056 //         example:  "x:y:z:energy"
00057 //       For each variable in the list a separate branch is created.
00058 //
00059 //      NOTE:
00060 //       -Use TTree to create branches with variables of different data types.
00061 //       -Use TTree when the number of branches is large (> 100). 
00062 //*-*
00063 
00064    Int_t i;
00065    fNvar = 0;
00066    fArgs = 0;
00067 
00068 //   Count number of variables (separated by :)
00069    Int_t nch = strlen(varlist);
00070    if (nch == 0) return;
00071    char *vars = new char[nch+1];
00072    strlcpy(vars,varlist,nch+1);
00073    Int_t *pvars = new Int_t[nch+1];
00074    fNvar = 1;
00075    pvars[0] = 0;
00076    for (i=1;i<nch;i++) {
00077       if (vars[i] == ':') {
00078          pvars[fNvar] = i+1;
00079          vars[i] = 0;
00080          fNvar++;
00081       }
00082    }
00083    fArgs = new Double_t[fNvar];
00084 
00085 //  Create one branch for each variable
00086    char descriptor[100];
00087    for (i=0;i<fNvar;i++) {
00088       Int_t pv = pvars[i];
00089       snprintf(descriptor,100,"%s/D",&vars[pv]);      
00090       TTree::Branch(&vars[pv],&fArgs[i],descriptor,bufsize);
00091    }
00092 
00093    delete [] vars;
00094    delete [] pvars;
00095 }
00096 
00097 //______________________________________________________________________________
00098 TNtupleD::~TNtupleD()
00099 {
00100 //*-*-*-*-*-*Default destructor for an Ntuple*-*-*-*-*-*-*-*-*-*-*-*
00101 //*-*        ================================
00102 
00103    delete [] fArgs;
00104    fArgs = 0;
00105 }
00106 
00107 //______________________________________________________________________________
00108 void TNtupleD::ResetBranchAddress(TBranch *branch)
00109 {
00110    // Reset the branch addresses to the internal fArgs array. Use this
00111    // method when the addresses were changed via calls to SetBranchAddress().
00112 
00113    if (branch) {
00114       UInt_t index = fBranches.IndexOf(branch);
00115       if (index>0) {
00116          branch->SetAddress(&fArgs[index]);
00117       }
00118    }
00119 }
00120 
00121 //______________________________________________________________________________
00122 void TNtupleD::ResetBranchAddresses()
00123 {
00124    // Reset the branch addresses to the internal fArgs array. Use this
00125    // method when the addresses were changed via calls to SetBranchAddress().
00126 
00127    for (Int_t i = 0; i < fNvar; i++) {
00128       TBranch *branch = (TBranch*)fBranches.UncheckedAt(i);
00129       if (branch) branch->SetAddress(&fArgs[i]);
00130    }
00131 }
00132 
00133 //______________________________________________________________________________
00134 void TNtupleD::Browse(TBrowser *b)
00135 {
00136    // Browse content.
00137 
00138    fLeaves.Browse( b );
00139 }
00140 
00141 
00142 //______________________________________________________________________________
00143 Int_t TNtupleD::Fill()
00144 {
00145 //*-*-*-*-*-*-*-*-*Fill a Ntuple with current values in fArgs*-*-*-*-*-*-*
00146 //*-*              ==========================================
00147 // Note that this function is protected.
00148 // Currently called only by TChain::Merge
00149 
00150    return TTree::Fill();
00151 }
00152 
00153 //______________________________________________________________________________
00154 Int_t TNtupleD::Fill(const Double_t *x)
00155 {
00156 //*-*-*-*-*-*-*-*-*Fill a Ntuple with an array of floats*-*-*-*-*-*-*-*-*-*
00157 //*-*              =====================================
00158 
00159 //*-*- Store array x into buffer
00160    for (Int_t i=0;i<fNvar;i++)  {
00161       fArgs[i] = x[i];
00162    }
00163 
00164    return TTree::Fill();
00165 }
00166 
00167 
00168 //______________________________________________________________________________
00169 Int_t TNtupleD::Fill(Double_t x0,Double_t x1,Double_t x2,Double_t x3,Double_t x4
00170               ,Double_t x5,Double_t x6,Double_t x7,Double_t x8,Double_t x9
00171               ,Double_t x10,Double_t x11,Double_t x12,Double_t x13,Double_t x14)
00172 {
00173 //*-*-*-*-*-*-*-*-*Fill a Ntuple: Each Ntuple item is an argument*-*-*-*-*-*-*
00174 //*-*              ==============================================
00175 
00176    if (fNvar >  0) fArgs[0]  = x0;
00177    if (fNvar >  1) fArgs[1]  = x1;
00178    if (fNvar >  2) fArgs[2]  = x2;
00179    if (fNvar >  3) fArgs[3]  = x3;
00180    if (fNvar >  4) fArgs[4]  = x4;
00181    if (fNvar >  5) fArgs[5]  = x5;
00182    if (fNvar >  6) fArgs[6]  = x6;
00183    if (fNvar >  7) fArgs[7]  = x7;
00184    if (fNvar >  8) fArgs[8]  = x8;
00185    if (fNvar >  9) fArgs[9]  = x9;
00186    if (fNvar > 10) fArgs[10] = x10;
00187    if (fNvar > 11) fArgs[11] = x11;
00188    if (fNvar > 12) fArgs[12] = x12;
00189    if (fNvar > 13) fArgs[13] = x13;
00190    if (fNvar > 14) fArgs[14] = x14;
00191 
00192    return TTree::Fill();
00193 }
00194 
00195 //_______________________________________________________________________
00196 Long64_t TNtupleD::ReadFile(const char *filename, const char * /*branchDescriptor*/)
00197 {
00198 // read from filename as many columns as variables in the ntuple
00199 // the function returns the number of rows found in the file
00200 // The second argument "branchDescriptor" is currently not used.
00201 // Lines in the input file starting with "#" are ignored.
00202          
00203    Long64_t nlines = 0;
00204    ifstream in;
00205    in.open(filename);
00206    while (1) {
00207       if ( in.peek() != '#' ) {
00208          for (Int_t i=0;i<fNvar;i++) in >> fArgs[i];
00209          if (!in.good()) break;
00210          TTree::Fill();
00211          nlines++;
00212       }
00213       in.ignore(8192,'\n');
00214       
00215    }
00216    in.close();
00217    return nlines;
00218 }
00219 
00220 //_______________________________________________________________________
00221 void TNtupleD::Streamer(TBuffer &b)
00222 {
00223 //*-*-*-*-*-*-*-*-*Stream a class object*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
00224 //*-*              =========================================
00225    if (b.IsReading()) {
00226       UInt_t R__s, R__c;
00227       Version_t R__v = b.ReadVersion(&R__s, &R__c);
00228       b.ReadClassBuffer(TNtupleD::Class(), this, R__v, R__s, R__c);
00229       if (fNvar <= 0) return;
00230       fArgs = new Double_t[fNvar];
00231       for (Int_t i=0;i<fNvar;i++) {
00232          TBranch *branch = (TBranch*)fBranches.UncheckedAt(i);
00233          if (branch) branch->SetAddress(&fArgs[i]);
00234       }      
00235    } else {
00236       b.WriteClassBuffer(TNtupleD::Class(),this);
00237    }
00238 }

Generated on Tue Jul 5 15:34:08 2011 for ROOT_528-00b_version by  doxygen 1.5.1