ROOTPWA
createGraphDiagram.cc
Go to the documentation of this file.
1 
2 //
3 // Copyright 2010
4 //
5 // This file is part of rootpwa
6 //
7 // rootpwa is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // rootpwa is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with rootpwa. If not, see <http://www.gnu.org/licenses/>.
19 //
21 //-------------------------------------------------------------------------
22 // File and Version Information:
23 // $Rev:: $: revision of last commit
24 // $Author:: $: author of last commit
25 // $Date:: $: date of last commit
26 //
27 // Description:
28 // reads in data files in .evt or tree format, calculates
29 // amplitudes for each event based on given key file, and writes
30 // out amplitudes in PWA2000 binary or ascii format
31 //
32 //
33 // Author List:
34 // Boris Grube TUM (original author)
35 //
36 //
37 //-------------------------------------------------------------------------
38 
39 
40 #include <iostream>
41 #include <unistd.h>
42 #include <vector>
43 #include <string>
44 #include <algorithm>
45 #include <cctype>
46 
47 #include "TSystem.h"
48 
49 #include "particleDataTable.h"
50 #include "waveDescription.h"
51 
52 
53 using namespace std;
54 using namespace rpwa;
55 
56 
57 void
58 usage(const string& progName,
59  const int errCode = 0)
60 {
61  cerr << "creates graphical representation of decay graph(s) specified in given key file(s)" << endl
62  << "output files will have the same name as the corresponding key files" << endl
63  << endl
64  << "usage:" << endl
65  << progName
66  << " [-p PDG file -f output format -v -h] key file(s)" << endl
67  << " where:" << endl
68  << " -p file path to particle data table file (default: ./particleDataTable.txt)" << endl
69  << " -f format output format: dot, ps, eps (default), svg, fig, dia, png, gif" << endl
70  << " -v verbose; print debug output (default: false)" << endl
71  << " -h print help" << endl
72  << endl;
73  exit(errCode);
74 }
75 
76 
77 int
78 main(int argc,
79  char** argv)
80 {
81  printCompilerInfo();
82  printLibraryInfo ();
83  printSvnVersion ();
84  cout << endl;
85 
86  // parse command line options
87  const string progName = argv[0];
88  string pdgFileName = "./particleDataTable.txt";
89  string outFormat = "eps";
90  bool debug = false;
91  extern char* optarg;
92  extern int optind;
93  int c;
94  while ((c = getopt(argc, argv, "p:f:vh")) != -1)
95  switch (c) {
96  case 'p':
97  pdgFileName = optarg;
98  break;
99  case 'f':
100  outFormat = optarg;
101  break;
102  case 'v':
103  debug = true;
104  break;
105  case 'h':
106  default:
107  usage(progName);
108  }
109 
110  // get input file names
111  if (optind >= argc) {
112  printErr << "you need to specify at least one key file to process. aborting." << endl;;
113  usage(progName, 1);
114  }
115  vector<string> keyFileNames;
116  while (optind < argc) {
117  const string fileName = argv[optind++];
118  if (fileName.substr(fileName.length() - 4) == ".key")
119  keyFileNames.push_back(fileName);
120  else
121  printWarn << "input file '" << fileName << "' is not a .key file. "
122  << "skipping." << endl;
123  }
124  if (keyFileNames.size() == 0) {
125  printErr << "none of the specified input files is a .key file. aborting.";
126  usage(progName, 1);
127  }
128 
129  // check output format
130  transform(outFormat.begin(), outFormat.end(), outFormat.begin(), (int(*)(int))tolower);
131  const string validFormats[] = {"dot", "ps", "eps", "svg", "fig", "dia", "png", "gif"};
132  bool isValidFormat = false;
133  for (unsigned int i = 0; i < sizeof(validFormats) / sizeof(validFormats[0]); ++i)
134  if (outFormat == validFormats[i]) {
135  isValidFormat = true;
136  break;
137  }
138  if (not isValidFormat) {
139  printErr << "requested format '" << outFormat << "' is not supported." << endl;
140  usage(progName, 1);
141  }
142 
143  // initialize particle data table
144  particleDataTable& pdt = particleDataTable::instance();
145  pdt.readFile(pdgFileName);
146 
147  // loop over key files
148  unsigned int countSuccess = 0;
149  for (unsigned int i = 0; i < keyFileNames.size(); ++i) {
150 
151  // parse key file and create decay topology
152  waveDescription waveDesc;
153  isobarDecayTopologyPtr decayTopo;
154  if ( not waveDesc.parseKeyFile(keyFileNames[i])
155  or not waveDesc.constructDecayTopology(decayTopo)) {
156  printErr << "problems constructing decay topology from key file '" << keyFileNames[i] << "'. "
157  << "skipping." << endl;
158  continue;
159  }
160  const string dotFileName = keyFileNames[i].substr(0, keyFileNames[i].length() - 4) + ".dot";
161  if (debug)
162  printDebug << "writing graph to file '" << dotFileName << "'" << endl;
163  if (not decayTopo->writeGraphViz(dotFileName)) {
164  printWarn << "there were problems writing graph to file '" << dotFileName << "'. "
165  << "skipping." << endl;
166  continue;
167  }
168 
169  // convert file to output format
170  const string outFileName = keyFileNames[i].substr(0, keyFileNames[i].length() - 4)
171  + "." + outFormat;
172  if (debug)
173  printDebug << "converting graph to file '" << outFileName << "'" << endl;
174  stringstream cmd;
175  if ( (outFormat == "ps" ) or (outFormat == "eps")
176  or (outFormat == "fig") or (outFormat == "dia")
177  or (outFormat == "png") or (outFormat == "gif")
178  or (outFormat == "svg")) {
179  cmd << "dot -T" << outFormat << " -o " << outFileName << " " << dotFileName;
180  if (debug)
181  printDebug << "executing command '" << cmd.str() << "'" << endl;
182  if (gSystem->Exec(cmd.str().c_str()) != 0)
183  printWarn << "command '" << cmd.str() << "' was not successful." << endl;
184  else
185  ++countSuccess;
186  // cleanup
187  cmd.str("");
188  cmd << "rm " << (debug ? "--verbose " : "") << dotFileName;
189  if (gSystem->Exec(cmd.str().c_str()) != 0)
190  printWarn << "command '" << cmd.str() << "' was not successful." << endl;
191  }
192  }
193 
194  printSucc << "created " << countSuccess << " out of " << keyFileNames.size()
195  << " diagram files" << endl;
196 
197  return 0;
198 }