Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TGraphStruct.cxx
Go to the documentation of this file.
1// @(#)root/hist:$Id$
2// Author: Olivier Couet 13/07/09
3
4/*************************************************************************
5 * Copyright (C) 1995-2000, 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 "TVirtualPad.h"
13#include "TGraphStruct.h"
14
15#include <cstdio>
16#include <iostream>
17
18#include <gvc.h>
19#include <gvplugin.h>
20
21#ifdef GVIZ_STATIC
23///extern gvplugin_library_t gvplugin_neato_layout_LTX_library;
24///extern gvplugin_library_t gvplugin_core_LTX_library;
25
26
28 { "gvplugin_dot_layout_LTX_library", (void*)(&gvplugin_dot_layout_LTX_library) },
29/// { "gvplugin_neato_layout_LTX_library", (void*)(&gvplugin_neato_layout_LTX_library) },
30/// { "gvplugin_core_LTX_library", (void*)(&gvplugin_core_LTX_library) },
31 { 0, 0 }
32};
33#endif
34
35
36/** \class TGraphStruct
37\ingroup gviz
38
39The Graph Structure is an interface to the graphviz package.
40
41The graphviz package is a graph visualization system. This interface consists in
42three classes:
43
44 - TGraphStruct: holds the graph structure. It uses the graphviz library to
45 layout the graphs and the ROOT graphics to paint them.
46 - TGraphNode: Is a graph node object which can be added in a TGraphStruct.
47 - TGraphEdge: Is an edge object connecting two nodes which can be added in
48 a TGraphStruct.
49
50Begin_Macro(source)
51../../../tutorials/visualisation/graphs/gr016_struct.C
52End_Macro
53
54A graph structure can be dumped into a "dot" file using DumpAsDotFile.
55*/
56
57////////////////////////////////////////////////////////////////////////////////
58/// Graph Structure default constructor.
59
61{
62 fNodes = 0;
63 fEdges = 0;
64 fGVGraph = 0;
65 fGVC = 0;
66
67 SetMargin();
68}
69
70////////////////////////////////////////////////////////////////////////////////
71/// Graph Structure default destructor.
72
83
84////////////////////////////////////////////////////////////////////////////////
85/// Add the edge "edge" in this TGraphStruct.
86
88{
89 if (!fEdges) fEdges = new TList;
90
91 fEdges->Add(edge);
92}
93
94////////////////////////////////////////////////////////////////////////////////
95/// Create an edge between n1 and n2 and put it in this graph.
96///
97/// Two edges can connect the same nodes the same way, so there
98/// is no need to check if an edge already exists.
99
101{
102 if (!fEdges) fEdges = new TList;
103
104 TGraphEdge *edge = new TGraphEdge(n1, n2);
105 fEdges->Add(edge);
106
107 return edge;
108}
109
110////////////////////////////////////////////////////////////////////////////////
111/// Add the node "node" in this TGraphStruct.
112
114{
115 if (!fNodes) fNodes = new TList;
116
117 fNodes->Add(node);
118}
119
120////////////////////////////////////////////////////////////////////////////////
121/// Create the node "name" if it does not exist and add it to this TGraphStruct.
122
123TGraphNode *TGraphStruct::AddNode(const char *name, const char *title)
124{
125 if (!fNodes) fNodes = new TList;
126
128
129 if (!node) {
130 node = new TGraphNode(name, title);
131 fNodes->Add(node);
132 }
133
134 return node;
135}
136
137////////////////////////////////////////////////////////////////////////////////
138/// Dump this graph structure as a "dot" file.
139
141{
142 if (!fGVGraph) {
143 Int_t ierr = Layout();
144 if (ierr) return;
145 }
146 FILE *file;
147 file=fopen(filename,"wt");
148 if (file) {
149 agwrite((Agraph_t*)fGVGraph, file);
150 fclose(file);
151 }
152}
153
154////////////////////////////////////////////////////////////////////////////////
155/// Draw the graph
156
158{
159 if (!fGVGraph) {
160 Int_t ierr = Layout();
161 if (ierr) return;
162 }
163
164 // Get the bounding box
165 if (gPad) {
168 }
169
171
172 // Draw the nodes
173 if (fNodes) {
174 TGraphNode *node;
175 node = (TGraphNode*) fNodes->First();
176 node->Draw();
177 for(Int_t i = 1; i < fNodes->GetSize(); i++){
178 node = (TGraphNode*)fNodes->After(node);
179 if (node) node->Draw();
180 }
181 }
182
183 // Draw the edges
184 if (fEdges) {
186 edge = (TGraphEdge*) fEdges->First();
187 edge->Draw();
188 for(Int_t i = 1; i < fEdges->GetSize(); i++){
190 if (edge) edge->Draw();
191 }
192 }
193}
194
195////////////////////////////////////////////////////////////////////////////////
196/// Layout the graph into a GraphViz data structure
197
199{
200 TGraphNode *node;
202
203 // Create the graph context.
204 if (fGVC) gvFreeContext(fGVC);
205#ifdef GVIZ_STATIC
207#else
208 fGVC = gvContext();
209#endif
210
211 // Create the graph.
212 if (fGVGraph) {
215 }
216#ifdef WITH_CGRAPH
217 fGVGraph = (GVizAgraph_t*)agopen((char*)"GVGraph", Agdirected, 0);
218#else
219 fGVGraph = (GVizAgraph_t*)agopen((char*)"GVGraph", AGDIGRAPH);
220#endif
221
222 // Put the GV nodes into the GV graph
223 if (fNodes) {
224 node = (TGraphNode*) fNodes->First();
225 node->CreateGVNode(fGVGraph);
226 for(Int_t i = 1; i < fNodes->GetSize(); i++){
227 node = (TGraphNode*)fNodes->After(node);
228 if (node) node->CreateGVNode(fGVGraph);
229 }
230 }
231
232 // Put the edges into the graph
233 if (fEdges) {
234 edge = (TGraphEdge*) fEdges->First();
235 edge->CreateGVEdge(fGVGraph);
236 for(Int_t i = 1; i < fEdges->GetSize(); i++){
238 if (edge) edge->CreateGVEdge(fGVGraph);
239 }
240 }
241
242 // Layout the graph
243 int ierr = gvLayout(fGVC, (Agraph_t*)fGVGraph, (char*)"dot");
244 if (ierr) return ierr;
245
246 // Layout the nodes
247 if (fNodes) {
248 node = (TGraphNode*) fNodes->First();
249 node->Layout();
250 for(Int_t i = 1; i < fNodes->GetSize(); i++){
251 node = (TGraphNode*)fNodes->After(node);
252 if (node) node->Layout();
253 }
254 }
255
256 // Layout the edges
257 if (fEdges) {
258 edge = (TGraphEdge*) fEdges->First();
259 edge->Layout();
260 for(Int_t i = 1; i < fEdges->GetSize(); i++){
262 if (edge) edge->Layout();
263 }
264 }
265
266 return 0;
267}
268
269////////////////////////////////////////////////////////////////////////////////
270/// Save primitive as a C++ statement(s) on output stream out
271
273{
274 SavePrimitiveConstructor(out, Class(), "graphstruct");
275
276 // Save the nodes
277 if (fNodes) {
278 TGraphNode *node = nullptr;
279 for (Int_t i = 0; i < fNodes->GetSize(); i++) {
280 node = static_cast<TGraphNode *>(i == 0 ? fNodes->First() : fNodes->After(node));
281 if (node) {
282 out << " TGraphNode *" << node->GetName() << " = graphstruct->AddNode(\"" << node->GetName() << "\", \""
283 << TString(node->GetTitle()).ReplaceSpecialCppChars() << "\");\n";
284 node->SaveAttributes(out);
285 }
286 }
287 }
288
289 // Save the edges
290 if (fEdges) {
291 TGraphEdge *edge = nullptr;
292 for (Int_t i = 0; i < fEdges->GetSize(); i++) {
293 edge = static_cast<TGraphEdge *>(i == 0 ? fEdges->First() : fEdges->After(edge));
294 if (edge) {
295 SavePrimitiveConstructor(out, TGraphEdge::Class(), "graphedge", TString::Format("%s, %s", edge->GetNode1()->GetName(), edge->GetNode2()->GetName()));
296 out << " graphstruct->AddEdge(graphedge);\n";
297 edge->SaveAttributes(out, "graphedge");
298 }
299 }
300 }
301
302 SavePrimitiveDraw(out, "graphstruct", option);
303}
304
305////////////////////////////////////////////////////////////////////////////////
306
308{
309}
const char Option_t
Option string (const char)
Definition RtypesCore.h:80
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t option
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char filename
char name[80]
Definition TGX11.cxx:110
#define gPad
Buffer base class used for serializing objects.
Definition TBuffer.h:43
virtual Int_t GetSize() const
Return the capacity of the collection, i.e.
An edge object connecting two nodes which can be added in a TGraphStruct.
Definition TGraphEdge.h:25
static TClass * Class()
A graph node object which can be added in a TGraphStruct.
Definition TGraphNode.h:27
void SaveAttributes(std::ostream &)
Save attributes as a C++ statement(s) on output stream out called by TGraphStruct::SavePrimitive.
void CreateGVNode(GVizAgraph_t *gv)
Create the GraphViz node into the GraphViz data structure gv.
void Layout()
Layout this node in the GraphViz space.
~TGraphStruct() override
Graph Structure default destructor.
TList * fEdges
List of edges in this TGraphStruct.
void AddNode(TGraphNode *node)
Add the node "node" in this TGraphStruct.
void SavePrimitive(std::ostream &out, Option_t *option="") override
Save primitive as a C++ statement(s) on output stream out.
GVC_s * fGVC
Graphviz context.
void Draw(Option_t *option="") override
Draw the graph.
TList * fNodes
List of nodes in this TGraphStruct.
Double_t fMargin
Margin around the graph (in dots)
Int_t Layout()
Layout the graph into a GraphViz data structure.
void SetMargin(Double_t m=10)
GVizAgraph_t * fGVGraph
Graphviz graph.
void AddEdge(TGraphEdge *edge)
Add the edge "edge" in this TGraphStruct.
void Streamer(TBuffer &) override
Stream an object of class TObject.
static TClass * Class()
void DumpAsDotFile(const char *filename)
Dump this graph structure as a "dot" file.
TGraphStruct()
Graph Structure default constructor.
A doubly linked list.
Definition TList.h:38
TObject * After(const TObject *obj) const override
Returns the object after object obj.
Definition TList.cxx:327
TObject * FindObject(const char *name) const override
Find an object in this list using its name.
Definition TList.cxx:575
void Add(TObject *obj) override
Definition TList.h:81
TObject * First() const override
Return the first object in the list. Returns 0 when list is empty.
Definition TList.cxx:656
const char * GetName() const override
Returns name of object.
Definition TNamed.h:49
const char * GetTitle() const override
Returns title of object.
Definition TNamed.h:50
virtual void AppendPad(Option_t *option="")
Append graphics object to current pad.
Definition TObject.cxx:203
static void SavePrimitiveDraw(std::ostream &out, const char *variable_name, Option_t *option=nullptr)
Save invocation of primitive Draw() method Skipped if option contains "nodraw" string.
Definition TObject.cxx:822
virtual void Draw(Option_t *option="")
Default Draw method for all objects.
Definition TObject.cxx:293
static void SavePrimitiveConstructor(std::ostream &out, TClass *cl, const char *variable_name, const char *constructor_agrs="", Bool_t empty_line=kTRUE)
Save object constructor in the output stream "out".
Definition TObject.cxx:771
Basic string class.
Definition TString.h:138
TString & ReplaceSpecialCppChars()
Find special characters which are typically used in printf() calls and replace them by appropriate es...
Definition TString.cxx:1121
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
Definition TString.cxx:2384