物理の駅 Physics station by 現役研究者

テクノロジーは共有されてこそ栄える

Gitでサブモジュールの追加

新しいサブモジュールを追加。 root_macros フォルダに リポジトリ root_macros.gitを追加する。

git submodule add https://gitlab.com/yoshimoto/root_macros.git root_macros

リモートが更新されたら取り込む

git submodule foreach git pull origin master

[Git] git submodule は癖がすごいとの噂だったが素直につきあっていけそうという話 | deadwood git submoduleしてるリポジトリをリモートの最新に更新する - Qiita

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.
}

Read track text file

Read track files

Track struct has 6 members. four double members and two int members. The double members include position x, position y, angle x, angle y. The int members include pulse height and pulse height volume.

Q1. Make track class.

class track {
public:
    //insert here.
    //insert here.
};

The track data files is as follows. Lines that start with # are comments.

#pos_x[mm] pos_y[mm] angle_x angle_y ph phv
-58.5483 156.9036   -0.0039 -0.0358    16.0 1430.8
-58.5148 156.8275   0.0016 -0.0544    16.0 6449.2
-58.5998 156.8236   -0.0370 -0.0273    16.0 6325.0
-58.5134 156.8441   -0.0415 -0.0422    16.0 1698.3
-58.5704 156.8770   -0.0004 -0.0117    16.0 454.6
-58.5388 156.8782   0.0233 -0.0309    16.0 759.4
-58.4927 156.8461   0.0506 -0.0412    16.0 1826.0
-58.5128 156.8928   0.0409 -0.0024    16.0 1784.3
-58.5469 156.8413   0.0558 -0.0208    16.0 2264.7

Q2. Make read function.

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

void read_txt(std::string filepath, std::vector<track> &vin)
{
    std::ifstream ifs(filepath);
    if (!ifs) {
        throw std::exception(("cannot open " + filepath).c_str());
    }
    std::string str;
    while (getline(ifs, str))
    {
        if (str.size() == 0) { continue; } //to ignore null lines
        if (str[0] == '#') { continue; } //to ignore comment lines
        stringstream ss(str);
        //insert here
        //insert here
        //insert here
    }
}

Download these files.

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

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

Q3. Make main function to check the size of the tracks each file.

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

自作Pythonモジュールをアップロードするまでの流れ

自分用のメモ以上でもメモ以下でもないので、詳細は各自検索されたし。

各モジュールのインストール

pip install nose
pip install unittest2
pip install wheel
pip install twine

アップロード前に nose でtestしておこう

nosetests

ホームディレクトリに .pypirc ファイルを作成

[pypi]
username: user
password: password

モジュールのソースとインストーラーを作り、アップロードする dist 内のファイルは事前に全て削除しておく。

python setup.py sdist
python setup.py bdist_wheel --universal
twine upload dist/*

--universal は2系と3系を共存させるため。

古い記述がGoogleから駆逐されずに残っていて、良くないよね。

参照

Python nose でユニットテストを書いてみた / 桃缶食べたい。

Pythonで作成したライブラリを、PyPIに公開/アップロードする

4. ソースコード配布物を作成する — Python 3.6.5 ドキュメント

文字型と整数型の上限値と下限値、各データ型のサイズ

参照: ATLAS Japan C++ Course -- Lesson 2

サンプルコード1

#include <iostream>
#include <cstdint>

using namespace std;
int main(int argc, char** argv) {

    cout 
        << "char min. = " << int(INT8_MIN)
        << " max. = " << int(INT8_MAX) << endl
        << "short int min. = " << INT16_MIN
        << " max. = " << INT16_MAX << endl
        << "long int min. = " << INT32_MIN
        << " max. = " << INT32_MAX << endl
        << "long long int min. = " << INT64_MIN
        << " max. = " << INT64_MAX << endl;

    cout 
        << "unsigned char max. = " << int(UINT8_MAX) << endl
        << "unsigned short int max. = " << UINT16_MAX << endl
        << "unsigned long int max. = " << UINT32_MAX << endl
        << "unsigned long long int max. = " << UINT64_MAX << endl;

    return 0;
}

サンプルコード2

#include <iostream>
#include <cstdint>

using namespace std;
int main(int argc, char** argv) {

    cout
        << "bytes of char = " << sizeof(char) << endl
        << "bytes of short = " << sizeof(short) << endl
        << "bytes of int = " << sizeof(int) << endl
        << "bytes of long = " << sizeof(long long) << endl;

    return 0;
}