36 #include "TClonesArray.h"
40 #include "TLorentzVector.h"
50 #include "../amplitude/particle.h"
52 #include "libconfig.h++"
56 using namespace libconfig;
60 cerr <<
"usage:" << endl
62 <<
" -e <file> -o <file> -w <file> -i <file> -n samples -m mass"
64 <<
" -e <file> acc or ps events in .evt or .root format"<< endl
65 <<
" -o <file> ROOT output file"<< endl
66 <<
" -w <file.root> use TFitBin tree as input"<< endl
67 <<
" -i <file> integral file"<< endl
68 <<
" -m mass center of mass bin"<< endl
69 <<
" -n samples sampling of the model parameters (default=1)"<< endl
70 <<
" -b width width of mass bin"<< endl
79 unsigned int reflIndex = 6;
81 if(waveName[0] ==
'V') {
84 if(waveName[reflIndex] ==
'-') {
86 }
else if(waveName[reflIndex] ==
'+') {
89 printErr <<
"Cannot parse parameter/wave name '" << waveName <<
"'. Cannot not determine reflectivity. Aborting." << endl;
97 vector<string>& waveNames,
98 vector<double>& waveThresholds,
102 unsigned int _nmbWavesPosRefl = 0;
103 unsigned int _nmbWavesNegRefl = 0;
105 printInfo <<
"Reading amplitude names from wave list file '" << waveListFileName <<
"'." << endl;
106 ifstream waveListFile(waveListFileName.c_str());
108 printErr <<
"Cannot open file '" << waveListFileName <<
"'. Exiting." << endl;
113 while(getline(waveListFile, line)) {
117 stringstream lineStream;
118 lineStream.str(line);
120 if(lineStream >> waveName) {
123 if(!(lineStream >> threshold))
126 cout <<
" reading line " << setw(3) << lineNmb + 1 <<
": " << waveName <<
", threshold = " << setw(4) << threshold <<
" MeV/c^2" << endl;
128 ++_nmbWavesPosRefl; refl.push_back(1);
130 ++_nmbWavesNegRefl; refl.push_back(-1);
132 waveNames.push_back(waveName);
133 waveThresholds.push_back(threshold);
135 printWarn <<
"Cannot parse line '" << line <<
"' in wave list file '" << waveListFileName <<
"'." << endl;
139 waveListFile.close();
140 printInfo <<
"Read " << lineNmb <<
" lines from wave list file '" << waveListFileName <<
"'." << endl;
144 int main(
int argc,
char** argv)
151 string output_file =
"genpw.root";
152 string integrals_file;
153 string waveListFileName =
"wavelist";
155 double binCenter = 0;
156 double binWidth = 60;
157 unsigned int nsamples = 1;
160 while ((c = getopt(argc, argv,
"e:o:w:i:m:n:h")) != -1) {
163 evtfilename = optarg;
166 output_file = optarg;
169 waveListFileName = optarg;
172 integrals_file = optarg;
175 binCenter = atof(optarg);
178 nsamples =
atoi(optarg);
181 binWidth = atof(optarg);
193 binCenter += 0.5 * binWidth;
195 TFile* outfile = TFile::Open(output_file.c_str(),
"RECREATE");
196 TH1D* hWeights =
new TH1D(
"hWeights",
"PW Weights", 100, 0, 100);
197 TTree* outtree =
new TTree(
"pwevents",
"pwevents");
199 TClonesArray*
p =
new TClonesArray(
"TLorentzVector");
204 outtree->Branch(
"weight", &weight,
"weight/d");
205 outtree->Branch(
"p", &p);
206 outtree->Branch(
"beam", &beam);
207 outtree->Branch(
"q", &q);
208 outtree->Branch(
"qbeam", &qbeam,
"qbeam/i");
212 ifstream intFile(integrals_file.c_str());
214 printErr <<
"Cannot open file '"
215 << integrals_file <<
"'. Exiting." << endl;
219 normInt.
scan(intFile);
224 TFile* fitresults = TFile::Open(waveListFileName.c_str(),
"READ");
226 if(!fitresults || fitresults->IsZombie()) {
227 cerr <<
"Cannot open start fit results file " << waveListFileName << endl;
233 fitresults->GetObject(
"pwa", tree);
235 cerr <<
"Cannot find fitbin tree '"<<
"pwa" <<
"' "<< endl;
238 tree->SetBranchAddress(
"fitResult_v2", &Bin);
241 unsigned int iBest = 0;
244 for(
unsigned int i = 0;
i < tree->GetEntriesFast(); ++
i) {
246 if (fabs(binCenter - Bin->massBinCenter()) <= fabs(binCenter - mBest)) {
250 mBest = Bin->massBinCenter();
256 cerr <<
"mBest= " << mBest << endl;
259 if((mBest < (binCenter-binWidth / 2.)) || (mBest > (binCenter + binWidth / 2.))) {
260 cerr <<
"No fit found for Mass bin m=" << binCenter << endl;
265 cerr <<
"Using data from Mass bin m=" << mBest <<
" bin " << iBest << endl;
266 tree->GetEntry(iBest);
271 for(
unsigned int isamples = 0; isamples < nsamples; ++isamples) {
273 TString tmpname(
"genamps");
276 ofstream tmpfile(tmpname.Data());
278 Bin->printAmpsGenPW(tmpfile);
285 waveListFileName =
"genamps0.txt";
289 vector<string> waveNames;
291 vector<vector<complex<double> >*> prodAmps(nsamples);
292 for(
unsigned int isamples = 0; isamples < nsamples; ++isamples) {
293 prodAmps[isamples] =
new vector<complex<double> >();
296 vector<int> reflectivities;
304 for(
unsigned int isamples = 0; isamples < nsamples; ++isamples) {
305 TString tmpname(
"genamps");
308 cerr <<
"Reading back file " << tmpname << endl;
309 ifstream wavefile(tmpname);
310 while(wavefile.good()) {
316 wavefile >> wavename >> RE >> IM;
319 if(wavename.Length() < 2) {
322 if((RE == 0) && (IM == 0)) {
326 if(wavename(0) ==
'V') {
328 rank = 2 *
atoi(wavename(1, 1).Data());
330 refl = wavename(9)==
'+' ? 0 : 1;
331 m = wavename(8)==
'0' ? 0 : 1;
333 wavename = wavename(3, wavename.Length());
334 }
else if(wavename !=
"flat") {
335 refl = wavename(6)==
'+' ? 0 : 1;
336 m = wavename(5)==
'0' ? 0 : 1;
340 std::complex<double> amp(RE, IM);
341 prodAmps[isamples]->push_back(amp);
342 cerr << wavename <<
" " << amp <<
" r=" << rank/2
344 <<
" m=" << m << endl;
345 wavefile.ignore(256,
'\n');
348 waveNames.push_back(wavename.Data());
349 reflectivities.push_back(refl);
351 ranks.push_back(rank);
358 cerr <<
"Rank of fit was:" << maxrank + 1 << endl;
361 unsigned int nmbWaves = waveNames.size();
364 vector<ifstream*> ampfiles;
368 vector<double> weights(nmbWaves);
380 for(
unsigned int iw = 0; iw < nmbWaves; ++iw) {
381 TString weightname(
"Wintens_");
382 weightname += waveNames[iw];
383 outtree->Branch(weightname.Data(), &weights[iw], (weightname +
"/d").Data());
387 vector<double> modelweights(nsamples);
388 for(
unsigned int isamples = 0; isamples < nsamples; ++isamples) {
389 TString weightname(
"W");
390 weightname += isamples;
391 outtree->Branch(weightname.Data(), &modelweights[isamples], (weightname +
"/d").Data());
395 for(
unsigned int iw = 0; iw < nmbWaves; ++iw) {
396 ampfiles.push_back(
new ifstream(waveNames[iw].c_str()));
401 unsigned int counter = 0;
402 if(evtfilename.find(
".evt") > 0) {
404 list< ::particle> f_mesons;
405 ifstream evtfile(evtfilename.c_str());
407 while(!evtfile.eof() && evtfile.good()) {
409 if(counter % 1000 == 0) {
412 if(counter++ % 10000 == 0) {
413 std::cout << counter;
418 f_mesons = e.f_mesons();
420 list< ::particle>::iterator it = f_mesons.begin();
421 while(it != f_mesons.end()) {
423 new ((*p)[p->GetEntries()]) TLorentzVector(pX.
x(), pX.
y(), pX.
z(), pX.
t());
424 q.push_back(it->Charge());
427 fourVec evtbeam = e.beam().get4P();
428 beam.SetPxPyPzE(evtbeam.
x(), evtbeam.
y(), evtbeam.
z(), evtbeam.
t());
429 qbeam = e.beam().Charge();
433 vector<complex<double> > decayamps(nmbWaves);
434 for(
unsigned int iw = 0; iw < nmbWaves; ++iw) {
435 complex<double> decayamp;
436 ampfiles[iw]->read((
char*) &decayamp,
sizeof(complex<double> ));
437 decayamps[iw] = decayamp;
441 for(
unsigned int isample=0; isample<nsamples; ++isample){
443 vector<complex<double> > posm0amps(maxrank + 1);
444 vector<complex<double> > posm1amps(maxrank + 1);
446 vector<complex<double> > negm0amps(maxrank + 1);
447 vector<complex<double> > negm1amps(maxrank + 1);
449 complex<double> flatamp;
451 for(
unsigned int iw = 0; iw < nmbWaves; ++iw) {
452 complex<double> decayamp = decayamps[iw];
453 string w1 = waveNames[iw];
455 flatamp = prodAmps[isample]->at(iw);
459 double nrm = sqrt(normInt.
val(w1, w1).real());
460 complex<double> amp = decayamp / nrm * prodAmps[isample]->at(iw);
462 weights[iw] = norm(amp);
465 if (reflectivities[iw] == 0) {
467 posm0amps[ranks[iw]] += amp;
468 }
else if(ms[iw] == 1) {
469 posm1amps[ranks[iw]] += amp;
471 }
else if(reflectivities[iw] == 1) {
473 negm0amps[ranks[iw]] += amp;
474 }
else if(ms[iw] == 1) {
475 negm1amps[ranks[iw]] += amp;
484 for(
int ir = 0; ir < maxrank + 1; ++ir) {
485 weight += norm(posm0amps[ir] + posm1amps[ir]);
487 weight += norm(negm0amps[ir] + negm1amps[ir]);
492 weight += norm(flatamp);
496 hWeights->Fill(weight);
498 modelweights[isample] = weight;
510 TClonesArray prodKinPar(
"TObjString");
511 TClonesArray prodKinMom(
"TVector3");
512 TClonesArray decayKinPar(
"TObjString");
513 TClonesArray decayKinMom(
"TVector3");
514 TClonesArray *pprodKinPar = &prodKinPar;
515 TClonesArray *pprodKinMom = &prodKinMom;
516 TClonesArray *pdecayKinPar = &decayKinPar;
517 TClonesArray *pdecayKinMom = &decayKinMom;
519 TFile* infile = TFile::Open(evtfilename.c_str(),
"READ");
521 infile->GetObject(
"rootPwaEvtTree;1", evt_tree);
523 evt_tree->SetBranchAddress(
"prodKinParticles", &pprodKinPar);
524 evt_tree->SetBranchAddress(
"prodKinMomenta", &pprodKinMom);
525 evt_tree->SetBranchAddress(
"decayKinParticles", &pdecayKinPar);
526 evt_tree->SetBranchAddress(
"decayKinMomenta", &pdecayKinMom);
528 unsigned int counter = 0;
529 unsigned long entries = evt_tree->GetEntries();
530 for(
unsigned long i = 0;
i < entries;
i++) {
532 evt_tree->GetEntry(
i);
533 if(counter % 1000 == 0) {
536 if(counter++ % 10000 == 0) {
537 std::cout << counter;
554 for(
int dp = 0; dp < decayKinPar.GetEntries(); dp++) {
556 q.push_back(dpar.
charge());
557 new ((*p)[p->GetEntries()]) TLorentzVector(*(TVector3*)decayKinMom[dp],
563 vector<complex<double> > posm0amps(maxrank + 1);
564 vector<complex<double> > posm1amps(maxrank + 1);
566 vector<complex<double> > negm0amps(maxrank + 1);
567 vector<complex<double> > negm1amps(maxrank + 1);
569 for(
unsigned int iw = 0; iw < nmbWaves; ++iw) {
570 complex<double> decayamp;
571 ampfiles[iw]->read((
char*) &decayamp,
sizeof(complex<double> ));
572 string w1 = waveNames[iw];
574 double nrm = sqrt(normInt.
val(w1, w1).real());
575 complex<double> amp = decayamp / nrm * prodAmps[0]->at(iw);
576 if(reflectivities[iw] == 1) {
578 posm0amps[ranks[iw]] += amp;
579 }
else if(ms[iw] == 1) {
580 posm1amps[ranks[iw]] += amp;
584 negm0amps[ranks[iw]] += amp;
585 }
else if(ms[iw] == 1) {
586 negm1amps[ranks[iw]] += amp;
594 for(
int ir = 0; ir < maxrank + 1; ++ir) {
595 weight += norm(posm0amps[ir]+posm1amps[ir]);
597 weight += norm(negm0amps[ir]+negm1amps[ir]);
601 hWeights->Fill(weight);
605 cout << endl <<
"Processed " << counter <<
" events" << endl;
612 for(
unsigned int iw = 0; iw < nmbWaves; ++iw) {
613 ampfiles[iw]->close();
617 for(
unsigned int isamples=0; isamples < nsamples; ++isamples) {
618 delete prodAmps[isamples];