Geant 4输出数据整理:ROOT文件转存为MAT文件

阅读: 评论:0

Geant 4输出数据整理:ROOT文件转存为MAT文件

Geant 4输出数据整理:ROOT文件转存为MAT文件

前言

Geant 4输出数据往往采用Root文件,其数据输出效率较高,优于文本文件格式,尤其是数据文件较大时。博主在工作中需要用Matlab处理数据,因此整理了Root文件转存为Mat文件的程序。对于1G大小的Root文件,程序处理时间约37秒(读取+保存),完全可以接受。

一、程序编写

参考文献:
1.linux下使用c++读取mat文件的步骤
2.ROOTPrimer.pdf(ROOT官网自查,第22页,关于库文件加载)

#include <iostream>
#include <fstream>
#include "string.h"#include <mat.h>
#include "matrix.h"  #include "TROOT.h"
#include "TFile.h"
#include "TTree.h"using namespace std;  void RootToMat(string fileNameRunTime);
int main()
{// 调用自定义函数RootToMat(fileNameRunTime);return 0;
}void RootToMat(string fileNameRunTime)
{// 打开 Root 文件TFile f("info_");           // open a root fileTTree* t = (TTree*)f.Get("infoOp");  // get the tree from the root filelong long int nentries = t->GetEntries(); int mcolumn = 6;double pID, pSiPM, pe, px, py, pt;t->SetBranchAddress("eventID",&pID);t->SetBranchAddress("SiPMNum",&pSiPM);t->SetBranchAddress("ePho",&pe);t->SetBranchAddress("xPos",&px);t->SetBranchAddress("yPos",&py);t->SetBranchAddress("gloTime",&pt);// 读取数据double *outA = new double[mcolumn*nentries];  for (long long int i=0; i<nentries; i++){t->GetEntry(i);outA[mcolumn*i+0] = pID;outA[mcolumn*i+1] = pSiPM;outA[mcolumn*i+2] = pe;outA[mcolumn*i+3] = px;outA[mcolumn*i+4] = py;outA[mcolumn*i+5] = pt;}// 保存数据MATFile *pmatFile = NULL;mxArray *pMxArray = NULL;pmatFile = matOpen("myOpPho.mat","w");pMxArray = mxCreateDoubleMatrix(mcolumn, nentries, mxREAL);mxSetData(pMxArray, outA);matPutVariable(pmatFile, "myOpPho", pMxArray);matClose(pmatFile);
}

二、 程序运行

g++ -I /usr/local/MATLAB/R2018a/extern/include/ -L /usr/local/MATLAB/R2018a/bin/glnxa64/ -cpp Demo_RootToMat.C -o RootToMat -lmat -lmx -Wl,-rpath /usr/local/MATLAB/R2018a/bin/glnxa64 `root-config --cflags --libs`./RootToMat

本文发布于:2024-01-27 23:59:14,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/17063711553425.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:文件   数据   Geant   MAT   ROOT
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23