TFile *f = new TFile("gamma.root")
(TFile *) 0x7f7d0e98ecd0
f->ls()
TFile** gamma.root TFile* gamma.root KEY: TH1F h0;1 152Eu gamma calibration
TCanvas *c1 = new TCanvas;
h0->Draw();
c1->SetLogy();
c1->Draw();
TSpectrum *s = new TSpectrum(500);//maximum number of peaks
TH1 * TSpectrum::Background( const TH1 *h,
Int_t niter = 20,
Option_t *option = ""
)
TH1F *hb0 = (TH1F*)s->Background(h0, 15, "same");//本底侵入到峰区域!
h0->Draw();
hb0->SetLineColor(kRed);
hb0->Draw("same");
c1->Draw();
TH1F *hb1 = (TH1F*)s->Background(h0, 30, "same");//给出了峰下连续谱的合理形状,本底参数合理。
h0->Draw();
hb1->SetLineColor(kRed);
hb1->Draw("same");
c1->Draw();
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:
By default, the background is removed before deconvolution. Specify the option "nobackground" to not remove the background.
得到寻峰值
Double_t *xpeaks, *ypeaks;
Int_t nfound = s->Search(hh,2,"",0.002);
xpeaks = s->GetPositionX();
ypeaks = s->GetPositionY();
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
nfound = s->Search(hh, 2, "", 0.001);
cout << nfound << endl;
hh->Draw();
c1->Draw();
65
#include <vector>
#include <algorithm>
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;
}
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(): 删除向量中的所有元素。
#include <map>
#include <algorithm>
map<Int_t, Double_t> m1;//第一个是键的类型,第二个是值的类型
multimap<Int_t, Double_t> mul1;
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值
}
cout << m1.size() << "," << mul1.size();
65,130
map<Int_t,Double_t>::iterator im1;
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.
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
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
更多用法见第5章