00001 #include "RootGraphix.hh"
00002 #include "EventHandler.hh"
00003 #include "TSystem.h"
00004 #include "TCanvas.h"
00005 #include "ConfigHandler.hh"
00006 #include "CommandSwitchFunctions.hh"
00007 #include "TCanvasImp.h"
00008 #include "TRootCanvas.h"
00009 #include "TGClient.h"
00010 #include "TGFrame.h"
00011 #include "TGTableLayout.h"
00012 #include "TRootEmbeddedCanvas.h"
00013 #include "TStyle.h"
00014 #include "TROOT.h"
00015 #include "TApplication.h"
00016 #include "TColor.h"
00017
00018 #include "utilities.hh"
00019
00020 const UInt_t fKeepRunning = 0x100000;
00021
00022 void* RunRootGraphix(void* mutexptr)
00023 {
00024 TMutex* mutex = (TMutex*)(mutexptr);
00025 while(mutex->TestBit(fKeepRunning)){
00026 gSystem->Sleep(100);
00027 TLockGuard lock(mutex);
00028 if(gSystem->ProcessEvents())
00029 break;
00030 }
00031 return 0;
00032 }
00033
00034 RootGraphix::RootGraphix() :
00035 BaseModule("RootGraphix","Draw graphical canvases using the ROOT GUI"),
00036 _mutex(), _thread(RunRootGraphix), _mainframe(0)
00037 {
00038 if(!gApplication)
00039 new TApplication("_app",0,0);
00040 _mutex.SetBit(fKeepRunning,true);
00041 LoadStyle();
00042
00043 RegisterParameter("single_window",_single_window = false,
00044 "Use a single window to contain all canvases");
00045 RegisterParameter("window_w",_window_w = 700,
00046 "Width of single window in pixels");
00047 RegisterParameter("window_h", _window_h = 700,
00048 "Height of single window in pixels");
00049
00050 }
00051
00052 RootGraphix::~RootGraphix()
00053 {
00054 Finalize();
00055
00056 }
00057
00058 int RootGraphix::Initialize()
00059 {
00060 CustomizeHistogramMenus();
00061 _thread.Run(&_mutex);
00062 return 0;
00063 }
00064
00065 int RootGraphix::Finalize()
00066 {
00067 if(_thread.GetState() == TThread::kRunningState){
00068 _mutex.SetBit(fKeepRunning,false);
00069 _thread.Join();
00070 }
00071 if(_mainframe){
00072 _canvases.clear();
00073 _mainframe->Cleanup();
00074 delete _mainframe;
00075 _mainframe = 0;
00076 }
00077
00078 for(size_t i=0; i < _canvases.size(); i++)
00079 delete _canvases[i];
00080 _canvases.clear();
00081
00082 return 0;
00083 }
00084
00085 int RootGraphix::Process(EventPtr evt)
00086 {
00087 for(size_t i=0; i < _canvases.size(); i++){
00088 Lock glock = AcquireLock();
00089 _canvases[i]->Update();
00090 }
00091 if(_mainframe){
00092 Lock glock = AcquireLock();
00093 int seconds = evt->GetRawEvent()->GetTimestamp() -
00094 EventHandler::GetInstance()->GetRunInfo()->starttime;
00095 _mainframe->SetWindowName(Form("daqman: %d events acquired, %d:%02d:%02d run time", evt->GetRawEvent()->GetID(), seconds/3600,(seconds/60)%60, seconds%60));
00096 }
00097 return 0;
00098 }
00099
00100
00101 RootGraphix::Lock RootGraphix::AcquireLock()
00102 {
00103 return std::auto_ptr<TLockGuard>(new TLockGuard(&_mutex));
00104 }
00105
00106 TCanvas* RootGraphix::GetCanvas(const char* title, bool preventclose,
00107 bool hidemenu)
00108 {
00109
00110 int n = _canvases.size();
00111 char name[100];
00112 sprintf(name, "canvas%d",n);
00113 TCanvas* canvas;
00114 Lock glock = AcquireLock();
00115 if(!_single_window){
00116 UInt_t wx = gClient->GetDisplayWidth();
00117 UInt_t wy = gClient->GetDisplayHeight();
00118 int topx = wx/2 * (n % 2) + 10*(n/4);
00119 int topy = wy/2 * (int)(n%4 > 1) + 10*(n/4);
00120
00121 canvas = new TCanvas(name, (title == 0 ? name : title),
00122 topx, topy, (int)(wx*0.49), (int)(wy*0.49));
00123 TRootCanvas* imp = (TRootCanvas*)(canvas->GetCanvasImp());
00124 if(imp && preventclose)
00125 imp->DontCallClose();
00126 if(imp && hidemenu)
00127 imp->ShowMenuBar(false);
00128 }
00129 else{
00130 if(!_mainframe){
00131 _mainframe = new TGMainFrame;
00132 _mainframe->DontCallClose();
00133 _mainframe->SetName("RootGraphixMainFrame");
00134 _mainframe->SetWindowName("daqman analysis display");
00135 }
00136 int nframes = n+1;
00137 int nx=1, ny=1;
00138 while(nx*ny<nframes){
00139 if(nx > ny)
00140 ++ny;
00141 else
00142 ++nx;
00143 }
00144 TGTableLayout* ml = new TGTableLayout(_mainframe,ny,nx,true);
00145 _mainframe->SetLayoutManager(ml);
00146 const int hints = kLHintsExpandX | kLHintsExpandY |
00147 kLHintsFillX | kLHintsFillY |
00148 kLHintsShrinkX | kLHintsShrinkY |
00149 kLHintsCenterX | kLHintsCenterY;
00150
00151
00152 int row=1, col=1;
00153 TList* subframes = _mainframe->GetList();
00154 if(subframes){
00155 TGFrameElement* el=0;
00156 TIter next(subframes);
00157 while( (el=(TGFrameElement*)next()) ){
00158 if(el->fLayout)
00159 delete el->fLayout;
00160 el->fLayout = new TGTableLayoutHints(col-1,col,row-1,row,hints);
00161 if(col < nx)
00162 ++col;
00163 else{
00164 col=1;
00165 ++row;
00166 }
00167 }
00168 }
00169
00170 TRootEmbeddedCanvas* ec = new TRootEmbeddedCanvas(name,_mainframe);
00171 _mainframe->AddFrame(ec, new TGTableLayoutHints(col-1,col,row-1,row,hints));
00172 _mainframe->MapSubwindows();
00173 _mainframe->MapWindow();
00174 _mainframe->Resize();
00175 _mainframe->Resize(_window_w,_window_h);
00176
00177 canvas = ec->GetCanvas();
00178 }
00179 _canvases.push_back(canvas);
00180 return canvas;
00181 }
00182
00183 void RootGraphix::LoadStyle()
00184 {
00185 TStyle *mystyle=new TStyle("mystyle","mystyle");
00186 *mystyle = *(gROOT->GetStyle("Plain"));
00187 mystyle->SetName("mystyle");
00188 gROOT->SetStyle("mystyle");
00189 mystyle->SetCanvasColor(kWhite);
00190
00191 mystyle->SetTitleFillColor(kWhite);
00192
00193 mystyle->SetFuncWidth(2);
00194 mystyle->SetHistLineWidth(2);
00195 mystyle->SetLegendBorderSize(0);
00196
00197 mystyle->SetStatBorderSize(0);
00198 mystyle->SetTitleBorderSize(0);
00199 mystyle->SetDrawBorder(0);
00200 mystyle->SetLabelSize(.04,"xyz");
00201 mystyle->SetTitleSize(.04,"xyz");
00202 mystyle->SetLabelFont(102,"xyz");
00203 mystyle->SetOptStat("");
00204 mystyle->SetStatFont(102);
00205 mystyle->SetTitleFont(102,"xyz");
00206 mystyle->SetTitleFont(102,"pad");
00207 mystyle->SetStatStyle(0);
00208 mystyle->SetStatX(1);
00209 mystyle->SetStatY(1);
00210 mystyle->SetStatW(.2);
00211 mystyle->SetStatH(.15);
00212 mystyle->SetTitleStyle(0);
00213 mystyle->SetTitleX(.2);
00214 mystyle->SetTitleW(.65);
00215 mystyle->SetTitleY(.98);
00216 mystyle->SetTitleH(.07);
00217 mystyle->SetStatColor(0);
00218 mystyle->SetStatBorderSize(0);
00219 mystyle->SetFillColor(10);
00220 mystyle->SetFillStyle(0);
00221 mystyle->SetTextFont(102);
00222 mystyle->SetCanvasBorderMode(0);
00223 mystyle->SetPadBorderMode(1);
00224 mystyle->SetFrameBorderMode(0);
00225 mystyle->SetDrawBorder(0);
00226
00227 mystyle->SetPalette(1,0);
00228 const Int_t NRGBs = 5;
00229 const Int_t NCont = 255;
00230
00231 Double_t stops[NRGBs] = { 0.00, 0.34, 0.61, 0.84, 1.00 };
00232 Double_t red[NRGBs] = { 0.00, 0.00, 0.87, 1.00, 0.51 };
00233 Double_t green[NRGBs] = { 0.00, 0.81, 1.00, 0.20, 0.00 };
00234 Double_t blue[NRGBs] = { 0.51, 1.00, 0.12, 0.00, 0.00 };
00235 TColor::CreateGradientColorTable(NRGBs, stops, red, green, blue, NCont);
00236 mystyle->SetNumberContours(NCont);
00237
00238
00239 }