Logo ROOT   6.08/07
Reference Guide
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 
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 
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 
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 
73 {
74  if (!fSteps) return;
75  fSteps->Delete();
76  delete fSteps; fSteps=0;
77 }
78 
79 
80 ////////////////////////////////////////////////////////////////////////////////
81 /// copy constructor.
82 
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);
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 
125 {
126  if (!gPad) {
127  gROOT->MakeDefCanvas();
128  gPad->SetFillColor(41);
129  gPad->SetFrameFillColor(19);
130  gPad->SetGrid();
131  }
132  if (fFrame) {
134  fFrame->Draw();
135  }
136  Paint(option);
137 
138 }
139 
140 ////////////////////////////////////////////////////////////////////////////////
141 /// Paint all objects added to each time step
142 
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 }
TGraphTime is used to draw a set of objects evolving with nsteps in time between tmin and tmax...
Definition: TGraphTime.h:31
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
An array of TObjects.
Definition: TObjArray.h:39
float xmin
Definition: THbookFile.cxx:93
virtual void SetMaximum(Double_t maximum=-1111)
Definition: TH1.h:399
Double_t fYmin
Definition: TGraphTime.h:39
const char Option_t
Definition: RtypesCore.h:62
virtual void Delete(Option_t *option="")
Remove all objects from the array AND delete all heap based objects.
Definition: TObjArray.cxx:329
float ymin
Definition: THbookFile.cxx:93
TH1 * fFrame
Definition: TGraphTime.h:42
virtual void SetMinimum(Double_t minimum=-1111)
Definition: TH1.h:400
#define gROOT
Definition: TROOT.h:364
Basic string class.
Definition: TString.h:137
void ToLower()
Change string to lower-case.
Definition: TString.cxx:1089
int Int_t
Definition: RtypesCore.h:41
virtual void Draw(Option_t *option="")
Default Draw method for all objects.
Definition: TObject.cxx:255
virtual void 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 ...
Definition: TGraphTime.cxx:174
Int_t fSleepTime
Definition: TGraphTime.h:35
virtual void Paint(Option_t *chopt="")
Paint all objects added to each time step.
Definition: TGraphTime.cxx:143
virtual void Sleep(UInt_t milliSec)
Sleep milliSec milli seconds.
Definition: TSystem.cxx:442
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:33
A doubly linked list.
Definition: TList.h:47
Int_t fNsteps
Definition: TGraphTime.h:36
float ymax
Definition: THbookFile.cxx:93
TObjArray * fSteps
Definition: TGraphTime.h:41
R__EXTERN TSystem * gSystem
Definition: TSystem.h:549
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2851
char * Form(const char *fmt,...)
float xmax
Definition: THbookFile.cxx:93
virtual void AddAt(TObject *obj, Int_t idx)
Add object at position ids.
Definition: TObjArray.cxx:239
virtual TObjLink * FirstLink() const
Definition: TList.h:101
Double_t fXmax
Definition: TGraphTime.h:38
virtual void Draw(Option_t *chopt="")
Draw this TGraphTime.
Definition: TGraphTime.cxx:124
TObject * UncheckedAt(Int_t i) const
Definition: TObjArray.h:91
#define ClassImp(name)
Definition: Rtypes.h:279
double Double_t
Definition: RtypesCore.h:55
Double_t fYmax
Definition: TGraphTime.h:40
Mother of all ROOT objects.
Definition: TObject.h:37
virtual ~TGraphTime()
GraphTime default destructor.
Definition: TGraphTime.cxx:72
virtual void Add(TObject *obj)
Definition: TList.h:81
Double_t fXmin
Definition: TGraphTime.h:37
TGraphTime()
default constructor.
Definition: TGraphTime.cxx:32
virtual Int_t Add(const TObject *obj, Int_t slot, Option_t *option="")
Add one object to a time slot.
Definition: TGraphTime.cxx:103
THist< 1, double, THistStatContent, THistStatUncertainty > TH1D
Definition: THist.hxx:301
#define gPad
Definition: TVirtualPad.h:289
virtual void SetTitle(const char *title)
Change (i.e.
Definition: TH1.cxx:6027
virtual void SetStats(Bool_t stats=kTRUE)
Set statistics option on/off.
Definition: TH1.cxx:8101
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition: TObject.cxx:911
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:52