ROOTPWA
wavelist_to_latex.C
Go to the documentation of this file.
1 // small macro converting a given wavelist to a latex file
2 // note: this script does not work for sequential decays
3 // author: Promme@web.de
4 // date: 16.12.10
5 //
6 #include <string>
7 #include <iostream>
8 
9 using namespace std;
10 
11 // taken from TrpwaCommonTools.cc in the gui/src directory 16.12.10
12 
13 // check if character is a sign and return the result
14 bool IsSign(char character, int& result){
15  result = 0;
16  if (character == '-' || character == '+'){
17  if (character == '-') result = -1; else result = +1;
18  return true;
19  }
20  else return false;
21 }
22 
23 // check if a number is coded to the text and return it
24 // the number must start at the given position
25 // pos is set to the next character after the number
26 bool IsNumber(string text, int& result, unsigned int& pos){
27  result = 0;
28  //if (pos < 0) return false;
29  unsigned int posend;
30  for (posend = pos; posend < text.size(); posend++){
31  if (!isdigit(text[posend])) break;
32  }
33  if (posend == pos) return false;
34  result = atoi(text.substr(pos,posend-pos).c_str());
35  pos = posend;
36  return true;
37 }
38 
39 string RemovePathExtension(string filename, string extension){
40  // remove the extension if available
41  if (extension != "" && filename.find(extension.c_str())!=string::npos){
42  int pos_ext = filename.find(extension.c_str());
43  filename.erase(pos_ext, filename.size()-pos_ext);
44  }
45  // remove the path if given
46  int slashpos = filename.rfind('/');
47  if (slashpos != (int) string::npos){
48  filename.erase(0, slashpos+1);
49  }
50  return filename;
51 }
52 
53 void GetJPCMreflISO1lsISO2(string wavename, int& J, int& P, int& C, int& M, int& refl, string& iso1, string& iso2, int& l, int& s){
54  // remove path and/or extension that might be .amp or .key if existent
55  string key = RemovePathExtension(wavename, ".amp");
56  key = RemovePathExtension(key, ".key");
57  J = 0;
58  P = 0;
59  C = 0;
60  M = 0;
61  refl = 0;
62  iso1 = "";
63  iso2 = "";
64  l = 0;
65  s = 0;
66 
67  // IGJPCMeIso1_ls_iso2
68 
69  //cout << " TrpwaSessionManager::GetJPCMreflISO1lsISO2() not implemented yet" << endl;
70  unsigned int charposlow(0);
71  unsigned int charposhigh(0);
72 
73  unsigned int stringsize = key.size();
74  // expecting the isospin
75  int I;
76  if (!IsNumber(key,I,charposlow)){
77  cout << " error decoding wavename " << key << ", parity expected at position " << charposlow << endl;
78  return;
79  }
80 //cout << "G" << endl;
81  // a sign for G parity
82  int G;
83  if (!IsSign(key[charposlow],G)){
84  cout << " error decoding wavename " << key << ", G parity not found " << endl;
85  return;
86  }
87  // do nothing with G parity information
88 //cout << "J" << endl;
89  charposlow++;
90  // expecting an integer for J
91  if (!IsNumber(key,J,charposlow)){
92  cout << " error decoding wavename " << key << ", J not found " << endl;
93  return;
94  }
95 //cout << "P" << endl;
96  if (!IsSign(key[charposlow],P)){
97  cout << " error decoding wavename " << key << ", parity not found " << endl;
98  return;
99  }
100 //cout << "C" << endl;
101  charposlow++;
102  // a sign for charge coniugation
103  if (!IsSign(key[charposlow],C)){
104  cout << " error decoding wavename " << key << ", C parity found " << endl;
105  return;
106  }
107 //cout << "M" << endl;
108  charposlow++;
109  // a digit for the M quantum number expected
110  if (!IsNumber(key,M,charposlow)){
111  cout << " error decoding wavename " << key << ", M not found " << endl;
112  return;
113  }
114 //cout << "e" << endl;
115  // a sign expected for reflectivity
116  if (!IsSign(key[charposlow],refl)){
117  cout << " error decoding wavename " << key << ", reflectivity not found " << endl;
118  return;
119  }
120 //cout << "iso1" << endl;
121  charposlow++;
122  charposhigh = charposlow;
123  // reading isobar 1
124  while(charposhigh < stringsize && key[charposhigh] != '_'){
125  charposhigh++;
126  }
127  iso1 = key.substr(charposlow, charposhigh-charposlow);
128  // skip the underscore
129 //cout << iso1 << "l" << endl;
130  charposlow=charposhigh;
131  charposlow++;
132  // decode the l
133  if (!isdigit(key[charposlow])){
134  cout << " error decoding wavename " << key << ", l expected at position " << charposlow << endl;
135  return;
136  }
137  char _l[] = {key[charposlow],'\0'};
138  l = atoi(_l);
139 //cout << "s" << endl;
140  charposlow++;
141  if (!isdigit(key[charposlow])){
142  cout << " error decoding wavename " << key << ", s expected at position " << charposlow << endl;
143  return;
144  }
145  char _s[] = {key[charposlow],'\0'};
146  s = atoi(_s);
147 //cout << "iso2" << endl;
148  charposlow++;
149  if (key[charposlow] != '_'){
150  cout << " error decoding wavename " << key << ", _ expected at position " << charposlow << endl;
151  return;
152  }
153  charposlow++;
154  // read the second isobar
155  iso2 = key.substr(charposlow, stringsize-charposlow);
156 }
157 
158 #include <fstream>
159 #include <map>
160 #include <sstream>
161 
162 void wavelist_to_latex(string wavelistname, string latexfilename = "wavelist.tex"){
163  ifstream wavelistfile(wavelistname.c_str());
164  if (!wavelistfile){
165  cout << " could not open wavelist " << wavelistname << endl;
166  return;
167  }
168  ofstream latexfile(latexfilename.c_str());
169  if (!latexfile){
170  cout << " could not open latexfile " << latexfilename << endl;
171  return;
172  }
173  map<string,string> isobars; // map of existing isobar names in the naming sheme and an index for it
174 
175  int nlines(0);
176  int nisobars(0);
177  latexfile << "\\begin{table}" << endl;
178  latexfile << " \\myfloatalign " << endl;
179  latexfile << " \\begin{tabularx}{\\textwidth}{ccrcl} \\toprule " << endl;
180  latexfile << " \\tableheadline{$J^{PC}$} &";
181  latexfile << " \\tableheadline{$M\\epsilon$} &";
182  latexfile << " \\tableheadline{$iso1$} &";
183  latexfile << " \\tableheadline{$\\left[ ^l _s \\right]$} &";
184  latexfile << " \\tableheadline{$iso2$} \\\\ \\midrule" << endl;
185  while (1){
186  char oneline[1024];
187  wavelistfile.getline(oneline, 1024);
188  if (!wavelistfile.good()) break;
189  // skip excluded entries
190  if (oneline[0] == '#') continue;
191  int J,P,C,M,refl,l,s;
192  string iso1, iso2, signP, signC, signrefl;
193  string wavename = oneline;
194  GetJPCMreflISO1lsISO2(wavename, J, P, C, M, refl, iso1, iso2, l, s);
195  // search if this isobars exist already and create/retrieve the index of it
196  map<string,string>::iterator it = isobars.find(iso1);
197  if (it != isobars.end()){
198  } else {
199  // construct a counting sheme out of 26 letters starting from #65 (A-Z)
200  stringstream iso;
201  int rest = nisobars;
202  int counter[] = {65,65,65};
203  while(rest){
204  counter[0]++;
205  if (counter[0] == 91){ // reached Z
206  counter[1]++;
207  counter[0] = 65; // restart from A
208  }
209  if (counter[1] == 91){
210  counter[2]++;
211  counter[1] = 65;
212  }
213  rest--;
214  }
215  iso << "\\isobar" << (char) counter[2] << (char) counter[1] << (char) counter[0];
216  isobars[iso1]=iso.str();
217  nisobars++;
218  }
219  it = isobars.find(iso2);
220  if (it != isobars.end()){
221  } else {
222  // construct a counting sheme out of 26 letters starting from #65 (A-Z)
223  stringstream iso;
224  int rest = nisobars;
225  int counter[] = {65,65,65};
226  while(rest){
227  counter[0]++;
228  if (counter[0] == 91){ // reached Z
229  counter[1]++;
230  counter[0] = 65; // restart from A
231  }
232  if (counter[1] == 91){
233  counter[2]++;
234  counter[1] = 65;
235  }
236  rest--;
237  }
238  iso << "\\isobar" << (char) counter[2] << (char) counter[1] << (char) counter[0];
239  isobars[iso2]=iso.str();
240  nisobars++;
241  }
242  // give the isobars a latex command name
243  iso1 = isobars[iso1];
244  iso2 = isobars[iso2];
245  signP = P < 0 ? "-" : "+";
246  signC = C < 0 ? "-" : "+";
247  signrefl = refl < 0 ? "-" : "+";
248  latexfile << " $" << J << "^{" << signP << signC << "}$ & $";
249  latexfile << M << signrefl << "$ & $";
250  latexfile << " " << iso1 << "$ & $";
251  latexfile << " \\left[ ^" << l << " _" << s << " \\right]" << "$ & $";
252  latexfile << " " << iso2 << " $ \\\\ " << endl;
253  nlines++;
254  }
255  latexfile << " \\bottomrule " << endl;
256  latexfile << " \\end{tabularx}" << endl;
257  latexfile << " \\caption[final partial wave set]{The final partial wave set used for fitting.}" << endl;
258  latexfile << " \\label{tab:pwset}" << endl;
259  latexfile << "\\end{table}" << endl;
260  latexfile << endl;
261 
262  latexfile << " % specification of latex commands to be filled by the user and put to proper place " << endl;
263  // create a definition of latex commands to be specified by the user
264  for (map<string,string>::iterator it = isobars.begin(); it != isobars.end(); it++){
265  latexfile << "\\newcommand{";
266  latexfile << it->second <<"}{"<< it->first <<"}" << endl;
267  }
268 
269  cout << " converted " << nlines << " waves " << endl;
270  latexfile.close();
271  wavelistfile.close();
272 }