Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TGraphTime.cxx
Go to the documentation of this file.
1 // @(#)root/hist:$Id$
2 // Author: Rene Brun 14/07/2009
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2009, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #include "TGraphTime.h"
13 #include "TVirtualPad.h"
14 #include "TH1.h"
15 #include "TROOT.h"
16 #include "TObjArray.h"
17 #include "TSystem.h"
18 
19 ClassImp(TGraphTime);
20 
21 /** \class TGraphTime
22  \ingroup Hist
23 TGraphTime is used to draw a set of objects evolving with nsteps in time between tmin and tmax.
24 Each time step has a new list of objects. This list can be identical to
25 the list of objects in the previous steps, but with different attributes.
26 see example of use in $ROOTSYS/tutorials/graphs/gtime.C
27 */
28 
29 ////////////////////////////////////////////////////////////////////////////////
30 /// default constructor.
31 
32 TGraphTime::TGraphTime(): TNamed()
33 {
34  fSleepTime = 0;
35  fNsteps = 0;
36  fXmin = 0;
37  fXmax = 1;
38  fYmin = 0;
39  fYmax = 1;
40  fSteps = 0;
41  fFrame = 0;
42 }
43 
44 
45 ////////////////////////////////////////////////////////////////////////////////
46 /// Create a TGraphTime with nsteps in range [xmin,xmax][ymin,ymax]
47 
48 TGraphTime::TGraphTime(Int_t nsteps, Double_t xmin, Double_t ymin, Double_t xmax, Double_t ymax)
49  :TNamed()
50 {
51  if (nsteps <= 0) {
52  Warning("TGraphTime", "Number of steps %d changed to 100",nsteps);
53  nsteps = 100;
54  }
55  fSleepTime = 0;
56  fNsteps = nsteps;
57  fXmin = xmin;
58  fXmax = xmax;
59  fYmin = ymin;
60  fYmax = ymax;
61  fSteps = new TObjArray(nsteps+1);
62  fFrame = new TH1D("frame","",100,fXmin,fXmax);
63  fFrame->SetMinimum(ymin);
64  fFrame->SetMaximum(ymax);
65  fFrame->SetStats(0);
66 }
67 
68 
69 ////////////////////////////////////////////////////////////////////////////////
70 /// GraphTime default destructor.
71 
72 TGraphTime::~TGraphTime()
73 {
74  if (!fSteps) return;
75  fSteps->Delete();
76  delete fSteps; fSteps=0;
77 }
78 
79 
80 ////////////////////////////////////////////////////////////////////////////////
81 /// copy constructor.
82 
83 TGraphTime::TGraphTime(const TGraphTime &gtime) : TNamed(gtime)
84 {
85  fSleepTime = gtime.fSleepTime;
86  fNsteps = gtime.fNsteps;
87  fXmin = gtime.fXmin;
88  fXmax = gtime.fXmax;
89  fYmin = gtime.fYmin;
90  fYmax = gtime.fYmax;
91  fSteps = new TObjArray(fNsteps+1);
92  fFrame = new TH1D("frame","",100,fXmin,fXmax);
93  fFrame->SetMinimum(fYmin);
94  fFrame->SetMaximum(fYmax);
95  fFrame->SetStats(0);
96 }
97 
98 ////////////////////////////////////////////////////////////////////////////////
99 /// Add one object to a time slot.
100 /// TGraphTime becomes the owner of this object.
101 /// object will be drawn with option
102 
103 Int_t TGraphTime::Add(const TObject *obj, Int_t slot, Option_t *option)
104 {
105  if (!fSteps) {
106  fNsteps = 100;
107  fSteps = new TObjArray(fNsteps+1);
108  }
109  if (slot < 0 || slot >= fNsteps) return -1;
110  TList *list = (TList*)fSteps->UncheckedAt(slot);
111  if (!list) {
112  list = new TList();
113  fSteps->AddAt(list,slot);
114  }
115  list->Add((TObject*)obj, option);
116  return slot;
117 }
118 
119 
120 ////////////////////////////////////////////////////////////////////////////////
121 /// Draw this TGraphTime.
122 /// for each time step the list of objects added to this step are drawn.
123 
124 void TGraphTime::Draw(Option_t *option)
125 {
126  if (!gPad) {
127  gROOT->MakeDefCanvas();
128  gPad->SetFillColor(41);
129  gPad->SetFrameFillColor(19);
130  gPad->SetGrid();
131  }
132  if (fFrame) {
133  fFrame->SetTitle(GetTitle());
134  fFrame->Draw();
135  }
136  Paint(option);
137 
138 }
139 
140 ////////////////////////////////////////////////////////////////////////////////
141 /// Paint all objects added to each time step
142 
143 void TGraphTime::Paint(Option_t *option)
144 {
145  TString opt = option;
146  opt.ToLower();
147  TObject *frame = gPad->GetPrimitive("frame");
148  TList *list = 0;
149  TObjLink *lnk;
150 
151  for (Int_t s=0;s<fNsteps;s++) {
152  list = (TList*)fSteps->UncheckedAt(s);
153  if (list) {
154  gPad->GetListOfPrimitives()->Remove(frame);
155  gPad->GetListOfPrimitives()->Clear();
156  if (frame) gPad->GetListOfPrimitives()->Add(frame);
157  lnk = list->FirstLink();
158  while(lnk) {
159  TObject *obj = lnk->GetObject();
160  obj->Draw(lnk->GetAddOption());
161  lnk = lnk->Next();
162  }
163  gPad->Update();
164  if (fSleepTime > 0) gSystem->Sleep(fSleepTime);
165  }
166  }
167 }
168 
169 ////////////////////////////////////////////////////////////////////////////////
170 /// Save this object to filename as an animated gif file
171 /// if filename is specified it must be of the form xxx.gif
172 /// otherwise a file yyy.gif is produced where yyy is the object name
173 
174 void TGraphTime::SaveAnimatedGif(const char *filename) const
175 {
176  TObject *frame = gPad->GetPrimitive("frame");
177  TList *list = 0;
178  TObjLink *lnk;
179 
180  for (Int_t s=0;s<fNsteps;s++) {
181  list = (TList*)fSteps->UncheckedAt(s);
182  if (list) {
183  gPad->GetListOfPrimitives()->Remove(frame);
184  gPad->GetListOfPrimitives()->Clear();
185  if (frame) gPad->GetListOfPrimitives()->Add(frame);
186  lnk = list->FirstLink();
187  while(lnk) {
188  TObject *obj = lnk->GetObject();
189  obj->Draw(lnk->GetAddOption());
190  lnk = lnk->Next();
191  }
192  gPad->Update();
193  if (strlen(filename) > 0) gPad->Print(Form("%s+",filename));
194  else gPad->Print(Form("%s+",GetName()));
195  if (fSleepTime > 0) gSystem->Sleep(fSleepTime);
196  }
197  }
198 }