#include #include #include struct tree_t { int step; float ts; float xcm; float InA; float amp[4]; float mean[4]; float sigma[4]; }; char *tree_leaflist = "step/I:ts/F:xcm/F:InA/F:amp[4]/F:mean[4]/F:sigma[4]/F"; int totree(int day, int run) { TFile *tfile = gROOT->FindObject("tfile"); if (tfile == 0) { tfile = new TFile("trees.root","update"); tfile->SetName("tfile"); if (tfile == 0) { std::cerr << "Error opening root file trees.root, cannot continue." << std::endl; return 0; } else { std::cout << "Opened root file trees.root for output." << std::endl; } } std::stringstream slogfile; slogfile << "run" << day << "-" << run; std::ifstream sin(slogfile.str().c_str()); if (!sin.is_open()) { std::cerr << "Error opening input file " << slogfile.str() << ", cannot continue." << std::endl; return 0; } struct tree_t record; std::stringstream streename; streename << "run" << day << "_" << run; TTree *tree = new TTree(streename.str().c_str(),slogfile.str().c_str()); TBranch *branch = tree->Branch("adcs",&record,tree_leaflist); float step,position,current,amp[4]; double timestamp0=-1; double timestamp; int steps=0; while (sin.good()) { sin >> step >> timestamp >> position >> current >> amp[0] >> amp[1] >> amp[2] >> amp[3]; if (sin.eof()) { break; } if (timestamp0 < 0) { timestamp0 = timestamp; } record.step = step; record.ts = timestamp-timestamp0; record.xcm = position*2.54; record.InA = current; record.amp[0] = amp[0]; record.amp[1] = amp[1]; record.amp[2] = amp[2]; record.amp[3] = amp[3]; std::stringstream stracefile; stracefile << slogfile.str() << "_traces/run" << run << "_trc_" << record.step << ".dat"; ifstream tin(stracefile.str().c_str()); if (!tin.is_open()) { std::cerr << "Error opening input file " << stracefile.str() << ", cannot continue." << std::endl; return 0; } float time,tamp[4]; double sum0 = 0; double sum1[4] = {0,0,0,0}; double sum2[4] = {0,0,0,0}; while (tin.good()) { tin >> time >> tamp[0] >> tamp[1] >> tamp[2] >> tamp[3]; if (tin.eof()) { break; } for (int i=0; i<4; ++i) { sum2[i] += tamp[i]*tamp[i]; sum1[i] += tamp[i]; } ++sum0; } for (int i=0; i<4; ++i) { record.mean[i] = sum1[i]/(sum0+1e-30); record.sigma[i] = sqrt(sum2[i]/(sum0+1e-30)-record.mean[i]*record.mean[i]); } tree->Fill(); steps++; } tfile->Write(); return steps; }