ROOT logo
// @(#)root/tree:$Id: TNtuple.cxx 41666 2011-10-31 19:10:37Z pcanal $
// Author: Rene Brun   06/04/96

/*************************************************************************
 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

//////////////////////////////////////////////////////////////////////////
//                                                                      //
// TNtuple                                                              //
//                                                                      //
// A simple tree restricted to a list of float variables only.          //
//                                                                      //
// Each variable goes to a separate branch.                             //
//                                                                      //
//  A Ntuple is created via                                             //
//     TNtuple(name,title,varlist,bufsize)                              //
//  It is filled via:                                                   //
//     TNtuple::Fill(*x)  or                                            //
//     TNtuple::Fill(v1,v2,v3.....)                                     //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

#include "TNtuple.h"
#include "TTree.h"
#include "TBranch.h"
#include "TLeaf.h"
#include "TBrowser.h"
#include "Riostream.h"
#include "TClass.h"

#include <string>

ClassImp(TNtuple)

//______________________________________________________________________________
TNtuple::TNtuple(): TTree()
{
//*-*-*-*-*-*Default constructor for Ntuple*-*-*-*-*-*-*-*-*-*-*-*-*-*
//*-*        ==============================

   fNvar = 0;
   fArgs = 0;
}

//______________________________________________________________________________
TNtuple::TNtuple(const char *name, const char *title, const char *varlist, Int_t bufsize)
       :TTree(name,title)
{
//*-*-*-*-*-*-*-*-*-*-*-*-*Create an Ntuple*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
//*-*                      ================
//       The parameter varlist describes the list of the ntuple variables
//       separated by a colon:
//         example:  "x:y:z:energy"
//       For each variable in the list a separate branch is created.
//
//      NOTE:
//       -Use TTree to create branches with variables of different data types.
//       -Use TTree when the number of branches is large (> 100). 
//*-*

   Int_t i;
   fNvar = 0;
   fArgs = 0;

//   Count number of variables (separated by :)
   Int_t nch = strlen(varlist);
   if (nch == 0) return;
   char *vars = new char[nch+1];
   strlcpy(vars,varlist,nch+1);
   Int_t *pvars = new Int_t[nch+1];
   fNvar = 1;
   pvars[0] = 0;
   for (i=1;i<nch;i++) {
      if (vars[i] == ':') {
         pvars[fNvar] = i+1;
         vars[i] = 0;
         fNvar++;
      }
   }
   fArgs = new Float_t[fNvar];

//  Create one branch for each variable
   for (i=0;i<fNvar;i++) {
      Int_t pv = pvars[i];
      TTree::Branch(&vars[pv],&fArgs[i],&vars[pv],bufsize);
   }

   delete [] vars;
   delete [] pvars;
}

//______________________________________________________________________________
TNtuple::~TNtuple()
{
//*-*-*-*-*-*Default destructor for an Ntuple*-*-*-*-*-*-*-*-*-*-*-*
//*-*        ================================

   delete [] fArgs;
   fArgs = 0;
}

//______________________________________________________________________________
TTree* TNtuple::CloneTree(Long64_t nentries /* = -1 */, Option_t* option /* = "" */)
{
   // Create a clone of this tree and copy nentries.
   //
   // By default copy all entries.
   // Note that only active branches are copied.
   // The compression level of the cloned tree is set to the destination file's
   // compression level.
   //
   // See TTree::CloneTree for more details.

   TNtuple *newtuple = dynamic_cast<TNtuple*> (TTree::CloneTree(nentries,option) );
   if (newtuple) {
      // To deal with the cases of some of the branches where dropped.
      newtuple->fNvar = newtuple->fBranches.GetEntries();
   }
   return newtuple;
}

//______________________________________________________________________________
void TNtuple::ResetBranchAddress(TBranch *branch)
{
   // Reset the branch addresses to the internal fArgs array. Use this
   // method when the addresses were changed via calls to SetBranchAddress().

   if (branch) {
      Int_t index = fBranches.IndexOf(branch);
      if (index>=0) {
         branch->SetAddress(&fArgs[index]);
      }
   }
}

//______________________________________________________________________________
void TNtuple::ResetBranchAddresses()
{
   // Reset the branch addresses to the internal fArgs array. Use this
   // method when the addresses were changed via calls to SetBranchAddress().

   for (Int_t i = 0; i < fNvar; i++) {
      TBranch *branch = (TBranch*)fBranches.UncheckedAt(i);
      if (branch) branch->SetAddress(&fArgs[i]);
   }
}

