.. _program_listing_file_larflow_KeyPoints_LoaderKeypointData.cxx: Program Listing for File LoaderKeypointData.cxx =============================================== |exhale_lsh| :ref:`Return to documentation for file ` (``larflow/KeyPoints/LoaderKeypointData.cxx``) .. |exhale_lsh| unicode:: U+021B0 .. UPWARDS ARROW WITH TIP LEFTWARDS .. code-block:: cpp #include "LoaderKeypointData.h" #include namespace larflow { namespace keypoints { bool LoaderKeypointData::_setup_numpy = false; LoaderKeypointData::LoaderKeypointData( std::vector& input_v ) : ttriplet(nullptr), tkeypoint(nullptr), tssnet(nullptr) { input_files.clear(); input_files = input_v; load_tree(); } LoaderKeypointData::~LoaderKeypointData() { if ( ttriplet ) delete ttriplet; if ( tkeypoint) delete tkeypoint; if ( tssnet ) delete tssnet; } void LoaderKeypointData::load_tree() { std::cout << "[LoaderKeypointData::load_tree()]" << std::endl; ttriplet = new TChain("larmatchtriplet"); tkeypoint = new TChain("keypointlabels"); tssnet = new TChain("ssnetlabels"); for (auto const& infile : input_files ) { std::cout << "add " << infile << " to chains" << std::endl; ttriplet->Add(infile.c_str()); tkeypoint->Add(infile.c_str()); tssnet->Add(infile.c_str()); } triplet_v = 0; kplabel_v[0] = 0; kplabel_v[1] = 0; kplabel_v[2] = 0; ssnet_label_v = 0; ssnet_weight_v = 0; ttriplet->SetBranchAddress( "triplet_v", &triplet_v ); tkeypoint->SetBranchAddress( "kplabel_nuvertex", &kplabel_v[0] ); tkeypoint->SetBranchAddress( "kplabel_trackends", &kplabel_v[1] ); tkeypoint->SetBranchAddress( "kplabel_showerstart", &kplabel_v[2] ); tssnet->SetBranchAddress( "trackshower_label_v", &ssnet_label_v ); tssnet->SetBranchAddress( "trackshower_weight_v", &ssnet_weight_v ); } unsigned long LoaderKeypointData::load_entry( int entry ) { unsigned long bytes = ttriplet->GetEntry(entry); bytes = tssnet->GetEntry(entry); bytes = tkeypoint->GetEntry(entry); return bytes; } unsigned long LoaderKeypointData::GetEntries() { return ttriplet->GetEntries(); } PyObject* LoaderKeypointData::sample_data( const int& num_max_samples, int& nfilled, bool withtruth ) { if ( !_setup_numpy ) { import_array1(0); _setup_numpy = true; } int index_col = (withtruth) ? 4 : 3; // make match index array //std::cout << "make triplets" << std::endl; PyArrayObject* matches = (PyArrayObject*)triplet_v->at(0).sample_triplet_matches( num_max_samples, nfilled, withtruth ); // count npos, nneg examples // also make list of indices of positive examples, these are the ones we will evaluate ssnet not int npos=0; int nneg=0; std::vector pos_index_v; for (size_t i=0; i& pos_match_index, PyArrayObject* match_array, PyArrayObject*& ssnet_label, PyArrayObject*& ssnet_top_weight, PyArrayObject*& ssnet_class_weight ) { int index_col = (withtruth) ? 4 : 3; // make ssnet label array int ssnet_label_nd = 1; npy_intp ssnet_label_dims1[] = { (long)pos_match_index.size() }; npy_intp ssnet_label_dims2[] = { (long)pos_match_index.size() }; npy_intp ssnet_label_dims3[] = { (long)pos_match_index.size() }; if ( !_exclude_neg_examples ) { // we're going to load negative triplet examples too ssnet_label_dims1[0] = num_max_samples; ssnet_label_dims2[0] = num_max_samples; ssnet_label_dims3[0] = num_max_samples; } ssnet_label = (PyArrayObject*)PyArray_SimpleNew( ssnet_label_nd, ssnet_label_dims1, NPY_LONG ); ssnet_top_weight = (PyArrayObject*)PyArray_SimpleNew( ssnet_label_nd, ssnet_label_dims2, NPY_FLOAT ); ssnet_class_weight = (PyArrayObject*)PyArray_SimpleNew( ssnet_label_nd, ssnet_label_dims3, NPY_FLOAT ); int nbg = 0; int ntrack = 0; int nshower = 0; for ( int i=0; i<(int)ssnet_label_dims1[0]; i++ ) { // get the sample index int idx = (_exclude_neg_examples ) ? pos_match_index[i] : i; // get the triplet index long index = *((long*)PyArray_GETPTR2(match_array,idx,index_col)); // ssnet label int label = ssnet_label_v->at( index ); if ( label==0 ) nbg++; else if (label==1) ntrack++; else if (label==2) nshower++; *((long*)PyArray_GETPTR1(ssnet_label,i)) = (long)label; *((float*)PyArray_GETPTR1(ssnet_top_weight,i)) = (float)ssnet_weight_v->at( index ); } int ntot = nbg+ntrack+nshower; float w_bg = (nbg) ? float(ntot)/float(nbg) : 0.; float w_tr = (ntrack) ? float(ntot)/float(ntrack) : 0.; float w_sh = (nshower) ? float(ntot)/float(nshower) : 0.; float w_norm = nbg*w_bg + ntrack*w_tr + nshower*w_sh; for ( int i=0; i<(int)ssnet_label_dims1[0]; i++ ) { long label = *((long*)PyArray_GETPTR1(ssnet_label,i)); if ( label==0 ) *((float*)PyArray_GETPTR1(ssnet_class_weight,i)) = w_bg/w_norm; else if ( label==1 ) *((float*)PyArray_GETPTR1(ssnet_class_weight,i)) = w_tr/w_norm; else if ( label==2 ) *((float*)PyArray_GETPTR1(ssnet_class_weight,i)) = w_sh/w_norm; } return 0; } int LoaderKeypointData::make_kplabel_arrays( const int& num_max_samples, int& nfilled, bool withtruth, std::vector& pos_match_index, PyArrayObject* match_array, PyArrayObject*& kplabel_label, PyArrayObject*& kplabel_weight ) { int index_col = (withtruth) ? 4 : 3; float sigma = 10.0; // cm int kplabel_nd = 2; int nclasses = 3; npy_intp kplabel_dims[] = { (long)pos_match_index.size(), (long)nclasses }; if ( !_exclude_neg_examples ) { kplabel_dims[0] = num_max_samples; } //std::cout << "make kplabel: " << kplabel_dims[0] << std::endl; kplabel_label = (PyArrayObject*)PyArray_SimpleNew( kplabel_nd, kplabel_dims, NPY_FLOAT ); std::vector npos(nclasses,0); std::vector nneg(nclasses,0); for (int i=0; i<(int)kplabel_dims[0]; i++ ) { // sample array index int idx = (_exclude_neg_examples) ? pos_match_index[i] : (int)i; // triplet index long index = *((long*)PyArray_GETPTR2(match_array,idx,index_col)); for (int c=0; cat( index )[0]; if (label==1) { // make soft label float dist = 0.; for (int j=0; j<3; j++) { float dx = kplabel_v[c]->at( index )[1+j]; dist += dx*dx; } *((float*)PyArray_GETPTR2(kplabel_label,i,c)) = exp( -dist/(sigma*sigma) ); npos[c]++; } else { // zero label *((float*)PyArray_GETPTR2(kplabel_label,i,c)) = 0.0; nneg[c]++; } } } // weights to balance positive and negative examples int kpweight_nd = 2; npy_intp kpweight_dims[] = { (long)pos_match_index.size(), nclasses }; if ( !_exclude_neg_examples ) kpweight_dims[0] = num_max_samples; kplabel_weight = (PyArrayObject*)PyArray_SimpleNew( kpweight_nd, kpweight_dims, NPY_FLOAT ); for (int c=0; cat( index )[1+j]; } else{ for (int j=0; j<3; j++ ) *((float*)PyArray_GETPTR3(kpshift_label,i,c,j)) = 0.; } } } return 0; } } }