3.0 TSpectrum, vector, map¶

TSpectrum¶

  • This class contains advanced spectra processing functions for:
    • One-dimensional background estimation
    • One-dimensional smoothing
    • One-dimensional deconvolution
    • One-dimensional peak search
In [1]:
TFile *f = new TFile("gamma.root")
(TFile *) 0x7f7d0e98ecd0
In [2]:
f->ls()
TFile**		gamma.root	
 TFile*		gamma.root	
  KEY: TH1F	h0;1	152Eu gamma calibration
In [3]:
TCanvas *c1 = new TCanvas;
h0->Draw();
c1->SetLogy();
c1->Draw();

创建TSpectrum对象¶

In [4]:
TSpectrum *s = new TSpectrum(500);//maximum number of peaks

生成本底谱¶

TH1 * TSpectrum::Background( const TH1 *h,
                             Int_t niter = 20,
                             Option_t  *option = "" 
                            )
  • Parameters:
    • h: input 1-d histogram
    • niter: numberIterations, (default value = 20) Increasing numberIterations make the result smoother and lower.
      • 通过观察本底谱的形状,选择合适的niter参数。减本底的重要原则是宁少勿多,避免减出负值!
In [5]:
TH1F *hb0 = (TH1F*)s->Background(h0, 15, "same");//本底侵入到峰区域!
h0->Draw();
hb0->SetLineColor(kRed);
hb0->Draw("same");
c1->Draw();
In [6]:
TH1F *hb1 = (TH1F*)s->Background(h0, 30, "same");//给出了峰下连续谱的合理形状,本底参数合理。
h0->Draw();
hb1->SetLineColor(kRed);
hb1->Draw("same");
c1->Draw();

减本底¶

In [7]:
h0->Clone("hh");
hh->Add(hh, hb1, 1, -1);//hh = 1 * hh-1 * hb
hh->Draw();
c1->SetLogy(0);
c1->Draw();

寻峰¶

Int_t TSpectrum::Search(const TH1  *hin,
                          Double_t sigma = 2,
                          Option_t *option = "",
                          Double_t threshold = 0.05 
                        )
  • Parameters:

    • hin: pointer to the histogram of source spectrum
    • sigma: sigma of searched peaks
    • threshold: (default=0.05) peaks with amplitude less than threshold * highest_peak are discarded. 0<threshold<1
  • By default, the background is removed before deconvolution. Specify the option "nobackground" to not remove the background.

    • 建议通过Background功能选择合适的本底形状,减完本底后再寻峰!
  • 得到寻峰值

Double_t *xpeaks, *ypeaks;
Int_t nfound = s->Search(hh,2,"",0.002); 
xpeaks = s->GetPositionX();
ypeaks = s->GetPositionY();
In [16]:
Double_t *xpeaks, *ypeaks;
Int_t nfound = s->Search(hh,2,"",0.005); //根据实际峰宽选择合适的sigma值!
cout<<nfound<<endl;
xpeaks = s->GetPositionX();
ypeaks = s->GetPositionY();
for(int i=0; i<nfound; i++)
    cout << i << ": " << int(xpeaks[i]) 
         << ", " << ypeaks[i] << endl;
hh->Draw();
c1->Draw();
21
0: 332, 984052
1: 135, 786091
2: 101, 664539
3: 322, 538447
4: 67, 355974
5: 288, 331300
6: 239, 171454
7: 688, 161288
8: 1217, 167250
9: 843, 152510
10: 356, 139327
11: 265, 136479
12: 968, 127081
13: 71, 117777
14: 946, 100460
15: 406, 53913
16: 762, 46709.3
17: 379, 38620.6
18: 342, 16484.9
19: 1125, 13541.8
20: 1053, 12435.7

改变阈值¶

In [9]:
nfound = s->Search(hh, 2, "", 0.001); 
cout << nfound << endl;
hh->Draw();
c1->Draw();
65

std::vector (c++ STL )¶

  • include
#include <vector>
#include <algorithm>
  • vector是STL(标准模板库)的一个关联容器。。
  • vector和数组类似,由于其大小(size)可变,常用于数组大小不可知的情况下来替代数组 (动态数组)。

声明¶

vector<int> vp;

尾部插入元素¶

for(int i=0; i<10; i ++) {

    v.push_back(i);
}

查找元素¶

  • 通过迭代器进行访问
vector<int> vec = {1, 2, 3, 4, 5};  
vector<int>::iterator it = vec.find(3); //可以用 auto it = vec.find(3);代替  

if (it != vec.end()) {  
    cout << "Found " << *it << endl;  
} 
else {  
    cout << "Element not found" << endl;  
}

遍历vector中的元素:¶

  • 下标遍历
for (int i=0; i<v.size(); i++) {
     cout << i << v[i] << endl;
}
  • 迭代器遍历
