TNtuple.cxx

Go to the documentation of this file.
00001 // @(#)root/tree:$Id: TNtuple.cxx 35477 2010-09-20 18:16:03Z brun $
00002 // Author: Rene Brun   06/04/96
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 // TNtuple                                                              //
00015 //                                                                      //
00016 // A simple tree restricted to a list of float variables only.          //
00017 //                                                                      //
00018 // Each variable goes to a separate branch.                             //
00019 //                                                                      //
00020 //  A Ntuple is created via                                             //
00021 //     TNtuple(name,title,varlist,bufsize)                              //
00022 //  It is filled via:                                                   //
00023 //     TNtuple::Fill(*x)  or                                            //
00024 //     TNtuple::Fill(v1,v2,v3.....)                                     //
00025 //                                                                      //
00026 //////////////////////////////////////////////////////////////////////////
00027 
00028 #include "TNtuple.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 #include <string>
00037 
00038 ClassImp(TNtuple)
00039 
00040 //______________________________________________________________________________
00041 TNtuple::TNtuple(): TTree()
00042 {
00043 //*-*-*-*-*-*Default constructor for Ntuple*-*-*-*-*-*-*-*-*-*-*-*-*-*
00044 //*-*        ==============================
00045 
00046    fNvar = 0;
00047    fArgs = 0;
00048 }
00049 
00050 //______________________________________________________________________________
00051 TNtuple::TNtuple(const char *name, const char *title, const char *varlist, Int_t bufsize)
00052        :TTree(name,title)
00053 {
00054 //*-*-*-*-*-*-*-*-*-*-*-*-*Create an Ntuple*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
00055 //*-*                      ================
00056 //       The parameter varlist describes the list of the ntuple variables
00057 //       separated by a colon:
00058 //         example:  "x:y:z:energy"
00059 //       For each variable in the list a separate branch is created.
00060 //
00061 //      NOTE:
00062 //       -Use TTree to create branches with variables of different data types.
00063 //       -Use TTree when the number of branches is large (> 100). 
00064 //*-*
00065 
00066    Int_t i;
00067    fNvar = 0;
00068    fArgs = 0;
00069 
00070 //   Count number of variables (separated by :)
00071    Int_t nch = strlen(varlist);
00072    if (nch == 0) return;
00073    char *vars = new char[nch+1];
00074    strlcpy(vars,varlist,nch+1);
00075    Int_t *pvars = new Int_t[nch+1];
00076    fNvar = 1;
00077    pvars[0] = 0;
00078    for (i=1;i<nch;i++) {
00079       if (vars[i] == ':') {
00080          pvars[fNvar] = i+1;
00081          vars[i] = 0;
00082          fNvar++;
00083       }
00084    }
00085    fArgs = new Float_t[fNvar];
00086 
00087 //  Create one branch for each variable
00088    for (i=0;i<fNvar;i++) {
00089       Int_t pv = pvars[i];
00090       TTree::Branch(&vars[pv],&fArgs[i],&vars[pv],bufsize);
00091    }
00092 
00093    delete [] vars;
00094    delete [] pvars;
00095 }
00096 
00097 //______________________________________________________________________________
00098 TNtuple::~TNtuple()
00099 {
00100 //*-*-*-*-*-*Default destructor for an Ntuple*-*-*-*-*-*-*-*-*-*-*-*
00101 //*-*        ================================
00102 
00103    delete [] fArgs;
00104    fArgs = 0;
00105 }
00106 
00107 //______________________________________________________________________________
00108 void TNtuple::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       Int_t index = fBranches.IndexOf(branch);
00115       if (index>=0) {
00116          branch->SetAddress(&fArgs[index]);
00117       }
00118    }
00119 }
00120 
00121 //______________________________________________________________________________
00122 void TNtuple::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 TNtuple::Browse(TBrowser *b)
00135 {
00136    // Browse content of the ntuple
00137 
00138    fLeaves.Browse( b );
00139 }
00140 
00141 
00142 //______________________________________________________________________________
00143 Int_t TNtuple::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 TNtuple::Fill(const Float_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 TNtuple::Fill(Float_t x0,Float_t x1,Float_t x2,Float_t x3,Float_t x4
00170               ,Float_t x5,Float_t x6,Float_t x7,Float_t x8,Float_t x9
00171               ,Float_t x10,Float_t x11,Float_t x12,Float_t x13,Float_t x14)
00172 {
00173    // Fill a Ntuple: Each Ntuple item is an argument
00174 
00175    if (fNvar >  0) fArgs[0]  = x0;
00176    if (fNvar >  1) fArgs[1]  = x1;
00177    if (fNvar >  2) fArgs[2]  = x2;
00178    if (fNvar >  3) fArgs[3]  = x3;
00179    if (fNvar >  4) fArgs[4]  = x4;
00180    if (fNvar >  5) fArgs[5]  = x5;
00181    if (fNvar >  6) fArgs[6]  = x6;
00182    if (fNvar >  7) fArgs[7]  = x7;
00183    if (fNvar >  8) fArgs[8]  = x8;
00184    if (fNvar >  9) fArgs[9]  = x9;
00185    if (fNvar > 10) fArgs[10] = x10;
00186    if (fNvar > 11) fArgs[11] = x11;
00187    if (fNvar > 12) fArgs[12] = x12;
00188    if (fNvar > 13) fArgs[13] = x13;
00189    if (fNvar > 14) fArgs[14] = x14;
00190 
00191    return TTree::Fill();
00192 }
00193 
00194 //_______________________________________________________________________
00195 Long64_t TNtuple::ReadFile(const char *filename, const char * /*branchDescriptor*/)
00196 {
00197    // Read from filename as many columns as variables in the ntuple
00198    // the function returns the number of rows found in the file
00199    // The second argument "branchDescriptor" is currently not used.
00200    // Lines in the input file starting with "#" are ignored.
00201 
00202    Long64_t nlines = 0;
00203    ifstream in;
00204    in.open(filename);
00205    while (1) {
00206       if ( in.peek() != '#' ) {
00207          for (Int_t i=0;i<fNvar;i++) in >> fArgs[i];
00208          if (!in.good()) break;
00209          TTree::Fill();
00210          nlines++;
00211       }
00212       in.ignore(8192,'\n');
00213    }
00214    in.close();
00215    return nlines;
00216 }
00217 
00218 
00219 //_______________________________________________________________________
00220 void TNtuple::Streamer(TBuffer &b)
00221 {
00222 //*-*-*-*-*-*-*-*-*Stream a class object*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
00223 //*-*              =========================================
00224    if (b.IsReading()) {
00225       UInt_t R__s, R__c;
00226       Version_t R__v = b.ReadVersion(&R__s, &R__c);
00227       if (R__v > 1) {
00228          b.ReadClassBuffer(TNtuple::Class(), this, R__v, R__s, R__c);
00229       } else {
00230          //====process old versions before automatic schema evolution
00231          TTree::Streamer(b);
00232          b >> fNvar;
00233          b.CheckByteCount(R__s, R__c, TNtuple::IsA());
00234          //====end of old versions
00235       }
00236       if (fNvar <= 0) return;
00237       fArgs = new Float_t[fNvar];
00238       for (Int_t i=0;i<fNvar;i++) {
00239          TBranch *branch = (TBranch*)fBranches.UncheckedAt(i);
00240          if (branch) branch->SetAddress(&fArgs[i]);
00241       }
00242    } else {
00243       b.WriteClassBuffer(TNtuple::Class(),this);
00244    }
00245 }

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