Physics-Station phst.hateblo.jp

旧 http://physics-station.blogspot.jp/ から当はてなブログに移行しました。間違ってるところがあればコメントください。記述の正確性は保証しません。

Pattern matching sample パターンマッチングサンプル

(ja) パターンマッチング可能な飛跡ファイル

https://1drv.ms/f/s!Ap9xAxIuzM0xlLxu6slGIjkY3gSjNg

Q1. どういう手段でもいいので、 beam_4372-2_u.txt の飛跡と beam_4372-2_d.txt の飛跡の位置ずれを計算しよう。

Q2. 計算方法について、互いに紹介しよう。

Q3. これは、位置ずれのピークを求めるソースコードである。 std::map はどういう働きをするのか調べよう。

Q4. 前回配布したファイルはパターンマッチングが成功しない。成功しないと言うためにはどうすればよいか考えよう。

https://1drv.ms/t/s!Ap9xAxIuzM0xlLlaOr2g5qaKpYqF2w

https://1drv.ms/t/s!Ap9xAxIuzM0xlLlbUgqNsGvbe4Eq6w

(en)

Trajectory file for pattern-matching

https://1drv.ms/f/s!Ap9xAxIuzM0xlLxu6slGIjkY3gSjNg

Q1. Calculate the position shift between the trajectory of beam_4372-2_u.txt and the trajectory ofbeam_4372-2_d.txt with any way.

Q2. Introduce each other about your calculation methods.

Q3. This is a source code to search peak position shift . Examine what std::map does.

  • Position shift of all trajectories are packed in a histogram for each bin_width .
  • Create a blurred histogram with surrounding n_bin_bg x n_bin_bg .
  • Subtract the histogram from the blurred histogram
  • Find the peak with the the maximum content

Q4. Pattern matching does not succeed in files distributed last time. Let's see what we should say to show that the pattern matching does not succeed.

https://1drv.ms/t/s!Ap9xAxIuzM0xlLlaOr2g5qaKpYqF2w

https://1drv.ms/t/s!Ap9xAxIuzM0xlLlbUgqNsGvbe4Eq6w

#include <string>
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
#include <map>

int main() {
    string path1 = "beam_4372-2_u.txt";
    string path2 = "beam_4372-2_d.txt";
    vector<track> vtrack1, vtrack2;

    cout << vtrack1.size() << " -> ";
    read_txt(path1, vtrack1);
    cout << vtrack1.size() << endl;

    cout << vtrack2.size() << " -> ";
    read_txt(path2, vtrack2);
    cout << vtrack2.size() << endl;

    map<string, int> hist;
    map<string, int> hist_bg;

    double bin_width = 0.002; //bin width
    int n_bin_bg = 3; //number of bin for background

    //add displacement to bins
    for (int i1 = 0; i1 < vtrack1.size(); i1++) {
        for (int i2 = 0; i2 < vtrack2.size(); i2++) {
            double dx = vtrack2[i2].px - vtrack1[i1].px;
            double dy = vtrack2[i2].py - vtrack1[i1].py;

            int dxi = round(dx / bin_width);
            int dyi = round(dy / bin_width);
            hist[to_string(dxi) + "_" + to_string(dyi)]++;
            for (int bgx = -(n_bin_bg - 1) / 2; bgx <= (n_bin_bg - 1) / 2; bgx++) {
                for (int bgy = -(n_bin_bg - 1) / 2; bgy <= (n_bin_bg - 1) / 2; bgy++) {
                    hist_bg[to_string(dxi + bgx) + "_" + to_string(dyi + bgy)]++;
                }
            }
        }
    }

    //search peak
    int max_value = 0;
    string peak_position = "";
    for (auto &p : hist) {
        p.second -= hist_bg[p.first] / double(n_bin_bg*n_bin_bg);
        if (max_value < p.second)
        {
            peak_position = p.first;
            max_value = p.second;
        }
    }

    //convert to position
    string buf;
    stringstream ss(peak_position);
    getline(ss, buf, '_');
    int peak_position_x = std::stoi(buf);
    getline(ss, buf, '_');
    int peak_posision_y = std::stoi(buf);

    //draw 7x7 bins
    for (int ix = -3; ix <= 3; ix++) {
        for (int iy = -3; iy <= 3; iy++) {
            string str = std::to_string(ix + peak_position_x) + "_" + std::to_string(iy + peak_posision_y);
            cout << hist[str] << " ";
        }
        cout << endl;
    }
    cout << "peak (x, y) = (" << peak_position_x*bin_width << ", " << peak_posision_y*bin_width << ") " << endl;
    cout << "peak bin content = " << max_value << endl;
    cin.get(); //to stop the console.
}