// @(#)root/hist:$Id$
// Author: Rene Brun   14/07/2009

/*************************************************************************
 * Copyright (C) 1995-2009, 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 "TGraphTime.h"
#include "TVirtualPad.h"
#include "TH1.h"
#include "TROOT.h"
#include "TObjArray.h"
#include "TSystem.h"

ClassImp(TGraphTime)

//______________________________________________________________________________
//
// TGraphTime is used to draw a set of objects evolving with nsteps in time between tmin and tmax.
// each time step has a new list of objects. This list can be identical to
// the list of objects in the previous steps, but with different attributes.
//   see example of use in $ROOTSYS/tutorials/graphs/gtime.C

//______________________________________________________________________________
TGraphTime::TGraphTime(): TNamed()
{
   // default constructor.

   fSleepTime = 0;
   fNsteps    = 0;
   fXmin      = 0;
   fXmax      = 1;
   fYmin      = 0;
   fYmax      = 1;
   fSteps     = 0;
   fFrame     = 0;
}


//______________________________________________________________________________
TGraphTime::TGraphTime(Int_t nsteps, Double_t xmin, Double_t ymin, Double_t xmax, Double_t ymax)
      :TNamed()
{
   // Create a TGraphTime with nsteps in range [xmin,xmax][ymin,ymax]

   if (nsteps <= 0) {
      Warning("TGraphTime", "Number of steps %d changed to 100",nsteps);
      nsteps = 100;
   }
   fSleepTime = 0;
   fNsteps    = nsteps;
   fXmin      = xmin;
   fXmax      = xmax;
   fYmin      = ymin;
   fYmax      = ymax;
   fSteps     = new TObjArray(nsteps+1);
   fFrame     = new TH1D("frame","",100,fXmin,fXmax);
   fFrame->SetMinimum(ymin);
   fFrame->SetMaximum(ymax);
   fFrame->SetStats(0);
}


//______________________________________________________________________________
TGraphTime::~TGraphTime()
{
   // GraphTime default destructor.

   if (!fSteps) return;
   fSteps->Delete();
   delete fSteps; fSteps=0;
}


//______________________________________________________________________________
TGraphTime::TGraphTime(const TGraphTime &gtime) : TNamed(gtime)
{
   // copy constructor.

   fSleepTime = gtime.fSleepTime;
   fNsteps    = gtime.fNsteps;
   fXmin      = gtime.fXmin;
   fXmax      = gtime.fXmax;
   fYmin      = gtime.fYmin;
   fYmax      = gtime.fYmax;
   fSteps     = new TObjArray(fNsteps+1);
   fFrame     = new TH1D("frame","",100,fXmin,fXmax);
   fFrame->SetMinimum(fYmin);
   fFrame->SetMaximum(fYmax);
   fFrame->SetStats(0);
}

//______________________________________________________________________________
Int_t TGraphTime::Add(const TObject *obj, Int_t slot, Option_t *option)
{
   // Add one object to a time slot.
   // TGraphTime becomes the owner of this object.
   // object will be drawn with option

   if (!fSteps) {
      fNsteps = 100;
      fSteps = new TObjArray(fNsteps+1);
   }
   if (slot < 0 || slot >= fNsteps) return -1;
   TList *list = (TList*)fSteps->UncheckedAt(slot);
   if (!list) {
      list = new TList();
      fSteps->AddAt(list,slot);
   }
   list->Add((TObject*)obj, option);
   return slot;
}


//______________________________________________________________________________
void TGraphTime::Draw(Option_t *option)
{
   // Draw this TGraphTime.
   // for each time step the list of objects added to this step are drawn.

   if (!gPad) {
      gROOT->MakeDefCanvas();
      gPad->SetFillColor(41);
      gPad->SetFrameFillColor(19);
      gPad->SetGrid();
   }
   if (fFrame) {
      fFrame->SetTitle(GetTitle());
      fFrame->Draw();
   }
   Paint(option);

}

//______________________________________________________________________________
void TGraphTime::Paint(Option_t *option)
{
   // Paint all objects added to each time step

   TString opt = option;
   opt.ToLower();
   TObject *frame = gPad->GetPrimitive("frame");
   TList *list = 0;
   TObjLink *lnk;

   for (Int_t s=0;s<fNsteps;s++) {
      list = (TList*)fSteps->UncheckedAt(s);
      if (list) {
         gPad->GetListOfPrimitives()->Remove(frame);
         gPad->GetListOfPrimitives()->Clear();
         if (frame) gPad->GetListOfPrimitives()->Add(frame);
         lnk = list->FirstLink();
         while(lnk) {
            TObject *obj = lnk->GetObject();
            obj->Draw(lnk->GetAddOption());
            lnk = lnk->Next();
         }
         gPad->Update();
         if (fSleepTime > 0) gSystem->Sleep(fSleepTime);
      }
   }
}

//______________________________________________________________________________
void TGraphTime::SaveAnimatedGif(const char *filename) const
{
   // Save this object to filename as an animated gif file
   // if filename is specified it must be of the form xxx.gif
   // otherwise a file yyy.gif is produced where yyy is the object name

   TObject *frame = gPad->GetPrimitive("frame");
   TList *list = 0;
   TObjLink *lnk;

   for (Int_t s=0;s<fNsteps;s++) {
      list = (TList*)fSteps->UncheckedAt(s);
      if (list) {
         gPad->GetListOfPrimitives()->Remove(frame);
         gPad->GetListOfPrimitives()->Clear();
         if (frame) gPad->GetListOfPrimitives()->Add(frame);
         lnk = list->FirstLink();
         while(lnk) {
            TObject *obj = lnk->GetObject();
            obj->Draw(lnk->GetAddOption());
            lnk = lnk->Next();
         }
         gPad->Update();
         if (strlen(filename) > 0) gPad->Print(Form("%s+",filename));
         else                      gPad->Print(Form("%s+",GetName()));
         if (fSleepTime > 0) gSystem->Sleep(fSleepTime);
      }
   }
}
 TGraphTime.cxx:1
 TGraphTime.cxx:2
 TGraphTime.cxx:3
 TGraphTime.cxx:4
 TGraphTime.cxx:5
 TGraphTime.cxx:6
 TGraphTime.cxx:7
 TGraphTime.cxx:8
 TGraphTime.cxx:9
 TGraphTime.cxx:10
 TGraphTime.cxx:11
 TGraphTime.cxx:12
 TGraphTime.cxx:13
 TGraphTime.cxx:14
 TGraphTime.cxx:15
 TGraphTime.cxx:16
 TGraphTime.cxx:17
 TGraphTime.cxx:18
 TGraphTime.cxx:19
 TGraphTime.cxx:20
 TGraphTime.cxx:21
 TGraphTime.cxx:22
 TGraphTime.cxx:23
 TGraphTime.cxx:24
 TGraphTime.cxx:25
 TGraphTime.cxx:26
 TGraphTime.cxx:27
 TGraphTime.cxx:28
 TGraphTime.cxx:29
 TGraphTime.cxx:30
 TGraphTime.cxx:31
 TGraphTime.cxx:32
 TGraphTime.cxx:33
 TGraphTime.cxx:34
 TGraphTime.cxx:35
 TGraphTime.cxx:36
 TGraphTime.cxx:37
 TGraphTime.cxx:38
 TGraphTime.cxx:39
 TGraphTime.cxx:40
 TGraphTime.cxx:41
 TGraphTime.cxx:42
 TGraphTime.cxx:43
 TGraphTime.cxx:44
 TGraphTime.cxx:45
 TGraphTime.cxx:46
 TGraphTime.cxx:47
 TGraphTime.cxx:48
 TGraphTime.cxx:49
 TGraphTime.cxx:50
 TGraphTime.cxx:51
 TGraphTime.cxx:52
 TGraphTime.cxx:53
 TGraphTime.cxx:54
 TGraphTime.cxx:55
 TGraphTime.cxx:56
 TGraphTime.cxx:57
 TGraphTime.cxx:58
 TGraphTime.cxx:59
 TGraphTime.cxx:60
 TGraphTime.cxx:61
 TGraphTime.cxx:62
 TGraphTime.cxx:63
 TGraphTime.cxx:64
 TGraphTime.cxx:65
 TGraphTime.cxx:66
 TGraphTime.cxx:67
 TGraphTime.cxx:68
 TGraphTime.cxx:69
 TGraphTime.cxx:70
 TGraphTime.cxx:71
 TGraphTime.cxx:72
 TGraphTime.cxx:73
 TGraphTime.cxx:74
 TGraphTime.cxx:75
 TGraphTime.cxx:76
 TGraphTime.cxx:77
 TGraphTime.cxx:78
 TGraphTime.cxx:79
 TGraphTime.cxx:80
 TGraphTime.cxx:81
 TGraphTime.cxx:82
 TGraphTime.cxx:83
 TGraphTime.cxx:84
 TGraphTime.cxx:85
 TGraphTime.cxx:86
 TGraphTime.cxx:87
 TGraphTime.cxx:88
 TGraphTime.cxx:89
 TGraphTime.cxx:90
 TGraphTime.cxx:91
 TGraphTime.cxx:92
 TGraphTime.cxx:93
 TGraphTime.cxx:94
 TGraphTime.cxx:95
 TGraphTime.cxx:96
 TGraphTime.cxx:97
 TGraphTime.cxx:98
 TGraphTime.cxx:99
 TGraphTime.cxx:100
 TGraphTime.cxx:101
 TGraphTime.cxx:102
 TGraphTime.cxx:103
 TGraphTime.cxx:104
 TGraphTime.cxx:105
 TGraphTime.cxx:106
 TGraphTime.cxx:107
 TGraphTime.cxx:108
 TGraphTime.cxx:109
 TGraphTime.cxx:110
 TGraphTime.cxx:111
 TGraphTime.cxx:112
 TGraphTime.cxx:113
 TGraphTime.cxx:114
 TGraphTime.cxx:115
 TGraphTime.cxx:116
 TGraphTime.cxx:117
 TGraphTime.cxx:118
 TGraphTime.cxx:119
 TGraphTime.cxx:120
 TGraphTime.cxx:121
 TGraphTime.cxx:122
 TGraphTime.cxx:123
 TGraphTime.cxx:124
 TGraphTime.cxx:125
 TGraphTime.cxx:126
 TGraphTime.cxx:127
 TGraphTime.cxx:128
 TGraphTime.cxx:129
 TGraphTime.cxx:130
 TGraphTime.cxx:131
 TGraphTime.cxx:132
 TGraphTime.cxx:133
 TGraphTime.cxx:134
 TGraphTime.cxx:135
 TGraphTime.cxx:136
 TGraphTime.cxx:137
 TGraphTime.cxx:138
 TGraphTime.cxx:139
 TGraphTime.cxx:140
 TGraphTime.cxx:141
 TGraphTime.cxx:142
 TGraphTime.cxx:143
 TGraphTime.cxx:144
 TGraphTime.cxx:145
 TGraphTime.cxx:146
 TGraphTime.cxx:147
 TGraphTime.cxx:148
 TGraphTime.cxx:149
 TGraphTime.cxx:150
 TGraphTime.cxx:151
 TGraphTime.cxx:152
 TGraphTime.cxx:153
 TGraphTime.cxx:154
 TGraphTime.cxx:155
 TGraphTime.cxx:156
 TGraphTime.cxx:157
 TGraphTime.cxx:158
 TGraphTime.cxx:159
 TGraphTime.cxx:160
 TGraphTime.cxx:161
 TGraphTime.cxx:162
 TGraphTime.cxx:163
 TGraphTime.cxx:164
 TGraphTime.cxx:165
 TGraphTime.cxx:166
 TGraphTime.cxx:167
 TGraphTime.cxx:168
 TGraphTime.cxx:169
 TGraphTime.cxx:170
 TGraphTime.cxx:171
 TGraphTime.cxx:172
 TGraphTime.cxx:173
 TGraphTime.cxx:174
 TGraphTime.cxx:175
 TGraphTime.cxx:176
 TGraphTime.cxx:177
 TGraphTime.cxx:178
 TGraphTime.cxx:179
 TGraphTime.cxx:180
 TGraphTime.cxx:181
 TGraphTime.cxx:182
 TGraphTime.cxx:183
 TGraphTime.cxx:184
 TGraphTime.cxx:185
 TGraphTime.cxx:186
 TGraphTime.cxx:187
 TGraphTime.cxx:188
 TGraphTime.cxx:189
 TGraphTime.cxx:190
 TGraphTime.cxx:191
 TGraphTime.cxx:192
 TGraphTime.cxx:193
 TGraphTime.cxx:194
 TGraphTime.cxx:195
 TGraphTime.cxx:196
 TGraphTime.cxx:197