ROOT logo
// Author: Witold Przygoda, przygoda@if.uj.edu.pl, last modifed on: 1 March 2009

#include <iostream>
#include <stdexcept>
#include <algorithm>
#include "hntuple.h"


ClassImp(HNtuple)
// ---------------------------------------------------------------------------------
// Dummy constructor
// ---------------------------------------------------------------------------------
HNtuple::HNtuple() : outFile(0), cname(0), ctitle(0), cbufsize(0), ptrNt(0), isNtuple(kFALSE), varArrayN(0), varArray(0)
{
}

// ---------------------------------------------------------------------------------
// This is ntuple constructor with "lazy"-delayed construction
// ntuple is created only after first fill attempt based on 
// variables which have been set 
// ---------------------------------------------------------------------------------
HNtuple::HNtuple(const char* name, const char* title, Int_t bufsize) : 
	outFile(0), cname(name), ctitle(title), cbufsize(bufsize), ptrNt(0), isNtuple(kFALSE), varArrayN(0), varArray(0)
{
}

// ---------------------------------------------------------------------------------
// Basic ntuple constructor with exactly the same parameters as in the case of ROOT TNtuple
// ---------------------------------------------------------------------------------
HNtuple::HNtuple(const char* name, const char* title, const char* varlist, Int_t bufsize) :
	outFile(0), cname(name), ctitle(title), cbufsize(bufsize), ptrNt(0), isNtuple(kFALSE), varArrayN(0), varArray(0)
{
   ptrNt = new TNtuple(name, title, varlist, bufsize);
   setMap(varlist, isNtuple);
}

// ---------------------------------------------------------------------------------
void HNtuple::setMap(const char* vlist, Bool_t& kPair)
{
   Int_t i = 0;
   varList = vlist;
   std::string sName = "";
   std::string::iterator sIter;
   for (sIter = varList.begin(); sIter != varList.end(); ++sIter)
   {
       if (*sIter != ':')
       {
          sName += *sIter;
       }
       else
       {
          // create a new pair ntuple variable - value
          vKeyOrder.insert(make_pair(sName, i++));
          if (kPair==kFALSE) vKeyValue.insert(make_pair(sName, 0.));
          sName.clear();
       }
   }
          // create the last new pair ntuple variable - value
          vKeyOrder.insert(make_pair(sName, i++));
          if (kPair==kFALSE) vKeyValue.insert(make_pair(sName, 0.));
          sName.clear();

   // create Float_t array based on variables string and number of ':' separators
   varArrayN = 1 + count_if(varList.begin(), varList.end(), std::bind2nd(std::equal_to<char>(), ':'));
   varArray = new Float_t[varArrayN];
   kPair = kTRUE;
}

// ---------------------------------------------------------------------------------
HNtuple::~HNtuple()
{
   if (ptrNt != 0) delete ptrNt;
   if (varArray != 0) delete [] varArray;
}


// ---------------------------------------------------------------------------------
Int_t HNtuple::Write(const char* name, Int_t option, Int_t bufsize) 
{
   return ptrNt->Write(name, option, bufsize); 
}

// ---------------------------------------------------------------------------------
void HNtuple::SetDirectory(TDirectory* dir)
{
   return ptrNt->SetDirectory( dir );
}

// ---------------------------------------------------------------------------------
Float_t& HNtuple::operator[](const std::string& key)
{
   if (isNtuple)
   {
      mIter = vKeyValue.find(key);
      if (mIter != vKeyValue.end())
      {
         return mIter->second;
      }
      throw std::invalid_argument(Form("An unknown variable : %s name in HNtuple tried to be assigned",key.data()));
   }

return vKeyValue[key]; 
}

// ---------------------------------------------------------------------------------
const Float_t& HNtuple::operator[](const std::string& key) const
{
   std::map<std::string, Float_t>::const_iterator mcIter = vKeyValue.find(key);
   if (mcIter != vKeyValue.end())
   {
      return (mcIter->second); 
   }
   throw std::invalid_argument(Form("An unknown variable : %s name in HNtuple tried to be assigned",key.data()));
}

