ROOT logo
// @(#)root/tree:$Id: TNtupleD.cxx 39225 2011-05-18 04:56:49Z pcanal $
// Author: Rene Brun   12/08/2001

/*************************************************************************
 * 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.             *
 *************************************************************************/

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

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

ClassImp(TNtupleD)

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

   fNvar = 0;
   fArgs = 0;
}

//______________________________________________________________________________
TNtupleD::TNtupleD(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 Double_t[fNvar];

//  Create one branch for each variable
   char descriptor[100];
   for (i=0;i<fNvar;i++) {
      Int_t pv = pvars[i];
      snprintf(descriptor,100,"%s/D",&vars[pv]);      
      TTree::Branch(&vars[pv],&fArgs[i],descriptor,bufsize);
   }

   delete [] vars;
   delete [] pvars;
}

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

   delete [] fArgs;
   fArgs = 0;
}

//______________________________________________________________________________
void TNtupleD::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) {
      UInt_t index = fBranches.IndexOf(branch);
      if (index>0) {
         branch->SetAddress(&fArgs[index]);
      }
   }
}

//______________________________________________________________________________
void TNtupleD::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 TNtupleD::Browse(TBrowser *b)
{
   // Browse content.

   fLeaves.Browse( b );
}


//______________________________________________________________________________
Int_t TNtupleD::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 TNtupleD::Fill(const Double_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 TNtupleD::Fill(Double_t x0,Double_t x1,Double_t x2,Double_t x3,Double_t x4
              ,Double_t x5,Double_t x6,Double_t x7,Double_t x8,Double_t x9
              ,Double_t x10,Double_t x11,Double_t x12,Double_t x13,Double_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 TNtupleD::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 TNtupleD::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);
      b.ReadClassBuffer(TNtupleD::Class(), this, R__v, R__s, R__c);
      if (fNvar <= 0) return;
      fArgs = new Double_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(TNtupleD::Class(),this);
   }
}
 TNtupleD.cxx:1
 TNtupleD.cxx:2
 TNtupleD.cxx:3
 TNtupleD.cxx:4
 TNtupleD.cxx:5
 TNtupleD.cxx:6
 TNtupleD.cxx:7
 TNtupleD.cxx:8
 TNtupleD.cxx:9
 TNtupleD.cxx:10
 TNtupleD.cxx:11
 TNtupleD.cxx:12
 TNtupleD.cxx:13
 TNtupleD.cxx:14
 TNtupleD.cxx:15
 TNtupleD.cxx:16
 TNtupleD.cxx:17
 TNtupleD.cxx:18
 TNtupleD.cxx:19
 TNtupleD.cxx:20
 TNtupleD.cxx:21
 TNtupleD.cxx:22
 TNtupleD.cxx:23
 TNtupleD.cxx:24
 TNtupleD.cxx:25
 TNtupleD.cxx:26
 TNtupleD.cxx:27
 TNtupleD.cxx:28
 TNtupleD.cxx:29
 TNtupleD.cxx:30
 TNtupleD.cxx:31
 TNtupleD.cxx:32
 TNtupleD.cxx:33
 TNtupleD.cxx:34
 TNtupleD.cxx:35
 TNtupleD.cxx:36
 TNtupleD.cxx:37
 TNtupleD.cxx:38
 TNtupleD.cxx:39
 TNtupleD.cxx:40
 TNtupleD.cxx:41
 TNtupleD.cxx:42
 TNtupleD.cxx:43
 TNtupleD.cxx:44
 TNtupleD.cxx:45
 TNtupleD.cxx:46
 TNtupleD.cxx:47
 TNtupleD.cxx:48
 TNtupleD.cxx:49
 TNtupleD.cxx:50
 TNtupleD.cxx:51
 TNtupleD.cxx:52
 TNtupleD.cxx:53
 TNtupleD.cxx:54
 TNtupleD.cxx:55
 TNtupleD.cxx:56
 TNtupleD.cxx:57
 TNtupleD.cxx:58
 TNtupleD.cxx:59
 TNtupleD.cxx:60
 TNtupleD.cxx:61
 TNtupleD.cxx:62
 TNtupleD.cxx:63
 TNtupleD.cxx:64
 TNtupleD.cxx:65
 TNtupleD.cxx:66
 TNtupleD.cxx:67
 TNtupleD.cxx:68
 TNtupleD.cxx:69
 TNtupleD.cxx:70
 TNtupleD.cxx:71
 TNtupleD.cxx:72
 TNtupleD.cxx:73
 TNtupleD.cxx:74
 TNtupleD.cxx:75
 TNtupleD.cxx:76
 TNtupleD.cxx:77
 TNtupleD.cxx:78
 TNtupleD.cxx:79
 TNtupleD.cxx:80
 TNtupleD.cxx:81
 TNtupleD.cxx:82
 TNtupleD.cxx:83
 TNtupleD.cxx:84
 TNtupleD.cxx:85
 TNtupleD.cxx:86
 TNtupleD.cxx:87
 TNtupleD.cxx:88
 TNtupleD.cxx:89
 TNtupleD.cxx:90
 TNtupleD.cxx:91
 TNtupleD.cxx:92
 TNtupleD.cxx:93
 TNtupleD.cxx:94
 TNtupleD.cxx:95
 TNtupleD.cxx:96
 TNtupleD.cxx:97
 TNtupleD.cxx:98
 TNtupleD.cxx:99
 TNtupleD.cxx:100
 TNtupleD.cxx:101
 TNtupleD.cxx:102
 TNtupleD.cxx:103
 TNtupleD.cxx:104
 TNtupleD.cxx:105
 TNtupleD.cxx:106
 TNtupleD.cxx:107
 TNtupleD.cxx:108
 TNtupleD.cxx:109
 TNtupleD.cxx:110
 TNtupleD.cxx:111
 TNtupleD.cxx:112
 TNtupleD.cxx:113
 TNtupleD.cxx:114
 TNtupleD.cxx:115
 TNtupleD.cxx:116
 TNtupleD.cxx:117
 TNtupleD.cxx:118
 TNtupleD.cxx:119
 TNtupleD.cxx:120
 TNtupleD.cxx:121
 TNtupleD.cxx:122
 TNtupleD.cxx:123
 TNtupleD.cxx:124
 TNtupleD.cxx:125
 TNtupleD.cxx:126
 TNtupleD.cxx:127
 TNtupleD.cxx:128
 TNtupleD.cxx:129
 TNtupleD.cxx:130
 TNtupleD.cxx:131
 TNtupleD.cxx:132
 TNtupleD.cxx:133
 TNtupleD.cxx:134
 TNtupleD.cxx:135
 TNtupleD.cxx:136
 TNtupleD.cxx:137
 TNtupleD.cxx:138
 TNtupleD.cxx:139
 TNtupleD.cxx:140
 TNtupleD.cxx:141
 TNtupleD.cxx:142
 TNtupleD.cxx:143
 TNtupleD.cxx:144
 TNtupleD.cxx:145
 TNtupleD.cxx:146
 TNtupleD.cxx:147
 TNtupleD.cxx:148
 TNtupleD.cxx:149
 TNtupleD.cxx:150
 TNtupleD.cxx:151
 TNtupleD.cxx:152
 TNtupleD.cxx:153
 TNtupleD.cxx:154
 TNtupleD.cxx:155
 TNtupleD.cxx:156
 TNtupleD.cxx:157
 TNtupleD.cxx:158
 TNtupleD.cxx:159
 TNtupleD.cxx:160
 TNtupleD.cxx:161
 TNtupleD.cxx:162
 TNtupleD.cxx:163
 TNtupleD.cxx:164
 TNtupleD.cxx:165
 TNtupleD.cxx:166
 TNtupleD.cxx:167
 TNtupleD.cxx:168
 TNtupleD.cxx:169
 TNtupleD.cxx:170
 TNtupleD.cxx:171
 TNtupleD.cxx:172
 TNtupleD.cxx:173
 TNtupleD.cxx:174
 TNtupleD.cxx:175
 TNtupleD.cxx:176
 TNtupleD.cxx:177
 TNtupleD.cxx:178
 TNtupleD.cxx:179
 TNtupleD.cxx:180
 TNtupleD.cxx:181
 TNtupleD.cxx:182
 TNtupleD.cxx:183
 TNtupleD.cxx:184
 TNtupleD.cxx:185
 TNtupleD.cxx:186
 TNtupleD.cxx:187
 TNtupleD.cxx:188
 TNtupleD.cxx:189
 TNtupleD.cxx:190
 TNtupleD.cxx:191
 TNtupleD.cxx:192
 TNtupleD.cxx:193
 TNtupleD.cxx:194
 TNtupleD.cxx:195
 TNtupleD.cxx:196
 TNtupleD.cxx:197
 TNtupleD.cxx:198
 TNtupleD.cxx:199
 TNtupleD.cxx:200
 TNtupleD.cxx:201
 TNtupleD.cxx:202
 TNtupleD.cxx:203
 TNtupleD.cxx:204
 TNtupleD.cxx:205
 TNtupleD.cxx:206
 TNtupleD.cxx:207
 TNtupleD.cxx:208
 TNtupleD.cxx:209
 TNtupleD.cxx:210
 TNtupleD.cxx:211
 TNtupleD.cxx:212
 TNtupleD.cxx:213
 TNtupleD.cxx:214
 TNtupleD.cxx:215
 TNtupleD.cxx:216
 TNtupleD.cxx:217
 TNtupleD.cxx:218
 TNtupleD.cxx:219
 TNtupleD.cxx:220
 TNtupleD.cxx:221
 TNtupleD.cxx:222
 TNtupleD.cxx:223
 TNtupleD.cxx:224
 TNtupleD.cxx:225
 TNtupleD.cxx:226
 TNtupleD.cxx:227
 TNtupleD.cxx:228
 TNtupleD.cxx:229
 TNtupleD.cxx:230
 TNtupleD.cxx:231
 TNtupleD.cxx:232
 TNtupleD.cxx:233
 TNtupleD.cxx:234
 TNtupleD.cxx:235
 TNtupleD.cxx:236
 TNtupleD.cxx:237
 TNtupleD.cxx:238
 TNtupleD.cxx:239
 TNtupleD.cxx:240