// // TPixieReader.cpp // Pixie-4toROOT // // Created by Bruce Pierson on 1/16/15. // Copyright (c) 2015 Bruce Pierson. All rights reserved. // #include "TPixieReader.h" void TPixieReader::Readright(string filename, CYCLEDATA ptubedata) { ifstream input; input.open(filename.c_str()); ofstream output[16]; for (uint i = 0; i < 16; i++) { stringstream outfilename; outfilename << "output_" << i << ".txt"; output[i].open(outfilename.str()); } uint iter[17] = {0}; uint counter_east(0), counter_west(0), counter_vert(0); double countrate_east(0), countrate_west(0), countrate_vert(0); double prevstarttime(0); while (!input.eof()) { unsigned short buf[6]; input.read((char*)&buf[0], 12); short nmbrofwords = buf[0] - 6; uint modnmbr = buf[1]; uint formatdescriptor = buf[2]; double starttime = (13. + 1./3.)*(buf[5] + buf[4]*pow(2,16) + buf[3]*pow(2,32)); while (nmbrofwords > 0) { input.read((char*)&buf[0], 6); nmbrofwords -= 3; bitset<4> flag = buf[0]; uint caseval = flag[0]*8 + flag[1]*4 + flag[2]*2 + flag[3]; iter[caseval]++; iter[16]++; uint nmbrofchannels(flag[0] + flag[1] + flag[2] + flag[3]); valarray channeltime(0.0, 4); valarray channelval(0.0, 4); switch (caseval) { case 8: counter_east++; break; case 4: counter_west++; break; case 2: counter_vert++; break; case 12: counter_east++; counter_west++; break; case 6: counter_east++; counter_vert++; break; case 10: counter_west++; counter_vert++; case 14: counter_east++; counter_west++; counter_vert++; default: break; } if (starttime - prevstarttime > 1E6) { double delta = (starttime - prevstarttime); prevstarttime = starttime; countrate_east = counter_east/delta*1E9; countrate_west = counter_west/delta*1E9; countrate_vert = counter_vert/delta*1E9; counter_east = 0; counter_west = 0; counter_vert = 0; cout << countrate_east << "\t" << countrate_west << "\t" << countrate_vert << endl; } switch (caseval) { case 0: nmbrofchannels = 0; break; case 1: nmbrofchannels = 1; input.read((char*)&buf[0], 4); nmbrofwords -= 2; channeltime[3] = starttime + (13. +1./3.)*buf[0]; channelval[3] = buf[1]; output[3] << fixed << channeltime[3] << "\t" << channelval[3] << endl; break; case 2: nmbrofchannels = 1; input.read((char*)&buf[0], 4); nmbrofwords -= 2; channeltime[2] = starttime + (13. +1./3.)*buf[0]; channelval[2] = buf[1]; output[2] << fixed << channeltime[2] << "\t" << channelval[2] << endl; break; case 3: nmbrofchannels = 2; input.read((char*)&buf[0], 4); nmbrofwords -= 2; channeltime[2] = starttime + (13. +1./3.)*buf[0]; channelval[2] = buf[1]; input.read((char*)&buf[0], 4); nmbrofwords -= 2; channeltime[3] = starttime + (13. +1./3.)*buf[0]; channelval[3] = buf[1]; output[2] << fixed << channeltime[2] << "\t" << channelval[2] << endl; output[1] << fixed << channeltime[3] << "\t" << channelval[3] << endl; output[3] << fixed << channeltime[2] << "\t" << channelval[2] << "\t" << channeltime[3] << "\t" << channelval[3] << endl; break; case 4: nmbrofchannels = 1; input.read((char*)&buf[0], 4); nmbrofwords -= 2; channeltime[1] = starttime + (13. +1./3.)*buf[0]; channelval[1] = buf[1]; output[4] << fixed << channeltime[1] << "\t" << channelval[1] << endl; break; case 5: nmbrofchannels = 2; input.read((char*)&buf[0], 4); nmbrofwords -= 2; channeltime[1] = starttime + (13. +1./3.)*buf[0]; channelval[1] = buf[1]; input.read((char*)&buf[0], 4); nmbrofwords -= 2; channeltime[3] = starttime + (13. +1./3.)*buf[0]; channelval[3] = buf[1]; output[4] << fixed << channeltime[1] << "\t" << channelval[1] << endl; output[1] << fixed << channeltime[3] << "\t" << channelval[3] << endl; output[5] << fixed << channeltime[1] << "\t" << channelval[1] << "\t" << channeltime[3] << "\t" << channelval[3] << endl; break; case 6: nmbrofchannels = 2; input.read((char*)&buf[0], 4); nmbrofwords -= 2; channeltime[1] = starttime + (13. +1./3.)*buf[0]; channelval[1] = buf[1]; input.read((char*)&buf[0], 4); nmbrofwords -= 2; channeltime[2] = starttime + (13. +1./3.)*buf[0]; channelval[2] = buf[1]; output[4] << fixed << channeltime[1] << "\t" << channelval[1] << endl; output[2] << fixed << channeltime[2] << "\t" << channelval[2] << endl; output[6] << fixed << channeltime[1] << "\t" << channelval[1] << "\t" << channeltime[3] << "\t" << channelval[3] << endl; break; case 7: nmbrofchannels = 3; input.read((char*)&buf[0], 4); nmbrofwords -= 2; channeltime[1] = starttime + (13. +1./3.)*buf[0]; channelval[1] = buf[1]; input.read((char*)&buf[0], 4); nmbrofwords -= 2; channeltime[2] = starttime + (13. +1./3.)*buf[0]; channelval[2] = buf[1]; input.read((char*)&buf[0], 4); nmbrofwords -= 2; channeltime[3] = starttime + (13. +1./3.)*buf[0]; channelval[3] = buf[1]; output[4] << fixed << channeltime[1] << "\t" << channelval[1] << endl; output[1] << fixed << channeltime[2] << "\t" << channelval[2] << endl; output[2] << fixed << channeltime[3] << "\t" << channelval[3] << endl; output[7] << fixed << channeltime[1] << "\t" << channelval[1] << channeltime[2] << "\t" << channelval[2] << "\t" << channeltime[3] << "\t" << channelval[3] << endl; break; case 8: nmbrofchannels = 1; input.read((char*)&buf[0], 4); nmbrofwords -= 2; channeltime[0] = starttime + (13. +1./3.)*buf[0]; channelval[0] = buf[1]; output[8] << fixed << channeltime[0] << "\t" << channelval[0] << endl; break; case 9: nmbrofchannels = 2; input.read((char*)&buf[0], 4); nmbrofwords -= 2; channeltime[0] = starttime + (13. +1./3.)*buf[0]; channelval[0] = buf[1]; input.read((char*)&buf[0], 4); nmbrofwords -= 2; channeltime[3] = starttime + (13. +1./3.)*buf[0]; channelval[3] = buf[1]; output[8] << fixed << channeltime[0] << "\t" << channelval[0] << endl; output[1] << fixed << channeltime[3] << "\t" << channelval[3] << endl; output[9] << fixed << channeltime[0] << "\t" << channelval[0] << "\t" << channeltime[3] << "\t" << channelval[3] << endl; break; case 10: nmbrofchannels = 2; input.read((char*)&buf[0], 4); nmbrofwords -= 2; channeltime[0] = starttime + (13. +1./3.)*buf[0]; channelval[0] = buf[1]; input.read((char*)&buf[0], 4); nmbrofwords -= 2; channeltime[2] = starttime + (13. +1./3.)*buf[0]; channelval[2] = buf[1]; output[8] << fixed << channeltime[0] << "\t" << channelval[0] << endl; output[2] << fixed << channeltime[2] << "\t" << channelval[2] << endl; output[10] << fixed << channeltime[0] << "\t" << channelval[0] << "\t" << channeltime[2] << "\t" << channelval[2] << endl; break; case 11: nmbrofchannels = 3; input.read((char*)&buf[0], 4); nmbrofwords -= 2; channeltime[0] = starttime + (13. +1./3.)*buf[0]; channelval[0] = buf[1]; input.read((char*)&buf[0], 4); nmbrofwords -= 2; channeltime[2] = starttime + (13. +1./3.)*buf[0]; channelval[2] = buf[1]; input.read((char*)&buf[0], 4); nmbrofwords -= 2; channeltime[3] = starttime + (13. +1./3.)*buf[0]; channelval[3] = buf[1]; output[8] << fixed << channeltime[1] << "\t" << channelval[1] << endl; output[2] << fixed << channeltime[2] << "\t" << channelval[2] << endl; output[1] << fixed << channeltime[3] << "\t" << channelval[3] << endl; output[11] << fixed << channeltime[1] << "\t" << channelval[1] << "\t" << channeltime[2] << "\t" << channelval[2] << "\t" << channeltime[3] << "\t" << channelval[3] << endl; break; case 12: nmbrofchannels = 2; input.read((char*)&buf[0], 4); nmbrofwords -= 2; channeltime[0] = starttime + (13. +1./3.)*buf[0]; channelval[0] = buf[1]; input.read((char*)&buf[0], 4); nmbrofwords -= 2; channeltime[1] = starttime + (13. +1./3.)*buf[0]; channelval[1] = buf[1]; output[8] << fixed << channeltime[0] << "\t" << channelval[0] << endl; output[4] << fixed << channeltime[1] << "\t" << channelval[1] << endl; output[12] << fixed << channeltime[0] << "\t" << channelval[0] << "\t" << channeltime[1] << "\t" << channelval[1] << endl; break; case 13: nmbrofchannels = 3; input.read((char*)&buf[0], 4); nmbrofwords -= 2; cout << nmbrofwords << endl; channeltime[0] = starttime + (13. +1./3.)*buf[0]; channelval[0] = buf[1]; input.read((char*)&buf[0], 4); nmbrofwords -= 2; channeltime[1] = starttime + (13. +1./3.)*buf[0]; channelval[1] = buf[1]; input.read((char*)&buf[0], 4); nmbrofwords -= 2; channeltime[3] = starttime + (13. +1./3.)*buf[0]; channelval[3] = buf[1]; output[8] << fixed << channeltime[0] << "\t" << channelval[0] << endl; output[4] << fixed << channeltime[2] << "\t" << channelval[2] << endl; output[1] << fixed << channeltime[3] << "\t" << channelval[3] << endl; output[14] << fixed << channeltime[0] << "\t" << channelval[0] << "\t" << channeltime[2] << "\t" << channelval[2] << "\t" << channeltime[3] << "\t" << channelval[3] << endl; break; case 14: nmbrofchannels = 3; input.read((char*)&buf, 4); nmbrofwords -= 2; channeltime[0] = starttime + (13. +1./3.)*buf[0]; channelval[0] = buf[1]; input.read((char*)&buf, 4); nmbrofwords -= 2; channeltime[1] = starttime + (13. +1./3.)*buf[0]; channelval[1] = buf[1]; input.read((char*)&buf, 4); nmbrofwords -= 2; channeltime[2] = starttime + (13. +1./3.)*buf[0]; channelval[2] = buf[1]; output[8] << fixed << channeltime[0] << "\t" << channelval[0] << endl; output[4] << fixed << channeltime[1] << "\t" << channelval[1] << endl; output[2] << fixed << channeltime[2] << "\t" << channelval[2] << endl; output[14] << fixed << channeltime[0] << "\t" << channelval[0] << "\t" << channeltime[1] << "\t" << channelval[1] << "\t" << channeltime[2] << "\t" << channelval[2] << endl; break; case 15: nmbrofchannels = 4; input.read((char*)&buf[0], 4); nmbrofwords -= 2; channeltime[0] = starttime + (13. +1./3.)*buf[0]; channelval[0] = buf[1]; input.read((char*)&buf[0], 4); nmbrofwords -= 2; channeltime[1] = starttime + (13. +1./3.)*buf[0]; channelval[1] = buf[1]; input.read((char*)&buf[0], 4); nmbrofwords -= 2; channeltime[2] = starttime + (13. +1./3.)*buf[0]; channelval[2] = buf[1]; input.read((char*)&buf[0], 4); nmbrofwords -= 2; channeltime[3] = starttime + (13. +1./3.)*buf[0]; channelval[3] = buf[1]; output[8] << fixed << channeltime[0] << "\t" << channelval[0] << endl; output[4] << fixed << channeltime[1] << "\t" << channelval[1] << endl; output[2] << fixed << channeltime[2] << "\t" << channelval[2] << endl; output[1] << fixed << channeltime[3] << "\t" << channelval[3] << endl; output[15] << fixed << channeltime[0] << "\t" << channelval[0] << "\t" << channeltime[1] << "\t" << channelval[1] << "\t" << channeltime[2] << "\t" << channelval[2] << "\t" << channeltime[3] << "\t" << channelval[3] << endl; break; default: break; } } } for (uint i = 0; i < 16; i++) { output[i].close(); cout << iter[i] << endl; } cout << iter[16] << endl; std::cout << "Hello, World!\n"; } void TPixieReader::Read(string filename, double* coef, CYCLEDATA ptubedata) { ifstream input; input.open(filename.c_str()); string tmp; string treetitle("cycle data"); uint ticks[4], channel[4], rt[4], lt[4];; double energy[4]; //roottree->Branch("Pneumatic System Data", &fptube_file); string rootfilename = filename.substr(0,filename.find(".",0)); tmp = rootfilename + "_ch0.root"; TFile *rootfile0 = new TFile(tmp.c_str(), "RECREATE"); TTree *roottree0 = new TTree("CycleDirectory", treetitle.c_str()); roottree0->Branch("ticks0", &ticks[0]); roottree0->Branch("channel0", &channel[0]); roottree0->Branch("energy0", &energy[0]); roottree0->Branch("rt0", &rt[0]); roottree0->Branch("lt0", <[0]); tmp = rootfilename + "_ch1.root"; TFile *rootfile1 = new TFile(tmp.c_str(), "RECREATE"); TTree *roottree1 = new TTree("CycleDirectory", treetitle.c_str()); roottree1->Branch("ticks1", &ticks[1]); roottree1->Branch("channel1", &channel[1]); roottree1->Branch("energy1", &energy[1]); roottree1->Branch("rt1", &rt[1]); roottree1->Branch("lt1", <[1]); tmp = rootfilename + "_ch2.root"; TFile *rootfile2 = new TFile(tmp.c_str(), "RECREATE"); TTree *roottree2 = new TTree("CycleDirectory", treetitle.c_str()); roottree2->Branch("ticks2", &ticks[2]); roottree2->Branch("channel2", &channel[2]); roottree2->Branch("energy2", &energy[2]); roottree2->Branch("rt2", &rt[2]); roottree2->Branch("lt2", <[2]); tmp = rootfilename + "_ch0_ch1.root"; TFile *rootfile01 = new TFile(tmp.c_str(), "RECREATE"); TTree *roottree01 = new TTree("CycleDirectory", treetitle.c_str()); roottree01->Branch("ticks0", &ticks[0]); roottree01->Branch("ticks1", &ticks[1]); roottree01->Branch("channel0", &channel[0]); roottree01->Branch("channel1", &channel[1]); roottree01->Branch("energy0", &energy[0]); roottree01->Branch("energy1", &energy[1]); roottree01->Branch("rt0", &rt[0]); roottree01->Branch("rt1", &rt[1]); roottree01->Branch("lt0", <[0]); roottree01->Branch("lt1", <[1]); tmp = rootfilename + "_ch0_ch2.root"; TFile *rootfile02 = new TFile(tmp.c_str(), "RECREATE"); TTree *roottree02 = new TTree("CycleDirectory", treetitle.c_str()); roottree02->Branch("ticks0", &ticks[0]); roottree02->Branch("ticks2", &ticks[2]); roottree02->Branch("channel0", &channel[0]); roottree02->Branch("channel2", &channel[2]); roottree02->Branch("energy0", &energy[0]); roottree02->Branch("energy2", &energy[2]); roottree02->Branch("rt0", &rt[0]); roottree02->Branch("rt2", &rt[2]); roottree02->Branch("lt0", <[0]); roottree02->Branch("lt2", <[2]); tmp = rootfilename + "_ch1_ch2.root"; TFile *rootfile12 = new TFile(tmp.c_str(), "RECREATE"); TTree *roottree12 = new TTree("CycleDirectory", treetitle.c_str()); roottree12->Branch("ticks1", &ticks[1]); roottree12->Branch("ticks2", &ticks[2]); roottree12->Branch("channel1", &channel[1]); roottree12->Branch("channel2", &channel[2]); roottree12->Branch("energy1", &energy[1]); roottree12->Branch("energy2", &energy[2]); roottree12->Branch("rt1", &rt[1]); roottree12->Branch("rt2", &rt[2]); roottree12->Branch("lt1", <[1]); roottree12->Branch("lt2", <[2]); tmp = rootfilename + "_ch0_ch1_ch2.root"; TFile *rootfile012 = new TFile(tmp.c_str(), "RECREATE"); TTree *roottree012 = new TTree("CycleDirectory", treetitle.c_str()); roottree012->Branch("ticks1", &ticks[0]); roottree012->Branch("ticks2", &ticks[1]); roottree012->Branch("ticks3", &ticks[2]); roottree012->Branch("channel1", &channel[0]); roottree012->Branch("channel2", &channel[1]); roottree012->Branch("channel3", &channel[2]); roottree012->Branch("energy1", &energy[0]); roottree012->Branch("energy2", &energy[1]); roottree012->Branch("energy3", &energy[2]); roottree012->Branch("rt1", &rt[0]); roottree012->Branch("rt2", &rt[1]); roottree012->Branch("rt3", &rt[2]); roottree012->Branch("lt1", <[0]); roottree012->Branch("lt2", <[1]); roottree012->Branch("lt3", <[2]); bool capsuletracker(false); uint timeofcapsule; uint iter[17] = {0}; uint counter_east(0), counter_west(0), counter_vert(0); double countrate_east(0), countrate_west(0), countrate_vert(0); double prevstarttime(0); while (!input.eof()) { unsigned short buf[6]; input.read((char*)&buf[0], 12); short nmbrofwords = buf[0] - 6; uint modnmbr = buf[1]; uint formatdescriptor = buf[2]; double starttime = (13. + 1./3.)*(buf[5] + buf[4]*pow(2,16) + buf[3]*pow(2,32)); while (nmbrofwords > 0) { input.read((char*)&buf[0], 6); nmbrofwords -= 3; bitset<4> flag = buf[0]; uint caseval = flag[0]*8 + flag[1]*4 + flag[2]*2 + flag[3]; iter[caseval]++; iter[16]++; uint nmbrofchannels(flag[0] + flag[1] + flag[2] + flag[3]); double startevent = (buf[1] + buf[2] * pow(2, 16)) * (13. + 1/3.) + starttime; double temp3 = (buf[0] * pow(2, 16)) * (13. + 1/3.) + starttime; switch (caseval) { case 8: input.read((char*)buf, 4); nmbrofwords -= 2; energy[0] = coef[0]*buf[1]*buf[1] + coef[1]*buf[1] + coef[2]; channel[0] = buf[1]; ticks[0] = buf[0] - timeofcapsule; rt[0] = starttime + 1000./75.*buf[0]; lt[0] = coef[9]*pow(countrate_east, 2) + coef[10]*countrate_east + coef[11]; rootfile0->cd(); roottree0->Fill(); counter_east++; break; case 4: input.read((char*)buf, 4); nmbrofwords -= 2; energy[1] = coef[3]*buf[1]*buf[1] + coef[4]*buf[1] + coef[5]; channel[1] = buf[1]; ticks[1] = buf[0] - timeofcapsule; rt[1] = starttime + 1000./75.*buf[0]; lt[1] = coef[12]*pow(countrate_west, 2) + coef[13]*countrate_west + coef[14]; rootfile1->cd(); roottree1->Fill(); counter_west++; break; case 2: input.read((char*)buf, 4); nmbrofwords -= 2; energy[2] = coef[6]*buf[1]*buf[1] + coef[7]*buf[1] + coef[8]; channel[2] = buf[1]; ticks[2] = buf[0] - timeofcapsule; rt[2] = starttime + 1000./75.*buf[0]; lt[2] = coef[15]*pow(countrate_vert, 2) + coef[16]*countrate_vert + coef[17]; rootfile2->cd(); roottree2->Fill(); counter_vert++; break; case 12: input.read((char*)buf, 4); nmbrofwords -= 2; energy[0] = coef[0]*buf[1]*buf[1] + coef[1]*buf[1] + coef[2]; channel[0] = buf[1]; ticks[0] = buf[0] - timeofcapsule; rt[0] = starttime + 1000./75.*buf[0]; lt[0] = coef[9]*pow(countrate_east, 2) + coef[10]*countrate_east + coef[11]; input.read((char*)buf, 4); nmbrofwords -= 2; energy[1] = coef[3]*buf[1]*buf[1] + coef[4]*buf[1] + coef[5]; channel[1] = buf[1]; ticks[1] = buf[0] - timeofcapsule; rt[1] = starttime + 1000./75.*buf[0]; lt[1] = coef[12]*pow(countrate_west, 2) + coef[13]*countrate_west + coef[14]; rootfile01->cd(); roottree01->Fill(); counter_east++; counter_west++; break; case 6: input.read((char*)buf, 4); nmbrofwords -= 2; energy[0] = coef[0]*buf[1]*buf[1] + coef[1]*buf[1] + coef[2]; channel[0] = buf[1]; ticks[0] = buf[0] - timeofcapsule; rt[0] = starttime + 1000./75.*buf[0]; lt[0] = coef[9]*pow(countrate_east, 2) + coef[10]*countrate_east + coef[11]; input.read((char*)buf, 4); nmbrofwords -= 2; energy[2] = coef[6]*buf[1]*buf[1] + coef[7]*buf[1] + coef[8]; channel[2] = buf[1]; ticks[2] = buf[0] - timeofcapsule; rt[2] = starttime + 1000./75.*buf[0]; lt[2] = coef[15]*pow(countrate_vert, 2) + coef[16]*countrate_west + coef[14]; rootfile02->cd(); roottree02->Fill(); counter_east++; counter_vert++; break; case 10: input.read((char*)buf, 4); nmbrofwords -= 2; energy[1] = coef[3]*buf[1]*buf[1] + coef[4]*buf[1] + coef[5]; channel[1] = buf[1]; ticks[1] = buf[0] - timeofcapsule; rt[1] = starttime + 1000./75.*buf[0]; lt[1] = coef[12]*pow(countrate_west, 2) + coef[13]*countrate_west + coef[14]; input.read((char*)buf, 4); nmbrofwords -= 2; energy[2] = coef[6]*buf[1]*buf[1] + coef[7]*buf[1] + coef[8]; channel[2] = buf[1]; ticks[2] = buf[0] - timeofcapsule; rt[2] = starttime + 1000./75.*buf[0]; lt[2] = coef[15]*pow(countrate_vert, 2) + coef[16]*countrate_vert + coef[17]; rootfile12->cd(); roottree12->Fill(); counter_west++; counter_vert++; break; case 14: input.read((char*)buf, 4); nmbrofwords -= 2; energy[0] = coef[0]*buf[1]*buf[1] + coef[1]*buf[1] + coef[2]; channel[0] = buf[1]; ticks[0] = buf[0] - timeofcapsule; rt[0] = starttime + 1000./75.*buf[0]; lt[0] = 0; input.read((char*)buf, 4); nmbrofwords -= 2; energy[1] = coef[3]*buf[1]*buf[1] + coef[4]*buf[1] + coef[5]; channel[1] = buf[1]; ticks[1] = buf[0] - timeofcapsule; rt[1] = starttime + 1000./75.*buf[0]; lt[1] = 0; input.read((char*)buf, 4); nmbrofwords -= 2; energy[2] = coef[6]*buf[1]*buf[1] + coef[7]*buf[1] + coef[8]; channel[2] = buf[1]; ticks[2] = buf[0] - timeofcapsule; rt[2] = starttime + 1000./75.*buf[0]; lt[2] = 0; rootfile012->cd(); roottree012->Fill(); counter_east++; counter_west++; counter_vert++; break; default: break; } if (starttime - prevstarttime > 1E6) { double delta = (starttime - prevstarttime); prevstarttime = starttime; countrate_east = counter_east/delta*1E9; countrate_west = counter_west/delta*1E9; countrate_vert = counter_vert/delta*1E9; counter_east = 0; counter_west = 0; counter_vert = 0; // cout << countrate_east << "\t" << countrate_west << "\t" << countrate_vert << endl; } } } roottree0->Write(); rootfile0->Close(); // roottree1->Write(); // rootfile1->Close(); // roottree2->Write(); // rootfile2->Close(); // roottree01->Write(); // rootfile01->Close(); // roottree02->Write(); // rootfile02->Close(); // roottree12->Write(); // rootfile12->Close(); // roottree012->Write(); // rootfile012->Close(); }