Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
my_test4.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file my_test4.cc
1 #include <iostream>
2 
3 #include <GTL/graph.h>
4 #include <GTL/bellman_ford.h>
5 #include <GTL/edge_map.h>
6 #include <GTL/node_map.h>
7 #include <GTL/dfs.h>
8 #include <memory>
9 #include <list>
10 #include <vector>
11 #include "helper.h"
12 
13 class vertex {
14 
15 public:
16  vertex() {};
17  vertex(int myId) {id=myId;}
18  virtual ~vertex() {cout<<" Vertex Detructor ..."<<endl;}
19  int id;
20 };
21 
22 class parton {
23 
24 public:
25 
26  parton() {};
27  parton (double mpt, double meta, double mphi, double me, bool mfinal) {pt=mpt;eta=meta;phi=mphi;e=me;final=mfinal;}
28  virtual ~parton() {cout<<" Parton Destructor ..."<<endl;}
29 
30  bool isFinal() {return final;}
31 
32  double pt, eta, phi, e;
33  bool final;
34 };
35 
36 class shower2 : public graph {
37 
38 public:
39 
40  shower2() : graph() {cout<<"Shower2 Constrcutor ..."<<endl;}
41  virtual ~shower2() {cout<<"Shower2 Detrcutor ..."<<endl;}
42 
43  node new_vertex(shared_ptr<vertex> v) {node n=graph::new_node();XX[n]=v; return n;}
44  //void new_parton(node s, node t, shared_ptr<parton> p) {edge e=graph::new_edge(s,t);PP[e]=p;}//std::move(p);} //; return e;}
45  void new_parton(node s, node t, unique_ptr<parton> p) {edge e=graph::new_edge(s,t);PP[e]=std::move(p);}
46 
47  //unique_ptr<parton> GetParton(edge e) {return unique_ptr<parton> (PP[e]);}
48 
49  //int GetNodeValue(node n) const {return XX[n];}
50  void save_node_info_handler (ostream *o, node n) const { *o<<"MyID "<<XX[n]->id<<endl;}
51  void save_edge_info_handler (ostream *o, edge n) const { *o<<"pT = "<<PP[n]->pt<<endl;}
52 
54  cout<<"Load edge ... "<<e<<endl;
55  struct GML_pair* tmp = read;
56 
57  while (tmp) {
58 
59  //printf ("*KEY* : %s \n", tmp->key);
60  if (((string) (tmp->key)).find("pT")<1)
61  {
62 
63  PP[e]=make_shared<parton>(tmp->value.floating,0,0,tmp->value.floating,false);
64  }
65 
66  tmp = tmp->next;
67  }
68  }
69 
70  //maybe smarter to store node id and edge source target in vertex/parton vector and recreate graph from scratch ....
72  cout<<"Load node ... "<<n<<endl;
73  //cout<<read->key<<endl;//" "<<read->value.floating<<endl;
74  struct GML_pair* tmp = read;
75  int i;
76  int level=2;
77  int nC=0;
78 
79  while (tmp) {
80 
81  //for (i = 0; i < level; i++) {
82  // printf (" ");
83  //}
84 
85  //printf ("*KEY* : %s \n", tmp->key);
86 
87  //printf ("%s", tmp->key.id);
88 
89  //if (((string) (tmp->key)).find("text")) cout<<"text .."<<endl;
90 
91  /*
92  switch (tmp->kind) {
93  case GML_INT:
94  printf (" *VALUE* (long) : %ld \n", tmp->value.integer);
95  break;
96 
97  case GML_DOUBLE:
98  printf (" *VALUE* (double) : %f \n", tmp->value.floating);
99  break;
100 
101  case GML_STRING:
102  printf (" *VALUE* (string) : %s \n", tmp->value.str);
103  break;
104 
105  case GML_LIST:
106  printf (" *VALUE* (list) : \n");
107  GML_print_list (tmp->value.list, level+1);
108  break;
109 
110  default:
111  break;
112  }
113  */
114 
115  //cout<<((string) (tmp->key)).find("text")<<endl;
116  if (((string) (tmp->key)).find("MyID")<1)
117  {
118  //cout<<n<<" "<<nC<<" "<<i<<endl;
119  //cout<<n<<" "<<tmp->value.integer<<endl;
120  XX[n]=make_shared<vertex>(tmp->value.integer);
121  }
122 
123  //nC++;
124  //cout<<nC<<endl;
125  tmp = tmp->next;
126  }
127  }
128 
129  double GetEdgeValue(edge n) const {return PP[n]->pt;}
130  int GetNodeValue(node n) const {return XX[n]->id;}
131  bool GetFinal(edge e) const {return PP[e]->final;}
132  //void pre_new_edge_handler(node s,node t) {};
133  //void post_new_edge_handler(edge e) {};
135  edge_iterator git3, gend3;
136  for (git3 = edges_begin(), gend3 = edges_end(); git3 != gend3; ++git3)
137  {
138  //cout<<*git3<<" "<<mS->GetEdgeValue(*git3)<<endl;
139  PP[*git3]=nullptr;
140  }
141  node_iterator git4, gend4;
142  for (git4 = nodes_begin(), gend4 = nodes_end(); git4 != gend4; ++git4)
143  {
144  XX[*git4]=nullptr;
145  }
146  }
147 
148  // vector<shared_ptr<vertex>> GetVertices() {};
149  /*
150  vector<shared_ptr<parton>> GetPartons() {
151  vector<shared_ptr<parton>> myv; list<edge> le=all_edges();
152  //int n=le.size();
153  //for (int i=0;i<n;i++)
154  int n=0;
155 
156  for (list<edge>::iterator it=le.begin(); it!=le.end(); ++it)
157  {
158  if (PP[*it]->isFinal())
159  myv.push_back(PP[*it]);
160  //cout<<PP[*it]->pt<<endl;
161  }
162  return myv;
163  }
164  */
165 
166  vector<weak_ptr<parton>> GetPartons() {
167  vector<weak_ptr<parton>> myv; list<edge> le=all_edges();
168  //int n=le.size();
169  //for (int i=0;i<n;i++)
170  int n=0;
171 
172  for (list<edge>::iterator it=le.begin(); it!=le.end(); ++it)
173  {
174  if (PP[*it]->isFinal())
175  myv.push_back(PP[*it]);
176  //cout<<PP[*it]->pt<<endl;
177  }
178  return myv;
179  }
180 
181  private:
182 
184  edge_map<shared_ptr<parton>> PP; //unique_ptr not working !???
185 
186 };
187 
188 int main (int argc, char* argv[])
189 {
190  cout << "Loading graph and preserving ids" << endl;
191  auto mS=make_shared<shower2>();
192  //auto G=make_shared<graph>();
193 
194  //cout<<G->load("test.gml", true).err_num<<endl;
195  if (mS->load("test.gml", true).err_num != GML_OK) {
196  cout << "Loading failed" << endl;
197 
198  //exit(1);
199  }
200 
201  //G->save();
202  //cout << "Loading OK" << endl;
203 
204  dfs search;
205  search.start_node();// defaulted to first node ... //mS->all_nodes().begin());
206  search.run(*mS);
207  cout<< search.number_of_reached_nodes () <<endl;
208 
209  dfs::dfs_iterator itt2, endt2;
210  for (itt2 = search.begin(), endt2=search.end(); itt2 !=endt2; ++itt2)
211  {
212  cout<<*itt2<<" "<<mS->GetNodeValue(*itt2)<<endl;
213  }
214 
215  mS=0;
216  //mS->clear();
217  cout<<"Finished ..."<<endl;
218 }
219