ROOTPWA
waveDescription.h
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 // class that reads/writes wave description from/to keyfiles,
29 // constructs decay topologies and amplitudes
30 // can be saved into .root files
31 //
32 //
33 // Author List:
34 // Boris Grube TUM (original author)
35 //
36 //
37 //-------------------------------------------------------------------------
38 
39 
40 #ifndef WAVEDESCRIPTION_H
41 #define WAVEDESCRIPTION_H
42 
43 
44 #include <string>
45 #include <vector>
46 #include <map>
47 
48 #include "TObject.h"
49 
50 #ifndef __CINT__
51 #include "libconfig.h++"
52 
53 #include "isobarDecayTopology.h"
54 #include "isobarAmplitude.h"
55 #else
56 namespace libconfig {
57  class Config;
58  class Setting;
59 }
60 #endif
61 
62 
63 namespace rpwa {
64 
65 
66  class waveDescription : public TObject {
67 
68  public:
69 
71  virtual ~waveDescription();
72 
73  void clear();
74 
75  waveDescription& operator =(const waveDescription& waveDesc);
76 
77 #ifndef __CINT__
78 
79  // construction of decay topology and amplitude objects
80  bool parseKeyFile (const std::string& keyFileName);
81  bool keyFileParsed() const { return _keyFileParsed; }
82 
83  std::string keyFileContents() const { return _keyFileLocalCopy; }
84  std::ostream& printKeyFileContents(std::ostream& out) const;
86  const bool fromTemplate = false) const;
87  bool constructAmplitude(isobarAmplitudePtr& amplitude) const;
88  bool constructAmplitude(isobarAmplitudePtr& amplitude,
89  const isobarDecayTopologyPtr& topo) const;
90 
91  template<class T>
92  static bool writeKeyFile(std::ostream& out,
93  const T& topoOrAmp,
94  const bool writeProdVert = true);
95  template<class T>
96  static bool writeKeyFile(const std::string& keyFileName,
97  const T& topoOrAmp,
98  const bool writeProdVert = true);
99 
100  static std::string waveNameFromTopology
101  (isobarDecayTopology topo,
102  const bool newConvention = false,
103  const isobarDecayVertexPtr& currentVertex = isobarDecayVertexPtr());
104 
105  static std::string waveLaTeXFromTopology
106  (isobarDecayTopology topo,
107  const isobarDecayVertexPtr& currentVertex = isobarDecayVertexPtr());
108 
109  static bool debug() { return _debug; }
110  static void setDebug(const bool debug = true) { _debug = debug; }
111 
112 
113  private:
114 
115  bool parseKeyFileLocalCopy();
116 
117  // helper functions for construction of decay topology and ampltiude
118  static bool constructXParticle(const libconfig::Setting& XQnKey,
119  particlePtr& X);
120  static productionVertexPtr mapProductionVertexType(const libconfig::Setting& prodVertKey,
121  const std::string& vertType,
122  const particlePtr& X);
123  static bool constructProductionVertex(const libconfig::Setting& rootKey,
124  const particlePtr& X,
125  productionVertexPtr& prodVert);
126  static bool constructParticle(const libconfig::Setting& particleKey,
128  const bool requirePartInTable = true);
129  static massDependencePtr mapMassDependenceType(const std::string& massDepType);
130  static bool constructDecayVertex(const libconfig::Setting& parentKey,
131  const particlePtr& parentParticle,
132  std::vector<isobarDecayVertexPtr>& decayVertices,
133  std::vector<particlePtr>& fsParticles,
134  const bool fromTemplate = false);
135  static isobarAmplitudePtr mapAmplitudeType(const std::string& formalismType,
136  const isobarDecayTopologyPtr& topo);
137 
138  // helper functions for writing key files from decay topology and ampltiude
139  static bool setProductionVertexKeys(libconfig::Setting& prodVertKey,
140  const productionVertexPtr& prodVert);
141  static bool setXQuantumNumbersKeys(libconfig::Setting& XQnKey,
142  const particle& X);
143  static bool setMassDependence(libconfig::Setting& isobarMassDepKey,
144  const massDependence& massDep);
145  static bool setXDecayKeys(libconfig::Setting& parentDecayKey,
146  const isobarDecayTopology& topo,
147  const isobarDecayVertex& vert);
148  static bool setKeysFromTopology(libconfig::Setting& rootKey,
149  const isobarDecayTopology& topo,
150  const bool setProdVert = true);
151  static bool setAmplitude(libconfig::Setting& amplitudeKey,
152  const isobarAmplitude& amplitude);
153  static bool setKeysFromAmplitude(libconfig::Setting& rootKey,
154  const isobarAmplitude& amplitude,
155  const bool setProdVert = true);
156  static bool writeKeyFile(FILE& outStream,
157  const isobarDecayTopology& topo,
158  const bool writeProdVert = true);
159  static bool writeKeyFile(FILE& outStream,
160  const isobarAmplitude& amplitude,
161  const bool writeProdVert = true);
162 
163 #endif // __CINT__
164 
165  libconfig::Config* _key;
167 
168  std::string _keyFileLocalCopy;
169 
170  static bool _debug;
171  static std::map<std::string, std::string> isobars;
172 
173 
174  ClassDef(waveDescription,2)
175 
176  };
177 
178 
179  template<class T>
180  inline
181  bool
182  waveDescription::writeKeyFile(std::ostream& out,
183  const T& topoOrAmp,
184  const bool writeProdVert)
185  {
186  // create pipe
187  int pipeFileDescriptors[2];
188  if (pipe(pipeFileDescriptors) == -1) {
189  printErr << "failed to create pipe. cannot write keys." << std::endl;
190  return false;
191  }
192  // open write end of pipe
193  FILE* pipeWriteEnd = fdopen(pipeFileDescriptors[1], "wt");
194  if (!pipeWriteEnd) {
195  printErr << "could not open write end of pipe. cannot write keys." << std::endl;
196  return false;
197  }
198  // write keys to pipe and close write end
199  if (not writeKeyFile(*pipeWriteEnd, topoOrAmp, writeProdVert)) {
200  printWarn << "problems writing keys for decay topology. cannot write keys." << std::endl;
201  fclose(pipeWriteEnd);
202  return false;
203  }
204  fclose(pipeWriteEnd);
205  // read keys from pipe
206  char buf;
207  unsigned int countChar = 0;
208  while (read(pipeFileDescriptors[0], &buf, 1) > 0) {
209  out << buf;
210  ++countChar;
211  }
212  close(pipeFileDescriptors[0]);
213  if (countChar > 0)
214  return true;
215  else {
216  printWarn << "nothing was written" << std::endl;
217  return false;
218  }
219  }
220 
221 
222  template<class T>
223  inline
224  bool
225  waveDescription::writeKeyFile(const std::string& keyFileName,
226  const T& topoOrAmp,
227  const bool writeProdVert)
228  {
229  if (_debug)
230  printDebug << "writing key file '" << keyFileName << "'" << std::endl;
231  std::ofstream outFile(keyFileName.c_str());
232  if (not outFile) {
233  printErr << "cannot create key file '" << keyFileName << "'" << std::endl;
234  return false;
235  }
236  if (writeKeyFile(outFile, topoOrAmp, writeProdVert)) {
237  outFile.close();
238  return true;
239  } else {
240  printWarn << "problems writing keys for decay topology. cannot write key file." << std::endl;
241  outFile.close();
242  return false;
243  }
244  }
245 
246 
247 } // namespace rpwa
248 
249 
250 #endif // WAVEDESCRIPTION_H