This is the whole code:
[code]#include
#include
#include
#include
#include “TFile.h”
#include “TTree.h”
#include “TH1.h”
#include “TH1F.h”
using namespace std;
// Constant parameters
double pitch = 90.0; // um
double length = 5.0; // cm
double cRate = 40.0; // MHz
const unsigned int nStrips = 254; // Number of strips for a sensor
const unsigned int nChannels = nStrips*2;
const int nThresholds = 32; // number of points for a scan
int Vcths[nThresholds] = {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};
// The tree and its branches
TTree *tree;
Int_t Event, thresh, nHits;
Int_t dataBits[nChannels];
// could use to_string in C++11
string int2str (int number) {
stringstream ss;
ss << number;
return ss.str();
}//end int str2int
int str2int (string str) {
string myString = str;
istringstream buffer(myString);
int value;
buffer >> value;
return value;
}//end double str2doub
// could use atof() instead (#include )
double str2doub (string str) {
string myString = str;
istringstream buffer(myString);
double value;
buffer >> value;
return value;
}//end double str2doub
void initializeTree() {
tree = new TTree(“eventTree”,“Tree with data of a step in the signal scan for a metal”);
tree->Branch(“Event”,&Event,“Event/I”);
tree->Branch(“thresh”,&thresh,“thresh/I”);
tree->Branch(“nHits”,&nHits,“nHits/I”);
tree->Branch(“dataBits”,dataBits,Form(“dataBits[%d]/I”, nChannels));
}//end void initializeTree
void treeFiller (string metal, int Vcth, unsigned int nEvents) {
string dataFileName = “/home/xtaldaq/Ph2_ACF/SignalScan/Data_” + metal + “_” + int2str(Vcth) + “.txt”;
ifstream dataFile (dataFileName.c_str());
unsigned int iEvent = 0;
string entry = “”;
while ( dataFile >> entry ) {
// First entry is the event number
Event = str2int(entry);
// Second entry is the threshold
dataFile >> entry;
thresh = str2int(entry);
//Third entry is the amount of hits for this threshold
dataFile >> entry;
nHits = str2int(entry);
//Fourth entry is the data bit string which will be pushed back into a vector
dataFile >> entry;
for (unsigned int strip=0; strip<entry.length(); strip++) {
if((entry[strip]=='1')) dataBits[strip] = 1;
else dataBits[strip] = 0;
}//end for strip
// Now we want to fill the TTree with these values
tree->Fill();
// Process nEvents at most
if (++iEvent>=nEvents) break;
if (iEvent%10000==0) cout << "." << flush;
}//end while entry
dataFile.close();
}//end void treeFiller
int macro (unsigned int maxEvents=100) {
string treeFileName = Form("TreeScan_%d.root", maxEvents);
TFile* treeFile = new TFile(treeFileName.c_str(),“recreate”);
initializeTree();
tree->Write();
treeFile->Close();
treeFile = new TFile(treeFileName.c_str(), “update”);
tree = (TTree*)treeFile->Get(“eventTree”);
tree->SetBranchAddress(“Event”,&Event);
tree->SetBranchAddress(“thresh”,&thresh);
tree->SetBranchAddress(“nHits”,&nHits);
tree->SetBranchAddress(“dataBits”,dataBits);
for (int iThresh=0; iThresh<nThresholds; ++iThresh) {
cout << "Theshold " << Vcths[iThresh] << " " << flush;
treeFiller("Sn", Vcths[iThresh], maxEvents); // to be looped on all thresholds instead
cout << " done!" << endl;
}//end for iThresh
tree->Write("", TObject::kOverwrite); // save only the new version of the tree
treeFile->Close();
return 0;
}// end macro[/code]