//______________________________________________________________________________
void TNtuple::Browse(TBrowser *b)
{
   // Browse content of the ntuple

   fLeaves.Browse( b );
}


//______________________________________________________________________________
Int_t TNtuple::Fill()
{
//*-*-*-*-*-*-*-*-*Fill a Ntuple with current values in fArgs*-*-*-*-*-*-*
//*-*              ==========================================
// Note that this function is protected.
// Currently called only by TChain::Merge

   return TTree::Fill();
}

//______________________________________________________________________________
Int_t TNtuple::Fill(const Float_t *x)
{
   // Fill a Ntuple with an array of floats


   // Store array x into buffer
   for (Int_t i=0;i<fNvar;i++)  {
      fArgs[i] = x[i];
   }

   return TTree::Fill();
}


//______________________________________________________________________________
Int_t TNtuple::Fill(Float_t x0,Float_t x1,Float_t x2,Float_t x3,Float_t x4
              ,Float_t x5,Float_t x6,Float_t x7,Float_t x8,Float_t x9
              ,Float_t x10,Float_t x11,Float_t x12,Float_t x13,Float_t x14)
{
   // Fill a Ntuple: Each Ntuple item is an argument

   if (fNvar >  0) fArgs[0]  = x0;
   if (fNvar >  1) fArgs[1]  = x1;
   if (fNvar >  2) fArgs[2]  = x2;
   if (fNvar >  3) fArgs[3]  = x3;
   if (fNvar >  4) fArgs[4]  = x4;
   if (fNvar >  5) fArgs[5]  = x5;
   if (fNvar >  6) fArgs[6]  = x6;
   if (fNvar >  7) fArgs[7]  = x7;
   if (fNvar >  8) fArgs[8]  = x8;
   if (fNvar >  9) fArgs[9]  = x9;
   if (fNvar > 10) fArgs[10] = x10;
   if (fNvar > 11) fArgs[11] = x11;
   if (fNvar > 12) fArgs[12] = x12;
   if (fNvar > 13) fArgs[13] = x13;
   if (fNvar > 14) fArgs[14] = x14;

   return TTree::Fill();
}

//_______________________________________________________________________
Long64_t TNtuple::ReadStream(istream &inputStream, const char * /*branchDescriptor*/, char delimiter)
{
   // Read from filename as many columns as variables in the ntuple
   // the function returns the number of rows found in the file
   // The second argument "branchDescriptor" is currently not used.
   // Lines in the input file starting with "#" are ignored.

   Long64_t nlines = 0;
   char newline = GetNewlineValue(inputStream);
   while (1) {
      if ( inputStream.peek() != '#' ) {
         for (Int_t i=0;i<fNvar;i++) {
            inputStream >> fArgs[i];
            if (inputStream.peek() == delimiter) {
               inputStream.get(); // skip delimiter.
            }
         }
         if (!inputStream.good()) break;
         TTree::Fill();
         ++nlines;
      }
      inputStream.ignore(8192,newline);
   }
   return nlines;
}


