11 #include "TDirectory.h"
19 #include <JANA/JEvent.h>
20 #include <JANA/JApplication.h>
29 LOAD_CCDB_CONSTANTS = 1.;
30 gPARMS->SetDefaultParameter(
"FCAL:LOAD_NONLIN_CCDB", LOAD_CCDB_CONSTANTS);
32 SHOWER_ENERGY_THRESHOLD = 50*
k_MeV;
33 gPARMS->SetDefaultParameter(
"FCAL:SHOWER_ENERGY_THRESHOLD", SHOWER_ENERGY_THRESHOLD);
52 gPARMS->SetDefaultParameter(
"FCAL:cutoff_enegry", cutoff_energy);
53 gPARMS->SetDefaultParameter(
"FCAL:linfit_slope", linfit_slope);
54 gPARMS->SetDefaultParameter(
"FCAL:linfit_intercept", linfit_intercept);
55 gPARMS->SetDefaultParameter(
"FCAL:expfit_param1", expfit_param1);
56 gPARMS->SetDefaultParameter(
"FCAL:expfit_param2", expfit_param2);
57 gPARMS->SetDefaultParameter(
"FCAL:expfit_param3", expfit_param3);
59 gPARMS->SetDefaultParameter(
"FCAL:P0", timeConst0);
60 gPARMS->SetDefaultParameter(
"FCAL:P1", timeConst1);
61 gPARMS->SetDefaultParameter(
"FCAL:P2", timeConst2);
62 gPARMS->SetDefaultParameter(
"FCAL:P3", timeConst3);
63 gPARMS->SetDefaultParameter(
"FCAL:P4", timeConst4);
67 FCAL_RADIATION_LENGTH = 3.1;
68 FCAL_CRITICAL_ENERGY = 0.035;
69 FCAL_SHOWER_OFFSET = 1.0;
71 gPARMS->SetDefaultParameter(
"FCAL:FCAL_RADIATION_LENGTH", FCAL_RADIATION_LENGTH);
72 gPARMS->SetDefaultParameter(
"FCAL:FCAL_CRITICAL_ENERGY", FCAL_CRITICAL_ENERGY);
73 gPARMS->SetDefaultParameter(
"FCAL:FCAL_SHOWER_OFFSET", FCAL_SHOWER_OFFSET);
76 COVARIANCEFILENAME =
"";
77 gPARMS->SetDefaultParameter(
"DFCALShower:VERBOSE",
VERBOSE,
"Verbosity level for DFCALShower objects and factories");
78 gPARMS->SetDefaultParameter(
"DFCALShower:COVARIANCEFILENAME", COVARIANCEFILENAME,
"File name for covariance files");
89 map<string, double> fcal_parms;
90 loop->GetCalib(
"FCAL/fcal_parms", fcal_parms);
91 if (fcal_parms.find(
"FCAL_C_EFFECTIVE")!=fcal_parms.end()){
92 FCAL_C_EFFECTIVE = fcal_parms[
"FCAL_C_EFFECTIVE"];
93 if(debug_level>0)jout<<
"FCAL_C_EFFECTIVE = "<<FCAL_C_EFFECTIVE<<endl;
95 jerr<<
"Unable to get FCAL_C_EFFECTIVE from FCAL/fcal_parms in Calib database!"<<endl;
107 cerr <<
"No geometry accessbile." << endl;
108 return RESOURCE_UNAVAILABLE;
113 if(LOAD_CCDB_CONSTANTS > 0.1) {
114 map<string, double> shower_calib_piecewise;
115 loop->GetCalib(
"FCAL/shower_calib_piecewise", shower_calib_piecewise);
116 cutoff_energy = shower_calib_piecewise[
"cutoff_energy"];
117 linfit_slope = shower_calib_piecewise[
"linfit_slope"];
118 linfit_intercept = shower_calib_piecewise[
"linfit_intercept"];
119 expfit_param1 = shower_calib_piecewise[
"expfit_param1"];
120 expfit_param2 = shower_calib_piecewise[
"expfit_param2"];
121 expfit_param3 = shower_calib_piecewise[
"expfit_param3"];
124 jout <<
"cutoff_energy = " << cutoff_energy << endl;
125 jout <<
"linfit_slope = " << linfit_slope << endl;
126 jout <<
"linfit_intercept = " << linfit_intercept << endl;
127 jout <<
"expfit_param1 = " << expfit_param1 << endl;
128 jout <<
"expfit_param2 = " << expfit_param2<< endl;
129 jout <<
"expfit_param3 = " << expfit_param3 << endl;
135 if(LOAD_CCDB_CONSTANTS > 0.1) {
136 map<string,double> timing_correction;
137 loop->GetCalib(
"FCAL/shower_timing_correction", timing_correction);
138 timeConst0 = timing_correction[
"P0"];
139 timeConst1 = timing_correction[
"P1"];
140 timeConst2 = timing_correction[
"P2"];
141 timeConst3 = timing_correction[
"P3"];
142 timeConst4 = timing_correction[
"P4"];
146 jout <<
"timeConst0 = " << timeConst0 << endl;
147 jout <<
"timeConst1 = " << timeConst1 << endl;
148 jout <<
"timeConst2 = " << timeConst2 << endl;
149 jout <<
"timeConst3 = " << timeConst3 << endl;
150 jout <<
"timeConst4 = " << timeConst4 << endl;
158 jerror_t result = LoadCovarianceLookupTables(eventLoop);
159 if (result!=NOERROR)
return result;
167 for (
int i=0; i<5; i++) {
168 for (
int j=0; j<=i; j++) {
169 delete CovarianceLookupTable[i][j];
170 CovarianceLookupTable[i][j] =
nullptr;
182 vector<const DFCALCluster*> fcalClusters;
183 eventLoop->Get(fcalClusters);
184 if(fcalClusters.size()<1)
return NOERROR;
187 DVector3 vertex(0.0, 0.0, m_zTarget);
189 vector< const DTrackWireBased* > allWBTracks;
190 eventLoop->Get( allWBTracks );
191 vector< const DTrackWireBased* > wbTracks = filterWireBasedTracks( allWBTracks );
196 for( vector< const DFCALCluster* >::const_iterator clItr = fcalClusters.begin();
197 clItr != fcalClusters.end(); ++clItr ){
208 GetCorrectedEnergyAndPosition( cluster , Ecorrected, pos_corrected, errZ, &vertex);
219 cTime += ( timeConst0 + timeConst1 * Ecorrected + timeConst2 * TMath::Power( Ecorrected, 2 ) +
220 timeConst3 * TMath::Power( Ecorrected, 3 ) + timeConst4 * TMath::Power( Ecorrected, 4 ) );
228 FillCovarianceMatrix( shower );
231 printf(
"FCAL shower: E=%f x=%f y=%f z=%f t=%f\n",
233 printf(
"FCAL shower: dE=%f dx=%f dy=%f dz=%f dt=%f\n",
235 printf(
"FCAL shower: Ex=%f Ey=%f Ez=%f Et=%f xy=%f\n",
237 printf(
"FCAL shower: xz=%f xt=%f yz=%f yt=%f zt=%f\n",
255 for(
size_t iTrk = 0; iTrk < wbTracks.size(); ++iTrk ){
257 if( !wbTracks[iTrk]->GetProjection(
SYS_FCAL, projPos, &projMom, &flightTime ) )
continue;
264 fcalFacePos.SetMag( fcalFacePos.Mag() * projPos.Z() / fcalFacePos.Z() );
266 double distance = ( fcalFacePos - projPos ).Mag();
268 if( distance < docaTr ){
274 timeTr = ( wbTracks[iTrk]->position().Z() - vertex.Z() ) /
SPEED_OF_LIGHT + flightTime;
285 vector< const DFCALHit* > fcalHits;
286 cluster->Get( fcalHits );
290 getE1925FromHits( e1e9, e9e25, fcalHits, getMaxHit( fcalHits ) );
298 getUVFromHits( sumU, sumV, fcalHits,
305 shower->AddAssociatedObject( cluster );
307 _data.push_back(shower);
325 float x0 = posInCal.Px();
326 float y0 = posInCal.Py();
330 double Ecutoff = cutoff_energy;
331 double A = linfit_slope;
332 double B = linfit_intercept;
333 double C = expfit_param1;
334 double D = expfit_param2;
335 double E = expfit_param3;
342 if ( Eclust <= Ecutoff ) {
344 Egamma = Eclust/(A*Eclust + B);
348 if ( Eclust > Ecutoff ) {
350 Egamma = Eclust/(C - exp(-D*Eclust+ E));
359 float dxV = x0-vertex->X();
360 float dyV = y0-vertex->Y();
361 float zV = vertex->Z();
363 double z0 = m_FCALfront - zV;
364 double zMax = FCAL_RADIATION_LENGTH*(FCAL_SHOWER_OFFSET
365 + log(Egamma/FCAL_CRITICAL_ENERGY));
367 double zed1 = z0 + zMax;
369 double r0 =
sqrt(dxV*dxV + dyV*dyV );
372 for ( niter=0; niter<100; niter++) {
374 zed = z0 + zMax/
sqrt( 1 + tt*tt );
375 if ( fabs( (zed-zed1) ) < 0.001) {
381 posInCal.SetZ( zed + zV );
386 pos_corrected = posInCal;
397 TAxis *xaxis = CovarianceLookupTable[0][0]->GetXaxis();
398 TAxis *yaxis = CovarianceLookupTable[0][0]->GetYaxis();
399 float minElookup = xaxis->GetBinLowEdge(1);
400 float maxElookup = xaxis->GetBinUpEdge(xaxis->GetNbins());
401 float minthlookup = yaxis->GetBinLowEdge(1);
402 float maxthlookup = yaxis->GetBinUpEdge(yaxis->GetNbins());
408 float shower_r =
sqrt(shower_x*shower_x + shower_y*shower_y);
409 float shower_theta = atan2(shower_r,shower_z);
410 float thlookup = shower_theta/3.14159265*180;
411 float Elookup = shower_E;
414 if (Elookup<minElookup) Elookup=minElookup;
415 if (Elookup>maxElookup) Elookup=maxElookup-0.0001;
416 if (thlookup<minthlookup) thlookup=minthlookup;
417 if (thlookup>maxthlookup) thlookup=maxthlookup-0.0001;
418 if (
VERBOSE>3)
printf(
"(%f,%F) limits (%f,%f) (%f,%f)\n",Elookup,thlookup,minElookup,maxElookup,minthlookup,maxthlookup);
421 for (
int i=0; i<5; i++) {
422 for (
int j=0; j<=i; j++) {
423 float val = CovarianceLookupTable[i][j]->Interpolate(Elookup, thlookup);
424 if (i==0 && j==0) val *= shower_E;
425 ErphiztCovariance(i,j) = ErphiztCovariance(j,i) = val;
429 float shower_phi = atan2(shower_y,shower_x);
430 float cosPhi = cos(shower_phi);
431 float sinPhi =
sin(shower_phi);
433 rotationmatrix(0,0) = 1;
434 rotationmatrix(3,3) = 1;
435 rotationmatrix(4,4) = 1;
436 rotationmatrix(1,1) = cosPhi;
437 rotationmatrix(1,2) = -sinPhi;
438 rotationmatrix(2,1) = sinPhi;
439 rotationmatrix(2,2) = cosPhi;
441 if (
VERBOSE>3) {
printf(
"(E,r,phi,z,t) "); ErphiztCovariance.Print(); }
442 DMatrixDSym &D = ErphiztCovariance.Similarity(rotationmatrix);
443 for (
int i=0; i<5; i++) {
444 for (
int j=0; j<5; j++)
455 std::thread::id this_id = std::this_thread::get_id();
456 stringstream idstring;
458 if (
VERBOSE>0)
printf(
"DFCALShower_factory::LoadCovarianceLookupTables(): Thread %s\n",idstring.str().c_str());
463 if (COVARIANCEFILENAME ==
"") USECCDB=1;
465 map<string,string> covariance_data;
468 if (eventLoop->GetJCalibration()->GetCalib(
"/FCAL/shower_covariance", covariance_data)) {
469 jerr <<
"Error loading /FCAL/shower_covariance !" << endl;
472 if (covariance_data.size() == 15) {
475 for(
auto element : covariance_data) {
476 cout <<
"\nTEST: " << element.first <<
" = " << element.second << endl;
480 jerr <<
"Wrong number of elements /FCAL/shower_covariance !" << endl;
485 for (
int i=0; i<5; i++) {
486 for (
int j=0; j<=i; j++) {
488 japp->RootWriteLock();
490 TDirectory *
savedir = gDirectory;
493 sprintf(histname,
"covariance_%i%i_thread%s",i,j,idstring.str().c_str());
499 stringstream matrixname;
500 matrixname <<
"covmatrix_" << i << j;
501 if (
VERBOSE>1) cout <<
"Using CCDB \"" << matrixname.str() <<
"\" " << covariance_data[matrixname.str()] << endl;
502 ss.str(covariance_data[matrixname.str()]);
505 sprintf(filename,
"%s%i%i.txt",COVARIANCEFILENAME.c_str(),i,j);
506 if (
VERBOSE>0) cout << filename << std::endl;
508 if (! ifs.is_open()) {
509 jerr <<
" Error: Cannot open file! " << filename << std::endl;
512 getline(ifs, line,
'\n');
514 if (
VERBOSE>1) cout << filename <<
" dump: " <<line<<endl;
519 CovarianceLookupTable[i][j] =
new TH2F(histname,
"Covariance histogram",10,0,12,10,0,12);
520 CovarianceLookupTable[i][j]->SetDirectory(
nullptr);
526 if (
VERBOSE>1)
printf(
"parsed dump: bins (%i,%i)\n",nxbins,nybins);
527 Float_t xbins[nxbins+1];
528 Float_t ybins[nybins+1];
529 for (
int count=0; count<=nxbins; count++) {
534 for (
int count=0; count<=nybins; count++) {
543 CovarianceLookupTable[i][j] =
new TH2F(histname,
"Covariance histogram",nxbins,xbins,nybins,ybins);
544 CovarianceLookupTable[i][j]->SetDirectory(
nullptr);
547 if (
VERBOSE>1)
printf(
"(%i,%i) (%i,%i) %e ",i,j,xbin,ybin,cont);
548 CovarianceLookupTable[i][j]->SetBinContent(xbin,ybin,cont);
550 if (ybin>nybins) { xbin++; ybin=1; }
566 unsigned int maxIndex = 0;
570 for( vector< const DFCALHit* >::const_iterator hit = hitVec.begin();
571 hit != hitVec.end(); ++hit ){
573 if( (**hit).E > eMaxSh ){
576 maxIndex = hit - hitVec.begin();
585 const vector< const DFCALHit* >& hits,
595 DVector3 u = ( showerVec - trackVec ).Unit();
606 for( vector< const DFCALHit* >::const_iterator hit = hits.begin();
607 hit != hits.end(); ++hit ){
609 hitLoc.SetX( (**hit).x - showerVec.X() );
610 hitLoc.SetY( (**hit).y - showerVec.Y() );
612 sumUSh += (**hit).E * pow( u.Dot( hitLoc ), 2 );
613 sumVSh += (**hit).E * pow( v.Dot( hitLoc ), 2 );
624 const vector< const DFCALHit* >& hits,
625 unsigned int maxIndex )
const {
630 const DFCALHit* maxHit = hits[maxIndex];
632 for( vector< const DFCALHit* >::const_iterator hit = hits.begin();
633 hit != hits.end(); ++hit ){
635 if( fabs( (**hit).x - maxHit->
x ) < 4.5 && fabs( (**hit).y - maxHit->
y ) < 4.5 )
638 if( fabs( (**hit).x - maxHit->
x ) < 8.5 && fabs( (**hit).y - maxHit->
y ) < 8.5 )
642 e1e9Sh = maxHit->
E/E9;
647 vector< const DTrackWireBased* >
650 vector< const DTrackWireBased* > finalTracks;
651 map< unsigned int, vector< const DTrackWireBased* > > sortedTracks;
656 for(
unsigned int i = 0; i < wbTracks.size(); ++i ){
658 unsigned int id = wbTracks[i]->candidateid;
660 if( sortedTracks.find(
id ) == sortedTracks.end() ){
662 sortedTracks[id] = vector< const DTrackWireBased* >();
665 sortedTracks[id].push_back( wbTracks[i] );
672 for( map<
unsigned int, vector< const DTrackWireBased* > >::const_iterator
673 anId = sortedTracks.begin();
674 anId != sortedTracks.end(); ++anId ){
677 unsigned int bestIndex = 0;
679 for(
unsigned int i = 0; i < anId->second.size(); ++i ){
681 if( anId->second[i]->Ndof < 15 )
continue;
683 if( anId->second[i]->FOM > maxFOM ){
685 maxFOM = anId->second[i]->FOM;
690 finalTracks.push_back( anId->second[bestIndex] );
void setDocaTrack(const double docaTrack)
void setTimeTrack(const double tTrack)
DVector3 getCentroid() const
static double blockLength()
void setE9E25(const double e9e25)
sprintf(text,"Post KinFit Cut")
jerror_t brun(JEventLoop *loop, int32_t runnumber)
double getTimeEWeight() const
jerror_t LoadCovarianceLookupTables(JEventLoop *eventLoop)
void setTime(const double time)
void getE1925FromHits(double &e1e9Sh, double &e9e25Sh, const vector< const DFCALHit * > &hits, unsigned int maxIndex) const
void setSumU(const double sumU)
bool GetFCALZ(double &z_fcal) const
z-location of front face of CCAL in cm
unsigned int getMaxHit(const vector< const DFCALHit * > &hitVec) const
void getUVFromHits(double &sumUSh, double &sumVSh, const vector< const DFCALHit * > &hits, const DVector3 &showerVec, const DVector3 &trackVec) const
void setPosition(const DVector3 &aPosition)
TMatrixFSym ExyztCovariance
DGeometry * GetDGeometry(unsigned int run_number)
void setEnergy(const double energy)
void setE1E9(const double e1e9)
void GetCorrectedEnergyAndPosition(const DFCALCluster *cluster, double &Ecorrected, DVector3 &pos_corrected, double &errZ, const DVector3 *aVertex)
jerror_t evnt(JEventLoop *eventLoop, uint64_t eventnumber)
Invoked via JEventProcessor virtual method.
jerror_t FillCovarianceMatrix(DFCALShower *shower)
printf("string=%s", string)
void setSumV(const double sumV)
DVector3 getPosition() const
vector< const DTrackWireBased * > filterWireBasedTracks(vector< const DTrackWireBased * > &wbTracks) const
bool GetTargetZ(double &z_target) const
z-location of center of target
void setNumBlocks(const int numBlocks)