最近做了一个三次谐波(TH)在空气中展宽的实验,处理数据时想从一张2D图上直接看到展宽的信息,列出两个小程序,仅供参考。
First one:
// spectrum broadening
// output 2-D data saved by data-2D.tx
#include <fstream>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include "TMath.h"
#include "TCanvas.h"
#include "TPad.h"
#include "TGraph.h"
#include "TH1.h"
#include "TRandom.h"
#include "TGaxis.h"
#include "TStyle.h"
#define num 280
using namespace std;
string num2str(Int_t i);
void spectrum()
{
// import data
Double_t wavelength[num];
Double_t S[100];
Double_t intensity[num], delay[99];
Int_t file_num[99];
string datafile, delayfile;
// string picname, picname1; // 生成root文件和gif文件
// import delay data
delayfile = "/home/dinghao/Documents/zhankuan/delay";
ifstream readdata;
readdata.open(delayfile.c_str(), ios_base::in);
if(readdata.fail())
{
cout << "fail to open delay file!" << endl;
continue;
}
for (Int_t i=2; i<=99; i++)
{
file_num[i] = 0;
delay[i] = 0;
readdata >> file_num[i] >> delay[i];
delay[i] = delay[i] * 0.0167 - 60*0.0167; // 延迟1对应0.0167ps or 5um, 以第19个文件三次谐波最大为零点
cout << file_num[i] << "t" << delay[i] << endl;
}
readdata.close();
FILE *gp=fopen("/home/dinghao/Documents/zhankuan/data-2D.txt","w+");
// main loop
// the number of data file is 2-99
for (Int_t m=2; m<=99; m++)
{
datafile = "/home/dinghao/Documents/zhankuan/"+num2str(m)+".txt";
// picname = "/home/dinghao/Documents/zhankuan/"+num2str(m)+".root";
// picname1 = "/home/dinghao/Documents/zhankuan/"+num2str(m)+".gif";
S[m] = 0;
ifstream readdata;
readdata.open(datafile.c_str(), ios_base::in);
if(readdata.fail())
{
cout << "fail to open file!" << endl;
continue;
}
for (Int_t i=0; i<num; i++)
{
wavelength[i] = 0;
intensity[i] = 0;
readdata >> wavelength[i] >> intensity[i];
S[m] = S[m] + 1.0/wavelength[i]*intensity[i];
fprintf(gp, "%f %f %fn", delay[m], wavelength[i], intensity[i]);
// cout << delay[m] << "t" << wavelength[i] << "t" << intensity[i] << endl;
}
readdata.close();
// close the creation of root and gif files
/*
TCanvas *c1 = new TCanvas("c1","third-order harmonic spectrum", 800, 600);
TFile *f1 = new TFile(picname.c_str(), "recreate");
TH1F *h1 = new TH1F("h1", "spectrum", num, wavelength[0], wavelength[num-1]);
for (Int_t k=0; k<num; k++)h1->SetBinContent(k, intensity[k]);
h1->GetXaxis()->SetTitle("Wavelength (nm)");
h1->GetYaxis()->SetTitle("Intensity");
h1->GetXaxis()->CenterTitle();
h1->GetYaxis()->CenterTitle();
h1->Draw("LP");
h1->SetLineWidth(3);
*/
// c1->Print(picname1.c_str());
// h1->Write();
// c1->Close();
cout << "The file of "+num2str(m)+".txt has been done!" << endl;
}
fclose(gp);
// output area
// save pulse energy imformation
FILE *fp=fopen("/home/dinghao/Documents/zhankuan/area.txt","w+");
for (Int_t m=2; m<=99; m++)
{
fprintf(fp,"%lf %lfn", delay[m], S[m]);
}
fclose(fp);
}
// number transform to string
string num2str(Int_t i)
{
stringstream s;
s << i;
return s.str();
}
Second:
// spectrum broadening
#include "TMath.h"
#include "TCanvas.h"
#include "TPad.h"
#include "TGraph.h"
#include "TH1.h"
#include "TH2.h"
#include "TRandom.h"
#include "TStyle.h"
#define num 27440
using namespace std;
void spectral()
{
TCanvas *spectrum = new TCanvas("spectrum", "third-order harmonic spectrum", 500, 300);
TH2F *h1 = new TH2F("h1", "Spectrum of third-order harmonic", 60,-14.0,5.7,2400,230.0,290.0);
Double_t wavelength[num];
Double_t intensity[num], delay[num];
FILE *fp=fopen("/home/dinghao/Documents/zhankuan/data-2D.txt","r");
for (Int_t i=0; i<num; i++)
{
fscanf(fp, "%lf %lf %lfn", &delay[i], &wavelength[i], &intensity[i]);
cout << delay[i] << "t" << wavelength[i] << "t" << intensity[i] << endl;
h1->Fill(delay[i], wavelength[i], intensity[i]);
}
h1->GetXaxis()->SetTitle("Delay (ps)");
h1->GetYaxis()->SetTitle("Wavelength (nm)");
h1->GetXaxis()->CenterTitle();
h1->GetYaxis()->CenterTitle();
h1->Draw("colz");
fclose(fp);
}
最后的结果:
https://blog.sciencenet.cn/blog-653020-551824.html
上一篇:
NetCDF安装记录下一篇:
Latex公式分行