TGraphTime.cxx

Go to the documentation of this file.
00001 // @(#)root/hist:$Id: TGraphTime.cxx 31667 2009-12-08 15:11:34Z brun $
00002 // Author: Rene Brun   14/07/2009
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2009, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 #include "TGraphTime.h"
00013 #include "TVirtualPad.h"
00014 #include "TH1.h"
00015 #include "TROOT.h"
00016 #include "TObjArray.h"
00017 #include "TSystem.h"
00018 
00019 ClassImp(TGraphTime)
00020 
00021 //______________________________________________________________________________
00022 //
00023 // TGraphTime is used to draw a set of objects evolving with nsteps in time between tmin and tmax.
00024 // each time step has a new list of objects. This list can be identical to
00025 // the list of objects in the previous steps, but with different attributes.
00026 //   see example of use in $ROOTSYS/tutorials/graphs/gtime.C
00027 
00028 //______________________________________________________________________________
00029 TGraphTime::TGraphTime(): TNamed()
00030 {
00031    // default constructor.
00032 
00033    fSleepTime = 0;
00034    fNsteps    = 0;
00035    fXmin      = 0;
00036    fXmax      = 1;
00037    fYmin      = 0;
00038    fYmax      = 1;
00039    fSteps     = 0;
00040    fFrame     = 0;
00041 }
00042 
00043 
00044 //______________________________________________________________________________
00045 TGraphTime::TGraphTime(Int_t nsteps, Double_t xmin, Double_t ymin, Double_t xmax, Double_t ymax)
00046       :TNamed()
00047 {
00048    // Create a TGraphTime with nsteps in range [xmin,xmax][ymin,ymax]
00049 
00050    if (nsteps <= 0) {
00051       Warning("TGraphTime", "Number of steps %d changed to 100",nsteps);
00052       nsteps = 100;
00053    }
00054    fSleepTime = 0;
00055    fNsteps    = nsteps;
00056    fXmin      = xmin;
00057    fXmax      = xmax;
00058    fYmin      = ymin;
00059    fYmax      = ymax;
00060    fSteps     = new TObjArray(nsteps+1);
00061    fFrame     = new TH1D("frame","",100,fXmin,fXmax);
00062    fFrame->SetMinimum(ymin);
00063    fFrame->SetMaximum(ymax);
00064    fFrame->SetStats(0);
00065 }
00066 
00067 
00068 //______________________________________________________________________________
00069 TGraphTime::~TGraphTime()
00070 {
00071    // GraphTime default destructor.
00072    
00073    if (!fSteps) return;
00074    fSteps->Delete();
00075    delete fSteps; fSteps=0;
00076 }
00077 
00078 
00079 //______________________________________________________________________________
00080 TGraphTime::TGraphTime(const TGraphTime &gtime) : TNamed(gtime)
00081 {
00082    // copy constructor.
00083 
00084    fSleepTime = gtime.fSleepTime;
00085    fNsteps    = gtime.fNsteps;
00086    fXmin      = gtime.fXmin;
00087    fXmax      = gtime.fXmax;
00088    fYmin      = gtime.fYmin;
00089    fYmax      = gtime.fYmax;
00090    fSteps     = new TObjArray(fNsteps+1);
00091    fFrame     = new TH1D("frame","",100,fXmin,fXmax);
00092    fFrame->SetMinimum(fYmin);
00093    fFrame->SetMaximum(fYmax);
00094    fFrame->SetStats(0);
00095 }
00096 
00097 //______________________________________________________________________________
00098 Int_t TGraphTime::Add(const TObject *obj, Int_t slot, Option_t *option)
00099 {
00100    // Add one object to a time slot. 
00101    // TGraphTime becomes the owner of this object.
00102    // object will be drawn with option
00103    
00104    if (!fSteps) {
00105       fNsteps = 100;
00106       fSteps = new TObjArray(fNsteps+1);
00107    }
00108    if (slot < 0 || slot >= fNsteps) return -1;
00109    TList *list = (TList*)fSteps->UncheckedAt(slot);
00110    if (!list) {
00111       list = new TList();
00112       fSteps->AddAt(list,slot);
00113    }
00114    list->Add((TObject*)obj, option);
00115    return slot;
00116 }
00117 
00118 
00119 //______________________________________________________________________________
00120 void TGraphTime::Draw(Option_t *option)
00121 {
00122    // Draw this TGraphTime.
00123    // for each time step the list of objects added to this step are drawn.
00124    
00125    if (!gPad) {
00126       gROOT->MakeDefCanvas();
00127       gPad->SetFillColor(41);
00128       gPad->SetFrameFillColor(19);
00129       gPad->SetGrid();
00130    }
00131    if (fFrame) {
00132       fFrame->SetTitle(GetTitle());
00133       fFrame->Draw();
00134    }
00135    Paint(option);
00136 
00137 }
00138 
00139 //______________________________________________________________________________
00140 void TGraphTime::Paint(Option_t *option)
00141 {
00142    // Paint all objects added to each time step
00143    
00144    TString opt = option;
00145    opt.ToLower();
00146    TObject *frame = gPad->GetPrimitive("frame");
00147    TList *list = 0;
00148    TObjLink *lnk;
00149 
00150    for (Int_t s=0;s<fNsteps;s++) {
00151       list = (TList*)fSteps->UncheckedAt(s);
00152       if (list) {
00153          gPad->GetListOfPrimitives()->Remove(frame);
00154          gPad->GetListOfPrimitives()->Clear();
00155          if (frame) gPad->GetListOfPrimitives()->Add(frame);
00156          lnk = list->FirstLink();
00157          while(lnk) {
00158             TObject *obj = lnk->GetObject();
00159             obj->Draw(lnk->GetAddOption());
00160             lnk = lnk->Next();
00161          }
00162          gPad->Update();
00163          if (fSleepTime > 0) gSystem->Sleep(fSleepTime);
00164       }
00165    }
00166 }
00167 
00168 //______________________________________________________________________________
00169 void TGraphTime::SaveAnimatedGif(const char *filename) const
00170 {
00171    // Save this object to filename as an animated gif file
00172    // if filename is specified it must be of the form xxx.gif
00173    // otherwise a file yyy.gif is produced where yyy is the object name
00174    
00175    TObject *frame = gPad->GetPrimitive("frame");
00176    TList *list = 0;
00177    TObjLink *lnk;
00178 
00179    for (Int_t s=0;s<fNsteps;s++) {
00180       list = (TList*)fSteps->UncheckedAt(s);
00181       if (list) {
00182          gPad->GetListOfPrimitives()->Remove(frame);
00183          gPad->GetListOfPrimitives()->Clear();
00184          if (frame) gPad->GetListOfPrimitives()->Add(frame);
00185          lnk = list->FirstLink();
00186          while(lnk) {
00187             TObject *obj = lnk->GetObject();
00188             obj->Draw(lnk->GetAddOption());
00189             lnk = lnk->Next();
00190          }
00191          gPad->Update();
00192          if (strlen(filename) > 0) gPad->Print(Form("%s+",filename));
00193          else                      gPad->Print(Form("%s+",GetName()));
00194          if (fSleepTime > 0) gSystem->Sleep(fSleepTime);
00195       }
00196    }
00197 }

Generated on Tue Jul 5 14:24:18 2011 for ROOT_528-00b_version by  doxygen 1.5.1