ROOT logo
// @(#)root/treeviewer:$Id: TTVSession.cxx 35537 2010-09-21 13:04:28Z brun $
//Author : Andrei Gheata   21/02/01

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


#include "Riostream.h"
#include "TTVSession.h"
#include "TTreeViewer.h"
#include "TTVLVContainer.h"
#include "TClonesArray.h"
#include "TInterpreter.h"


ClassImp(TTVRecord)

//______________________________________________________________________________
TTVRecord::TTVRecord()
{
   // TTVRecord default constructor

   fName = "";
   fScanRedirected = kFALSE;
   fCutEnabled     = kTRUE;
   fUserCode = "";
   fAutoexec = kFALSE;
}
//______________________________________________________________________________
void TTVRecord::ExecuteUserCode()
{
   // Execute user-defined code

   if (fUserCode.Length()) {
      char code[250];
      code[0] = 0;
      snprintf(code,250, "%s", fUserCode.Data());
      gInterpreter->ProcessLine(code);
   }
}
//______________________________________________________________________________
void TTVRecord::FormFrom(TTreeViewer *tv)
{
   // Populate members from treeviewer tv

   if (!tv)  return;
   fX        = tv->ExpressionItem(0)->GetTrueName();
   fXAlias   = tv->ExpressionItem(0)->GetAlias();
   fY        = tv->ExpressionItem(1)->GetTrueName();
   fYAlias   = tv->ExpressionItem(1)->GetAlias();
   fZ        = tv->ExpressionItem(2)->GetTrueName();
   fZAlias   = tv->ExpressionItem(2)->GetAlias();
   fCut      = tv->ExpressionItem(3)->GetTrueName();
   fCutAlias = tv->ExpressionItem(3)->GetAlias();
   fOption   = tv->GetGrOpt();
   fScanRedirected = tv->IsScanRedirected();
   fCutEnabled = tv->IsCutEnabled();
}
//______________________________________________________________________________
void TTVRecord::PlugIn(TTreeViewer *tv)
{
   // Change treeviewer status to this record

   TTVLVEntry *item;
   // change X expression
   item = tv->ExpressionItem(0);
   item->SetExpression(fX.Data(), fXAlias.Data());
   item = tv->ExpressionItem(1);
   item->SetExpression(fY.Data(), fYAlias.Data());
   item = tv->ExpressionItem(2);
   item->SetExpression(fZ.Data(), fZAlias.Data());
   item = tv->ExpressionItem(3);
   item->SetExpression(fCut.Data(), fCutAlias.Data());
   tv->SetGrOpt(fOption.Data());
   tv->SetScanRedirect(fScanRedirected);
   tv->SetCutMode(fCutEnabled);
   if (fCutEnabled)
      item->SetSmallPic(gClient->GetPicture("cut_t.xpm"));
   else
      item->SetSmallPic(gClient->GetPicture("cut-disable_t.xpm"));
}
//______________________________________________________________________________
void TTVRecord::SaveSource(ofstream &out)
{
   // Save the TTVRecord in a C++ macro file

   char quote = '"';
   out <<"//--- tree viewer record"<<endl;
   out <<"   tv_record = tv_session->AddRecord(kTRUE);"<<endl;
   out <<"   tv_session->SetRecordName("<<quote<<GetName()<<quote<<");"<<endl;
   out <<"   tv_record->fX        = "<<quote<<fX.Data()<<quote<<";"<<endl;
   out <<"   tv_record->fY        = "<<quote<<fY.Data()<<quote<<";"<<endl;
   out <<"   tv_record->fZ        = "<<quote<<fZ.Data()<<quote<<";"<<endl;
   out <<"   tv_record->fCut      = "<<quote<<fCut.Data()<<quote<<";"<<endl;
   out <<"   tv_record->fXAlias   = "<<quote<<fXAlias.Data()<<quote<<";"<<endl;
   out <<"   tv_record->fYAlias   = "<<quote<<fYAlias.Data()<<quote<<";"<<endl;
   out <<"   tv_record->fZAlias   = "<<quote<<fZAlias.Data()<<quote<<";"<<endl;
   out <<"   tv_record->fCutAlias = "<<quote<<fCutAlias.Data()<<quote<<";"<<endl;
   out <<"   tv_record->fOption   = "<<quote<<fOption.Data()<<quote<<";"<<endl;
   if (fScanRedirected)
      out <<"   tv_record->fScanRedirected = kTRUE;"<<endl;
   else
      out <<"   tv_record->fScanRedirected = kFALSE;"<<endl;
   if (fCutEnabled)
      out <<"   tv_record->fCutEnabled = kTRUE;"<<endl;
   else
      out <<"   tv_record->fCutEnabled = kFALSE;"<<endl;
   if (fUserCode.Length()) {
      out <<"   tv_record->SetUserCode(\""<<fUserCode.Data()<<"\");"<<endl;
      if (fAutoexec) {
         out <<"   tv_record->SetAutoexec();"<<endl;
      }
   }
}

