9 #include <arich/modules/arichBtest/arichBtestModule.h>
16 #include <framework/datastore/StoreObjPtr.h>
17 #include <framework/dataobjects/EventMetaData.h>
18 #include <framework/datastore/StoreArray.h>
19 #include <framework/logging/Logger.h>
20 #include <framework/gearbox/Unit.h>
23 #include <boost/algorithm/string.hpp>
24 #include <boost/foreach.hpp>
28 #include <arich/dataobjects/ARICHDigit.h>
29 #include <arich/dataobjects/ARICHAeroHit.h>
30 #include "arich/geometry/ARICHGeometryPar.h"
31 #include "arich/geometry/ARICHBtestGeometryPar.h"
33 #include "arich/modules/arichBtest/arichBtestData.h"
45 const char* record_strings[] = {
"Event",
"Begin_run",
"Pause",
"Resume",
"End_run" };
69 setDescription(
"Module for the ARICH Beamtest data analysis. It creates track form the MWPC hits and reads the HAPD hits");
72 addParam(
"outputFileName", m_outFile,
"Output Root Filename",
string(
"output.root"));
73 vector<string> defaultList;
74 addParam(
"runList", m_runList,
"Data Filenames.", defaultList);
75 vector<int> defaultMask;
76 addParam(
"mwpcTrackMask", m_MwpcTrackMask,
"Create track from MWPC layers", defaultMask);
78 addParam(
"beamMomentum", m_beamMomentum,
"Momentum of the beam [GeV]", 0.0);
80 for (
int i = 0; i < 32; i++) m_tdc[i] = 0;
96 void arichBtestModule::initialize()
98 B2INFO(
"arichBtestModule::initialize()");
102 digits.registerInDataStore();
104 m_file =
new TFile(m_outFile.c_str(),
"RECREATE");
105 m_tuple =
new TNtuple(
"nt",
"Btest data hits",
"x:y:xrec:yrec:m:c:gx:gy");
107 for (
int i = 0; i < 6; i++) {
108 sprintf(hapdName,
"hapd%d", i);
109 hapd[i] =
new TH1F(hapdName, hapdName, 145, -0.5, 144.5);
113 for (
int i = 0; i < 4; i++) {
114 for (
int k = 0; k < 2; k++) {
115 sprintf(name,
"mwpc%d_a%d_", i, k);
116 mwpc_diff[i][k] =
new TH1F(strcat(name,
"diff"), name, 300, -150, 150);
117 sprintf(name,
"mwpc%d_a%d_", i, k);
118 mwpc_sum[i][k] =
new TH1F(strcat(name,
"sum"), name, 200, -0.5, 199.5);
119 sprintf(name,
"mwpc%d_a%d_", i, k);
120 mwpc_sum_cut[i][k] =
new TH1F(strcat(name,
"sum_cut"), name, 200, -0.5, 199.5);
121 sprintf(name,
"mwpc%d_a%d_", i, k);
122 mwpc_residuals[i][k] =
new TH1F(strcat(name,
"resd"), name, 200, -100, 100);
123 sprintf(name,
"mwpc%d_a%d_", i, k);
124 mwpc_residualsz[i][k] =
new TH2F(strcat(name,
"resd_z"), name, 200, -25, 25, 400, -1000, 1000);
126 for (
int k = 0; k < 5; k++) {
127 sprintf(name,
"mwpc%d_a%d_", i, k);
128 mwpc_tdc[i][k] =
new TH1F(strcat(name,
"tdc"), name, 1024, -1024, 1024);
130 sprintf(name,
"mwpc%d_", i);
131 mwpc_xy[i] =
new TH2F(strcat(name,
"xy"), name, 120, -30, 30, 120, -30, 30);
152 void arichBtestModule::beginRun()
155 B2INFO(
"arichBtestModule::beginRun()");
158 B2INFO(
"arichBtestModule::eventMetaDataPtr run:" << eventMetaDataPtr->getRun());
159 B2INFO(
"arichBtestModule::eventMetaDataPtr exp:" << eventMetaDataPtr->getExperiment());
162 m_mwpc = _arichbtgp->
getMwpc();
164 static int first = 1;
166 m_runCurrent = m_runList.begin();
176 if (m_runCurrent < m_runList.end()) {
178 m_eveList.push_back(m_events);
181 m_fp = gzopen((*m_runCurrent).c_str(),
"rb");
183 B2INFO(
"Cannot open " << *m_runCurrent);
186 B2INFO(
"File opened " << *m_runCurrent);
193 int arichBtestModule::skipdata(gzFile fp)
196 gzread(fp, &u,
sizeof(
unsigned int));
197 gzseek(fp, u *
sizeof(
unsigned int), SEEK_CUR);
201 void arichBtestModule::readmwpc(
unsigned int* dbuf,
unsigned int len,
int print1290)
204 const int unsigned MAXV1290 = 32;
211 for (
int i = 0; i < 32; i++) m_tdc[i] = 0XFFFF;
214 for (
unsigned int i = 0; i < len; i++) {
215 int rid = (dbuf[i] >> 27) & 0x1F;
218 if (print1290) printf(
"Filler 0x%08x %u.data\n", dbuf[i], i);
221 if (print1290) printf(
"Global Header 0x%08x %u.data\n", dbuf[i], i);
224 nhits = ((dbuf[i] >> 5) & 0xFFFF);
225 if (print1290) printf(
"Global Trailer 0x%08x %u.data STATUS=0x%03x nhits=%u\n", dbuf[i], i, (dbuf[i] >> 24) & 0x7, nhits);
228 if (print1290) printf(
"V1290 nhits!=len %u %u\n", nhits, len);
232 if (print1290) printf(
"Global Trigger TimeTag 0x%08x %u.data\n", dbuf[i], i);
235 if (print1290) printf(
"TDC header 0x%08x %u.data evid=%u wc=%u\n", dbuf[i], i, (dbuf[i] >> 12) & 0xFFF, dbuf[i] & 0xFFF);
239 if (print1290) printf(
"TDC trailer 0x%08x %u.data\n", dbuf[i], i);
243 if (print1290) printf(
"TDC Error 0x%08x %u.data ERR=0x%x\n", dbuf[i], i, dbuf[i] & 0x3FFF);
248 edge = (dbuf[i] >> 26) & 0x1;
249 tdcch = (dbuf[i] >> 21) & 0x1F;
250 tdcl = dbuf[i] & 0x1FFFFF;
251 if (tdcch < MAXV1290) {
255 printf(
"V1290 0x%08x %u. [ch=%2u] edge=%u data=%u \n", dbuf[i], i, tdcch, edge, tdcl);
256 m_tdc[tdcch] = tdcl / 40;
262 if (print1290) printf(
"Unknown 0x%08x %u.data\n", dbuf[i], i);
271 int arichBtestModule::readhapd(
unsigned int len,
unsigned int* data)
280 for (
int module = 0; module < 6; module++) {
281 int istart = 19 * module;
282 int istop = 19 * module + 18;
283 for (
int i = istart; i < istop; i++) {
284 for (
int j = 0; j < 8; j++) {
285 unsigned int kdata = data[i] & (bmask << (j * 4));
287 int channelID = j + 8 * (i - istart);
289 if (_arichgp->
isActive(module, channelID)) {
291 hapd[module]->Fill(channelID);
294 double globalTime = 0;
296 double rposx = 0, rposy = 0;
298 int channel = eposhapd.second;
300 if ((channel < 108 && channel > 71) || channel < 36) channel = 108 - (int(channel / 6) * 2 + 1) * 6 +
302 else channel = 144 - (int((channel - 36) / 6) * 2 + 1) * 6 + channel - 36;
304 if (abs(loc.X()) > 2.3 || abs(loc.Y()) > 2.3)
continue;
306 arichDigits.
appendNew(module + 1, channel, globalTime);
310 m_tuple ->Fill(-poshapd.first, poshapd.second, rechit.x(), rechit.y(), module, channelID, rposx, rposy);
321 int arichBtestModule::getTrack(
int mask, TVector3& r, TVector3& dir)
326 for (
int i = 0; i < 4; i++) {
329 for (
int k = 0; k < 4; k++)
mwpc_tdc[i][k]->Fill(m_tdc[w->tdc[k]] - t0);
330 mwpc_tdc[i][4]->Fill(m_tdc[w->atdc] - t0);
333 for (
int k = 0; k < 2; k++) {
335 w->diff[k] = m_tdc[w->tdc[2 * k]] - m_tdc[w->tdc[2 * k + 1]];
336 w->sum[k] = m_tdc[w->tdc[2 * k + 1]] + m_tdc[w->tdc[2 * k]] - 2 * m_tdc[w->atdc];
338 if (w->sum[k] < w->cutll[k] || w->sum[k] > w->cutul[k])
continue;
341 w->reco[k] = w->diff[k] * w->slp[k] + w->offset[k];
342 w->reco[k] += w->pos[k];
347 w->reco[2] = w->pos[2];
348 if (!w->status[0] && !w->status[1])
mwpc_xy[i]->Fill(w->reco[0], w->reco[1]);
350 if (mask & (1 << i)) {
351 if (!w->status[0] && !w->status[1]) {
360 int ii[4] = {0, 1, 0, 0};
362 for (
int i = 0; i < 4; i++) {
363 if (mask & (1 << i)) {
371 r.SetXYZ(m_mwpc[i0].reco[1], m_mwpc[i0].reco[0], m_mwpc[i0].reco[2]);
372 dir.SetXYZ(m_mwpc[i1].reco[1] - m_mwpc[i0].reco[1],
373 m_mwpc[i1].reco[0] - m_mwpc[i0].reco[0],
374 m_mwpc[i1].reco[2] - m_mwpc[i0].reco[2]);
380 for (
int i = 0; i < 4; i++) {
382 double l = (w->reco[2] - r.z()) / dir.z() ;
383 TVector3 rext = r + dir * l;
384 if (!w->status[0])
mwpc_residuals[i][0]->Fill(w->reco[0] - rext.y());
385 if (!w->status[1])
mwpc_residuals[i][1]->Fill(w->reco[1] - rext.x());
388 for (
int k = 0; k < axis->GetNbins(); k++) {
389 double ll = (w->reco[2] + axis->GetBinCenter(k + 1) - r.z()) / dir.z();
390 TVector3 rextt = r + dir * ll;
391 mwpc_residualsz[i][0]->Fill(w->reco[0] - rextt.y(), axis->GetBinCenter(k + 1));
392 mwpc_residualsz[i][1]->Fill(w->reco[1] - rextt.x(), axis->GetBinCenter(k + 1));
401 int arichBtestModule::readdata(gzFile fp,
int rec_id,
int)
404 unsigned int len, data[10000];
405 gzread(fp, &len,
sizeof(
unsigned int));
407 gzread(fp, data,
sizeof(
unsigned int)*len);
413 int retval = getTrack(*(m_MwpcTrackMask.begin()), r, dir);
421 dir *= m_beamMomentum * Unit::GeV;
434 TVector3 rrel = rc - rot * rc;
437 r.SetX(-r.X()); dir.SetX(-dir.X());
444 B2DEBUG(50,
"-----------> " << rc.x() <<
" " << rc.y() <<
" " << rc.z() <<
"::::" << rrel.x() <<
" " << rrel.y() <<
" " <<
445 rrel.z() <<
" ----> R " << r.x() <<
" " << r.y() <<
" " << r.z() <<
" ----> S " << dir.x() <<
" " << dir.y() <<
" " <<
449 arichAeroHits.
appendNew(particleId, r, dir);
458 for (
unsigned int j = 0; j < len; j++) {
469 void arichBtestModule::event()
481 static char msg[1024];
484 const int sint =
sizeof(
unsigned int);
486 if (gzread(m_fp, hdr, sint * 2) != 2 * sint && m_end) {
487 B2INFO(
"[" << m_events <<
"] End of file: " << *m_runCurrent);
489 if (m_runCurrent == m_runList.end()) {
491 eventMetaDataPtr->setEndOfData();
497 }
while (m_end && m_runCurrent != m_runList.end());
498 if (m_events % 1000 == 0) {
501 B2INFO(
"neve= [" << m_events <<
"] in " << (
double)(m_time - m_timestart) / 60. <<
" min (" <<
int(
502 m_time - m_timestart) <<
"s) from " << *m_runCurrent);
510 gzseek(m_fp, 2 *
sizeof(
unsigned int), SEEK_CUR);
512 case BEGIN_RECORD_TYPE: {
513 gzread(m_fp, &beginrec,
sizeof(beginrec));
514 time_t t = beginrec.
time;
515 sprintf(msg,
"BeginRec run %u time %s", beginrec.
runno, ctime(&t));
519 case END_RECORD_TYPE: {
520 gzread(m_fp, &endrec,
sizeof(endrec));
521 time_t t = endrec.
time;
522 sprintf(msg,
"EndRec run %u time %s", endrec.
runno, ctime(&t));
526 case EVENT_RECORD_TYPE: {
527 gzread(m_fp, &rec,
sizeof(rec));
528 int print = !(rec.evtno % 10000);
531 sprintf(msg,
"EventRec run %u evt %u mstime %u, time %s", rec.runno, rec.evtno, rec.mstime, ctime(&t));
535 int pos = gztell(m_fp);
537 int buflen = rec.len -
sizeof(rec) /
sizeof(
int);
542 while (i < buflen && j < 5) {
543 n[j] = readdata(m_fp, j, print);
548 if (gzseek(m_fp, pos + buflen *
sizeof(
unsigned int), SEEK_SET) < 0) B2ERROR(
"gzseek returns -1 ");
552 B2ERROR(
"IO error unknown record type " << type);
555 if (gzeof(m_fp)) m_end = 1;
558 void arichBtestModule::endRun()
560 B2INFO(
" arichBtestModule: End Run !!!");
567 m_eveList.push_back(m_events);
571 void arichBtestModule::terminate()
574 BOOST_FOREACH(
const string & fname, m_runList) {
575 B2INFO(m_eveList[j] <<
" events processed from file " << fname);
578 for (
int i = 0; i < 4; i++) {
580 B2INFO(i <<
" a1=" << m_mwpc[i].tdc[0] <<
" a2=" << m_mwpc[i].tdc[1] <<
" a3=" << m_mwpc[i].tdc[2] <<
" a2=" << m_mwpc[i].tdc[3]
581 <<
" A=" << m_mwpc[i].atdc);
The Class for ARICH Beamtest Geometry Parameters.
The Class for ARICH Geometry Parameters.
Beamtest ARICH Geometry Tracking Class.
bool registerInDataStore(DataStore::EStoreFlags storeFlags=DataStore::c_WriteOut)
Register the object/array in the DataStore.
Accessor to arrays stored in the data store.
T * appendNew()
Construct a new T object at the end of the array.
Type-safe access to single objects in the data store.
TH2F * mwpc_xy[4]
calculated x-y track positions
TH1F * mwpc_residuals[4][2]
residuals from mwpcs
TH1F * mwpc_sum_cut[4][2]
tdc sum from mwpcs, with sum cut applied
TH2F * mwpc_residualsz[4][2]
z-residuals from mwpcs
TH1F * mwpc_diff[4][2]
tdc difference from mwpcs
TH1F * mwpc_sum[4][2]
tdc sum from mwpcs
TH1F * hapd[6]
histogram of hits for each hapd
TNtuple * m_tuple
ntuple containing hapd hits
TH1F * mwpc_tdc[4][5]
tdc information from mwpcs
TVector3 getRotationCenter()
Get the rotation center of the Aerogel RICH frame.
std::pair< double, double > GetHapdChannelPosition(int)
Get the position of the HAPD channel.
std::pair< int, int > GetHapdElectronicMap(int)
Get the mapping of the electronic channel to the HAPD module nr and the channel number.
TVector3 getChannelCenterGlob(int modID, int chanID)
get center of chanID channel of modID detector module (in global coordinates)
TVector2 getChannelCenterLoc(int chID)
get center position of chID channel (in detector module local coordinates)
TRotation getFrameRotation()
Get the rotation matrix of the Aerogel RICH frame.
ARICHTracking * getMwpc()
Get the pointer of the tracking MWPCs.
bool isActive(int module, int channel)
check the activity of the channel
TVector3 getTrackingShift()
Get the tracking shift.
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Abstract base class for different kinds of events.
Begin record structure for the beamtest data.
unsigned int runno
Run number.
unsigned int time
Timestamp of the run.
Event record structure for the beamtest data.