for(auto it = v.begin(); it != v.end(); it++) {
    cout << *it <<endl;
}

其他常用函数¶

  • pop_back(): 删除向量的最后一个元素。

  • erase(): 删除指定位置的元素。

  • clear(): 删除向量中的所有元素。

std::map (c++ STL )¶

  • include
#include <map>
#include <algorithm>
  • map是STL(标准模板库)的一个关联容器。
  • map提供一对一的数据处理,key-value键值对,其类型可以自己定义,第一个称为关键字,第二个为关键字的值

  • multimap可以允许多个key值是相同的,而map的key值不允许重复。
  • map, multimap, 按照key值的大小自动排序,key值由小到大。

定义map变量¶

In [10]:
map<Int_t, Double_t> m1;//第一个是键的类型,第二个是值的类型
multimap<Int_t, Double_t> mul1;

赋值¶

In [11]:
for(int i=0; i<nfound; i++){
    int key = xpeaks[i];
    double value = ypeaks[i];
    m1.insert(make_pair(key, value));//插入key,value值
    m1.insert(make_pair(key, value));//插入key,value值
    mul1.insert(make_pair(key, value));
    mul1.insert(make_pair(key, value));//重复插入key,value值    
}

查看大小¶

In [12]:
cout << m1.size() << "," << mul1.size();
65,130

查找元素¶

  • 通过迭代器进行访问

map<Int_t,Double_t>::iterator im1;

  • im1->first来访问key,使用im->second访问value
  • maps.find() 查找一个元素
  • find(key): 返回键是key的映射的迭代器
In [13]:
int i = 762;
auto im1 = m1.find(i);//map<Int_t,Double_t>::iterator im1=m1.find()
if(im1 != m1.end()) 
    cout << im1->first << ", " << im1->second << endl;
else 
    cout << "key=" << i << " is not found." << endl;
i = 100;
im1 = m1.find(i);
if(im1 != m1.end()) 
    cout << im1->first << ", " << im1->second << endl;
else 
    cout << "key=" << i << " is not found." << endl;
762, 46709.3
key=100 is not found.

正向遍历¶

  • m1.begin()返回指向map头部的迭代器
  • m1.end()返回指向map末尾的迭代器
In [14]:
int num = 0;
cout<<"map elements:"<<endl;

for(auto im=m1.begin(); im!= m1.end(); im++) {
    if(num > 10) break;
    cout << "  " << im->first << ", " << im->second << endl;
    num++;
}
num = 0;
cout << "multimap elements:" << endl;
for(auto imul=mul1.begin(); imul!=mul1.end(); imul++){
    if(num > 10) break;
    cout << "  " << imul->first << ", " << imul->second << endl;
    num++;
}
map elements:
  59, 57259.4
  63, 71649.2
  67, 355974
  71, 117777
  78, 18438.1
  86, 28650.3
  94, 43538.8
  96, 57888.3
  101, 664539
  135, 786091
  160, 17473.4
multimap elements:
  59, 57259.4
  59, 57259.4
  63, 71649.2
  63, 71649.2
  67, 355974
  67, 355974
  71, 117777
  71, 117777
  78, 18438.1
  78, 18438.1
  86, 28650.3

反向遍历¶

  • m1.rbegin()返回指向map尾部的逆向迭代器
  • m1.rend()返回指向map头部的逆向迭代器
In [15]:
num = 0;
cout << "map elements:" << endl;
for(auto im=m1.rbegin(); im!=m1.rend(); im++) {
    if(num > 10) break;
    cout << "  " << im->first << ", " << im->second << endl;
    num++;
}
num = 0;
cout << "multimap elements:" <<endl;
for(auto imul=mul1.rbegin(); imul!=mul1.rend(); imul++){
    if(num > 10) break;
    cout << "  " << imul->first << ", " << imul->second << endl;
    num++;
}
map elements:
  2231, 1541.63
  1516, 1940.93
  1318, 2344.95
  1261, 9518.89
  1258, 4217.79
  1217, 167250
  1125, 13541.8
  1104, 2206.72
  1084, 1579.06
  1053, 12435.7
  975, 1833.7
multimap elements:
  2231, 1541.63
  2231, 1541.63
  1516, 1940.93
  1516, 1940.93
  1318, 2344.95
  1318, 2344.95
  1261, 9518.89
  1261, 9518.89
  1258, 4217.79
  1258, 4217.79
  1217, 167250

其他成员函数¶

  • maps.clear(): 清空
  • maps.erase(): 删除一个元素
  • maps.lower_bound(): 会返回一个迭代器,它指向键值和参数相等或大于参数的第一个元素。
  • maps.upper_bound(): 返回一个迭代器,它指向键值大于函数参数的第一个元素。

更多用法见第5章