9#include <arich/modules/arichBtest/arichBtestModule.h>
12#include <framework/datastore/StoreObjPtr.h>
13#include <framework/dataobjects/EventMetaData.h>
14#include <framework/datastore/StoreArray.h>
15#include <framework/logging/Logger.h>
16#include <framework/gearbox/Unit.h>
19#include <arich/dataobjects/ARICHDigit.h>
20#include <arich/dataobjects/ARICHAeroHit.h>
21#include "arich/geometry/ARICHGeometryPar.h"
22#include "arich/geometry/ARICHBtestGeometryPar.h"
24#include "arich/modules/arichBtest/arichBtestData.h"
30#include <Math/Vector3D.h>
31#include <Math/Rotation3D.h>
57 setDescription(
"Module for the ARICH Beamtest data analysis. It creates track form the MWPC hits and reads the HAPD hits");
60 addParam(
"outputFileName",
m_outFile,
"Output Root Filename", std::string(
"output.root"));
61 std::vector<std::string> defaultList;
63 std::vector<int> defaultMask;
68 for (
int i = 0; i < 32; i++)
m_tdc[i] = 0;
86 B2INFO(
"arichBtestModule::initialize()");
90 digits.registerInDataStore();
93 m_tuple =
new TNtuple(
"nt",
"Btest data hits",
"x:y:xrec:yrec:m:c:gx:gy");
95 for (
int i = 0; i < 6; i++) {
96 sprintf(hapdName,
"hapd%d", i);
97 hapd[i] =
new TH1F(hapdName, hapdName, 145, -0.5, 144.5);
101 for (
int i = 0; i < 4; i++) {
102 for (
int k = 0; k < 2; k++) {
103 sprintf(name,
"mwpc%d_a%d_", i, k);
104 mwpc_diff[i][k] =
new TH1F(strcat(name,
"diff"), name, 300, -150, 150);
105 sprintf(name,
"mwpc%d_a%d_", i, k);
106 mwpc_sum[i][k] =
new TH1F(strcat(name,
"sum"), name, 200, -0.5, 199.5);
107 sprintf(name,
"mwpc%d_a%d_", i, k);
108 mwpc_sum_cut[i][k] =
new TH1F(strcat(name,
"sum_cut"), name, 200, -0.5, 199.5);
109 sprintf(name,
"mwpc%d_a%d_", i, k);
110 mwpc_residuals[i][k] =
new TH1F(strcat(name,
"resd"), name, 200, -100, 100);
111 sprintf(name,
"mwpc%d_a%d_", i, k);
112 mwpc_residualsz[i][k] =
new TH2F(strcat(name,
"resd_z"), name, 200, -25, 25, 400, -1000, 1000);
114 for (
int k = 0; k < 5; k++) {
115 sprintf(name,
"mwpc%d_a%d_", i, k);
116 mwpc_tdc[i][k] =
new TH1F(strcat(name,
"tdc"), name, 1024, -1024, 1024);
118 sprintf(name,
"mwpc%d_", i);
119 mwpc_xy[i] =
new TH2F(strcat(name,
"xy"), name, 120, -30, 30, 120, -30, 30);
143 B2INFO(
"arichBtestModule::beginRun()");
146 B2INFO(
"arichBtestModule::eventMetaDataPtr run:" << eventMetaDataPtr->getRun());
147 B2INFO(
"arichBtestModule::eventMetaDataPtr exp:" << eventMetaDataPtr->getExperiment());
152 static int first = 1;
169 m_fp = gzopen((*m_runCurrent).c_str(),
"rb");
184 gzread(fp, &u,
sizeof(
unsigned int));
185 gzseek(fp, u *
sizeof(
unsigned int), SEEK_CUR);
192 const int unsigned MAXV1290 = 32;
199 for (
int i = 0; i < 32; i++)
m_tdc[i] = 0XFFFF;
202 for (
unsigned int i = 0; i < len; i++) {
203 int rid = (dbuf[i] >> 27) & 0x1F;
206 if (print1290) printf(
"Filler 0x%08x %u.data\n", dbuf[i], i);
209 if (print1290) printf(
"Global Header 0x%08x %u.data\n", dbuf[i], i);
212 nhits = ((dbuf[i] >> 5) & 0xFFFF);
213 if (print1290) printf(
"Global Trailer 0x%08x %u.data STATUS=0x%03x nhits=%u\n", dbuf[i], i, (dbuf[i] >> 24) & 0x7, nhits);
216 if (print1290) printf(
"V1290 nhits!=len %u %u\n", nhits, len);
220 if (print1290) printf(
"Global Trigger TimeTag 0x%08x %u.data\n", dbuf[i], i);
223 if (print1290) printf(
"TDC header 0x%08x %u.data evid=%u wc=%u\n", dbuf[i], i, (dbuf[i] >> 12) & 0xFFF, dbuf[i] & 0xFFF);
227 if (print1290) printf(
"TDC trailer 0x%08x %u.data\n", dbuf[i], i);
231 if (print1290) printf(
"TDC Error 0x%08x %u.data ERR=0x%x\n", dbuf[i], i, dbuf[i] & 0x3FFF);
236 edge = (dbuf[i] >> 26) & 0x1;
237 tdcch = (dbuf[i] >> 21) & 0x1F;
238 tdcl = dbuf[i] & 0x1FFFFF;
239 if (tdcch < MAXV1290) {
243 printf(
"V1290 0x%08x %u. [ch=%2u] edge=%u data=%u \n", dbuf[i], i, tdcch, edge, tdcl);
244 m_tdc[tdcch] = tdcl / 40;
250 if (print1290) printf(
"Unknown 0x%08x %u.data\n", dbuf[i], i);
266 unsigned int bmask = 0xF;
268 for (
int module = 0; module < 6; module++) {
269 int istart = 19 * module;
270 int istop = 19 * module + 18;
271 for (
int i = istart; i < istop; i++) {
272 for (
int j = 0; j < 8; j++) {
273 unsigned int kdata = data[i] & (bmask << (j * 4));
275 int channelID = j + 8 * (i - istart);
277 if (_arichgp->
isActive(module, channelID)) {
279 hapd[module]->Fill(channelID);
282 double globalTime = 0;
284 double rposx = 0, rposy = 0;
286 int channel = eposhapd.second;
288 if ((channel < 108 && channel > 71) || channel < 36) channel = 108 - (int(channel / 6) * 2 + 1) * 6 +
290 else channel = 144 - (int((channel - 36) / 6) * 2 + 1) * 6 + channel - 36;
292 if (abs(loc.X()) > 2.3 || abs(loc.Y()) > 2.3)
continue;
294 arichDigits.
appendNew(module + 1, channel, globalTime);
298 m_tuple ->Fill(-poshapd.first, poshapd.second, rechit.X(), rechit.Y(), module, channelID, rposx, rposy);
314 for (
int i = 0; i < 4; i++) {
317 for (
int k = 0; k < 4; k++)
mwpc_tdc[i][k]->Fill(
m_tdc[w->tdc[k]] - t0);
321 for (
int k = 0; k < 2; k++) {
323 w->diff[k] =
m_tdc[w->tdc[2 * k]] -
m_tdc[w->tdc[2 * k + 1]];
324 w->sum[k] =
m_tdc[w->tdc[2 * k + 1]] +
m_tdc[w->tdc[2 * k]] - 2 *
m_tdc[w->atdc];
326 if (w->sum[k] < w->cutll[k] || w->sum[k] > w->cutul[k])
continue;
329 w->reco[k] = w->diff[k] * w->slp[k] + w->offset[k];
330 w->reco[k] += w->pos[k];
335 w->reco[2] = w->pos[2];
336 if (!w->status[0] && !w->status[1])
mwpc_xy[i]->Fill(w->reco[0], w->reco[1]);
338 if (mask & (1 << i)) {
339 if (!w->status[0] && !w->status[1]) {
348 int ii[4] = {0, 1, 0, 0};
350 for (
int i = 0; i < 4; i++) {
351 if (mask & (1 << i)) {
368 for (
int i = 0; i < 4; i++) {
370 double l = (w->reco[2] - r.Z()) / dir.Z() ;
371 ROOT::Math::XYZVector rext = r + dir * l;
372 if (!w->status[0])
mwpc_residuals[i][0]->Fill(w->reco[0] - rext.Y());
373 if (!w->status[1])
mwpc_residuals[i][1]->Fill(w->reco[1] - rext.X());
376 for (
int k = 0; k < axis->GetNbins(); k++) {
377 double ll = (w->reco[2] + axis->GetBinCenter(k + 1) - r.Z()) / dir.Z();
378 ROOT::Math::XYZVector rextt = r + dir * ll;
379 mwpc_residualsz[i][0]->Fill(w->reco[0] - rextt.Y(), axis->GetBinCenter(k + 1));
380 mwpc_residualsz[i][1]->Fill(w->reco[1] - rextt.X(), axis->GetBinCenter(k + 1));
392 unsigned int len, data[10000];
393 gzread(fp, &len,
sizeof(
unsigned int));
395 gzread(fp, data,
sizeof(
unsigned int)*len);
397 ROOT::Math::XYZVector r;
398 ROOT::Math::XYZVector dir;
422 ROOT::Math::XYZVector rrel = rc - rot * rc;
425 r.SetX(-r.X()); dir.SetX(-dir.X());
432 B2DEBUG(50,
"-----------> " << rc.X() <<
" " << rc.Y() <<
" " << rc.Z() <<
"::::" << rrel.X() <<
" " << rrel.Y() <<
" " <<
433 rrel.Z() <<
" ----> R " << r.X() <<
" " << r.Y() <<
" " << r.Z() <<
" ----> S " << dir.X() <<
" " << dir.Y() <<
" " <<
437 arichAeroHits.
appendNew(particleId, r, dir);
446 for (
unsigned int j = 0; j < len; j++) {
469 static char msg[1024];
472 const int sint =
sizeof(
unsigned int);
474 if (gzread(
m_fp, hdr, sint * 2) != 2 * sint &&
m_end) {
479 eventMetaDataPtr->setEndOfData();
489 B2INFO(
"neve= [" <<
m_events <<
"] in " << (
double)(m_time -
m_timestart) / 60. <<
" min (" <<
int(
498 gzseek(
m_fp, 2 *
sizeof(
unsigned int), SEEK_CUR);
500 case BEGIN_RECORD_TYPE: {
501 gzread(
m_fp, &beginrec,
sizeof(beginrec));
502 time_t t = beginrec.
time;
503 sprintf(msg,
"BeginRec run %u time %s", beginrec.
runno, ctime(&t));
507 case END_RECORD_TYPE: {
508 gzread(
m_fp, &endrec,
sizeof(endrec));
509 time_t t = endrec.
time;
510 sprintf(msg,
"EndRec run %u time %s", endrec.
runno, ctime(&t));
514 case EVENT_RECORD_TYPE: {
515 gzread(
m_fp, &rec,
sizeof(rec));
516 int print = !(rec.evtno % 10000);
519 sprintf(msg,
"EventRec run %u evt %u mstime %u, time %s", rec.runno, rec.evtno, rec.mstime, ctime(&t));
523 int pos = gztell(
m_fp);
525 int buflen = rec.len -
sizeof(rec) /
sizeof(
int);
530 while (i < buflen && j < 5) {
536 if (gzseek(
m_fp, pos + buflen *
sizeof(
unsigned int), SEEK_SET) < 0) B2ERROR(
"gzseek returns -1 ");
540 B2ERROR(
"IO error unknown record type " << type);
548 B2INFO(
" arichBtestModule: End Run !!!");
562 for (
const std::string& fname :
m_runList) {
563 B2INFO(
m_eveList[j] <<
" events processed from file " << fname);
566 for (
int i = 0; i < 4; i++) {
568 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]
569 <<
" A=" <<
m_mwpc[i].atdc);
The Class for ARICH Beamtest Geometry Parameters.
The Class for ARICH Geometry Parameters.
Beamtest ARICH Geometry Tracking Class.
void Print()
Debug printouts.
void setDescription(const std::string &description)
Sets the description of the module.
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.
static const double mm
[millimeters]
static const double GeV
Standard of [energy, momentum, mass].
std::string m_outFile
output file name
time_t m_timestart
time of the first processed event
ARICHTracking * m_mwpc
Pointer to the tracking chambers.
int m_tdc[32]
raw MWPC TDC buffer
std::vector< std::string >::iterator m_runCurrent
current run
TFile * m_file
pointer to the root file
double m_beamMomentum
Momentum of the particles in the beam [GeV].
std::vector< std::string > m_runList
The filenames of the runs.
int m_events
number of events
std::vector< int > m_MwpcTrackMask
Bit mask of the MWPC tracking chambers used for the track creation.
gzFile m_fp
file descriptor of the data file
std::vector< int > m_eveList
The eventnumbers for each of the runs.
TH2F * mwpc_xy[4]
calculated x-y track positions
TH1F * mwpc_residuals[4][2]
residuals from mwpcs
int skipdata(gzFile fp)
Skip the data part of the record.
TH1F * mwpc_sum_cut[4][2]
tdc sum from mwpcs, with sum cut applied
virtual void initialize() override
Initialize the Module.
TH2F * mwpc_residualsz[4][2]
z-residuals from mwpcs
TH1F * mwpc_diff[4][2]
tdc difference from mwpcs
virtual void event() override
Running over all events.
virtual void endRun() override
Is called after processing the last event of a run.
virtual void terminate() override
Is called at the end of your Module.
virtual void beginRun() override
Called when entering a new run.
TH1F * mwpc_sum[4][2]
tdc sum from mwpcs
int getTrack(int mask, ROOT::Math::XYZVector &r, ROOT::Math::XYZVector &dir)
Beamtest Track reconstruction.
arichBtestModule()
Constructor.
TH1F * hapd[6]
histogram of hits for each hapd
TNtuple * m_tuple
ntuple containing hapd hits
void readmwpc(unsigned int *dbuf, unsigned int len, int print=0)
Read the MWPC information from the data buffer.
int readdata(gzFile fp, int rec_id, int print)
Read the data from the file (can be compressed)
int readhapd(unsigned int len, unsigned int *data)
Read the HAPD hits from the data buffer.
TH1F * mwpc_tdc[4][5]
tdc information from mwpcs
std::pair< double, double > GetHapdChannelPosition(int)
Get the position of the HAPD channel.
ROOT::Math::XYZVector getRotationCenter()
Get the rotation center of the Aerogel RICH frame.
ROOT::Math::Rotation3D getFrameRotation()
Get the rotation matrix of the Aerogel RICH frame.
std::pair< int, int > GetHapdElectronicMap(int)
Get the mapping of the electronic channel to the HAPD module nr and the channel number.
static ARICHBtestGeometryPar * Instance()
Static method to get a reference to the ARICHBtestGeometryPar instance.
static ARICHGeometryPar * Instance()
Static method to get a reference to the ARICHGeometryPar instance.
ROOT::Math::XYZVector getTrackingShift()
Get the tracking shift.
ARICHTracking * getMwpc()
Get the pointer of the tracking MWPCs.
bool isActive(int module, int channel)
check the activity of the channel
ROOT::Math::XYZVector getChannelCenterGlob(int modID, int chanID)
get center of chanID channel of modID detector module (in global coordinates)
ROOT::Math::XYVector getChannelCenterLoc(int chID)
get center position of chID channel (in detector module local coordinates)
void addParam(const std::string &name, T ¶mVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
#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.