// @(#)root/base:$Id$
// Author: Philippe Canal  13/05/2003

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

////////////////////////////////////////////////////////////////////////////
//                                                                        //
// TBranchProxyDirector                                                   //
//                                                                        //
// This class is used to 'drive' and hold a serie of TBranchProxy objects //
// which represent and give access to the content of TTree object.        //
// This is intended to be used as part of a generate Selector class       //
// which will hold the directory and its associate                        //
//                                                                        //
////////////////////////////////////////////////////////////////////////////

#include "TBranchProxyDirector.h"
#include "TBranchProxy.h"
#include "TFriendProxy.h"
#include "TTree.h"
#include "TEnv.h"
#include "TH1F.h"
#include "TPad.h"
#include "TList.h"

#include <algorithm>

namespace std {} using namespace std;

ClassImp(ROOT::TBranchProxyDirector);

namespace ROOT {

   // Helper function to call Reset on each TBranchProxy
   void Reset(TBranchProxy *x) { x->Reset(); }

   // Helper function to call SetReadEntry on all TFriendProxy
   void ResetReadEntry(TFriendProxy *x) { x->ResetReadEntry(); }

   // Helper class to call Update on all TFriendProxy
   struct Update {
      Update(TTree *newtree) : fNewTree(newtree) {}
      TTree *fNewTree;
      void operator()(TFriendProxy *x) { x->Update(fNewTree); }
   };


   TBranchProxyDirector::TBranchProxyDirector(TTree* tree, Long64_t i) :
      fTree(tree),
      fEntry(i)
   {
      // Simple constructor
   }

   TBranchProxyDirector::TBranchProxyDirector(TTree* tree, Int_t i) :
      // cint has a problem casting int to long long
      fTree(tree),
      fEntry(i)
   {
      // Simple constructor
   }

   void TBranchProxyDirector::Attach(TBranchProxy* p) {

      // Attach a TBranchProxy object to this director.  The director just
      // 'remembers' this BranchProxy and does not own it.  It will be use
      // to apply Tree wide operation (like reseting).
      fDirected.push_back(p);
   }

   void TBranchProxyDirector::Attach(TFriendProxy* p) {

      // Attach a TFriendProxy object to this director.  The director just
      // 'remembers' this BranchProxy and does not own it.  It will be use
      // to apply Tree wide operation (like reseting).
      fFriends.push_back(p);
   }

   TH1F* TBranchProxyDirector::CreateHistogram(const char *options) {
      // Create a temporary 1D histogram.

      Int_t nbins = gEnv->GetValue("Hist.Binning.1D.x",100);
      Double_t vmin=0, vmax=0;
      Double_t xmin=0, xmax=0;
      Bool_t canExtend = kTRUE;
      TString opt( options );
      Bool_t optSame = opt.Contains("same");
      if (optSame) canExtend = kFALSE;

      if (gPad && optSame) {
         TListIter np(gPad->GetListOfPrimitives());
         TObject *op;
         TH1 *oldhtemp = 0;
         while ((op = np()) && !oldhtemp) {
            if (op->InheritsFrom(TH1::Class())) oldhtemp = (TH1 *)op;
         }
         if (oldhtemp) {
            nbins = oldhtemp->GetXaxis()->GetNbins();
            vmin = oldhtemp->GetXaxis()->GetXmin();
            vmax = oldhtemp->GetXaxis()->GetXmax();
         } else {
            vmin = gPad->GetUxmin();
            vmax = gPad->GetUxmax();
         }
      } else {
         vmin = xmin;
         vmax = xmax;
         if (xmin < xmax) canExtend = kFALSE;
      }
      TH1F *hist = new TH1F("htemp","htemp",nbins,vmin,vmax);
      hist->SetLineColor(fTree->GetLineColor());
      hist->SetLineWidth(fTree->GetLineWidth());
      hist->SetLineStyle(fTree->GetLineStyle());
      hist->SetFillColor(fTree->GetFillColor());
      hist->SetFillStyle(fTree->GetFillStyle());
      hist->SetMarkerStyle(fTree->GetMarkerStyle());
      hist->SetMarkerColor(fTree->GetMarkerColor());
      hist->SetMarkerSize(fTree->GetMarkerSize());
      if (canExtend) hist->SetCanExtend(TH1::kAllAxes);
      hist->GetXaxis()->SetTitle("var");
      hist->SetBit(kCanDelete);
      hist->SetDirectory(0);

      if (opt.Length() && opt.Contains("e")) hist->Sumw2();
      return hist;
   }

   void TBranchProxyDirector::SetReadEntry(Long64_t entry) {

      // move to a new entry to read
      fEntry = entry;
      if (!fFriends.empty()) {
         for_each(fFriends.begin(),fFriends.end(),ResetReadEntry);
      }
   }

   TTree* TBranchProxyDirector::SetTree(TTree *newtree) {

      // Set the BranchProxy to be looking at a new tree.
      // Reset all.
      // Return the old tree.

      TTree* oldtree = fTree;
      fTree = newtree;
      fEntry = -1;
      //if (fInitialized) fInitialized = setup();
      //fprintf(stderr,"calling SetTree for %p\n",this);
      for_each(fDirected.begin(),fDirected.end(),Reset);
      Update update(fTree);
      for_each(fFriends.begin(),fFriends.end(),update);
      return oldtree;
   }

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