36 #include <JANA/JApplication.h>
37 #include <JANA/JFactory.h>
40 #include <TDirectory.h>
81 double locNumTAGHhits = 500;
82 double locNumTAGMhits = 200;
83 double locNumPShits = 200;
84 double locNumRFhits = 200;
133 dTreeInterface->Create_Branches(locTreeBranchRegister);
149 dBeamCurrentFactory->init();
150 dBeamCurrentFactory->brun(eventLoop, runnumber);
175 uint32_t trig_mask, fp_trig_mask;
177 loop->GetSingle(trig_words);
187 int trig_bits = fp_trig_mask > 0 ? 10 + fp_trig_mask:trig_mask;
194 vector<const DEventRFBunch*> rfBunch;
198 vector<const Df250WindowRawData*> windowraws;
199 loop->Get(windowraws);
202 vector<const DPSCPair*> cpairs;
206 vector<const DPSPair*> fpairs;
210 vector<const DTAGHHit*> taghhits;
214 vector<const DTAGMHit*> tagmhits;
218 vector<const DBeamPhoton*> beamPhotons;
219 loop->Get(beamPhotons);
222 vector<const DBeamCurrent*> beamCurrent;
223 loop->Get(beamCurrent);
225 japp->RootFillLock(
this);
226 if (!beamCurrent.empty())
229 Bool_t isFiducial = beamCurrent[0]->is_fiducial;
230 dTreeFillData.Fill_Single<Bool_t>(
"isFiducial",isFiducial);
237 dTreeFillData.Fill_Single<ULong64_t>(
"eventnum",eventnumber);
240 unsigned int nRF = rfBunch.size();
241 dTreeFillData.Fill_Single<UShort_t>(
"nRF",nRF);
242 for (
unsigned int i_RF = 0; i_RF < nRF; i_RF++)
244 dTreeFillData.Fill_Array<Double_t>(
"RFTime",rfBunch[i_RF]->dTime,i_RF);
245 dTreeFillData.Fill_Array<Double_t>(
"RFTimeVar",rfBunch[i_RF]->dTimeVariance,i_RF);
246 dTreeFillData.Fill_Array<UShort_t>(
"RFNumParticleVotes",rfBunch[i_RF]->dNumParticleVotes,i_RF);
250 unsigned int nPSC = cpairs.size();
251 dTreeFillData.Fill_Single<UShort_t>(
"nPSC",nPSC);
252 for (
unsigned int i_PSC = 0; i_PSC < nPSC; i_PSC++)
254 const DPSCHit* clhit = cpairs[i_PSC]->ee.first;
255 const DPSCHit* crhit = cpairs[i_PSC]->ee.second;
257 dTreeFillData.Fill_Array<Double_t>(
"PSCtime_lhit",clhit->
t,i_PSC);
258 dTreeFillData.Fill_Array<Double_t>(
"PSCtime_rhit",crhit->
t,i_PSC);
259 dTreeFillData.Fill_Array<Int_t>(
"PSCmodule_lhit",clhit->
module,i_PSC);
260 dTreeFillData.Fill_Array<Int_t>(
"PSCmodule_rhit",crhit->
module,i_PSC);
267 unsigned int nPS = fpairs.size();
268 dTreeFillData.Fill_Single<UShort_t>(
"nPS",nPS);
269 for (
unsigned int i_PS = 0; i_PS < nPS; i_PS++)
276 dTreeFillData.Fill_Array<Double_t>(
"PSenergy_lhit",flhit->
E,i_PS);
277 dTreeFillData.Fill_Array<Double_t>(
"PSenergy_rhit",frhit->
E,i_PS);
278 dTreeFillData.Fill_Array<Double_t>(
"PStime_lhit",flhit->
t,i_PS);
279 dTreeFillData.Fill_Array<Double_t>(
"PStime_rhit",frhit->
t,i_PS);
280 dTreeFillData.Fill_Array<Int_t>(
"PScolumn_lhit",flhit->
column,i_PS);
281 dTreeFillData.Fill_Array<Int_t>(
"PScolumn_rhit",frhit->
column,i_PS);
296 unsigned int htag = 0;
297 unsigned int htag_DBeam = 0;
298 for (
unsigned int i=0; i < taghhits.size(); i++) {
301 if (std::isnan(tag->
t) || std::isnan(tag->
E))
continue;
302 dTreeFillData.Fill_Array<Double_t>(
"TAGHenergy",tag->
E,htag);
303 dTreeFillData.Fill_Array<Double_t>(
"TAGHtime",tag->
t,htag);
304 dTreeFillData.Fill_Array<UInt_t>(
"TAGHcounter",tag->
counter_id,htag);
305 unsigned int same = 0;
306 for (
unsigned int j=0; j < beamPhotons.size(); j++)
309 beamPhotons[j]->GetSingle(tagh);
312 if (std::isnan(tagh->
t) || std::isnan(tagh->
E))
321 if (same > 1 &&
VERBOSE) jerr<<
"Found more than one match for TAGH."<<endl;
322 if (same == 0) dTreeFillData.Fill_Array<Bool_t>(
"TAGH_DBeam",
false,htag);
323 else dTreeFillData.Fill_Array<Bool_t>(
"TAGH_DBeam",
true,htag);
328 unsigned int mtag = 0;
329 unsigned int mtag_DBeam = 0;
330 for (
unsigned int i=0; i < tagmhits.size(); i++) {
333 if (tag->
row!=0)
continue;
334 if (std::isnan(tag->
t) || std::isnan(tag->
E))
continue;
335 dTreeFillData.Fill_Array<Double_t>(
"TAGMenergy",tag->
E,mtag);
336 dTreeFillData.Fill_Array<Double_t>(
"TAGMtime",tag->
t,mtag);
337 dTreeFillData.Fill_Array<UInt_t>(
"TAGMcolumn",tag->
column,mtag);
338 unsigned int same = 0;
339 for (
unsigned int j=0; j < beamPhotons.size(); j++)
342 beamPhotons[j]->GetSingle(tagm);
345 if (tagm->
row != 0)
continue;
346 if (std::isnan(tagm->
t) || std::isnan(tagm->
E))
348 if (
VERBOSE) jerr<<
"Found TAGM with NAN."<<tagm->
column<<endl;
351 if (tagm->
t != tag->
t || tagm->
E != tag->
E || tagm->
column != tag->
column)
continue;
355 if (same > 1 &&
VERBOSE) jerr<<
"Found more than one match for TAGM."<<endl;
356 if (same == 0) dTreeFillData.Fill_Array<Bool_t>(
"TAGM_DBeam",
false,mtag);
357 else dTreeFillData.Fill_Array<Bool_t>(
"TAGM_DBeam",
true,mtag);
362 unsigned int htag_Check = 0;
363 for (
unsigned int i=0; i < beamPhotons.size(); i++) {
365 beamPhotons[i]->GetSingle(tag);
368 if (std::isnan(tag->
t) || std::isnan(tag->
E))
continue;
371 if (htag_Check != htag_DBeam &&
VERBOSE) jerr<<
"TAGH: "<<htag_DBeam<<
" , "<<htag_Check<<endl;
374 unsigned int mtag_Check = 0;
375 for (
unsigned int i=0; i < beamPhotons.size(); i++) {
377 beamPhotons[i]->GetSingle(tag);
380 if (tag->
row!=0)
continue;
381 if (std::isnan(tag->
t) || std::isnan(tag->
E))
continue;
384 if (mtag_Check != mtag_DBeam &&
VERBOSE) jerr<<
"TAGM: "<<mtag_DBeam<<
" , "<<mtag_Check<<endl;
386 dTreeFillData.Fill_Single<UShort_t>(
"ntagh",htag);
387 dTreeFillData.Fill_Single<UShort_t>(
"ntagm",mtag);
391 unsigned int hit = 0;
392 unsigned int ntpol = 0;
393 for(
unsigned int i=0; i< windowraws.size(); i++) {
395 if (windowraw->
rocid!=84)
continue;
396 if (!(windowraw->
slot==13||windowraw->
slot==14))
continue;
397 unsigned int rocid = windowraw->
rocid;
398 unsigned int slot = windowraw->
slot;
399 unsigned int channel = windowraw->
channel;
400 unsigned int itrigger = windowraw->
itrigger;
402 const vector<uint16_t> &samplesvector = windowraw->
samples;
403 unsigned int nsamples = samplesvector.size();
406 if (nsamples==0 &&
VERBOSE) jerr <<
"Raw samples vector is empty." << endl;
408 ULong64_t w_integral = 0;
409 unsigned int w_max = 0;
410 unsigned int w_min = 0;
411 unsigned int w_samp1 = 0;
412 unsigned int i_w_max = 0;
413 unsigned int i_w_min = 0;
414 for (uint16_t c_samp=0; c_samp<nsamples; c_samp++) {
415 dTreeFillData.Fill_Array<UShort_t>(
"waveform",samplesvector[c_samp],ntpol);
418 w_integral = samplesvector[0];
419 w_min = samplesvector[0];
420 w_max = samplesvector[0];
421 w_samp1 = samplesvector[0];
424 w_integral += samplesvector[c_samp];
425 if (w_min > samplesvector[c_samp])
427 w_min = samplesvector[c_samp];
430 if (w_max < samplesvector[c_samp])
432 w_max = samplesvector[c_samp];
438 unsigned int sector = GetSector(slot,channel);
439 double phi = GetPhi(sector);
441 dTreeFillData.Fill_Array<UShort_t>(
"rocid",rocid,hit);
442 dTreeFillData.Fill_Array<UShort_t>(
"slot",slot,hit);
443 dTreeFillData.Fill_Array<UShort_t>(
"channel",channel,hit);
444 dTreeFillData.Fill_Array<UInt_t>(
"itrigger",itrigger,hit);
445 dTreeFillData.Fill_Array<ULong64_t>(
"w_integral",w_integral,hit);
446 dTreeFillData.Fill_Array<UShort_t>(
"w_max",w_max,hit);
447 dTreeFillData.Fill_Array<UShort_t>(
"w_min",w_min,hit);
448 dTreeFillData.Fill_Array<UShort_t>(
"i_w_min",i_w_min,hit);
449 dTreeFillData.Fill_Array<UShort_t>(
"i_w_max",i_w_max,hit);
450 dTreeFillData.Fill_Array<UShort_t>(
"w_samp1",w_samp1,hit);
451 dTreeFillData.Fill_Array<UShort_t>(
"sector",sector,hit);
452 dTreeFillData.Fill_Array<Double_t>(
"phi",phi,hit);
455 unsigned int nadc = hit;
456 if (nadc>
NSECTORS &&
VERBOSE) jerr <<
"TPOL_tree plugin error: nadc exceeds nmax(" <<
NSECTORS <<
")." << endl;
457 dTreeFillData.Fill_Single<UShort_t>(
"nadc",nadc);
458 dTreeFillData.Fill_Single<UShort_t>(
"ntpol",ntpol);
460 if (nadc == 0 && htag == 0 && mtag == 0 && nPS == 0 && nPSC == 0)
return NOERROR;
462 dTreeInterface->Fill(dTreeFillData);
465 japp->RootFillUnLock(
this);
473 if (slot == 13) sector = 25 - channel;
475 if (channel <= 8) sector = 9 - channel;
476 else sector =
NSECTORS + 9 - channel;
479 if (sector == 9) sector = 6;
480 else if (sector == 6) sector = 9;
481 if (sector == 0 &&
VERBOSE) jerr <<
"sector did not change from initial value (0)." << endl;
494 int lastbelowsamp=0;
double peakheight = w_max-w_min;
495 double threshold = w_min + peakheight/2.0;
496 double firstaboveheight=0, lastbelowheight=0;
498 if (peakheight > minpeakheight) {
499 for (uint16_t c_samp=0; c_samp<waveform.size(); c_samp++) {
500 if (waveform[c_samp]>threshold) {
501 firstaboveheight = waveform[c_samp];
502 lastbelowsamp = c_samp-1;
503 lastbelowheight = waveform[c_samp-1];
507 w_time = lastbelowsamp + (threshold-lastbelowheight)/(firstaboveheight-lastbelowheight);
527 delete dTreeInterface;
531 cout<<
"Time in minutes for the plugin to complete: "<<t*1.0/CLOCKS_PER_SEC/60.0<<endl<<endl;
double GetPulseTime(const vector< uint16_t > waveform, double w_min, double w_max, double minpeakheight)
~JEventProcessor_TPOL_tree()
const double SECTOR_DIVISION
static thread_local DTreeFillData dTreeFillData
jerror_t brun(jana::JEventLoop *eventLoop, int32_t runnumber)
Called everytime a new run number is detected.
void Register_Single(string locBranchName)
double GetPhi(int sector)
jerror_t evnt(jana::JEventLoop *eventLoop, uint64_t eventnumber)
Called every event.
vector< uint16_t > samples
jerror_t init(void)
Called once at program start.
int GetSector(int slot, int channel)
static DTreeInterface * Create_DTreeInterface(string locTreeName, string locFileName)
jerror_t erun(void)
Called everytime run number changes, provided brun has been called.
jerror_t fini(void)
Called after last event of last event source has been processed.
void Register_FundamentalArray(string locBranchName, string locArraySizeName, size_t locInitialArraySize=10)
JEventProcessor_TPOL_tree()
static const int NUM_COARSE_COLUMNS
static const double SECTOR_DIVISION
static const unsigned int kColumnCount
static const int NUM_FINE_COLUMNS
static const unsigned int kCounterCount
static const int NSECTORS