void PHG4SvtxClusterizer::ClusterCylinderCells(PHCompositeNode *topNode) { //---------- // Get Nodes //---------- // get the SVX geometry object PHG4CylinderCellGeomContainer* geom_container = findNode::getClass<PHG4CylinderCellGeomContainer>(topNode,"CYLINDERCELLGEOM_SVTX"); if (!geom_container) return; PHG4HitContainer* g4hits = findNode::getClass<PHG4HitContainer>(topNode,"G4HIT_SVTX"); if (!g4hits) return; PHG4CylinderCellContainer* cells = findNode::getClass<PHG4CylinderCellContainer>(topNode,"G4CELL_SVTX"); if (!cells) return; //----------- // Clustering //----------- // sort hits layer by layer std::multimap<int,SvtxHit*> layer_hits_mmap; for (SvtxHitMap::Iter iter = _hits->begin(); iter != _hits->end(); ++iter) { SvtxHit* hit = &iter->second; layer_hits_mmap.insert(make_pair(hit->get_layer(),hit)); } // loop over cylinder layers PHG4CylinderCellGeomContainer::ConstRange layerrange = geom_container->get_begin_end(); for(PHG4CylinderCellGeomContainer::ConstIterator layeriter = layerrange.first; layeriter != layerrange.second; ++layeriter) { int layer = layeriter->second->get_layer(); int nphibins = layeriter->second->get_phibins(); // loop over all hits/cells in this layer std::map<PHG4CylinderCell*,SvtxHit*> cell_hit_map; std::vector<PHG4CylinderCell*> cell_list; for (std::multimap<int,SvtxHit*>::iterator hiter = layer_hits_mmap.lower_bound(layer); hiter != layer_hits_mmap.upper_bound(layer); ++hiter) { SvtxHit* hit = hiter->second; PHG4CylinderCell* cell = cells->findCylinderCell(hit->get_cellid()); cell_list.push_back(cell); cell_hit_map.insert(make_pair(cell,hit)); } if (cell_list.size() == 0) continue; // if no cells, go to the next layer sort(cell_list.begin(), cell_list.end(), PHG4SvtxClusterizer::lessthan); typedef adjacency_list <vecS, vecS, undirectedS> Graph; typedef graph_traits<Graph>::vertex_descriptor Vertex; Graph G; for(unsigned int i=0; i<cell_list.size(); i++) { for(unsigned int j=i+1; j<cell_list.size(); j++) { if( are_adjacent(cell_list[i], cell_list[j], nphibins) ) add_edge(i,j,G); } add_edge(i,i,G); } // Find the connections between the vertices of the graph (vertices are the rawhits, // connections are made when they are adjacent to one another) vector<int> component(num_vertices(G)); // this is the actual clustering, performed by boost connected_components(G, &component[0]); // Loop over the components(hit cells) compiling a list of the // unique connected groups (ie. clusters). set<int> cluster_ids; // unique components multimap<int, PHG4CylinderCell*> clusters; for (unsigned int i=0; i<component.size(); i++) { cluster_ids.insert( component[i] ); clusters.insert( make_pair(component[i], cell_list[i]) ); } typedef multimap<int, PHG4CylinderCell*>::iterator mapiterator; for (set<int>::iterator clusiter = cluster_ids.begin(); clusiter != cluster_ids.end(); clusiter++ ) { int clusid = *clusiter; pair<mapiterator,mapiterator> clusrange = clusters.equal_range(clusid); mapiterator mapiter = clusrange.first; int layer = mapiter->second->get_layer(); PHG4CylinderCellGeom* geom = geom_container->GetLayerCellGeom(layer); SvtxCluster clus; clus.set_layer( layer ); float clus_energy = 0.0; unsigned int clus_adc = 0; // determine the size of the cluster in phi and z // useful for track fitting the cluster set<int> phibins; set<int> zbins; for (mapiter = clusrange.first; mapiter != clusrange.second; mapiter++ ) { PHG4CylinderCell* cell = mapiter->second; phibins.insert(cell->get_binphi()); zbins.insert(cell->get_binz()); } float pitch = geom->get_phistep()*geom->get_radius(); float thickness = geom->get_thickness(); float length = geom->get_zstep(); float phisize = phibins.size()*pitch; float zsize = zbins.size()*length; double xsum = 0.0; double ysum = 0.0; double zsum = 0.0; unsigned int nhits = 0; for(mapiter = clusrange.first; mapiter != clusrange.second; mapiter++ ) { PHG4CylinderCell* cell = mapiter->second; SvtxHit* hit = cell_hit_map[cell]; clus.insert_hit(hit->get_id()); clus_energy += hit->get_e(); clus_adc += hit->get_adc(); // compute the hit center double r = geom->get_radius(); double phi = geom->get_phicenter(cell->get_binphi()); double x = r*cos(phi); double y = r*sin(phi); double z = geom->get_zcenter(cell->get_binz()); if (_make_e_weights[layer]) { xsum += x * hit->get_adc(); ysum += y * hit->get_adc(); zsum += z * hit->get_adc(); } else { xsum += x; ysum += y; zsum += z; } ++nhits; } double clusx = NAN; double clusy = NAN; double clusz = NAN; if (_make_e_weights[layer]) { clusx = xsum / clus_adc; clusy = ysum / clus_adc; clusz = zsum / clus_adc; } else { clusx = xsum / nhits; clusy = ysum / nhits; clusz = zsum / nhits; } double radius = sqrt(clusx*clusx+clusy*clusy); double clusphi = atan2( clusy, clusx); clus.set_position( 0 , clusx ); clus.set_position( 1 , clusy ); clus.set_position( 2 , clusz ); clus.set_e(clus_energy); clus.set_adc(clus_adc); float invsqrt12 = 1.0/sqrt(12.); TMatrixF DIM(3,3); DIM[0][0] = pow(0.5*thickness,2); DIM[0][1] = 0.0; DIM[0][2] = 0.0; DIM[1][0] = 0.0; DIM[1][1] = pow(0.5*phisize,2); DIM[1][2] = 0.0; DIM[2][0] = 0.0; DIM[2][1] = 0.0; DIM[2][2] = pow(0.5*zsize,2); TMatrixF ERR(3,3); ERR[0][0] = pow(0.5*thickness*invsqrt12,2); ERR[0][1] = 0.0; ERR[0][2] = 0.0; ERR[1][0] = 0.0; ERR[1][1] = pow(0.5*phisize*invsqrt12,2); ERR[1][2] = 0.0; ERR[2][0] = 0.0; ERR[2][1] = 0.0; ERR[2][2] = pow(0.5*zsize*invsqrt12,2); TMatrixF ROT(3,3); ROT[0][0] = cos(clusphi); ROT[0][1] = -sin(clusphi); ROT[0][2] = 0.0; ROT[1][0] = sin(clusphi); ROT[1][1] = cos(clusphi); ROT[1][2] = 0.0; ROT[2][0] = 0.0; ROT[2][1] = 0.0; ROT[2][2] = 1.0; TMatrixF ROT_T(3,3); ROT_T.Transpose(ROT); TMatrixF COVAR_DIM(3,3); COVAR_DIM = ROT * DIM * ROT_T; clus.set_size( 0 , 0 , COVAR_DIM[0][0] ); clus.set_size( 0 , 1 , COVAR_DIM[0][1] ); clus.set_size( 0 , 2 , COVAR_DIM[0][2] ); clus.set_size( 1 , 0 , COVAR_DIM[1][0] ); clus.set_size( 1 , 1 , COVAR_DIM[1][1] ); clus.set_size( 1 , 2 , COVAR_DIM[1][2] ); clus.set_size( 2 , 0 , COVAR_DIM[2][0] ); clus.set_size( 2 , 1 , COVAR_DIM[2][1] ); clus.set_size( 2 , 2 , COVAR_DIM[2][2] ); TMatrixF COVAR_ERR(3,3); COVAR_ERR = ROT * ERR * ROT_T; clus.set_error( 0 , 0 , COVAR_ERR[0][0] ); clus.set_error( 0 , 1 , COVAR_ERR[0][1] ); clus.set_error( 0 , 2 , COVAR_ERR[0][2] ); clus.set_error( 1 , 0 , COVAR_ERR[1][0] ); clus.set_error( 1 , 1 , COVAR_ERR[1][1] ); clus.set_error( 1 , 2 , COVAR_ERR[1][2] ); clus.set_error( 2 , 0 , COVAR_ERR[2][0] ); clus.set_error( 2 , 1 , COVAR_ERR[2][1] ); clus.set_error( 2 , 2 , COVAR_ERR[2][2] ); if (clus_energy > get_threshold_by_layer(layer)) { SvtxCluster* ptr = _clusterlist->insert(clus); if (!ptr->IsValid()) { static bool first = true; if (first) { cout << PHWHERE << "ERROR: Invalid SvtxClusters are being produced" << endl; ptr->identify(); first = false; } } if (verbosity>1) { cout << "r=" << radius << " phi=" << clusphi << " z=" << clusz << endl; cout << "pos=(" << clus.get_position(0) << ", " << clus.get_position(1) << ", " << clus.get_position(2) << ")" << endl; cout << endl; } } else if (verbosity>1) { cout << "removed r=" << radius << " phi=" << clusphi << " z=" << clusz << endl; cout << "pos=(" << clus.get_position(0) << ", " << clus.get_position(1) << ", " << clus.get_position(2) << ")" << endl; cout << endl; } } } return; }