//_______________________________________________________________________
void TNtuple::Streamer(TBuffer &b)
{
//*-*-*-*-*-*-*-*-*Stream a class object*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
//*-*              =========================================
   if (b.IsReading()) {
      UInt_t R__s, R__c;
      Version_t R__v = b.ReadVersion(&R__s, &R__c);
      if (R__v > 1) {
         b.ReadClassBuffer(TNtuple::Class(), this, R__v, R__s, R__c);
      } else {
         //====process old versions before automatic schema evolution
         TTree::Streamer(b);
         b >> fNvar;
         b.CheckByteCount(R__s, R__c, TNtuple::IsA());
         //====end of old versions
      }
      if (fNvar <= 0) return;
      fArgs = new Float_t[fNvar];
      for (Int_t i=0;i<fNvar;i++) {
         TBranch *branch = (TBranch*)fBranches.UncheckedAt(i);
         if (branch) branch->SetAddress(&fArgs[i]);
      }
   } else {
      b.WriteClassBuffer(TNtuple::Class(),this);
   }
}
 TNtuple.cxx:1
 TNtuple.cxx:2
 TNtuple.cxx:3
 TNtuple.cxx:4
 TNtuple.cxx:5
 TNtuple.cxx:6
 TNtuple.cxx:7
 TNtuple.cxx:8
 TNtuple.cxx:9
 TNtuple.cxx:10
 TNtuple.cxx:11
 TNtuple.cxx:12
 TNtuple.cxx:13
 TNtuple.cxx:14
 TNtuple.cxx:15
 TNtuple.cxx:16
 TNtuple.cxx:17
 TNtuple.cxx:18
 TNtuple.cxx:19
 TNtuple.cxx:20
 TNtuple.cxx:21
 TNtuple.cxx:22
 TNtuple.cxx:23
 TNtuple.cxx:24
 TNtuple.cxx:25
 TNtuple.cxx:26
 TNtuple.cxx:27
 TNtuple.cxx:28
 TNtuple.cxx:29
 TNtuple.cxx:30
 TNtuple.cxx:31
 TNtuple.cxx:32
 TNtuple.cxx:33
 TNtuple.cxx:34
 TNtuple.cxx:35
 TNtuple.cxx:36
 TNtuple.cxx:37
 TNtuple.cxx:38
 TNtuple.cxx:39
 TNtuple.cxx:40
 TNtuple.cxx:41
 TNtuple.cxx:42
 TNtuple.cxx:43
 TNtuple.cxx:44
 TNtuple.cxx:45
 TNtuple.cxx:46
 TNtuple.cxx:47
 TNtuple.cxx:48
 TNtuple.cxx:49
 TNtuple.cxx:50
 TNtuple.cxx:51
 TNtuple.cxx:52
 TNtuple.cxx:53
 TNtuple.cxx:54
 TNtuple.cxx:55
 TNtuple.cxx:56
 TNtuple.cxx:57
 TNtuple.cxx:58
 TNtuple.cxx:59
 TNtuple.cxx:60
 TNtuple.cxx:61
 TNtuple.cxx:62
 TNtuple.cxx:63
 TNtuple.cxx:64
 TNtuple.cxx:65
 TNtuple.cxx:66
 TNtuple.cxx:67
 TNtuple.cxx:68
 TNtuple.cxx:69
 TNtuple.cxx:70
 TNtuple.cxx:71
 TNtuple.cxx:72
 TNtuple.cxx:73
 TNtuple.cxx:74
 TNtuple.cxx:75
 TNtuple.cxx:76
 TNtuple.cxx:77
 TNtuple.cxx:78
 TNtuple.cxx:79
 TNtuple.cxx:80
 TNtuple.cxx:81
 TNtuple.cxx:82
 TNtuple.cxx:83
 TNtuple.cxx:84
 TNtuple.cxx:85
 TNtuple.cxx:86
 TNtuple.cxx:87
 TNtuple.cxx:88
 TNtuple.cxx:89
 TNtuple.cxx:90
 TNtuple.cxx:91
 TNtuple.cxx:92
 TNtuple.cxx:93
 TNtuple.cxx:94
 TNtuple.cxx:95
 TNtuple.cxx:96
 TNtuple.cxx:97
 TNtuple.cxx:98
 TNtuple.cxx:99
 TNtuple.cxx:100
 TNtuple.cxx:101
 TNtuple.cxx:102
 TNtuple.cxx:103
 TNtuple.cxx:104
 TNtuple.cxx:105
 TNtuple.cxx:106
 TNtuple.cxx:107
 TNtuple.cxx:108
 TNtuple.cxx:109
 TNtuple.cxx:110
 TNtuple.cxx:111
 TNtuple.cxx:112
 TNtuple.cxx:113
 TNtuple.cxx:114
 TNtuple.cxx:115
 TNtuple.cxx:116
 TNtuple.cxx:117
 TNtuple.cxx:118
 TNtuple.cxx:119
 TNtuple.cxx:120
 TNtuple.cxx:121
 TNtuple.cxx:122
 TNtuple.cxx:123
 TNtuple.cxx:124
 TNtuple.cxx:125
 TNtuple.cxx:126
 TNtuple.cxx:127
 TNtuple.cxx:128
 TNtuple.cxx:129
 TNtuple.cxx:130
 TNtuple.cxx:131
 TNtuple.cxx:132
 TNtuple.cxx:133
 TNtuple.cxx:134
 TNtuple.cxx:135
 TNtuple.cxx:136
 TNtuple.cxx:137
 TNtuple.cxx:138
 TNtuple.cxx:139
 TNtuple.cxx:140
 TNtuple.cxx:141
 TNtuple.cxx:142
 TNtuple.cxx:143
 TNtuple.cxx:144
 TNtuple.cxx:145
 TNtuple.cxx:146
 TNtuple.cxx:147
 TNtuple.cxx:148
 TNtuple.cxx:149
 TNtuple.cxx:150
 TNtuple.cxx:151
 TNtuple.cxx:152
 TNtuple.cxx:153
 TNtuple.cxx:154
 TNtuple.cxx:155
 TNtuple.cxx:156
 TNtuple.cxx:157
 TNtuple.cxx:158
 TNtuple.cxx:159
 TNtuple.cxx:160
 TNtuple.cxx:161
 TNtuple.cxx:162
 TNtuple.cxx:163
 TNtuple.cxx:164
 TNtuple.cxx:165
 TNtuple.cxx:166
 TNtuple.cxx:167
 TNtuple.cxx:168
 TNtuple.cxx:169
 TNtuple.cxx:170
 TNtuple.cxx:171
 TNtuple.cxx:172
 TNtuple.cxx:173
 TNtuple.cxx:174
 TNtuple.cxx:175
 TNtuple.cxx:176
 TNtuple.cxx:177
 TNtuple.cxx:178
 TNtuple.cxx:179
 TNtuple.cxx:180
 TNtuple.cxx:181
 TNtuple.cxx:182
 TNtuple.cxx:183
 TNtuple.cxx:184
 TNtuple.cxx:185
 TNtuple.cxx:186
 TNtuple.cxx:187
 TNtuple.cxx:188
 TNtuple.cxx:189
 TNtuple.cxx:190
 TNtuple.cxx:191
 TNtuple.cxx:192
 TNtuple.cxx:193
 TNtuple.cxx:194
 TNtuple.cxx:195
 TNtuple.cxx:196
 TNtuple.cxx:197
 TNtuple.cxx:198
 TNtuple.cxx:199
 TNtuple.cxx:200
 TNtuple.cxx:201
 TNtuple.cxx:202
 TNtuple.cxx:203
 TNtuple.cxx:204
 TNtuple.cxx:205
 TNtuple.cxx:206
 TNtuple.cxx:207
 TNtuple.cxx:208
 TNtuple.cxx:209
 TNtuple.cxx:210
 TNtuple.cxx:211
 TNtuple.cxx:212
 TNtuple.cxx:213
 TNtuple.cxx:214
 TNtuple.cxx:215
 TNtuple.cxx:216
 TNtuple.cxx:217
 TNtuple.cxx:218
 TNtuple.cxx:219
 TNtuple.cxx:220
 TNtuple.cxx:221
 TNtuple.cxx:222
 TNtuple.cxx:223
 TNtuple.cxx:224
 TNtuple.cxx:225
 TNtuple.cxx:226
 TNtuple.cxx:227
 TNtuple.cxx:228
 TNtuple.cxx:229
 TNtuple.cxx:230
 TNtuple.cxx:231
 TNtuple.cxx:232
 TNtuple.cxx:233
 TNtuple.cxx:234
 TNtuple.cxx:235
 TNtuple.cxx:236
 TNtuple.cxx:237
 TNtuple.cxx:238
 TNtuple.cxx:239
 TNtuple.cxx:240
 TNtuple.cxx:241
 TNtuple.cxx:242
 TNtuple.cxx:243
 TNtuple.cxx:244
 TNtuple.cxx:245
 TNtuple.cxx:246
 TNtuple.cxx:247
 TNtuple.cxx:248
 TNtuple.cxx:249
 TNtuple.cxx:250
 TNtuple.cxx:251
 TNtuple.cxx:252
 TNtuple.cxx:253
 TNtuple.cxx:254
 TNtuple.cxx:255
 TNtuple.cxx:256
 TNtuple.cxx:257
 TNtuple.cxx:258
 TNtuple.cxx:259
 TNtuple.cxx:260
 TNtuple.cxx:261
 TNtuple.cxx:262
 TNtuple.cxx:263
 TNtuple.cxx:264
 TNtuple.cxx:265
 TNtuple.cxx:266
 TNtuple.cxx:267
 TNtuple.cxx:268