-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathBESDataToRootFile.cpp
More file actions
127 lines (117 loc) · 5.58 KB
/
BESDataToRootFile.cpp
File metadata and controls
127 lines (117 loc) · 5.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "Riostream.h"
#include <cstdio>
#include <vector>
#include <string>
#include <fstream>
#include "TString.h"
#include "TMath.h"
#include "TH1D.h"
#include <iostream> // to use cout for debugging
using namespace std;
/*const char* */ string concatenateHistoname(string,string,string,string);
int BESDataToRootFile(){ // main
string myString;// temporary string to hold ifstream instance
string collidingSpecies;/////////////
string collisionEnergy;
string collisionEnergyStr;////////////////////
string particleName;// eg. pi+
Float_t myFloat; //temporary float to hold energy value
Double_t myDouble; // temporary double to hold values from histo row
Int_t histonum = 0; // track number of histos created/parsed
//TODO: std::vector<TH1D> histoList; // vector (list) of histograms
ifstream in;
in.open(Form("./BESData_sorted.txt"));
// ^ data file with Beam Scan Energy data, !!!!!!bins sorted!!!!!
TFile* f = new TFile("BESData.root","RECREATE");// .root file to be created
//const char* collidingSpeciesPtr = NULL;
/*
nested loop structure:
while(not eof){
while
}
*/
while(in >> collidingSpecies){
while(collidingSpecies!="Au+Au") {in >> collidingSpecies;}
in >> collisionEnergy;
for(int i = 0; i<8; i++) in >> myString; // skipping 8 strings
// single histogram creation:
for(int cent = 0; cent<9; cent++){// iterate 9X for 9 centralities
std::vector<Double_t> binEdgesVec;
std::vector<Double_t> binContent;
std::vector<Double_t> binContentErrStat;
std::vector<Double_t> binContentErrSys;
in >> particleName;
if(!in.good()) cout<< "particleName not read correctly"<< endl;
in >> myString;// read & skip string corresponding to centrality
Int_t binNum = 0;
Double_t tempDouble; /////////// flag used in debugging
Double_t binHighEdge; // only pushed_back after completion of while loop
// to avoid redundancy
/////// FIXME binEdgesVec.push_back(0.0);// to set the left-most edge to zero DONE
while(in>>myDouble){// get single content from each of the 5 columns, and repeat
// until in.fail(), i,e. fail bit encountered
// fstream malfunction if input stream statement within loop body
// instead of in loop condition
binNum++;// first iteration gets first bin, etc.
binEdgesVec.push_back(myDouble);
in >> binHighEdge;// not pushed_back until the while loop terminates
in >> myDouble;
binContent.push_back(myDouble);
in >> myDouble;
binContentErrStat.push_back(myDouble);
in >> myDouble;
binContentErrSys.push_back(myDouble);
cout << "bin: " << binNum << " " << binEdgesVec[binNum-1] << " " << binContent[binNum-1] << endl;
}// end of while loop to capture every histo bin
//TODO:
/*if (in.fail())cout << "Error! Fail bit!"<< endl; // this is what happens
else if (in.bad())cout << "Error! Not fail bit but bad bit!"<<endl;
else cout << "Error! But neither fail bit nor bad bit!"<< endl;*/
in.clear(); // to restore input stream in a good state after failbit
in >> myString;// read end of histo: ---------------- and do nothing
binEdgesVec.push_back(binHighEdge);
string centString = std::to_string(cent);////////////////////
//TH1D *cent0_pion_minus__3 = new TH1D(name,"0-5%",52, xAxis2);// example constructor
// cent$centrality$_$particleName$_$collidingSpecies$_$collisionEnergy$// histoname format
string histoname = concatenateHistoname(centString,particleName,collidingSpecies,collisionEnergy);
// TH1D constructor takes const char*, not string, as name args:
const char* histonameConstCharPtr = histoname.c_str();// convert string to const char*
histonum++;// just to track
cout << "Histoname: " << histoname << ", Histonum: "<< histonum<< endl;
//for(int c = 0; c<binNum; c++){cout<< binEdgesArr[c]<<endl;}return 0;//////////////
// fourth arg in TH1D constructor is a pointer
//to list (of bin edges), in this case a vector:
Double_t* a = &binEdgesVec[0];
TH1D *h = new TH1D(histonameConstCharPtr,histonameConstCharPtr, binNum, a);
// ^ here histoname needs to be a const char*
// check if pointers for two histos are not different:
cout << "his. pointer " << histonum <<":" << h << endl;
cout << "arr. pointer " << histonum <<":" << /*a*/&binEdgesVec[0] << endl;
h->GetXaxis()->SetRangeUser(0.,10.);
cout << "bin num: " << binNum << endl;
for(int j = 1; j<=binNum; j++){// fill in bin content and error for every bin
// Y-axis of data transformed to match BGBW fit y-axis:
h->SetBinContent(j,
binContent[j-1]);//*2*TMath::Pi());//*(binEdgesVec[j]+binEdgesVec[j+1])/2);
// ^ (data y-value multiplied by 2pi// not:*pt; pt = ptLow+0.5 of binWidth)
// add errors in quadrature:
h->SetBinError(j,TMath::Sqrt(binContentErrStat[j-1]*binContentErrStat[j-1]
+binContentErrSys[j-1]*binContentErrSys[j-1]));
}
cout <<"--------------------------------------------"<< endl<< endl;
// TODO: histoList.push_back(*h); // Not necessary if histo list not created
}// end of for loop to capture each centrality
}// end of global while loop to capture each collisionEnergy
in.close();
f->Write(); // writes objects in memory (TH1D objects in this case) to TFile
delete f; // instead of f->Close(), in order to automatically delete all objects
// owned by this file
//f->Close();
return 0;
}
string concatenateHistoname(string centStr,string pName,string colSp,string colEn){
string initText = "cent";
string undScr = "_";//underscore
string addedString = initText+centStr+undScr+pName+undScr+colSp+undScr+colEn;
return addedString; //type: const char*: to be done later
}