ROOTPWA
addamp.cc
Go to the documentation of this file.
1 
2 //
3 // Copyright 2009 Sebastian Neubert
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 
23 // programm to add up two amps with a specific branching and phase
24 #include <complex>
25 #include <iostream>
26 #include <fstream>
27 #include <cstdlib>
28 #include <string>
29 #include <vector>
30 #include <unistd.h>
31 #include <stdlib.h>
32 
33 
34 using namespace std;
35 
36 
37 void printUsage(char* prog)
38 {
39  cerr << "Add amplitude files with a phase between and a branching ratio." << endl;
40  cerr << "usage single amp mode:" << endl;
41  cerr << " " << prog << " inputfile1 inputfile2 outputfile [phase [ratio]]" << endl;
42  cerr << "usage multiple amp mode:" << endl;
43  cerr << " " << prog << " filelist <backup dir>" << endl;
44  cerr << " filelist format: " << endl;
45  cerr << " <file1>" << endl;
46  cerr << " <file2>" << endl;
47  cerr << " <outfile>" << endl;
48  cerr << " <phase>" << endl;
49  cerr << " <amplitude ratio>" <<endl;
50 }
51 
52 
53 int main(int argc, char** argv)
54 {
55 
56  vector<string> file1v;
57  vector<string> file2v;
58  vector<string> outfilev;
59  vector<double> phasev;
60  vector<double> branchv;
61  string backupDir("./");
62 
63  if (argc == 3) { // multiple amp mode
64  // open filelist
65  ifstream flist(argv[1]);
66  backupDir=argv[2];
67  while(flist.good()){
68  char f1[200];
69  flist.getline(f1,200);
70  char f2[200];
71  flist.getline(f2,200);
72  char f3[200];
73  flist.getline(f3,200);
74 
75  file1v.push_back(f1);
76  file2v.push_back(f2);
77  outfilev.push_back(f3);
78 
79  char ph[60];
80  double p;
81  flist.getline(ph,60);
82  if(string(ph)=="pi")p=3.14159592654;
83  else p=atof(ph);
84 
85  char br[60];
86  flist.getline(br,60);
87 
88  phasev.push_back(p);
89  branchv.push_back(atof(br));
90  } // read filelist
91  } // end if multiple file mode
92  else if (argc > 3 && argc <= 6) {
93  file1v.push_back(argv[1]);
94  file2v.push_back(argv[2]);
95  outfilev.push_back(argv[3]);
96  if (argc > 4) {
97  double phase;
98  if (string(argv[4]) == "pi")
99  phase = 3.14159592654;
100  else
101  phase = atof(argv[4]);
102  phasev.push_back(phase);
103  }
104  else
105  phasev.push_back(0);
106  if (argc > 5)
107  branchv.push_back(atof(argv[5]));
108  else
109  branchv.push_back(1);
110  }
111  else {
112  printUsage(argv[0]);
113  return 1;
114  }
115 
116 
117  // loop through all amplitudes in list
118  unsigned int n=file1v.size();
119  cerr << n << " amplitudes in list" << endl;
120  for(unsigned int i=0;i<n;++i){
121 
122  cout << endl;
123 
124  ifstream file1;
125  file1.open(file1v[i].c_str());
126  cout << file1v[i] << endl;
127  if(file1.fail()){
128  cerr << "*** Cannot open inputfile! Skipping!" << endl;
129  continue;
130  }
131  cout << file2v[i] << endl;
132  ifstream file2;
133  file2.open(file2v[i].c_str());
134  if(file2.fail()){
135  cerr << "*** Cannot open inputfile! Skipping!" << endl;
136  continue;
137  }
138 
139  if(outfilev[i]==file1v[i] || outfilev[i]==file2v[i]){
140  cerr << "Overwriting of files not allowed! Skipping" << endl;
141  continue;
142  }
143 
144  ofstream out(outfilev[i].c_str());
145 
146  cout << "Phase=" <<phasev[i] << " Br=" << branchv[i] << endl;
147  cout << "---> " << outfilev[i] << endl;
148 
149  complex<double> a1;
150  complex<double> a2;
151 
152  complex<double> phase(1,0);
153  double phi=phasev[i];
154  phase.real()=cos(phi);
155  phase.imag()=sin(phi);
156 
157  double R=branchv[i]; // branching ratio
158 
159  complex<double> amp;
160 
161  while (file1.read((char*) &a1,sizeof(complex<double>))){
162  //cout << a1 << endl;
163  file2.read((char*) &a2,sizeof(a2));
164  amp=1./sqrt(1+R)*(a1+R*phase*a2);
165  out.write((char*)&amp,sizeof(amp));
166  }
167 
168  file1.close();
169  file2.close();
170  out.close();
171 
172  // move original amplitudes to backup directory
173  if (backupDir != "./") {
174  string com("mv ");
175  com.append(file1v[i].c_str());
176  com.append(" ");
177  com.append(backupDir);
178  int ret = system(com.c_str());
179  if (ret != 0)
180  cerr << "command '" << com << "' was not successful." << endl;
181 
182  com=("mv ");
183  com.append(file2v[i].c_str());
184  com.append(" ");
185  com.append(backupDir);
186  ret = system(com.c_str());
187  if (ret != 0)
188  cerr << "command '" << com << "' was not successful." << endl;
189  }
190 
191  } // end loop over files
192 
193  return 0;
194 
195 }