// ---------------------------------------------------------------------------------
Int_t HNtuple::fill()
{
// This function is similar to Fill(...) from NTuple class besides
// the fact that is has no parameter and is small "f" :)

   Int_t i;
   if (isNtuple==kTRUE)
   {
      for (i = 0; i < varArrayN; ++i) varArray[i] = 0.;
      for (mIter = vKeyValue.begin(); mIter != vKeyValue.end(); ++mIter)
      {
        varArray[ vKeyOrder[(*mIter).first] ] = (*mIter).second;
        // reset of map array
        (*mIter).second = 0.;
      }
   }
   else
   {
      // ntuple not booked yet, we create it here based on variables 
      // set with the function setVal
      
      std::string vList;
      for (mIter = vKeyValue.begin(); mIter != vKeyValue.end(); ++mIter)
      {
         vList += (*mIter).first + ":";
      }
      vList.erase(vList.find_last_of(":"),1);

      //-------- here a part of NTuple Ctor
      if (outFile) outFile->cd();
       else std::cerr << "WARNING: NTuple booked but not attached to any file. Storing in memory!" << std::endl;
      ptrNt = new TNtuple(cname, ctitle, vList.c_str(), cbufsize);
      isNtuple = kTRUE;
      setMap(vList.c_str(), isNtuple);
	 //-------- fill
         for (i = 0; i < varArrayN; ++i) varArray[i] = 0.;
         for (mIter = vKeyValue.begin(); mIter != vKeyValue.end(); ++mIter)
         {
           varArray[ vKeyOrder[(*mIter).first] ] = (*mIter).second;
           // reset of map array
           (*mIter).second = 0.;
         }
   }

   // filling the ROOT ntuple
   return ptrNt->Fill(varArray);
}
// ****************************************************************************



 hntuple.cc:1
 hntuple.cc:2
 hntuple.cc:3
 hntuple.cc:4
 hntuple.cc:5
 hntuple.cc:6
 hntuple.cc:7
 hntuple.cc:8
 hntuple.cc:9
 hntuple.cc:10
 hntuple.cc:11
 hntuple.cc:12
 hntuple.cc:13
 hntuple.cc:14
 hntuple.cc:15
 hntuple.cc:16
 hntuple.cc:17
 hntuple.cc:18
 hntuple.cc:19
 hntuple.cc:20
 hntuple.cc:21
 hntuple.cc:22
 hntuple.cc:23
 hntuple.cc:24
 hntuple.cc:25
 hntuple.cc:26
 hntuple.cc:27
 hntuple.cc:28
 hntuple.cc:29
 hntuple.cc:30
 hntuple.cc:31
 hntuple.cc:32
 hntuple.cc:33
 hntuple.cc:34
 hntuple.cc:35
 hntuple.cc:36
 hntuple.cc:37
 hntuple.cc:38
 hntuple.cc:39
 hntuple.cc:40
 hntuple.cc:41
 hntuple.cc:42
 hntuple.cc:43
 hntuple.cc:44
 hntuple.cc:45
 hntuple.cc:46
 hntuple.cc:47
 hntuple.cc:48
 hntuple.cc:49
 hntuple.cc:50
 hntuple.cc:51
 hntuple.cc:52
 hntuple.cc:53
 hntuple.cc:54
 hntuple.cc:55
 hntuple.cc:56
 hntuple.cc:57
 hntuple.cc:58
 hntuple.cc:59
 hntuple.cc:60
 hntuple.cc:61
 hntuple.cc:62
 hntuple.cc:63
 hntuple.cc:64
 hntuple.cc:65
 hntuple.cc:66
 hntuple.cc:67
 hntuple.cc:68
 hntuple.cc:69
 hntuple.cc:70
 hntuple.cc:71
 hntuple.cc:72
 hntuple.cc:73
 hntuple.cc:74
 hntuple.cc:75
 hntuple.cc:76
 hntuple.cc:77
 hntuple.cc:78
 hntuple.cc:79
 hntuple.cc:80
 hntuple.cc:81
 hntuple.cc:82
 hntuple.cc:83
 hntuple.cc:84
 hntuple.cc:85
 hntuple.cc:86
 hntuple.cc:87
 hntuple.cc:88
 hntuple.cc:89
 hntuple.cc:90
 hntuple.cc:91
 hntuple.cc:92
 hntuple.cc:93
 hntuple.cc:94
 hntuple.cc:95
 hntuple.cc:96
 hntuple.cc:97
 hntuple.cc:98
 hntuple.cc:99
 hntuple.cc:100
 hntuple.cc:101
 hntuple.cc:102
 hntuple.cc:103
 hntuple.cc:104
 hntuple.cc:105
 hntuple.cc:106
 hntuple.cc:107
 hntuple.cc:108
 hntuple.cc:109
 hntuple.cc:110
 hntuple.cc:111
 hntuple.cc:112
 hntuple.cc:113
 hntuple.cc:114
 hntuple.cc:115
 hntuple.cc:116
 hntuple.cc:117
 hntuple.cc:118
 hntuple.cc:119
 hntuple.cc:120
 hntuple.cc:121
 hntuple.cc:122
 hntuple.cc:123
 hntuple.cc:124
 hntuple.cc:125
 hntuple.cc:126
 hntuple.cc:127
 hntuple.cc:128
 hntuple.cc:129
 hntuple.cc:130
 hntuple.cc:131
 hntuple.cc:132
 hntuple.cc:133
 hntuple.cc:134
 hntuple.cc:135
 hntuple.cc:136
 hntuple.cc:137
 hntuple.cc:138
 hntuple.cc:139
 hntuple.cc:140
 hntuple.cc:141
 hntuple.cc:142
 hntuple.cc:143
 hntuple.cc:144
 hntuple.cc:145
 hntuple.cc:146
 hntuple.cc:147
 hntuple.cc:148
 hntuple.cc:149
 hntuple.cc:150
 hntuple.cc:151
 hntuple.cc:152
 hntuple.cc:153
 hntuple.cc:154
 hntuple.cc:155
 hntuple.cc:156
 hntuple.cc:157
 hntuple.cc:158
 hntuple.cc:159
 hntuple.cc:160
 hntuple.cc:161
 hntuple.cc:162
 hntuple.cc:163
 hntuple.cc:164
 hntuple.cc:165
 hntuple.cc:166
 hntuple.cc:167