// $Id$ // // Author: David Lawrence June 24, 2004 // // // DEventSourceHDDM methods // #include #include using namespace std; #include "DEventSourceHDDM.h" #include "JANA/JFactory_base.h" #include "JANA/JEventLoop.h" #include "JANA/JEvent.h" //------------------------------------------------------------------ // Binary predicate used to sort hits //------------------------------------------------------------------ class MCTrackHitSort{ public: bool operator()(DMCTrackHit* const &thit1, DMCTrackHit* const &thit2) const { return thit1->z < thit2->z; } }; bool MCTrackHitSort_C(DMCTrackHit* const &thit1, DMCTrackHit* const &thit2) { return thit1->z < thit2->z; } //---------------- // Constructor //---------------- DEventSourceHDDM::DEventSourceHDDM(const char* source_name):JEventSource(source_name) { /// Constructor for DEventSourceHDDM object fin = open_s_HDDM((char*)source_name); hddm_s = NULL; if(fin)source_is_open = 1; flush_on_free = true; } //---------------- // Destructor //---------------- DEventSourceHDDM::~DEventSourceHDDM() { // Close file and set pointers to NULL if(fin)close_s_HDDM(fin); hddm_s = NULL; fin = NULL; } //---------------- // GetEvent //---------------- jerror_t DEventSourceHDDM::GetEvent(JEvent &event) { /// Implementation of JEventSource virtual function if(!fin){ return EVENT_SOURCE_NOT_OPEN; } hddm_s = read_s_HDDM(fin); ++Nevents_read; // each open HDDM file takes up about 1M of memeory so it's // worthwhile to close it as soon as we can if(!hddm_s){ if(fin)close_s_HDDM(fin); fin = NULL; } int event_number = -1; int run_number = -1; // Copy the reference info into the JEvent object event.SetJEventSource(this); event.SetEventNumber(event_number); event.SetRunNumber(run_number); event.SetRef(hddm_s); return hddm_s==NULL ? NO_MORE_EVENTS_IN_SOURCE:NOERROR; } //---------------- // FreeEvent //---------------- void DEventSourceHDDM::FreeEvent(JEvent &event) { s_HDDM_t *my_hddm_s = (s_HDDM_t*)event.GetRef(); if(flush_on_free && my_hddm_s!=NULL)flush_s_HDDM(my_hddm_s, 0); } //---------------- // GetObjects //---------------- jerror_t DEventSourceHDDM::GetObjects(JEvent &event, JFactory_base *factory) { /// This gets called through the virtual method of the /// JEventSource base class. It creates the objects of the type /// which factory is based. It uses the s_HDDM_t object /// kept in the ref field of the JEvent object passed. // We must have a factory to hold the data if(!factory)throw RESOURCE_UNAVAILABLE; // HDDM doesn't support tagged factories if(strcmp(factory->Tag(), ""))return OBJECT_NOT_AVAILABLE; // The ref field of the JEvent is just the s_HDDM_t pointer. s_HDDM_t *my_hddm_s = (s_HDDM_t*)event.GetRef(); if(!my_hddm_s)throw RESOURCE_UNAVAILABLE; // Get name of data class we're trying to extract string dataClassName = factory->dataClassName(); if(dataClassName =="DMCTrackHit") return Extract_DMCTrackHit(my_hddm_s, dynamic_cast*>(factory)); if(dataClassName =="DMCThrown") return Extract_DMCThrown(my_hddm_s, dynamic_cast*>(factory)); return OBJECT_NOT_AVAILABLE; } //------------------ // Extract_DMCTrackHit //------------------ jerror_t DEventSourceHDDM::Extract_DMCTrackHit(s_HDDM_t *hddm_s, JFactory *factory) { /// Copies the data from the given hddm_s structure. This is called /// from JEventSourceHDDM::GetObjects. If factory is NULL, this /// returns OBJECT_NOT_AVAILABLE immediately. if(factory==NULL)return OBJECT_NOT_AVAILABLE; // The following routines will create DMCTrackHit objects and add them // to data. vector data; GetCDCHits(hddm_s, data); GetFDCHits(hddm_s, data); GetBCALHits(hddm_s, data); GetTOFHits(hddm_s, data); GetCherenkovHits(hddm_s, data); GetFCALHits(hddm_s, data); GetUPVHits(hddm_s, data); // sort hits by z sort(data.begin(), data.end(), MCTrackHitSort_C); // Some systems will use negative phis. Force them all to // be in the 0 to 2pi range for(unsigned int i=0;iphi<0.0)mctrackhit->phi += 2.0*M_PI; } // Copy into factory factory->CopyTo(data); return NOERROR; } //------------------- // GetCDCHits //------------------- jerror_t DEventSourceHDDM::GetCDCHits(s_HDDM_t *hddm_s, vector& data) { // Loop over Physics Events s_PhysicsEvents_t* PE = hddm_s->physicsEvents; if(!PE) return NOERROR; for(unsigned int i=0; imult; i++){ s_HitView_t *hits = PE->in[i].hitView; if (hits == HDDM_NULL || hits->centralDC == HDDM_NULL || hits->centralDC->cdcTruthPoints == HDDM_NULL)continue; s_CdcTruthPoints_t *cdctruthpoints = hits->centralDC->cdcTruthPoints; s_CdcTruthPoint_t *cdctruthpoint = cdctruthpoints->in; for(unsigned int j=0; jmult; j++, cdctruthpoint++){ DMCTrackHit *mctrackhit = new DMCTrackHit; mctrackhit->r = cdctruthpoint->r; mctrackhit->phi = cdctruthpoint->phi; mctrackhit->z = cdctruthpoint->z; mctrackhit->track = cdctruthpoint->track; mctrackhit->primary = cdctruthpoint->primary; mctrackhit->system = SYS_CDC; data.push_back(mctrackhit); } } return NOERROR; } //------------------- // GetFDCHits //------------------- jerror_t DEventSourceHDDM::GetFDCHits(s_HDDM_t *hddm_s, vector& data) { // Loop over Physics Events s_PhysicsEvents_t* PE = hddm_s->physicsEvents; if(!PE) return NOERROR; for(unsigned int i=0; imult; i++){ s_HitView_t *hits = PE->in[i].hitView; if (hits == HDDM_NULL || hits->forwardDC == HDDM_NULL || hits->forwardDC->fdcChambers == HDDM_NULL)continue; s_FdcChambers_t* fdcChambers = hits->forwardDC->fdcChambers; s_FdcChamber_t *fdcChamber = fdcChambers->in; for(unsigned int j=0; jmult; j++, fdcChamber++){ s_FdcTruthPoints_t *fdcTruthPoints = fdcChamber->fdcTruthPoints; if(fdcTruthPoints == HDDM_NULL)continue; s_FdcTruthPoint_t *truth = fdcTruthPoints->in; for(unsigned int k=0; kmult; k++, truth++){ float x = truth->x; float y = truth->y; DMCTrackHit *mctrackhit = new DMCTrackHit; mctrackhit->r = sqrt(x*x + y*y); mctrackhit->phi = atan2(y,x); mctrackhit->z = truth->z; mctrackhit->track = truth->track; mctrackhit->primary = truth->primary; mctrackhit->system = SYS_FDC; data.push_back(mctrackhit); } } } return NOERROR; } //------------------- // GetBCALHits //------------------- jerror_t DEventSourceHDDM::GetBCALHits(s_HDDM_t *hddm_s, vector& data) { // Loop over Physics Events s_PhysicsEvents_t* PE = hddm_s->physicsEvents; if(!PE) return NOERROR; for(unsigned int i=0; imult; i++){ s_HitView_t *hits = PE->in[i].hitView; if (hits == HDDM_NULL || hits->barrelEMcal == HDDM_NULL || hits->barrelEMcal->bcalTruthShowers == HDDM_NULL)continue; s_BcalTruthShowers_t *bcalTruthShowers = hits->barrelEMcal->bcalTruthShowers; s_BcalTruthShower_t *bcalTruthShower = bcalTruthShowers->in; for(unsigned int j=0; jmult; j++, bcalTruthShower++){ DMCTrackHit *mctrackhit = new DMCTrackHit; mctrackhit->r = bcalTruthShower->r; mctrackhit->phi = bcalTruthShower->phi; mctrackhit->z = bcalTruthShower->z; mctrackhit->track = bcalTruthShower->track; mctrackhit->primary = bcalTruthShower->primary; mctrackhit->system = SYS_BCAL; data.push_back(mctrackhit); } } return NOERROR; } //------------------- // GetTOFHits //------------------- jerror_t DEventSourceHDDM::GetTOFHits(s_HDDM_t *hddm_s, vector& data) { // Loop over Physics Events s_PhysicsEvents_t* PE = hddm_s->physicsEvents; if(!PE) return NOERROR; for(unsigned int i=0; imult; i++){ s_HitView_t *hits = PE->in[i].hitView; if (hits == HDDM_NULL || hits->forwardTOF == HDDM_NULL || hits->forwardTOF->ftofTruthPoints == HDDM_NULL)continue; s_FtofTruthPoints_t *ftoftruthpoints = hits->forwardTOF->ftofTruthPoints; s_FtofTruthPoint_t *ftoftruthpoint = ftoftruthpoints->in; for(unsigned int j=0; jmult; j++, ftoftruthpoint++){ float x = ftoftruthpoint->x; float y = ftoftruthpoint->y; DMCTrackHit *mctrackhit = new DMCTrackHit; mctrackhit->r = sqrt(x*x + y*y); mctrackhit->phi = atan2(y,x); mctrackhit->z = ftoftruthpoint->z; mctrackhit->track = ftoftruthpoint->track; mctrackhit->primary = ftoftruthpoint->primary; mctrackhit->system = SYS_TOF; data.push_back(mctrackhit); } } return NOERROR; } //------------------- // GetCherenkovHits //------------------- jerror_t DEventSourceHDDM::GetCherenkovHits(s_HDDM_t *hddm_s, vector& data) { return NOERROR; } //------------------- // GetFCALHits //------------------- jerror_t DEventSourceHDDM::GetFCALHits(s_HDDM_t *hddm_s, vector& data) { // Loop over Physics Events s_PhysicsEvents_t* PE = hddm_s->physicsEvents; if(!PE) return NOERROR; for(unsigned int i=0; imult; i++){ s_HitView_t *hits = PE->in[i].hitView; if (hits == HDDM_NULL || hits->forwardEMcal == HDDM_NULL || hits->forwardEMcal->fcalTruthShowers == HDDM_NULL)continue; s_FcalTruthShowers_t *fcalTruthShowers = hits->forwardEMcal->fcalTruthShowers; s_FcalTruthShower_t *fcalTruthShower = fcalTruthShowers->in; for(unsigned int j=0; jmult; j++, fcalTruthShower++){ float x = fcalTruthShower->x; float y = fcalTruthShower->y; DMCTrackHit *mctrackhit = new DMCTrackHit; mctrackhit->r = sqrt(x*x + y*y); mctrackhit->phi = atan2(y,x); mctrackhit->z = fcalTruthShower->z; mctrackhit->track = fcalTruthShower->track; mctrackhit->primary = fcalTruthShower->primary; mctrackhit->system = SYS_FCAL; data.push_back(mctrackhit); } } return NOERROR; } //------------------- // GetUPVHits //------------------- jerror_t DEventSourceHDDM::GetUPVHits(s_HDDM_t *hddm_s, vector& data) { return NOERROR; } //------------------ // Extract_DMCThrown //------------------ jerror_t DEventSourceHDDM::Extract_DMCThrown(s_HDDM_t *hddm_s, JFactory *factory) { /// Copies the data from the given hddm_s structure. This is called /// from JEventSourceHDDM::GetObjects. If factory is NULL, this /// returns OBJECT_NOT_AVAILABLE immediately. if(factory==NULL)return OBJECT_NOT_AVAILABLE; vector data; // Loop over Physics Events s_PhysicsEvents_t* PE = hddm_s->physicsEvents; if(!PE) return NOERROR; for(unsigned int i=0; imult; i++){ // ------------ Reactions -------------- s_Reactions_t *reactions=PE->in[i].reactions; if(!reactions)continue; for(unsigned int j=0; jmult; j++){ s_Vertices_t *vertices = reactions->in[j].vertices; if(vertices){ for(unsigned int k=0; kmult; k++){ s_Origin_t *origin = vertices->in[k].origin; s_Products_t *products = vertices->in[k].products; if(products && origin){ for(unsigned int m=0;mmult;m++){ s_Product_t *product = &products->in[m]; DMCThrown *mcthrown = new DMCThrown; mcthrown->x = origin->vx; mcthrown->y = origin->vy; mcthrown->z = origin->vz; mcthrown->type = product->type; mcthrown->q = (float)ParticleCharge(product->type); mcthrown->mass = ParticleMass(product->type); mcthrown->E = product->momentum->E; double px = product->momentum->px; double py = product->momentum->py; double pz = product->momentum->pz; mcthrown->p = sqrt(px*px + py*py + pz*pz); mcthrown->phi = atan2(py, px); if(mcthrown->phi<0.0)mcthrown->phi += 2.0*M_PI; mcthrown->theta = acos(pz/mcthrown->p); data.push_back(mcthrown); } } } } } } // Copy into factory factory->CopyTo(data); return NOERROR; }