ClassImp(TTVSession)

//______________________________________________________________________________
TTVSession::TTVSession(TTreeViewer *tv)
{
   // constructor

   fName    = "";
   fList    = new TClonesArray("TTVRecord", 100); // is 100 enough ?
   fViewer  = tv;
   fCurrent = 0;
   fRecords = 0;
}
//______________________________________________________________________________
TTVSession::~TTVSession()
{
   // destructor

   fList->Delete();
   delete fList;
}
//______________________________________________________________________________
TTVRecord *TTVSession::AddRecord(Bool_t fromFile)
{
   // add a record

   TClonesArray &list = *fList;
   TTVRecord *newrec = new(list[fRecords++])TTVRecord();
   if (!fromFile) newrec->FormFrom(fViewer);
   fCurrent = fRecords - 1;
   if (fRecords > 1) fViewer->ActivateButtons(kTRUE, kTRUE, kFALSE, kTRUE);
   else              fViewer->ActivateButtons(kTRUE, kFALSE, kFALSE, kTRUE);
   if (!fromFile) {
      TString name = "";
      if (strlen(newrec->GetZ())) name += newrec->GetZ();
      if (strlen(newrec->GetY())) {
         if (name.Length()) name += ":";
         name += newrec->GetY();
      }
      if (strlen(newrec->GetX())) {
         if (name.Length()) name += ":";
         name += newrec->GetX();
      }
      SetRecordName(name.Data());
   }
   return newrec;
}
//______________________________________________________________________________
TTVRecord *TTVSession::GetRecord(Int_t i)
{
   // return record at index i

   if (!fRecords) return 0;
   fCurrent = i;
   if (i < 0)           fCurrent = 0;
   if (i > fRecords-1)  fCurrent = fRecords - 1;
   if (fCurrent>0 && fCurrent<fRecords-1)
      fViewer->ActivateButtons(kTRUE, kTRUE, kTRUE, kTRUE);
   if (fCurrent == 0) {
      if (fRecords > 1) fViewer->ActivateButtons(kTRUE, kFALSE, kTRUE, kTRUE);
      else              fViewer->ActivateButtons(kTRUE, kFALSE, kFALSE, kTRUE);
   }
   if (fCurrent == fRecords-1) {
      if (fRecords > 1) fViewer->ActivateButtons(kTRUE, kTRUE, kFALSE, kTRUE);
      else              fViewer->ActivateButtons(kTRUE, kFALSE, kFALSE, kTRUE);
   }
   fViewer->SetCurrentRecord(fCurrent);
   return (TTVRecord *)fList->UncheckedAt(fCurrent);
}
//______________________________________________________________________________
void TTVSession::SetRecordName(const char *name)
{
   // Set record name

   Int_t crt = fCurrent;
   TTVRecord *current = GetRecord(fCurrent);
   current->SetName(name);
   fViewer->UpdateCombo();
   fCurrent = crt;
   fViewer->SetCurrentRecord(fCurrent);
}
//______________________________________________________________________________
void TTVSession::RemoveLastRecord()
{
   //--- Remove current record from list

   if (!fRecords) return;
   TTVRecord *rec = (TTVRecord *)fList->UncheckedAt(fRecords);
   delete rec;
   fList->RemoveAt(fRecords--);
   if (fCurrent > fRecords-1) fCurrent = fRecords - 1;
   Int_t crt = fCurrent;
   fViewer->UpdateCombo();
   fCurrent = crt;
   if (!fRecords) {
      fViewer->ActivateButtons(kFALSE, kFALSE, kFALSE, kFALSE);
      return;
   }
   GetRecord(fCurrent);
}
//______________________________________________________________________________
void TTVSession::Show(TTVRecord *rec)
{
   // Display record rec

   rec->PlugIn(fViewer);
   fViewer->ExecuteDraw();
   if (rec->HasUserCode() && rec->MustExecuteCode()) rec->ExecuteUserCode();
   fViewer->SetHistogramTitle(rec->GetName());
}
//______________________________________________________________________________
void TTVSession::SaveSource(ofstream &out)
{
   // Save the TTVSession in a C++ macro file

   out<<"//--- session object"<<endl;
   out<<"   tv_session = new TTVSession(treeview);"<<endl;
   out<<"   treeview->SetSession(tv_session);"<<endl;
   TTVRecord *record;
   for (Int_t i=0; i<fRecords; i++) {
      record = GetRecord(i);
      record->SaveSource(out);
   }
   out<<"//--- Connect first record"<<endl;
   out<<"   tv_session->First();"<<endl;
}
//______________________________________________________________________________
void TTVSession::UpdateRecord(const char *name)
{
   //--- Updates current record according to new X, Y, Z settings

   TTVRecord *current = (TTVRecord *)fList->UncheckedAt(fCurrent);
   current->FormFrom(fViewer);
   SetRecordName(name);
}

 TTVSession.cxx:1
 TTVSession.cxx:2
 TTVSession.cxx:3
 TTVSession.cxx:4
 TTVSession.cxx:5
 TTVSession.cxx:6
 TTVSession.cxx:7
 TTVSession.cxx:8
 TTVSession.cxx:9
 TTVSession.cxx:10
 TTVSession.cxx:11
 TTVSession.cxx:12
 TTVSession.cxx:13
 TTVSession.cxx:14
 TTVSession.cxx:15
 TTVSession.cxx:16
 TTVSession.cxx:17
 TTVSession.cxx:18
 TTVSession.cxx:19
 TTVSession.cxx:20
 TTVSession.cxx:21
 TTVSession.cxx:22
 TTVSession.cxx:23
 TTVSession.cxx:24
 TTVSession.cxx:25
 TTVSession.cxx:26
 TTVSession.cxx:27
 TTVSession.cxx:28
 TTVSession.cxx:29
 TTVSession.cxx:30
 TTVSession.cxx:31
 TTVSession.cxx:32
 TTVSession.cxx:33
 TTVSession.cxx:34
 TTVSession.cxx:35
 TTVSession.cxx:36
 TTVSession.cxx:37
 TTVSession.cxx:38
 TTVSession.cxx:39
 TTVSession.cxx:40
 TTVSession.cxx:41
 TTVSession.cxx:42
 TTVSession.cxx:43
 TTVSession.cxx:44
 TTVSession.cxx:45
 TTVSession.cxx:46
 TTVSession.cxx:47
 TTVSession.cxx:48
 TTVSession.cxx:49
 TTVSession.cxx:50
 TTVSession.cxx:51
 TTVSession.cxx:52
 TTVSession.cxx:53
 TTVSession.cxx:54
 TTVSession.cxx:55
 TTVSession.cxx:56
 TTVSession.cxx:57
 TTVSession.cxx:58
 TTVSession.cxx:59
 TTVSession.cxx:60
 TTVSession.cxx:61
 TTVSession.cxx:62
 TTVSession.cxx:63
 TTVSession.cxx:64
 TTVSession.cxx:65
 TTVSession.cxx:66
 TTVSession.cxx:67
 TTVSession.cxx:68
 TTVSession.cxx:69
 TTVSession.cxx:70
 TTVSession.cxx:71
 TTVSession.cxx:72
 TTVSession.cxx:73
 TTVSession.cxx:74
 TTVSession.cxx:75
 TTVSession.cxx:76
 TTVSession.cxx:77
 TTVSession.cxx:78
 TTVSession.cxx:79
 TTVSession.cxx:80
 TTVSession.cxx:81
 TTVSession.cxx:82
 TTVSession.cxx:83
 TTVSession.cxx:84
 TTVSession.cxx:85
 TTVSession.cxx:86
 TTVSession.cxx:87
 TTVSession.cxx:88
 TTVSession.cxx:89
 TTVSession.cxx:90
 TTVSession.cxx:91
 TTVSession.cxx:92
 TTVSession.cxx:93
 TTVSession.cxx:94
 TTVSession.cxx:95
 TTVSession.cxx:96
 TTVSession.cxx:97
 TTVSession.cxx:98
 TTVSession.cxx:99
 TTVSession.cxx:100
 TTVSession.cxx:101
 TTVSession.cxx:102
 TTVSession.cxx:103
 TTVSession.cxx:104
 TTVSession.cxx:105
 TTVSession.cxx:106
 TTVSession.cxx:107
 TTVSession.cxx:108
 TTVSession.cxx:109
 TTVSession.cxx:110
 TTVSession.cxx:111
 TTVSession.cxx:112
 TTVSession.cxx:113
 TTVSession.cxx:114
 TTVSession.cxx:115
 TTVSession.cxx:116
 TTVSession.cxx:117
 TTVSession.cxx:118
 TTVSession.cxx:119
 TTVSession.cxx:120
 TTVSession.cxx:121
 TTVSession.cxx:122
 TTVSession.cxx:123
 TTVSession.cxx:124
 TTVSession.cxx:125
 TTVSession.cxx:126
 TTVSession.cxx:127
 TTVSession.cxx:128
 TTVSession.cxx:129
 TTVSession.cxx:130
 TTVSession.cxx:131
 TTVSession.cxx:132
 TTVSession.cxx:133
 TTVSession.cxx:134
 TTVSession.cxx:135
 TTVSession.cxx:136
 TTVSession.cxx:137
 TTVSession.cxx:138
 TTVSession.cxx:139
 TTVSession.cxx:140
 TTVSession.cxx:141
 TTVSession.cxx:142
 TTVSession.cxx:143
 TTVSession.cxx:144
 TTVSession.cxx:145
 TTVSession.cxx:146
 TTVSession.cxx:147
 TTVSession.cxx:148
 TTVSession.cxx:149
 TTVSession.cxx:150
 TTVSession.cxx:151
 TTVSession.cxx:152
 TTVSession.cxx:153
 TTVSession.cxx:154
 TTVSession.cxx:155
 TTVSession.cxx:156
 TTVSession.cxx:157
 TTVSession.cxx:158
 TTVSession.cxx:159
 TTVSession.cxx:160
 TTVSession.cxx:161
 TTVSession.cxx:162
 TTVSession.cxx:163
 TTVSession.cxx:164
 TTVSession.cxx:165
 TTVSession.cxx:166
 TTVSession.cxx:167
 TTVSession.cxx:168
 TTVSession.cxx:169
 TTVSession.cxx:170
 TTVSession.cxx:171
 TTVSession.cxx:172
 TTVSession.cxx:173
 TTVSession.cxx:174
 TTVSession.cxx:175
 TTVSession.cxx:176
 TTVSession.cxx:177
 TTVSession.cxx:178
 TTVSession.cxx:179
 TTVSession.cxx:180
 TTVSession.cxx:181
 TTVSession.cxx:182
 TTVSession.cxx:183
 TTVSession.cxx:184
 TTVSession.cxx:185
 TTVSession.cxx:186
 TTVSession.cxx:187
 TTVSession.cxx:188
 TTVSession.cxx:189
 TTVSession.cxx:190
 TTVSession.cxx:191
 TTVSession.cxx:192
 TTVSession.cxx:193
 TTVSession.cxx:194
 TTVSession.cxx:195
 TTVSession.cxx:196
 TTVSession.cxx:197
 TTVSession.cxx:198
 TTVSession.cxx:199
 TTVSession.cxx:200
 TTVSession.cxx:201
 TTVSession.cxx:202
 TTVSession.cxx:203
 TTVSession.cxx:204
 TTVSession.cxx:205
 TTVSession.cxx:206
 TTVSession.cxx:207
 TTVSession.cxx:208
 TTVSession.cxx:209
 TTVSession.cxx:210
 TTVSession.cxx:211
 TTVSession.cxx:212
 TTVSession.cxx:213
 TTVSession.cxx:214
 TTVSession.cxx:215
 TTVSession.cxx:216
 TTVSession.cxx:217
 TTVSession.cxx:218
 TTVSession.cxx:219
 TTVSession.cxx:220
 TTVSession.cxx:221
 TTVSession.cxx:222
 TTVSession.cxx:223
 TTVSession.cxx:224
 TTVSession.cxx:225
 TTVSession.cxx:226
 TTVSession.cxx:227
 TTVSession.cxx:228
 TTVSession.cxx:229
 TTVSession.cxx:230
 TTVSession.cxx:231
 TTVSession.cxx:232
 TTVSession.cxx:233
 TTVSession.cxx:234
 TTVSession.cxx:235
 TTVSession.cxx:236
 TTVSession.cxx:237
 TTVSession.cxx:238
 TTVSession.cxx:239
 TTVSession.cxx:240
 TTVSession.cxx:241
 TTVSession.cxx:242
 TTVSession.cxx:243
 TTVSession.cxx:244
 TTVSession.cxx:245
 TTVSession.cxx:246
 TTVSession.cxx:247
 TTVSession.cxx:248
 TTVSession.cxx:249
 TTVSession.cxx:250
 TTVSession.cxx:251
 TTVSession.cxx:252
 TTVSession.cxx:253
 TTVSession.cxx:254
 TTVSession.cxx:255
 TTVSession.cxx:256