diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index 21606db70..aca10d42b 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -4,6 +4,6 @@ Please provide a detailed description of the changes this pull request introduce - [X] Have you added a label? (bug/enhancement/physics etc.) - [X] Have you assigned at least 1 reviewer? - [X] Is this PR related to an open issue / project? -- [X] Does this PR affect CAF data format? If so, please assign a CAF maintainer as additional reviewer. +- [X] Does this PR affect CAF data format? If so, please assign a CAF maintainer as additional reviewer. @PetrilloAtWork and @tjones79 - [X] Does this PR require merging another PR in a different repository (such as sbnanobj/sbnobj etc.)? If so, please link it in the description. - [X] Are you submitting this PR on behalf of someone else who made the code changes? If so, please mention them in the description. diff --git a/CMakeLists.txt b/CMakeLists.txt index 8591bc48b..01e159952 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,12 +16,13 @@ cmake_minimum_required(VERSION 3.20 FATAL_ERROR) find_package(cetmodules 3.20.00 REQUIRED) -project(sbncode VERSION 10.06.00.01 LANGUAGES CXX) +project(sbncode LANGUAGES CXX) message(STATUS "\n\n ========================== ${PROJECT_NAME} ==========================") include(CetCMakeEnv) +set(${PROJECT_NAME}_CMAKE_PROJECT_VERSION_STRING 10.06.00.14-lf01) cet_cmake_env() # The specification of the C++ standard is required because CET macros @@ -77,6 +78,7 @@ find_package( CLHEP REQUIRED ) find_package( ROOT REQUIRED ) find_package( Geant4 REQUIRED ) find_package( Boost COMPONENTS system filesystem REQUIRED ) +find_package( geant4reweight REQUIRED ) # macros for dictionary and simple_plugin include(ArtDictionary) diff --git a/fcl/caf/cafmaker_common_defs.fcl b/fcl/caf/cafmaker_common_defs.fcl index 66fcdf7ab..b4d45abbf 100644 --- a/fcl/caf/cafmaker_common_defs.fcl +++ b/fcl/caf/cafmaker_common_defs.fcl @@ -1,3 +1,4 @@ +#include "eventweight_geant4_sbn.fcl" #include "eventweight_genie_sbn.fcl" #include "eventweight_flux_sbn.fcl" @@ -15,6 +16,7 @@ cafmaker_common_producers: { rns: { module_type: "RandomNumberSaver" } genieweight: @local::sbn_eventweight_genie fluxweight: @local::sbn_eventweight_flux + geant4weight: @local::sbn_eventweight_geant4 } END_PROLOG diff --git a/sbncode/BeamSpillInfoRetriever/BNBEXTRetriever/CMakeLists.txt b/sbncode/BeamSpillInfoRetriever/BNBEXTRetriever/CMakeLists.txt deleted file mode 100644 index 1c49f8646..000000000 --- a/sbncode/BeamSpillInfoRetriever/BNBEXTRetriever/CMakeLists.txt +++ /dev/null @@ -1,20 +0,0 @@ - -cet_build_plugin(BNBEXTRetriever art::module - LIBRARIES - art::Persistency_Common - art::Utilities canvas::canvas - cetlib::cetlib cetlib_except::cetlib_except - messagefacility::MF_MessageLogger - sbnobj::Common_POTAccounting - sbndaq_artdaq_core::sbndaq-artdaq-core_Overlays_Common - sbndaq_artdaq_core::sbndaq-artdaq-core_Overlays - sbndaq_artdaq_core::sbndaq-artdaq-core_Overlays_ICARUS - artdaq_core::artdaq-core_Utilities - lardata::Utilities - larcore::Geometry_AuxDetGeometry_service -) - -install_headers() -install_fhicl() -install_source() - diff --git a/sbncode/BeamSpillInfoRetriever/BNBRetriever/BNBRetriever_module.cc b/sbncode/BeamSpillInfoRetriever/BNBRetriever/BNBRetriever_module.cc deleted file mode 100644 index 2265d0e2c..000000000 --- a/sbncode/BeamSpillInfoRetriever/BNBRetriever/BNBRetriever_module.cc +++ /dev/null @@ -1,781 +0,0 @@ -/** ******************************************************************** - * @file BNBRetriever_module.cc - * @date Wed April 9 2021 - * @author J. Zennamo (FNAL) - * - * Based heavily on code by Z. Pavlovic written for MicroBooNE - * Based heavily on code by NOvA collaboration (Thanks NOvA!): - * https://cdcvs.fnal.gov/redmine/projects/novaart/repository/entry/trunk/IFDBSpillInfo/BNBInfo_module.cc - * Database implementation by Justin Mueller - */ - -#include "art/Framework/Core/EDProducer.h" -#include "art/Framework/Core/ModuleMacros.h" -#include "art/Framework/Principal/Event.h" -#include "art/Framework/Principal/Handle.h" -#include "art/Framework/Principal/Run.h" -#include "art/Framework/Principal/SubRun.h" -#include "canvas/Utilities/InputTag.h" -#include "fhiclcpp/types/Atom.h" -#include "messagefacility/MessageLogger/MessageLogger.h" -#include "larcorealg/CoreUtils/counter.h" - -#include "artdaq-core/Data/Fragment.hh" -#include "sbndaq-artdaq-core/Overlays/ICARUS/ICARUSTriggerV3Fragment.hh" -#include "sbnobj/Common/Trigger/ExtraTriggerInfo.h" - -#include "sbnobj/Common/POTAccounting/BNBSpillInfo.h" - -#include "ifdh_art/IFBeamService/IFBeam_service.h" -#include "ifbeam_c.h" -#include "MWRData.h" - -#include -#include -#include - -#include -#include -#include - -namespace sbn { - class BNBRetriever; -} - -class sbn::BNBRetriever : public art::EDProducer { -public: - - struct Config { - - using Name = fhicl::Name; - using Comment = fhicl::Comment; - - fhicl::Atom TimePadding { - Name{ "TimePadding" }, - Comment{ "extension to the time window considered when collecting spills [seconds]" }, - 0.0333 // default - }; - - fhicl::Atom RawDataLabel { - Name{ "raw_data_label" }, - Comment{ "art data product instance name for trigger information (product label is 'daq')" } - }; - - fhicl::Atom DeviceUsedForTiming { - Name{ "DeviceUsedForTiming" }, - Comment{ "name in the IFBeam database of the device used to extract spill times" } - }; - - fhicl::Atom URL { - Name{ "URL" }, - Comment{ "IFBeam database access URL" } - }; - - fhicl::Atom Bundle { - Name{ "Bundle" }, - Comment{ "" } // explain what this is and which database/table it's looking for - }; - - fhicl::Atom TimeWindow { - Name{ "TimeWindow" }, - Comment{ "" } // explain what this is, what's for and its unit - }; - - fhicl::Atom MultiWireBundle { - Name{ "MultiWireBundle" }, - Comment{ "" } // explain what this is and which database/table it's looking for - }; - - fhicl::Atom MWR_TimeWindow { - Name{ "MWR_TimeWindow" }, - Comment{ "" } // explain what this is, what's for and its unit - }; - - fhicl::Atom TriggerDatabaseFile { - Name{ "TriggerDatabaseFile" }, - Comment{ "" } // explain what this is, what's for and its unit - }; - - - }; // Config - - using Parameters = art::EDProducer::Table; - - - explicit BNBRetriever(Parameters const& params); - // The compiler-generated destructor is fine for non-base - // classes without bare pointers or other resource use. - - // Plugins should not be copied or assigned. - BNBRetriever(BNBRetriever const&) = delete; - BNBRetriever(BNBRetriever&&) = delete; - BNBRetriever& operator=(BNBRetriever const&) = delete; - BNBRetriever& operator=(BNBRetriever&&) = delete; - - // Required functions. - void produce(art::Event& e) override; - void beginSubRun(art::SubRun& sr) override; - void endSubRun(art::SubRun& sr) override; - -private: - // input labels - std::vector< sbn::BNBSpillInfo > fOutbeamInfos; - double fTimePad; - std::string fURL; - MWRData mwrdata; - int run_number; - std::string raw_data_label; - std::string fDeviceUsedForTiming; - unsigned int TotalBeamSpills; - // - art::ServiceHandle ifbeam_handle; - std::unique_ptr bfp; - std::unique_ptr bfp_mwr; - - // - std::string fTriggerDatabaseFile; - sqlite3 *db; - int rc; - - struct TriggerInfo_t { - int gate_type = 0; ///< Source of the spill: `1`: BNB, `2`: NuMI - double t_current_event = 0; - double t_previous_event = 0; - unsigned int number_of_gates_since_previous_event = 0; // FIXME needs to be integral type - std::int64_t WR_to_Spill_conversion = 0; - }; - - struct MWRdata_t { - std::vector< std::vector > MWR_times; - std::vector< std::vector< std::vector< int > > > unpacked_MWR; - }; - - - static constexpr double MWRtoroidDelay = -0.035; ///< the same time point is measured _t_ by MWR and _t + MWRtoroidDelay`_ by the toroid [ms] - - /// Returns the information of the trigger in the current event. - TriggerInfo_t extractTriggerInfo(art::Event const& e) const; - - /** - * @brief Determines spill times and extracts data based on multiwire devices. - * @param triggerInfo information from the trigger of this event - * @return times and unpacked data, per device (`"M875BB"`, `"M876BB"`, `"MMBTBB"`) - */ - MWRdata_t extractSpillTimes(TriggerInfo_t const& triggerInfo) const; - - /** - * @brief Matches spill times with multiwire chamber data from the database. - * @param eventID ID of the event the information is associated to - * @param triggerInfo information from the trigger of this event - * @param MWRdata data from multiwire chambers - * @param isFirstEventInRun whether we are processing the first event of the run - * @param[out] beamInfos container to _add_ spill information records to - * @return count of matched spills - */ - int matchMultiWireData( - art::EventID const& eventID, - TriggerInfo_t const& triggerInfo, - MWRdata_t const& MWRdata, bool isFirstEventInRun, - std::vector< sbn::BNBSpillInfo >& beamInfos - ) const; - - /** - * @brief Assembles and returns a spill information record. - * @param eventID ID of the event the information is associated to - * @param time time of the spill - * @param MWRdata all extracted data from multiwire chambers - * @param matched_MWR data from multiwire chambers matched with the time - * @return a `sbn::BNBSpillInfo` object with information on the spill at `time` - */ - sbn::BNBSpillInfo makeBNBSpillInfo - (art::EventID const& eventID, double time, MWRdata_t const& MWRdata, std::vector const& matched_MWR) const; - -/** - * @brief SQLite callback function for retrieving trigger_type from a query. - * @param data Pointer to the integer where the trigger type will be stored. - * @param argc Count of the number of columns returned by the query. - * @param argv Array of c-strings containing the column data of the query. - * @param columns Array of c-strings listing the names of the columns. - * @return 0 if successful. - */ -// int callback_trigger_type(void *data, -// int argc, -// char **argv, -// char **columns); - -/** - * @brief Queries the trigger database and finds the trigger_type of the matching trigger (if any). - * @param db The pointer to the SQLite database instance. - * @param run The run number of the current event (helps with queries). - * @param gate_time The time in milliseconds of the gate. - * @param threshold The required absolute time difference between gate and trigger. - * @return trigger_type -1: No matching trigger, 0: Majority, 1: MinBias - */ - int get_trigger_type_matching_gate(sqlite3 *db, - int func(void*,int,char**,char**), - int run, - long long int gate_time, - float threshold) const; - -}; - -int callback_trigger_type(void *data, int argc, char **argv, char **columns) -{ - int *result = static_cast(data); - // Does this query return non-NULL values? - if(argc > 0 && argv[0]) - *result = std::stoi(argv[0]); - else - *result = -1; - - return 0; -} - -int sbn::BNBRetriever::get_trigger_type_matching_gate(sqlite3 *db, int func(void*,int,char**,char**), int run, long long int gate_time, float threshold) const -{ - int trigger_type(-1), query_status; - std::stringstream query; - query << "SELECT trigger_type FROM triggerdata WHERE gate_type=1 AND run_number =" - << run - << " AND ABS(1000000000*wr_seconds + wr_nanoseconds - " - << std::fixed << gate_time - << ") < " - << threshold*1000000 - << " ORDER BY ABS(1000000000*wr_seconds + wr_nanoseconds - " - << std::fixed << gate_time - << ") LIMIT 1;"; - - query_status = sqlite3_exec(db, query.str().c_str(), func, &trigger_type, NULL); - if (query_status != SQLITE_OK) - { - mf::LogError("BNBEXTRetriever") << "SQL error: " << sqlite3_errmsg(db); - trigger_type = -1; - } - return trigger_type; -} - -sbn::BNBRetriever::BNBRetriever(Parameters const& params) - : EDProducer{params}, - fTimePad(params().TimePadding()), - raw_data_label(params().RawDataLabel()), - fDeviceUsedForTiming(params().DeviceUsedForTiming()), - bfp( ifbeam_handle->getBeamFolder(params().Bundle(), params().URL(), params().TimeWindow())), - bfp_mwr( ifbeam_handle->getBeamFolder(params().MultiWireBundle(), params().URL(), params().MWR_TimeWindow())), - fTriggerDatabaseFile(params().TriggerDatabaseFile()) -{ - - // Check fTimePad is positive - if (fTimePad < 0) { - throw art::Exception(art::errors::Configuration) - << "Parameter `TimePadding` must be non-negative (" << fTimePad << " was specified).\n"; - }//End Time check - - // how close in time does the spill time have to be from the DAQ time (in seconds). - // If these are too large then it fails to capture the device - // If these are too small then the time jitter in devices means we miss good data - // - // These values should likely not be changed unless authors of the IFBeam API are consulted - // - bfp->set_epsilon(0.02); //20 ms, this was tuned by hand and compared to IFBeamDB times - bfp_mwr->set_epsilon(0.5); - - //bfp_mwr->setValidWindow(86400); - bfp_mwr->setValidWindow(3605); - produces< std::vector< sbn::BNBSpillInfo >, art::InSubRun >(); - TotalBeamSpills = 0; - - cet::search_path sp("FW_SEARCH_PATH"); - std::string trigDB_path = sp.find_file(fTriggerDatabaseFile.c_str()); - - rc = sqlite3_open(trigDB_path.c_str(), &db); - if(rc) - { - fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); - throw art::Exception(art::errors::NotFound) - << "Can't open database: " << sqlite3_errmsg(db); - } - -} - - -void sbn::BNBRetriever::produce(art::Event& e) -{ - - // If this is the first event in the run, then ignore it - // We do not currently have the ability to figure out the first - // spill that the DAQ was sensitive to, so don't try to save any - // spill information - // - // TODO: long-term goal -- can we fix this? - // FIXME This is wrong.... - // Need to use: ICARUSTriggerV3Fragment long getTotalTriggerBNBMaj() const - - if (e.event() == 1) return; - - run_number = e.id().run(); - - TriggerInfo_t const triggerInfo = extractTriggerInfo(e); - - //We only want to process BNB gates, i.e. type 1 - if(triggerInfo.gate_type != 1) return; - // Keep track of the number of beam gates the DAQ thinks - // are in this job - TotalBeamSpills += triggerInfo.number_of_gates_since_previous_event; - - - MWRdata_t const MWRdata = extractSpillTimes(triggerInfo); - - - int const spill_count = matchMultiWireData(e.id(), triggerInfo, MWRdata, e.event() == 1, fOutbeamInfos); - - - if(spill_count > int(triggerInfo.number_of_gates_since_previous_event)) - mf::LogDebug("BNBRetriever")<< "Event Spills : " << spill_count << ", DAQ Spills : " << triggerInfo.number_of_gates_since_previous_event << " \t \t ::: WRONG!"<< std::endl; - else - mf::LogDebug("BNBRetriever")<< "Event Spills : " << spill_count << ", DAQ Spills : " << triggerInfo.number_of_gates_since_previous_event << std::endl; - -}//end iteration over art::Events - - -sbn::BNBRetriever::TriggerInfo_t sbn::BNBRetriever::extractTriggerInfo(art::Event const& e) const { - - //Here we read in the artdaq Fragments and extract three pieces of information: - // 1. The time of the current event, t_current_event - // 2. the time of the previously triggered event, t_previous_event (NOTE: Events are non-sequential!) - // 3. the number of beam spills since the previously triggered event, number_of_gates_since_previous_event - - auto const & raw_data = e.getProduct< std::vector >({ raw_data_label, "ICARUSTriggerV3" }); - auto const & extraTrigInfo = e.getProduct< sbn::ExtraTriggerInfo >("daqTrigger"); - - TriggerInfo_t triggerInfo; - - triggerInfo.WR_to_Spill_conversion = extraTrigInfo.WRtimeToTriggerTime; - - for(auto raw_datum : raw_data){ - - uint64_t artdaq_ts = raw_datum.timestamp(); - icarus::ICARUSTriggerV3Fragment frag(raw_datum); - std::string data = frag.GetDataString(); - char *buffer = const_cast(data.c_str()); - icarus::ICARUSTriggerInfo datastream_info = icarus::parse_ICARUSTriggerV3String(buffer); - triggerInfo.gate_type = datastream_info.gate_type; - triggerInfo.number_of_gates_since_previous_event = frag.getDeltaGatesBNBMaj(); - - /* - The DAQ trigger time is issued at the Beam Extraction Signal (BES) which is issued - 36 ms *after* the $1D of the BNB, which is what is used in the IFBeam database - - We subtract 36ms from the Trigger time to match our triggers to the spills in the - IFBeam database - - */ - - triggerInfo.t_current_event = static_cast(artdaq_ts-3.6e7)/(1000000000.0); //check this offset... - if(triggerInfo.gate_type == 1) - triggerInfo.t_previous_event = (static_cast(frag.getLastTimestampBNBMaj()-3.6e7))/(1e9); - else - triggerInfo.t_previous_event = (static_cast(frag.getLastTimestampOther()-3.6e7))/(1000000000.0); - - } - - mf::LogDebug("BNBRetriever") << std::setprecision(19) << "Previous : " << triggerInfo.t_previous_event << ", Current : " << triggerInfo.t_current_event << ", Spill Count " << triggerInfo.number_of_gates_since_previous_event << std::endl; - - return triggerInfo; -} - - -sbn::BNBRetriever::MWRdata_t sbn::BNBRetriever::extractSpillTimes(TriggerInfo_t const& triggerInfo) const { - - // These lines get everything primed within the IFBeamDB - // They seem redundant but they are needed - // try{auto cur_vec_temp = bfp->GetNamedVector((triggerInfo.t_previous_event)-fTimePad,"E:THCURR");} catch (WebAPIException &we) {} - try{auto cur_vec_temp = bfp->GetNamedVector((triggerInfo.t_current_event)+fTimePad,"E:THCURR");} catch (WebAPIException &we) {} - try{auto packed_M876BB_temp = bfp_mwr->GetNamedVector((triggerInfo.t_current_event)+fTimePad,"E:M875BB{4440:888}.RAW");} catch (WebAPIException &we) {} - - //The multiwire chambers provide their - // data in a vector format but we'll have - // to sort through it in std::string format - // to correctly unpack it - std::vector< std::vector< std::vector< int > > > unpacked_MWR; - std::vector< std::vector< double> > MWR_times; - unpacked_MWR.resize(3); - MWR_times.resize(3); - std::string packed_data_str; - - //Create a list of all the MWR devices with their different - // memory buffer increments - // generally in the format: "E:.{Memory Block}" - std::vector vars = bfp_mwr->GetDeviceList(); - mf::LogDebug("BNBRetriever") << " Number of MWR Device Blocks Found : " << vars.size() << std::endl; - // Tracking the time from the IFBeamDB - double time_for_mwr; - - // this is an iterator to track which of the - // three devices we will be working with - int dev = 0; - - // The MWR devices are annoying and have confusing buffer - // what we'll do is sort through all of them first and then - // match them to the closest spills in time - // - - // int t_steps = int(((triggerInfo.t_previous_event - fTimePad) - (triggerInfo.t_current_event + fTimePad))/0.5)+25; - int t_steps = int(((triggerInfo.t_current_event + fTimePad) - (triggerInfo.t_previous_event - fTimePad - 20.))/0.5)+25; - mf::LogDebug("BNBRetriever") << " t_steps " << t_steps << std::endl; - - for(int t = 0; t < t_steps; t++){//Iterate through time increments - for (std::string const& var : vars) {// Iterate through the devices - - //Make sure we have a device - if(var.empty()){ - //mf::LogDebug("BNBRetriever") << " NO MWR DEVICES?!" << std::endl; - continue; - } - /// Check the device name and interate the double-vector index - if(var.find("M875BB") != std::string::npos ) dev = 0; - else if(var.find("M876BB") != std::string::npos ) dev = 1; - else if(var.find("MMBTBB") != std::string::npos ) dev = 2; - else{ - //mf::LogDebug("BNBRetriever") << " NOT matched to a MWR DEVICES?!" << var << std::endl; - continue;} - - time_for_mwr = 0; - - try{ - //Pull the MWR data for the device - // these data are "packed" - std::vector packed_MWR = bfp_mwr->GetNamedVector((triggerInfo.t_previous_event)-20.-fTimePad+double(0.5*t),var,&time_for_mwr); - - //We'll convert this into a format - // that we can unpack doubles >> strings - // - packed_data_str.clear(); - packed_data_str += std::to_string(int(time_for_mwr)); - packed_data_str.append(","); - packed_data_str.append(var); - packed_data_str.append(",,"); - - /* for(auto const value: packed_MWR){ - packed_data_str += ','; - packed_data_str += std::to_string(int(value)); - }*/ - for(int j = 0; j < int(packed_MWR.size()); j++){ - packed_data_str += std::to_string(int(packed_MWR[j])); - if(j < int(packed_MWR.size())-1) - packed_data_str.append(","); - } - - // Use Zarko's unpacking function to turn this into consumeable data - std::vector MWR_times_temp; - - // There is a 35 ms offset between the toriod and the MWR times - // we'll just remove that here to match to the spill times - std::vector< std::vector< int > > unpacked_MWR_temp = mwrdata.unpackMWR(packed_data_str,MWR_times_temp,MWRtoroidDelay); - - //There are four events that are packed into one MWR IFBeam entry - for(std::size_t s: util::counter(unpacked_MWR_temp.size())){ - - // If this entry has a unique time them store it for later - if(std::find(MWR_times[dev].begin(), MWR_times[dev].end(), MWR_times_temp[s]) == MWR_times[dev].end()){ - unpacked_MWR[dev].push_back(unpacked_MWR_temp[s]); - MWR_times[dev].push_back(MWR_times_temp[s]); - }//check for unique time - }//Iterate through the unpacked events - }//try - catch (WebAPIException &we) { - //Ignore when we can't find the MWR devices - // they don't always report and the timing of them can be annoying - - }//catch - }// Iterate over all the multiwire devices - }// Iterate over all times - - mf::LogDebug("BNBRetriever") << " Number of MWR[0] times : " << MWR_times[0].size() << std::endl; - mf::LogDebug("BNBRetriever") << " Number of MWR[0]s : " << unpacked_MWR[0].size() << std::endl; - mf::LogDebug("BNBRetriever") << " Number of MWR[1] times : " << MWR_times[1].size() << std::endl; - mf::LogDebug("BNBRetriever") << " Number of MWR[1]s : " << unpacked_MWR[1].size() << std::endl; - mf::LogDebug("BNBRetriever") << " Number of MWR[2] times : " << MWR_times[2].size() << std::endl; - mf::LogDebug("BNBRetriever") << " Number of MWR[2]s : " << unpacked_MWR[2].size() << std::endl; - - return { std::move(MWR_times), std::move(unpacked_MWR) }; -} - - -int sbn::BNBRetriever::matchMultiWireData( - art::EventID const& eventID, - TriggerInfo_t const& triggerInfo, - MWRdata_t const& MWRdata, bool isFirstEventInRun, - std::vector< sbn::BNBSpillInfo >& beamInfos -) const { - - auto const& [ MWR_times, unpacked_MWR ] = MWRdata; // alias - - //Here we will start collecting all the other beamline devices - // First we get the times that the beamline device fired - // we have to pick a specific variable to use - std::vector times_temps = bfp->GetTimeList(fDeviceUsedForTiming); - - mf::LogDebug("BNBRetriever") << "matchMultiWireData:: Number of time spills : " << times_temps.size() << std::endl; - - // We'll keep track of how many of these spills match to our - // DAQ trigger times - int spill_count = 0; - int spills_removed = 0; - std::vector matched_MWR; - matched_MWR.resize(3); - - - // NOTE: for now, this is dead code because we don't - // do anything for the first event in a run. We may want to revisit - // this later to understand if there is a way we can do the POT - // accounting in the first event. - // - // Need to handle the first event in a run differently - if(isFirstEventInRun){ - - //We'll remove the spills after our event - int spills_after_our_target = 0; - // iterate through all the spills to find the - // spills that are after our triggered event - for (size_t i = 0; i < times_temps.size(); i++) { - if(times_temps[i] > (triggerInfo.t_current_event+fTimePad)){ - spills_after_our_target++; - } - }//end loop through spill times - - // Remove the spills after our trigger - times_temps.erase(times_temps.end()-spills_after_our_target,times_temps.end()); - - // Remove the spills before the start of our Run - times_temps.erase(times_temps.begin(), times_temps.end() - std::min(int(triggerInfo.number_of_gates_since_previous_event), int(times_temps.size()))); - - }//end fix for "first event" - - ///reject time_stamps which have a trigger_type == 1 from data-base - //To-Do - - // mf::LogDebug("BNBRetriever") << "Total number of Times we're going to test: " << times_temps.size() << std::endl; - // mf::LogDebug("BNBRetriever") << std::setprecision(19) << "Upper Limit : " << (triggerInfo.t_current_event)+fTimePad << std::endl; - // mf::LogDebug("BNBRetriever") << std::setprecision(19) << "Lower Limit : " << (triggerInfo.t_previous_event)+fTimePad << std::endl; - - // Iterating through each of the beamline times - for (size_t i = 0; i < times_temps.size(); i++) { - - // Only continue if these times are matched to our DAQ time - //mf::LogDebug("BNBRetriever") << std::setprecision(19) << "Time # : " << i << std::endl; - - if(!isFirstEventInRun){//We already addressed the "first event" above - if(times_temps[i] > (triggerInfo.t_current_event)+fTimePad){ - //mf::LogDebug("BNBRetriever") << std::setprecision(19) << "Removed! : " << times_temps[i] << std::endl; - spills_removed++; - continue;} - if(times_temps[i] <= (triggerInfo.t_previous_event)+fTimePad){ - spills_removed++; - //mf::LogDebug("BNBRetriever") << std::setprecision(19) << "Removed! : " << times_temps[i] << std::endl; - continue;} - } - - //check if this spill is is minbias - /* - 40 ms was selected to be close to but outside the 66 ms - time of the next spill (when the beam is running at 15 Hz) - DocDB 33155 provides documentation of this - */ - - mf::LogDebug("BNBRetriever") << std::setprecision(19) << "matchMultiWireData:: trigger type : " << get_trigger_type_matching_gate(db, callback_trigger_type, run_number, times_temps[i]*1.e9-triggerInfo.WR_to_Spill_conversion+3.6e7, 40.) << " times : spill " << times_temps[i]*1.e9 << " - " << triggerInfo.WR_to_Spill_conversion << " + " << 3.6e7 << std::endl; - - if(get_trigger_type_matching_gate(db, callback_trigger_type, run_number, times_temps[i]*1.e9-triggerInfo.WR_to_Spill_conversion+3.6e7, 40.) == 1){ - mf::LogDebug("BNBRetriever") << std::setprecision(19) << "matchMultiWireData:: Skipped a MinBias gate at : " << times_temps[i]*1000. << std::endl; - - continue; - } - - //Great we found a matched spill! Let's count it - spill_count++; - - //Loop through the multiwire devices: - - for(int dev = 0; dev < int(MWR_times.size()); dev++){ - - //Loop through the multiwire times: - double Tdiff = 1000000000.; - matched_MWR[dev] = 0; - - for(int mwrt = 0; mwrt < int(MWR_times[dev].size()); mwrt++){ - - //found a candidate match! - if(fabs((MWR_times[dev][mwrt] - times_temps[i])) >= Tdiff){continue;} - - bool best_match = true; - - //Check for a better match... - for (size_t j = 0; j < times_temps.size(); j++) { - if( j == i) continue; - if(times_temps[j] > (triggerInfo.t_current_event+fTimePad)){continue;} - if(times_temps[j] <= (triggerInfo.t_previous_event+fTimePad)){continue;} - - //is there a better match later in the spill sequence - if(fabs((MWR_times[dev][mwrt] - times_temps[j])) < - fabs((MWR_times[dev][mwrt] - times_temps[i]))){ - //we can have patience... - best_match = false; - break; - } - }//end better match check - - //Verified best match! - if(best_match == true){ - matched_MWR[dev] = mwrt; - Tdiff = fabs((MWR_times[dev][mwrt] - times_temps[i])); - } - - }//end loop over MWR times - - }//end loop over MWR devices - - sbn::BNBSpillInfo spillInfo = makeBNBSpillInfo(eventID, times_temps[i], MWRdata, matched_MWR); - - beamInfos.push_back(std::move(spillInfo)); - - // We do not write these to the art::Events because - // we can filter events but want to keep all the POT - // information, so we'll write it to the SubRun - - }//end iteration over beam device times - - // mf::LogDebug("BNBRetriever") << "matchMultiWireData:: Total spills counted: " << spill_count << " Total spills removed : " << spills_removed << std::endl; - - return spill_count; -} - - -sbn::BNBSpillInfo sbn::BNBRetriever::makeBNBSpillInfo - (art::EventID const& eventID, double time, MWRdata_t const& MWRdata, std::vector const& matched_MWR) const -{ - - auto const& [ MWR_times, unpacked_MWR ] = MWRdata; // alias - - // initializing all of our device carriers - // device definitions can be found in BNBSpillInfo.h - - double TOR860 = 0; // units e12 protons - double TOR875 = 0; // units e12 protons - double LM875A = 0; // units R/s - double LM875B = 0; // units R/s - double LM875C = 0; // units R/s - double HP875 = 0; // units mm - double VP875 = 0; // units mm - double HPTG1 = 0; // units mm - double VPTG1 = 0; // units mm - double HPTG2 = 0; // units mm - double VPTG2 = 0; // units mm - double BTJT2 = 0; // units Deg C - double THCURR = 0; // units kiloAmps - - double TOR860_time = 0; // units s - - // Here we request all the devices - // since sometimes devices fail to report we'll - // allow each to throw an exception but still move forward - // interpreting these failures will be part of the beam quality analyses - try{bfp->GetNamedData(time, "E:TOR860@",&TOR860,&TOR860_time);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:TOR875",&TOR875);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:LM875A",&LM875A);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:LM875B",&LM875B);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:LM875C",&LM875C);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:HP875",&HP875);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:VP875",&VP875);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:HPTG1",&HPTG1);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:VPTG1",&VPTG1);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:HPTG2",&HPTG2);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:VPTG2",&VPTG2);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:BTJT2",&BTJT2);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:THCURR",&THCURR);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - - //crunch the times - unsigned long int time_closest_int = (int) TOR860_time; - double time_closest_ns = (TOR860_time - time_closest_int)*1e9; - - //Store everything in our data-product - sbn::BNBSpillInfo beamInfo; - beamInfo.TOR860 = TOR860*1e12; //add in factor of 1e12 protons to get correct POT units - beamInfo.TOR875 = TOR875*1e12; //add in factor of 1e12 protons to get correct POT units - beamInfo.LM875A = LM875A; - beamInfo.LM875B = LM875B; - beamInfo.LM875C = LM875C; - beamInfo.HP875 = HP875; - beamInfo.VP875 = VP875; - beamInfo.HPTG1 = HPTG1; - beamInfo.VPTG1 = VPTG1; - beamInfo.HPTG2 = HPTG2; - beamInfo.VPTG2 = VPTG2; - beamInfo.BTJT2 = BTJT2; - beamInfo.THCURR = THCURR; - beamInfo.spill_time_s = time_closest_int; - beamInfo.spill_time_ns = time_closest_ns; - - for(auto const& MWRdata: unpacked_MWR){ - std::ignore = MWRdata; - assert(!MWRdata.empty()); - } - - if(unpacked_MWR[0].empty()){ - beamInfo.M875BB.clear(); - beamInfo.M875BB_spill_time_diff = -999;//units in seconds - } - else{ - beamInfo.M875BB = unpacked_MWR[0][matched_MWR[0]]; - beamInfo.M875BB_spill_time_diff = (MWR_times[0][matched_MWR[0]] - time); - } - - if(unpacked_MWR[1].empty()){ - beamInfo.M876BB.clear(); - beamInfo.M876BB_spill_time_diff = -999;//units in seconds - } - else{ - beamInfo.M876BB = unpacked_MWR[1][matched_MWR[1]]; - beamInfo.M876BB_spill_time_diff = (MWR_times[1][matched_MWR[1]] - time); - } - - if(unpacked_MWR[2].empty()){ - beamInfo.MMBTBB.clear(); - beamInfo.MMBTBB_spill_time_diff = -999;//units in seconds - } - else{ - beamInfo.MMBTBB = unpacked_MWR[2][matched_MWR[2]]; - beamInfo.MMBTBB_spill_time_diff = (MWR_times[2][matched_MWR[2]] - time); - } - // We do not write these to the art::Events because - // we can filter events but want to keep all the POT - // information, so we'll write it to the SubRun - - beamInfo.event = eventID.event(); // the rest of ID is known by art::SubRun - - return beamInfo; -} - - -void sbn::BNBRetriever::beginSubRun(art::SubRun& sr) -{ - return; -} - -//____________________________________________________________________________ -void sbn::BNBRetriever::endSubRun(art::SubRun& sr) -{ - // We will add all of the BNBSpillInfo data-products to the - // art::SubRun so it persists - // currently this is ~2.7 kB/event or ~0.07 kB/spill - -mf::LogDebug("BNBRetriever")<< "Total number of DAQ Spills : " << TotalBeamSpills << std::endl; -mf::LogDebug("BNBRetriever")<< "Total number of Selected Spills : " << fOutbeamInfos.size() << std::endl; - - auto p = std::make_unique< std::vector< sbn::BNBSpillInfo > >(); - std::swap(*p, fOutbeamInfos); - - sr.put(std::move(p), art::subRunFragment()); - - return; -} - -DEFINE_ART_MODULE(sbn::BNBRetriever) diff --git a/sbncode/BeamSpillInfoRetriever/BNBRetriever/CMakeLists.txt b/sbncode/BeamSpillInfoRetriever/BNBRetriever/CMakeLists.txt deleted file mode 100644 index a34b60d6a..000000000 --- a/sbncode/BeamSpillInfoRetriever/BNBRetriever/CMakeLists.txt +++ /dev/null @@ -1,34 +0,0 @@ -find_package(ifbeam) -find_package(ifdh_art) - - -art_make_library(LIBRARIES Boost::system - LIBRARY_NAME sbn_BNBSpillInfoRetriever_MWRData - SOURCE MWRData.cpp -) - - -cet_build_plugin(BNBRetriever art::module - LIBRARIES - art::Persistency_Common - art::Utilities canvas::canvas - cetlib::cetlib cetlib_except::cetlib_except - ROOT::X3d - Boost::system - messagefacility::MF_MessageLogger - ifbeam::ifbeam - ifdh_art::IFBeam_service - SQLite::SQLite3 - sbndaq_artdaq_core::sbndaq-artdaq-core_Overlays_Common - sbndaq_artdaq_core::sbndaq-artdaq-core_Overlays - sbndaq_artdaq_core::sbndaq-artdaq-core_Overlays_ICARUS - artdaq_core::artdaq-core_Utilities - sbn_BNBSpillInfoRetriever_MWRData - sbnobj::Common_POTAccounting - larcorealg::CoreUtils -) - -install_headers() -install_fhicl() -install_source() - diff --git a/sbncode/BeamSpillInfoRetriever/BNBRetriever/MWRData.cpp b/sbncode/BeamSpillInfoRetriever/BNBRetriever/MWRData.cpp deleted file mode 100644 index 48d0bda3f..000000000 --- a/sbncode/BeamSpillInfoRetriever/BNBRetriever/MWRData.cpp +++ /dev/null @@ -1,71 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include "MWRData.h" - -using namespace std; - -namespace sbn{ - - std::vector< std::vector < int > > MWRData::unpackMWR(std::string packed_data, std::vector &time_stamp, double timeoffset) const -{ - - std::vector > unpacked_data; - unpacked_data.resize(4); - short data[444]; - - std::vector row(0); - boost::split(row, packed_data, boost::is_any_of(",")); - if (row.size()==447) { - for (int i=3;i<447;i++) { - data[i-3]=atoi(row[i].c_str()); - } - string devname=row[1].substr(0,8); - for (int idev=0;idev<4;idev++) { - mwrpulse_t mwr=getMWRdata(data,idev); - time_stamp.push_back(mwr.sheader.timesec+mwr.sheader.timensec/1000000000.+timeoffset); - for (int ich=0;ich<48;ich++) { - unpacked_data[idev].push_back(mwr.hor[ich]); - } - for (int ich=0;ich<48;ich++) { - unpacked_data[idev].push_back(mwr.ver[ich]); - } - } - } else { - cout <<"BeamSpillInfoRetriever: MRWData: Bad data!"< -namespace sbn{ -class MWRData -{ - typedef struct swicheader_t { - long timesec; - long timensec; - long gpstime1; - long gpstime2; - short boosterevent; - short mievent; - short hz15micnt; - long delta1f; - short pulsemi; - short pulsesc; - } swicheader_t; - - typedef struct mwrpulse_t { - short hor[48]; - short ver[48]; - swicheader_t sheader; - } mwrpulse_t; - - static long flipByte(long data) - { - return ((data>>16)&0x0000FFFF) | ((data<<16)&0xFFFF0000); - } - - mwrpulse_t getMWRdata(short* data, int nblock) const; - - public: - std::vector< std::vector < int > > unpackMWR(std::string packed_data, std::vector &time_stamp, double timeoffset=0) const; -}; -} - -#endif /* #ifndef _MWRDATA_H */ diff --git a/sbncode/BeamSpillInfoRetriever/CMakeLists.txt b/sbncode/BeamSpillInfoRetriever/CMakeLists.txt index 3fcd2f930..5332b931d 100644 --- a/sbncode/BeamSpillInfoRetriever/CMakeLists.txt +++ b/sbncode/BeamSpillInfoRetriever/CMakeLists.txt @@ -1,20 +1,26 @@ -add_subdirectory(BNBRetriever) +find_package(ifbeam) +find_package(ifdh_art) + +include_directories( + ${IFBEAM_FQ_DIR}/include + ${IFDH_ART_INC} +) + add_subdirectory(SBNDBNBRetriever) add_subdirectory(SBNDBNBZEROBIASRetriever) add_subdirectory(SBNDBNBEXTRetriever) add_subdirectory(NuMIRetriever) -add_subdirectory(BNBEXTRetriever) add_subdirectory(NuMIEXTRetriever) +add_subdirectory(ICARUSBNBEXTRetriever) +add_subdirectory(ICARUSBNBRetriever) add_subdirectory(job) -find_package(ifbeam) -find_package(ifdh_art) - art_make_library(LIBRARIES Boost::system LIBRARY_NAME sbn_MWRData SOURCE MWRData.cpp ) + art_make_library( LIBRARIES art::Persistency_Common @@ -26,9 +32,21 @@ art_make_library( sbnobj::Common_POTAccounting sbn_MWRData larcorealg::CoreUtils + + LIBRARY_NAME sbn_POTTools + SOURCE POTTools.cpp +) + +art_make_library( + LIBRARIES + + ROOT::Hist + ROOT::MathCore + sbnobj::Common_POTAccounting + - LIBRARY_NAME sbn_SBNDPOTTools - SOURCE SBNDPOTTools.cpp + LIBRARY_NAME sbn_getFOM + SOURCE getFOM.cpp ) install_headers() diff --git a/sbncode/BeamSpillInfoRetriever/ICARUSBNBEXTRetriever/CMakeLists.txt b/sbncode/BeamSpillInfoRetriever/ICARUSBNBEXTRetriever/CMakeLists.txt new file mode 100644 index 000000000..416f70c33 --- /dev/null +++ b/sbncode/BeamSpillInfoRetriever/ICARUSBNBEXTRetriever/CMakeLists.txt @@ -0,0 +1,12 @@ +find_package(ifbeam) +find_package(ifdh_art) + +cet_build_plugin(ICARUSBNBEXTRetriever art::module + LIBRARIES + sbn_POTTools +) + +install_headers() +install_fhicl() +install_source() + diff --git a/sbncode/BeamSpillInfoRetriever/BNBEXTRetriever/BNBEXTRetriever_module.cc b/sbncode/BeamSpillInfoRetriever/ICARUSBNBEXTRetriever/ICARUSBNBEXTRetriever_module.cc similarity index 83% rename from sbncode/BeamSpillInfoRetriever/BNBEXTRetriever/BNBEXTRetriever_module.cc rename to sbncode/BeamSpillInfoRetriever/ICARUSBNBEXTRetriever/ICARUSBNBEXTRetriever_module.cc index 1b7c79dd5..96674365f 100644 --- a/sbncode/BeamSpillInfoRetriever/BNBEXTRetriever/BNBEXTRetriever_module.cc +++ b/sbncode/BeamSpillInfoRetriever/ICARUSBNBEXTRetriever/ICARUSBNBEXTRetriever_module.cc @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////// -// Class: BNBEXTRetriever +// Class: ICARUSBNBEXTRetriever // Plugin Type: producer -// File: BNBEXTRetriever_module.cc +// File: ICARUSBNBEXTRetriever_module.cc // // Created by hand Thurs June 24th 2021 by J. Zennamo (FNAL) // @@ -16,16 +16,16 @@ #include "canvas/Utilities/InputTag.h" #include "fhiclcpp/types/Atom.h" #include "messagefacility/MessageLogger/MessageLogger.h" -#include "lardata/Utilities/AssociationUtil.h" +/*#include "lardata/Utilities/AssociationUtil.h" #include "lardataobj/Utilities/sparse_vector.h" #include "larcoreobj/SimpleTypesAndConstants/RawTypes.h" -#include "larcorealg/Geometry/Exceptions.h" +#include "larcorealg/Geometry/Exceptions.h"*/ #include "artdaq-core/Data/Fragment.hh" #include "sbndaq-artdaq-core/Overlays/ICARUS/ICARUSTriggerV3Fragment.hh" -#include "lardataalg/DetectorInfo/DetectorPropertiesStandard.h" -#include "lardata/DetectorInfoServices/DetectorPropertiesService.h" +/*#include "lardataalg/DetectorInfo/DetectorPropertiesStandard.h" +#include "lardata/DetectorInfoServices/DetectorPropertiesService.h"*/ #include "sbnobj/Common/POTAccounting/EXTCountInfo.h" #include @@ -36,10 +36,10 @@ #include namespace sbn { - class BNBEXTRetriever; + class ICARUSBNBEXTRetriever; } -class sbn::BNBEXTRetriever : public art::EDProducer { +class sbn::ICARUSBNBEXTRetriever : public art::EDProducer { public: struct Config { @@ -57,15 +57,15 @@ class sbn::BNBEXTRetriever : public art::EDProducer { using Parameters = art::EDProducer::Table; - explicit BNBEXTRetriever(Parameters const& params); + explicit ICARUSBNBEXTRetriever(Parameters const& params); // The compiler-generated destructor is fine for non-base // classes without bare pointers or other resource use. // Plugins should not be copied or assigned. - BNBEXTRetriever(BNBEXTRetriever const&) = delete; - BNBEXTRetriever(BNBEXTRetriever&&) = delete; - BNBEXTRetriever& operator=(BNBEXTRetriever const&) = delete; - BNBEXTRetriever& operator=(BNBEXTRetriever&&) = delete; + ICARUSBNBEXTRetriever(ICARUSBNBEXTRetriever const&) = delete; + ICARUSBNBEXTRetriever(ICARUSBNBEXTRetriever&&) = delete; + ICARUSBNBEXTRetriever& operator=(ICARUSBNBEXTRetriever const&) = delete; + ICARUSBNBEXTRetriever& operator=(ICARUSBNBEXTRetriever&&) = delete; // Required functions. void produce(art::Event& e) override; @@ -85,7 +85,7 @@ class sbn::BNBEXTRetriever : public art::EDProducer { }; -sbn::BNBEXTRetriever::BNBEXTRetriever(Parameters const& params) +sbn::ICARUSBNBEXTRetriever::ICARUSBNBEXTRetriever(Parameters const& params) : EDProducer{params}, raw_data_label_(params().RawDataLabel()) { @@ -97,7 +97,7 @@ sbn::BNBEXTRetriever::BNBEXTRetriever(Parameters const& params) scale_factor = 0; } -void sbn::BNBEXTRetriever::produce(art::Event& e) +void sbn::ICARUSBNBEXTRetriever::produce(art::Event& e) { //Here we read in the artdaq Fragments and extract three pieces of information: @@ -152,7 +152,7 @@ void sbn::BNBEXTRetriever::produce(art::Event& e) } //end loop over events -void sbn::BNBEXTRetriever::beginSubRun(art::SubRun& sr) +void sbn::ICARUSBNBEXTRetriever::beginSubRun(art::SubRun& sr) { TotalEXTCounts = 0; totalMinBias = 0; @@ -162,7 +162,7 @@ void sbn::BNBEXTRetriever::beginSubRun(art::SubRun& sr) } //____________________________________________________________________________ -void sbn::BNBEXTRetriever::endSubRun(art::SubRun& sr) +void sbn::ICARUSBNBEXTRetriever::endSubRun(art::SubRun& sr) { // We will add all of the EXTCountInfo data-products to the // art::SubRun so it persists @@ -173,7 +173,7 @@ void sbn::BNBEXTRetriever::endSubRun(art::SubRun& sr) std::cout << "FAILED! " << std::endl; // probably want to throw an exception here - for(auto ExtInfo : fOutExtInfos){ + for(auto &ExtInfo : fOutExtInfos){ ExtInfo.gates_since_last_trigger *= scale_factor; @@ -186,4 +186,4 @@ void sbn::BNBEXTRetriever::endSubRun(art::SubRun& sr) return; } -DEFINE_ART_MODULE(sbn::BNBEXTRetriever) +DEFINE_ART_MODULE(sbn::ICARUSBNBEXTRetriever) diff --git a/sbncode/BeamSpillInfoRetriever/ICARUSBNBRetriever/CMakeLists.txt b/sbncode/BeamSpillInfoRetriever/ICARUSBNBRetriever/CMakeLists.txt new file mode 100644 index 000000000..6b1a7a729 --- /dev/null +++ b/sbncode/BeamSpillInfoRetriever/ICARUSBNBRetriever/CMakeLists.txt @@ -0,0 +1,14 @@ +find_package(ifbeam) +find_package(ifdh_art) + +cet_build_plugin(ICARUSBNBRetriever art::module + LIBRARIES + sbn_POTTools + sbn_getFOM + SQLite::SQLite3 +) + +install_headers() +install_fhicl() +install_source() + diff --git a/sbncode/BeamSpillInfoRetriever/ICARUSBNBRetriever/ICARUSBNBRetriever_module.cc b/sbncode/BeamSpillInfoRetriever/ICARUSBNBRetriever/ICARUSBNBRetriever_module.cc new file mode 100644 index 000000000..cee5fa7a5 --- /dev/null +++ b/sbncode/BeamSpillInfoRetriever/ICARUSBNBRetriever/ICARUSBNBRetriever_module.cc @@ -0,0 +1,495 @@ +/** ******************************************************************** + * @file ICARUSBNBRetriever_module.cc + * @date Wed April 9 2021 + * @author J. Zennamo (FNAL) + * + * Based heavily on code by Z. Pavlovic written for MicroBooNE + * Based heavily on code by NOvA collaboration (Thanks NOvA!): + * https://cdcvs.fnal.gov/redmine/projects/novaart/repository/entry/trunk/IFDBSpillInfo/BNBInfo_module.cc + * Database implementation by Justin Mueller + */ + +#include "art/Framework/Core/EDProducer.h" +#include "messagefacility/MessageLogger/MessageLogger.h" +#include "sbnobj/Common/POTAccounting/BNBSpillInfo.h" +#include "sbncode/BeamSpillInfoRetriever/POTTools.h" +#include "sbnobj/Common/Trigger/ExtraTriggerInfo.h" +#include "sbndaq-artdaq-core/Overlays/ICARUS/ICARUSTriggerV3Fragment.hh" +#include +#include "sbncode/BeamSpillInfoRetriever/POTTools.h" +#include "sbncode/BeamSpillInfoRetriever/getFOM.h" + +namespace sbn { + class ICARUSBNBRetriever; +} + +class sbn::ICARUSBNBRetriever : public art::EDProducer { +public: + + struct Config { + + using Name = fhicl::Name; + using Comment = fhicl::Comment; + + fhicl::Atom TimePadding { + Name{ "TimePadding" }, + Comment{ "extension to the time window considered when collecting spills [seconds]" }, + 0.0333 // default + }; + + fhicl::Atom BESOffset { + Name{ "BESOffset" }, + Comment{ "Offset between beam early warning signal and $1D [seconds]" }, + }; + + fhicl::Atom RawDataLabel { + Name{ "raw_data_label" }, + Comment{ "art data product instance name for trigger information (product label is 'daq')" } + }; + + fhicl::Atom DeviceUsedForTiming { + Name{ "DeviceUsedForTiming" }, + Comment{ "name in the IFBeam database of the device used to extract spill times" } + }; + + fhicl::Atom URL { + Name{ "URL" }, + Comment{ "IFBeam database access URL" } + }; + + fhicl::Atom Bundle { + Name{ "Bundle" }, + Comment{ "Name of the collection of devices to grab from IFbeam for everything except MWRdevices." } + }; + + fhicl::Atom TimeWindow { + Name{ "TimeWindow" }, + Comment{ "Window of time in seconds to use for non-mwr ifbeam queries." } + }; + + fhicl::Atom MultiWireBundle { + Name{ "MultiWireBundle" }, + Comment{ "name of collections of devices to grab from IFbeam for use as MWRdevices." } + }; + + fhicl::Atom VP873Bundle { + Name{ "VP873Bundle" }, + Comment{ "name of collections of devices to grab from IFbeam for use as VP873." } + }; + + fhicl::Atom OffsetBundle { + Name{ "OffsetBundle" }, + Comment{ "name of collections of devices to grab from IFbeam for use for offsets." } + }; + fhicl::Atom MWR_TimeWindow { + Name{ "MWR_TimeWindow" }, + Comment{ "Window of time in seconds to use for mwr ifbeam queries." } + }; + + fhicl::Atom TriggerDatabaseFile { + Name{ "TriggerDatabaseFile" }, + Comment{ "path of local database of all recorded events and their trigger, in SQLite format" } + }; + + + }; // Config + + using Parameters = art::EDProducer::Table; + + + explicit ICARUSBNBRetriever(Parameters const& params); + // The compiler-generated destructor is fine for non-base + // classes without bare pointers or other resource use. + + // Plugins should not be copied or assigned. + ICARUSBNBRetriever(ICARUSBNBRetriever const&) = delete; + ICARUSBNBRetriever(ICARUSBNBRetriever&&) = delete; + ICARUSBNBRetriever& operator=(ICARUSBNBRetriever const&) = delete; + ICARUSBNBRetriever& operator=(ICARUSBNBRetriever&&) = delete; + + // Required functions. + void produce(art::Event& e) override; + void beginSubRun(art::SubRun& sr) override; + void endSubRun(art::SubRun& sr) override; + +private: + // input labels + std::vector< sbn::BNBSpillInfo > fOutbeamInfos; + double fTimePad; + double fBESOffset; + std::string fURL; + MWRData mwrdata; + int run_number; + std::string raw_data_label; + std::string fDeviceUsedForTiming; + unsigned int TotalBeamSpills; + // + art::ServiceHandle ifbeam_handle; + std::unique_ptr bfp; + std::unique_ptr vp873; + std::unique_ptr offsets; + std::unique_ptr bfp_mwr; + + // + std::string fTriggerDatabaseFile; + sqlite3 *db; + int rc; + + static constexpr double MWRtoroidDelay = -0.035; ///< the same time point is measured _t_ by MWR and _t + MWRtoroidDelay`_ by the toroid [ms] + + /// Returns the information of the trigger in the current event. + sbn::pot::TriggerInfo_t extractTriggerInfo(art::Event const& e) const; + /** + * @brief Matches spill times with multiwire chamber data from the database. + * @param eventID ID of the event the information is associated to + * @param triggerInfo information from the trigger of this event + * @param MWRdata data from multiwire chambers + * @param isFirstEventInRun whether we are processing the first event of the run + * @param[out] beamInfos container to _add_ spill information records to + * @return count of matched spills + */ + int matchMultiWireData( + art::EventID const& eventID, + sbn::pot::TriggerInfo_t const& triggerInfo, + sbn::pot::MWRdata_t const& MWRdata, + std::vector< sbn::BNBSpillInfo >& beamInfos + ) const; + +/** + * @brief SQLite callback function for retrieving trigger_type from a query. + * @param data Pointer to the integer where the trigger type will be stored. + * @param argc Count of the number of columns returned by the query. + * @param argv Array of c-strings containing the column data of the query. + * @param columns Array of c-strings listing the names of the columns. + * @return 0 if successful. + */ +// int callback_trigger_type(void *data, +// int argc, +// char **argv, +// char **columns); + +/** + * @brief Queries the trigger database and finds the trigger_type of the matching trigger (if any). + * @param db The pointer to the SQLite database instance. + * @param run The run number of the current event (helps with queries). + * @param gate_time The time in milliseconds of the gate. + * @param threshold The required absolute time difference between gate and trigger. + * @return trigger_type -1: No matching trigger, 0: Majority, 1: MinBias + */ + int get_trigger_type_matching_gate(sqlite3 *db, + int func(void*,int,char**,char**), + int run, + long long int gate_time, + float threshold) const; + +}; + +int callback_trigger_type(void *data, int argc, char **argv, char **columns) +{ + int *result = static_cast(data); + // Does this query return non-NULL values? + if(argc > 0 && argv[0]) + *result = std::stoi(argv[0]); + else + *result = -1; + + return 0; +} + +int sbn::ICARUSBNBRetriever::get_trigger_type_matching_gate(sqlite3 *db, int func(void*,int,char**,char**), int run, long long int gate_time, float threshold) const +{ + int trigger_type(-1), query_status; + std::stringstream query; + query << "SELECT trigger_type FROM triggerdata WHERE gate_type=1 AND run_number =" + << run + << " AND ABS(1000000000*wr_seconds + wr_nanoseconds - " + << std::fixed << gate_time + << ") < " + << threshold*1000000 + << " ORDER BY ABS(1000000000*wr_seconds + wr_nanoseconds - " + << std::fixed << gate_time + << ") LIMIT 1;"; + + query_status = sqlite3_exec(db, query.str().c_str(), func, &trigger_type, NULL); + if (query_status != SQLITE_OK) + { + mf::LogError("BNBEXTRetriever") << "SQL error: " << sqlite3_errmsg(db); + trigger_type = -1; + } + return trigger_type; +} + +sbn::ICARUSBNBRetriever::ICARUSBNBRetriever(Parameters const& params) + : EDProducer{params}, + fTimePad(params().TimePadding()), + fBESOffset(params().BESOffset()), + raw_data_label(params().RawDataLabel()), + fDeviceUsedForTiming(params().DeviceUsedForTiming()), + bfp( ifbeam_handle->getBeamFolder(params().Bundle(), params().URL(), params().TimeWindow())), + vp873( ifbeam_handle->getBeamFolder(params().VP873Bundle(), params().URL(), params().TimeWindow())), + offsets( ifbeam_handle->getBeamFolder(params().OffsetBundle(), params().URL(), params().TimeWindow())), + bfp_mwr( ifbeam_handle->getBeamFolder(params().MultiWireBundle(), params().URL(), params().MWR_TimeWindow())), + fTriggerDatabaseFile(params().TriggerDatabaseFile()) +{ + + // Check fTimePad is positive + if (fTimePad < 0) { + throw art::Exception(art::errors::Configuration) + << "Parameter `TimePadding` must be non-negative (" << fTimePad << " was specified).\n"; + }//End Time check + + // how close in time does the spill time have to be from the DAQ time (in seconds). + // If these are too large then it fails to capture the device + // If these are too small then the time jitter in devices means we miss good data + // + // These values should likely not be changed unless authors of the IFBeam API are consulted + // + bfp->set_epsilon(0.02); //20 ms, this was tuned by hand and compared to IFBeamDB times + bfp_mwr->set_epsilon(0.5); + vp873->set_epsilon(0.02); + offsets->set_epsilon(600); + + //bfp_mwr->setValidWindow(86400); + bfp_mwr->setValidWindow(3605); + produces< std::vector< sbn::BNBSpillInfo >, art::InSubRun >(); + TotalBeamSpills = 0; + + cet::search_path sp("FW_SEARCH_PATH"); + std::string trigDB_path = sp.find_file(fTriggerDatabaseFile.c_str()); + + rc = sqlite3_open(trigDB_path.c_str(), &db); + if(rc) + { + fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); + throw art::Exception(art::errors::NotFound) + << "Can't open database: " << sqlite3_errmsg(db); + } + +} + + +void sbn::ICARUSBNBRetriever::produce(art::Event& e) +{ + + // If this is the first event in the run, then ignore it + // We do not currently have the ability to figure out the first + // spill that the DAQ was sensitive to, so don't try to save any + // spill information + + if (e.event() == 1) return; + + run_number = e.id().run(); + + sbn::pot::TriggerInfo_t const triggerInfo = extractTriggerInfo(e); + + //We only want to process BNB gates, i.e. type 1 + if(triggerInfo.gate_type != 1) return; + // Keep track of the number of beam gates the DAQ thinks + // are in this job + TotalBeamSpills += triggerInfo.number_of_gates_since_previous_event; + sbn::pot::MWRdata_t const MWRdata = extractSpillTimes(triggerInfo, bfp, bfp_mwr, fTimePad, MWRtoroidDelay, mwrdata); + + int const spill_count = matchMultiWireData(e.id(), triggerInfo, MWRdata, fOutbeamInfos); + + if(spill_count > int(triggerInfo.number_of_gates_since_previous_event)) + mf::LogDebug("ICARUSBNBRetriever")<< "Event Spills : " << spill_count << ", DAQ Spills : " << triggerInfo.number_of_gates_since_previous_event << " \t \t ::: WRONG!"<< std::endl; + else + mf::LogDebug("ICARUSBNBRetriever")<< "Event Spills : " << spill_count << ", DAQ Spills : " << triggerInfo.number_of_gates_since_previous_event << std::endl; + +}//end iteration over art::Events + + +sbn::pot::TriggerInfo_t sbn::ICARUSBNBRetriever::extractTriggerInfo(art::Event const& e) const { + + //Here we read in the artdaq Fragments and extract three pieces of information: + // 1. The time of the current event, t_current_event + // 2. the time of the previously triggered event, t_previous_event (NOTE: Events are non-sequential!) + // 3. the number of beam spills since the previously triggered event, number_of_gates_since_previous_event + + auto const & raw_data = e.getProduct< std::vector >({ raw_data_label, "ICARUSTriggerV3" }); + auto const & extraTrigInfo = e.getProduct< sbn::ExtraTriggerInfo >("daqTrigger"); + + sbn::pot::TriggerInfo_t triggerInfo; + + triggerInfo.WR_to_Spill_conversion = extraTrigInfo.WRtimeToTriggerTime; + + for(auto raw_datum : raw_data){ + + uint64_t artdaq_ts = raw_datum.timestamp(); + icarus::ICARUSTriggerV3Fragment frag(raw_datum); + std::string data = frag.GetDataString(); + char *buffer = const_cast(data.c_str()); + icarus::ICARUSTriggerInfo datastream_info = icarus::parse_ICARUSTriggerV3String(buffer); + triggerInfo.gate_type = datastream_info.gate_type; + triggerInfo.number_of_gates_since_previous_event = frag.getDeltaGatesBNBMaj(); + + /* + The DAQ trigger time is issued at the Beam Extraction Signal (BES) which is issued + 36 ms *after* the $1D of the BNB, which is what is used in the IFBeam database + + We subtract 36ms from the Trigger time to match our triggers to the spills in the + IFBeam database + + */ + + double BESinTSUnits = fBESOffset*1e9; // flc param is in seconds need to convert to match TS + triggerInfo.t_current_event = static_cast(artdaq_ts-BESinTSUnits)/(1000000000.0); //check this offset... + if(triggerInfo.gate_type == 1) + triggerInfo.t_previous_event = (static_cast(frag.getLastTimestampBNBMaj()-BESinTSUnits))/(1e9); + else + triggerInfo.t_previous_event = (static_cast(frag.getLastTimestampOther()-BESinTSUnits))/(1000000000.0); + + } + + mf::LogDebug("ICARUSBNBRetriever") << std::setprecision(19) << "Previous : " << triggerInfo.t_previous_event << ", Current : " << triggerInfo.t_current_event << ", Spill Count " << triggerInfo.number_of_gates_since_previous_event << std::endl; + + return triggerInfo; +} + +int sbn::ICARUSBNBRetriever::matchMultiWireData( + art::EventID const& eventID, + sbn::pot::TriggerInfo_t const& triggerInfo, + sbn::pot::MWRdata_t const& MWRdata, + std::vector< sbn::BNBSpillInfo >& beamInfos +) const { + + auto const& [ MWR_times, unpacked_MWR ] = MWRdata; // alias + + //Here we will start collecting all the other beamline devices + // First we get the times that the beamline device fired + // we have to pick a specific variable to use + std::vector times_temps = bfp->GetTimeList(fDeviceUsedForTiming); + + mf::LogDebug("ICARUSBNBRetriever") << "matchMultiWireData:: Number of time spills : " << times_temps.size() << std::endl; + + // We'll keep track of how many of these spills match to our + // DAQ trigger times + int spill_count = 0; + int spills_removed = 0; + std::vector matched_MWR; + matched_MWR.resize(3); + + ///reject time_stamps which have a trigger_type == 1 from data-base + //To-Do + + // mf::LogDebug("ICARUSBNBRetriever") << "Total number of Times we're going to test: " << times_temps.size() << std::endl; + // mf::LogDebug("ICARUSBNBRetriever") << std::setprecision(19) << "Upper Limit : " << (triggerInfo.t_current_event)+fTimePad << std::endl; + // mf::LogDebug("ICARUSBNBRetriever") << std::setprecision(19) << "Lower Limit : " << (triggerInfo.t_previous_event)+fTimePad << std::endl; + + // Iterating through each of the beamline times + for (size_t i = 0; i < times_temps.size(); i++) { + + // Only continue if these times are matched to our DAQ time + //mf::LogDebug("ICARUSBNBRetriever") << std::setprecision(19) << "Time # : " << i << std::endl; + + if(times_temps[i] > (triggerInfo.t_current_event)+fTimePad){ + //mf::LogDebug("ICARUSBNBRetriever") << std::setprecision(19) << "Removed! : " << times_temps[i] << std::endl; + spills_removed++; + continue;} + if(times_temps[i] <= (triggerInfo.t_previous_event)+fTimePad){ + spills_removed++; + //mf::LogDebug("ICARUSBNBRetriever") << std::setprecision(19) << "Removed! : " << times_temps[i] << std::endl; + continue;} + + //check if this spill is is minbias + /* + 40 ms was selected to be close to but outside the 66 ms + time of the next spill (when the beam is running at 15 Hz) + DocDB 33155 provides documentation of this + */ + + double BESinTSUnits = fBESOffset*1e9; // flc param is in seconds need to convert to match TS + mf::LogDebug("ICARUSBNBRetriever") << std::setprecision(19) << "matchMultiWireData:: trigger type : " << get_trigger_type_matching_gate(db, callback_trigger_type, run_number, times_temps[i]*1.e9-triggerInfo.WR_to_Spill_conversion+BESinTSUnits, 40.) << " times : spill " << times_temps[i]*1.e9 << " - " << triggerInfo.WR_to_Spill_conversion << " + " << BESinTSUnits << std::endl; + + if(get_trigger_type_matching_gate(db, callback_trigger_type, run_number, times_temps[i]*1.e9-triggerInfo.WR_to_Spill_conversion+BESinTSUnits, 40.) == 1){ + mf::LogDebug("ICARUSBNBRetriever") << std::setprecision(19) << "matchMultiWireData:: Skipped a MinBias gate at : " << times_temps[i]*1000. << std::endl; + + continue; + } + + //Great we found a matched spill! Let's count it + spill_count++; + + //Loop through the multiwire devices: + + for(int dev = 0; dev < int(MWR_times.size()); dev++){ + + //Loop through the multiwire times: + double Tdiff = 1000000000.; + matched_MWR[dev] = 0; + + for(int mwrt = 0; mwrt < int(MWR_times[dev].size()); mwrt++){ + + //found a candidate match! + if(fabs((MWR_times[dev][mwrt] - times_temps[i])) >= Tdiff){continue;} + + bool best_match = true; + + //Check for a better match... + for (size_t j = 0; j < times_temps.size(); j++) { + if( j == i) continue; + if(times_temps[j] > (triggerInfo.t_current_event+fTimePad)){continue;} + if(times_temps[j] <= (triggerInfo.t_previous_event+fTimePad)){continue;} + + //is there a better match later in the spill sequence + if(fabs((MWR_times[dev][mwrt] - times_temps[j])) < + fabs((MWR_times[dev][mwrt] - times_temps[i]))){ + //we can have patience... + best_match = false; + break; + } + }//end better match check + + //Verified best match! + if(best_match == true){ + matched_MWR[dev] = mwrt; + Tdiff = fabs((MWR_times[dev][mwrt] - times_temps[i])); + } + + }//end loop over MWR times + + }//end loop over MWR devices + + sbn::BNBSpillInfo spillInfo = sbn::pot::makeBNBSpillInfo(eventID, times_temps[i], MWRdata, matched_MWR, bfp, offsets, vp873); + std::tuple allFOM = sbn::getBNBqualityFOM(spillInfo); + spillInfo.FOM = std::get<0>(allFOM); + spillInfo.PreFitFOM = std::get<1>(allFOM); + spillInfo.NoMultiWireFOM = std::get<2>(allFOM); + + beamInfos.push_back(std::move(spillInfo)); + + // We do not write these to the art::Events because + // we can filter events but want to keep all the POT + // information, so we'll write it to the SubRun + + }//end iteration over beam device times + + // mf::LogDebug("ICARUSBNBRetriever") << "matchMultiWireData:: Total spills counted: " << spill_count << " Total spills removed : " << spills_removed << std::endl; + + return spill_count; +} + +void sbn::ICARUSBNBRetriever::beginSubRun(art::SubRun& sr) +{ + return; +} + +//____________________________________________________________________________ +void sbn::ICARUSBNBRetriever::endSubRun(art::SubRun& sr) +{ + // We will add all of the BNBSpillInfo data-products to the + // art::SubRun so it persists + // currently this is ~2.7 kB/event or ~0.07 kB/spill + +mf::LogDebug("ICARUSBNBRetriever")<< "Total number of DAQ Spills : " << TotalBeamSpills << std::endl; +mf::LogDebug("ICARUSBNBRetriever")<< "Total number of Selected Spills : " << fOutbeamInfos.size() << std::endl; + + auto p = std::make_unique< std::vector< sbn::BNBSpillInfo > >(); + std::swap(*p, fOutbeamInfos); + + sr.put(std::move(p), art::subRunFragment()); + + return; +} + +DEFINE_ART_MODULE(sbn::ICARUSBNBRetriever) diff --git a/sbncode/BeamSpillInfoRetriever/SBNDPOTTools.cpp b/sbncode/BeamSpillInfoRetriever/POTTools.cpp similarity index 59% rename from sbncode/BeamSpillInfoRetriever/SBNDPOTTools.cpp rename to sbncode/BeamSpillInfoRetriever/POTTools.cpp index 9acb26ab2..a83d5f304 100644 --- a/sbncode/BeamSpillInfoRetriever/SBNDPOTTools.cpp +++ b/sbncode/BeamSpillInfoRetriever/POTTools.cpp @@ -1,12 +1,10 @@ -#include "SBNDPOTTools.h" +#include "POTTools.h" using namespace std; -namespace sbn{ +namespace sbn::pot{ - // Function for extracting the PTB information (current timestamp, - // previous timestamp, gate counter). Used in SBND modules only. - sbn::PTBInfo_t extractPTBInfo(art::Handle > cont_frags, int HLT) { + sbn::pot::PTBInfo_t extractPTBInfo(art::Handle > cont_frags, int HLT) { bool foundHLT = false; PTBInfo_t PTBInfo; for (auto const& cont : *cont_frags) @@ -22,11 +20,11 @@ namespace sbn{ if (ctb_frag.Trigger(word_i)->IsHLT() && ctb_frag.Trigger(word_i)->IsTrigger(HLT)) { foundHLT = true; - uint64_t RawprevPTBTimeStamp = ctb_frag.PTBWord(word_i)->prevTS * 20; - uint64_t RawcurrPTBTimeStamp = ctb_frag.Trigger(word_i)->timestamp * 20; - double currTS_candidate = std::bitset<64>(RawcurrPTBTimeStamp/20).to_ullong()/50e6; + uint64_t RawprevPTBTimeStamp = ctb_frag.PTBWord(word_i)->prevTS; + uint64_t RawcurrPTBTimeStamp = ctb_frag.Trigger(word_i)->timestamp; + std::uint64_t currTS_candidate = std::bitset<64>(RawcurrPTBTimeStamp).to_ullong() * 20; if(currTS_candidate < PTBInfo.currPTBTimeStamp){ - PTBInfo.prevPTBTimeStamp = std::bitset<64>(RawprevPTBTimeStamp / 20).to_ullong()/50e6; + PTBInfo.prevPTBTimeStamp = std::bitset<64>(RawprevPTBTimeStamp).to_ullong() * 20; PTBInfo.currPTBTimeStamp = currTS_candidate; PTBInfo.GateCounter = ctb_frag.Trigger(word_i)->gate_counter; } @@ -45,7 +43,35 @@ namespace sbn{ } } - // Function for extracting the TDC timestamp. Used in SBND modules only. + std::vector extractAllPTBInfo(std::vector const& cont_frags) { + std::vector PTBInfoVec; + for (auto const& cont : cont_frags) + { + artdaq::ContainerFragment cont_frag(cont); + for (size_t fragi = 0; fragi < cont_frag.block_count(); ++fragi) + { + artdaq::Fragment frag = *cont_frag[fragi]; + sbndaq::CTBFragment ctb_frag(frag); + for(size_t word_i = 0; word_i < ctb_frag.NWords(); ++word_i) + { + if(ctb_frag.Trigger(word_i)){ + PTBInfo_t PTBInfo; + uint64_t RawprevPTBTimeStamp = ctb_frag.PTBWord(word_i)->prevTS; + uint64_t RawcurrPTBTimeStamp = ctb_frag.Trigger(word_i)->timestamp; + PTBInfo.prevPTBTimeStamp = std::bitset<64>(RawprevPTBTimeStamp).to_ullong() * 20; + PTBInfo.currPTBTimeStamp = std::bitset<64>(RawcurrPTBTimeStamp).to_ullong() * 20; + PTBInfo.GateCounter = ctb_frag.Trigger(word_i)->gate_counter; + PTBInfo.isHLT = ctb_frag.Trigger(word_i)->IsHLT(); + PTBInfo.triggerWord = ctb_frag.Trigger(word_i)->trigger_word; + PTBInfoVec.push_back(PTBInfo); + } + } + } + } + + return PTBInfoVec; + } + double extractTDCTimeStamp(art::Handle > cont_frags) { double TDCTimeStamp = 0; @@ -62,10 +88,8 @@ namespace sbn{ return TDCTimeStamp; } - // Collect device information to make BNBSpillInfo object. Used in - // ICARUS and SBND modules. sbn::BNBSpillInfo makeBNBSpillInfo - (art::EventID const& eventID, double time, MWRdata_t const& MWRdata, std::vector const& matched_MWR, std::unique_ptr const& bfp) + (art::EventID const& eventID, double time, MWRdata_t const& MWRdata, std::vector const& matched_MWR, std::unique_ptr const& bfp, const std::unique_ptr & offsets,const std::unique_ptr & vp873) { auto const& [ MWR_times, unpacked_MWR ] = MWRdata; // alias @@ -73,41 +97,81 @@ namespace sbn{ // initializing all of our device carriers // device definitions can be found in BNBSpillInfo.h - double TOR860 = 0; // units e12 protons - double TOR875 = 0; // units e12 protons - double LM875A = 0; // units R/s - double LM875B = 0; // units R/s - double LM875C = 0; // units R/s - double HP875 = 0; // units mm - double VP875 = 0; // units mm - double HPTG1 = 0; // units mm - double VPTG1 = 0; // units mm - double HPTG2 = 0; // units mm - double VPTG2 = 0; // units mm - double BTJT2 = 0; // units Deg C - double THCURR = 0; // units kiloAmps - - double TOR860_time = 0; // units s + double TOR860 = -999; // units e12 protons + double TOR875 = -999; // units e12 protons + double LM875A = -999; // units R/s + double LM875B = -999; // units R/s + double LM875C = -999; // units R/s + double HP873 = -999; // units mm + double VP873 = -999; // units mm; not in the first IFBeam query bunch + double HP875 = -999; // units mm + double VP875 = -999; // units mm + double HPTG1 = -999; // units mm + double VPTG1 = -999; // units mm + double HPTG2 = -999; // units mm + double VPTG2 = -999; // units mm + double BTJT2 = -999; // units Deg C + double THCURR = -999; // units kiloAmps + double M875HS = -999; // units mm + double M875VS = -999; // units mm + double M875HM = -999; // units mm + double M875VM = -999; // units mm + double M876HS = -999; // units mm + double M876VS = -999; // units mm + double M876HM = -999; // units mm + double M876VM = -999; // units mm + + double HP875Offset =-999;//units mm; make a another separate IFBeam query bunch for offsets + double VP875Offset =-999;//units mm + double VP873Offset =-999;//units mm + double HPTG1Offset =-999;//units mm + double HPTG2Offset =-999;//units mm + double VPTG1Offset =-999;//units mm + double VPTG2Offset =-999;//units mm + + double TOR860_time = -999; // units s + + double FOM =-999; + // Here we request all the devices // since sometimes devices fail to report we'll // allow each to throw an exception but still move forward // interpreting these failures will be part of the beam quality analyses - - try{bfp->GetNamedData(time, "E:TOR860@",&TOR860,&TOR860_time);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:TOR875",&TOR875);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:LM875A",&LM875A);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:LM875B",&LM875B);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:LM875C",&LM875C);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:HP875",&HP875);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:VP875",&VP875);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:HPTG1",&HPTG1);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:VPTG1",&VPTG1);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:HPTG2",&HPTG2);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:VPTG2",&VPTG2);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:BTJT2",&BTJT2);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:THCURR",&THCURR);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - + + try{bfp->GetNamedData(time, "E:TOR860@",&TOR860,&TOR860_time);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:TOR875",&TOR875);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:LM875A",&LM875A);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:LM875B",&LM875B);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:LM875C",&LM875C);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:HP873",&HP873);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{vp873->GetNamedData(time, "E:VP873",&VP873);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:HP875",&HP875);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:VP875",&VP875);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:HPTG1",&HPTG1);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:VPTG1",&VPTG1);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:HPTG2",&HPTG2);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:VPTG2",&VPTG2);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:BTJT2",&BTJT2);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:THCURR",&THCURR);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + + try{bfp->GetNamedData(time, "E:M875HS",&M875HS);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:M875VS",&M875VS);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:M875HM",&M875HM);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:M875VM",&M875VM);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:M876HS",&M876HS);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:M876VS",&M876VS);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:M876HM",&M876HM);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:M876VM",&M876VM);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + + try{offsets->GetNamedData(time, "E_VP873S",&VP873Offset);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{offsets->GetNamedData(time, "E_HP875S",&HP875Offset);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{offsets->GetNamedData(time, "E_VP875S",&VP875Offset);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{offsets->GetNamedData(time, "E_HPTG1S",&HPTG1Offset);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{offsets->GetNamedData(time, "E_HPTG2S",&HPTG2Offset);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{offsets->GetNamedData(time, "E_VPTG1S",&VPTG1Offset);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{offsets->GetNamedData(time, "E_VPTG2S",&VPTG2Offset);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + //crunch the times unsigned long int time_closest_int = (int) TOR860_time; double time_closest_ns = (TOR860_time - time_closest_int)*1e9; @@ -119,6 +183,8 @@ namespace sbn{ beamInfo.LM875A = LM875A; beamInfo.LM875B = LM875B; beamInfo.LM875C = LM875C; + beamInfo.HP873 = HP873; + beamInfo.VP873 = VP873; beamInfo.HP875 = HP875; beamInfo.VP875 = VP875; beamInfo.HPTG1 = HPTG1; @@ -127,9 +193,27 @@ namespace sbn{ beamInfo.VPTG2 = VPTG2; beamInfo.BTJT2 = BTJT2; beamInfo.THCURR = THCURR; + beamInfo.M875HS = M875HS; + beamInfo.M875VS = M875VS; + beamInfo.M875HM = M875HM; + beamInfo.M875VM = M875VM; + beamInfo.M876HS = M876HS; + beamInfo.M876VS = M876VS; + beamInfo.M876HM = M876HM; + beamInfo.M876VM = M876VM; beamInfo.spill_time_s = time_closest_int; beamInfo.spill_time_ns = time_closest_ns; - + beamInfo.VP873 = VP873; + beamInfo.VP873Offset = VP873Offset; + beamInfo.HP875Offset = HP875Offset; + beamInfo.VP875Offset = VP875Offset; + beamInfo.HPTG1Offset = HPTG1Offset; + beamInfo.HPTG2Offset = HPTG2Offset; + beamInfo.VPTG1Offset = VPTG1Offset; + beamInfo.VPTG2Offset = VPTG2Offset; + beamInfo.FOM = FOM; + + for(auto const& MWRdata: unpacked_MWR){ std::ignore = MWRdata; assert(!MWRdata.empty()); @@ -166,30 +250,31 @@ namespace sbn{ // information, so we'll write it to the SubRun beamInfo.event = eventID.event(); // the rest of ID is known by art::SubRun - + + //for(size_t m=0; m< beamInfo.M875BB.size(); m++){ + //std::cout << "M875BB: " < const& bfp) { - double TOR860 = 0; // units e12 protons - double TOR875 = 0; // units e12 protons - double LM875A = 0; // units R/s - double LM875B = 0; // units R/s - double LM875C = 0; // units R/s - double HP875 = 0; // units mm - double VP875 = 0; // units mm - double HPTG1 = 0; // units mm - double VPTG1 = 0; // units mm - double HPTG2 = 0; // units mm - double VPTG2 = 0; // units mm - double BTJT2 = 0; // units Deg C - double THCURR = 0; // units kiloAmps + double TOR860 = -999; // units e12 protons + double TOR875 = -999; // units e12 protons + double LM875A = -999; // units R/s + double LM875B = -999; // units R/s + double LM875C = -999; // units R/s + double HP875 = -999; // units mm + double VP875 = -999; // units mm + double HPTG1 = -999; // units mm + double VPTG1 = -999; // units mm + double HPTG2 = -999; // units mm + double VPTG2 = -999; // units mm + double BTJT2 = -999; // units Deg C + double THCURR = -999; // units kiloAmps - double TOR860_time = 0; // units s + double TOR860_time = -999; // units s // Here we request all the devices // since sometimes devices fail to report we'll @@ -221,8 +306,7 @@ namespace sbn{ } } - // extract spill times from the IFBeam data base. Used in SBND and ICARUS. - sbn::MWRdata_t extractSpillTimes(TriggerInfo_t const& triggerInfo, std::unique_ptr const& bfp, std::unique_ptr const& bfp_mwr, double fTimePad, double MWRtoroidDelay, sbn::MWRData mwrdata) { + sbn::pot::MWRdata_t extractSpillTimes(TriggerInfo_t const& triggerInfo, std::unique_ptr const& bfp, std::unique_ptr const& bfp_mwr, double fTimePad, double MWRtoroidDelay, sbn::MWRData mwrdata) { // These lines get everything primed within the IFBeamDB. try{bfp->FillCache((triggerInfo.t_current_event)+fTimePad);} catch (WebAPIException &we) {}; @@ -331,4 +415,5 @@ namespace sbn{ return { std::move(MWR_times), std::move(unpacked_MWR) }; } + } diff --git a/sbncode/BeamSpillInfoRetriever/POTTools.h b/sbncode/BeamSpillInfoRetriever/POTTools.h new file mode 100644 index 000000000..1e54647a8 --- /dev/null +++ b/sbncode/BeamSpillInfoRetriever/POTTools.h @@ -0,0 +1,92 @@ +#ifndef _SBNDPOTTOOLS_H +#define _SBNDPOTTOOLS_H + +#include "art/Framework/Principal/Event.h" +#include "art/Framework/Principal/Handle.h" +#include "messagefacility/MessageLogger/MessageLogger.h" +#include "ifdh_art/IFBeamService/IFBeam_service.h" + +#include "sbnobj/Common/POTAccounting/BNBSpillInfo.h" +#include "sbndaq-artdaq-core/Overlays/SBND/PTBFragment.hh" +#include "sbndaq-artdaq-core/Overlays/SBND/TDCTimestampFragment.hh" +#include "artdaq-core/Data/ContainerFragment.hh" + +#include "sbncode/BeamSpillInfoRetriever/MWRData.h" +#include "larcorealg/CoreUtils/counter.h" +#include + +namespace sbn::pot{ + + typedef struct PTBInfo_t { + std::uint64_t currPTBTimeStamp = UINT64_MAX; ///< Timestamp in UTC nanoseconds since Unix epoch (converted from 20ns clock ticks) + std::uint64_t prevPTBTimeStamp = 0; + unsigned int GateCounter = 0; + bool isHLT = false; + uint64_t triggerWord = 0; ///< Timestamp in s since beam extraction signal + } PTBInfo_t; + + typedef struct TriggerInfo_t { + int gate_type = 0; ///< Source of the spill: `1`: BNB, `2`: NuMI + double t_current_event = 0; ///< Timestamp in UTC seconds since Unix epoch (converted from 20ns clock ticks) + double t_previous_event = 0; + unsigned int number_of_gates_since_previous_event = 0; + std::int64_t WR_to_Spill_conversion = 0; + } TriggerInfo_t; + + typedef struct MWRdata_t { + std::vector< std::vector > MWR_times; + std::vector< std::vector< std::vector< int > > > unpacked_MWR; + } MWRdata_t; + + /** + * @brief Extracts information from PTB for a single HLT for use in SBND POT accounting. + * + * @param cont_frags The PTB fragments to examine. + * @param HLT The high level trigger we are searching for. + */ + PTBInfo_t extractPTBInfo(art::Handle > cont_frags, int HLT); + + /** + * @brief Extracts ALL PTB information for using in SBND CAF files. + * + * @param cont_frags The PTB fragments to examine. + * @return Vector of PTBInfo_t containing all triggers found. + */ + std::vector extractAllPTBInfo(std::vector const& cont_frags); + + /** + * @brief Extracts information from TDC for use in SBND POT accounting. + * + * @param cont_frags The TDC fragments to examine. + */ + double extractTDCTimeStamp(art::Handle > cont_frags); + + /** + * @brief Checks for a known IFBeam issue where TOR clocks are incorrect. + * @return true if a time in IFBeam corresponds to a broken clock false if + * it is a real spill. + * @details Check validity by seeing if all devices fire at once or + * if time is just a single outlier. + * @param time The time for which we want to check for a spill. + * @param bfp beamfolder object to check. + */ + bool BrokenClock(double time, std::unique_ptr const& bfp); + + /** + * @brief extract spill times from the IFBeam data base. Used in SBND and ICARUS. + * + * @param triggerInfo triggerInfo object to match + * @param bfp beamfolder to match + * @param bfp_mwr mwr devices have separate daq system, much treat separately + * @param fTimePad Padding to use in ifbeam queries + * @param MWRtoroidDelay Delay between MWR and toroids + * @param mwrdata MWRData to unpack + */ + MWRdata_t extractSpillTimes(TriggerInfo_t const& triggerInfo, std::unique_ptr const& bfp, std::unique_ptr const& bfp_mwr, double fTimePad, double MWRtoroidDelay, sbn::MWRData mwrdata ); + /** + * @brief Compile spill information into BNBSpillInfo object + */ + sbn::BNBSpillInfo makeBNBSpillInfo(art::EventID const& eventID, double time, MWRdata_t const& MWRdata, std::vector const& matched_MWR, std::unique_ptr const& bfp, std::unique_ptr const& offsets, std::unique_ptr const& vp873); +} + +#endif diff --git a/sbncode/BeamSpillInfoRetriever/SBNDBNBEXTRetriever/CMakeLists.txt b/sbncode/BeamSpillInfoRetriever/SBNDBNBEXTRetriever/CMakeLists.txt index d7f43205a..8a16ef2e9 100644 --- a/sbncode/BeamSpillInfoRetriever/SBNDBNBEXTRetriever/CMakeLists.txt +++ b/sbncode/BeamSpillInfoRetriever/SBNDBNBEXTRetriever/CMakeLists.txt @@ -3,7 +3,7 @@ find_package(ifdh_art) cet_build_plugin(SBNDBNBEXTRetriever art::module LIBRARIES - sbn_SBNDPOTTools + sbn_POTTools ) install_headers() diff --git a/sbncode/BeamSpillInfoRetriever/SBNDBNBEXTRetriever/SBNDBNBEXTRetriever_module.cc b/sbncode/BeamSpillInfoRetriever/SBNDBNBEXTRetriever/SBNDBNBEXTRetriever_module.cc index 298734bff..a8b3f9d67 100644 --- a/sbncode/BeamSpillInfoRetriever/SBNDBNBEXTRetriever/SBNDBNBEXTRetriever_module.cc +++ b/sbncode/BeamSpillInfoRetriever/SBNDBNBEXTRetriever/SBNDBNBEXTRetriever_module.cc @@ -8,7 +8,7 @@ #include "art/Framework/Core/EDProducer.h" #include "messagefacility/MessageLogger/MessageLogger.h" #include "sbnobj/Common/POTAccounting/EXTCountInfo.h" -#include "sbncode/BeamSpillInfoRetriever/SBNDPOTTools.h" +#include "sbncode/BeamSpillInfoRetriever/POTTools.h" namespace sbn { class SBNDBNBEXTRetriever; @@ -28,11 +28,10 @@ class sbn::SBNDBNBEXTRetriever : public art::EDProducer { SBNDBNBEXTRetriever & operator = (SBNDBNBEXTRetriever const &) = delete; SBNDBNBEXTRetriever & operator = (SBNDBNBEXTRetriever &&) = delete; - private: // Declare member data here. std::vector< sbn::EXTCountInfo > fOutExtInfos; - TriggerInfo_t extractTriggerInfo(art::Event const& e) const; + sbn::pot::TriggerInfo_t extractTriggerInfo(art::Event const& e) const; // input labels float TotalEXTCounts; }; @@ -47,7 +46,7 @@ void sbn::SBNDBNBEXTRetriever::produce(art::Event & e) { art::InputTag PTB_itag("daq", "ContainerPTB"); auto PTB_cont_frags = e.getHandle(PTB_itag); - PTBInfo_t PTBInfo = extractPTBInfo(PTB_cont_frags, 4); + sbn::pot::PTBInfo_t PTBInfo = sbn::pot::extractPTBInfo(PTB_cont_frags, 4); int SingleEventGateCounter = PTBInfo.GateCounter; TotalEXTCounts += SingleEventGateCounter; diff --git a/sbncode/BeamSpillInfoRetriever/SBNDBNBRetriever/CMakeLists.txt b/sbncode/BeamSpillInfoRetriever/SBNDBNBRetriever/CMakeLists.txt index ea89ae916..efbf13789 100644 --- a/sbncode/BeamSpillInfoRetriever/SBNDBNBRetriever/CMakeLists.txt +++ b/sbncode/BeamSpillInfoRetriever/SBNDBNBRetriever/CMakeLists.txt @@ -3,7 +3,8 @@ find_package(ifdh_art) cet_build_plugin(SBNDBNBRetriever art::module LIBRARIES - sbn_SBNDPOTTools + sbn_POTTools + sbn_getFOM ) install_headers() diff --git a/sbncode/BeamSpillInfoRetriever/SBNDBNBRetriever/SBNDBNBRetriever_module.cc b/sbncode/BeamSpillInfoRetriever/SBNDBNBRetriever/SBNDBNBRetriever_module.cc index 5e709c2b0..b76da2059 100644 --- a/sbncode/BeamSpillInfoRetriever/SBNDBNBRetriever/SBNDBNBRetriever_module.cc +++ b/sbncode/BeamSpillInfoRetriever/SBNDBNBRetriever/SBNDBNBRetriever_module.cc @@ -8,7 +8,8 @@ #include "art/Framework/Core/EDProducer.h" #include "messagefacility/MessageLogger/MessageLogger.h" #include "sbnobj/Common/POTAccounting/BNBSpillInfo.h" -#include "sbncode/BeamSpillInfoRetriever/SBNDPOTTools.h" +#include "sbncode/BeamSpillInfoRetriever/POTTools.h" +#include "sbncode/BeamSpillInfoRetriever/getFOM.h" namespace sbn { class SBNDBNBRetriever; @@ -35,16 +36,19 @@ class sbn::SBNDBNBRetriever : public art::EDProducer { double fBESOffset; std::string fDeviceUsedForTiming; std::unique_ptr bfp; + std::unique_ptr offsets; + std::unique_ptr vp873; std::unique_ptr bfp_mwr; + sbn::MWRData mwrdata; art::ServiceHandle ifbeam_handle; static constexpr double MWRtoroidDelay = -0.035; ///< the same time point is measured _t_ by MWR and _t + MWRtoroidDelay`_ by the toroid [ms] - TriggerInfo_t extractTriggerInfo(art::Event const& e) const; + sbn::pot::TriggerInfo_t extractTriggerInfo(art::Event const& e) const; int matchMultiWireData( art::EventID const& eventID, - TriggerInfo_t const& triggerInfo, - MWRdata_t const& MWRdata, + sbn::pot::TriggerInfo_t const& triggerInfo, + sbn::pot::MWRdata_t const& MWRdata, std::vector< sbn::BNBSpillInfo >& beamInfos ) const; unsigned int TotalBeamSpills; @@ -56,12 +60,19 @@ sbn::SBNDBNBRetriever::SBNDBNBRetriever(fhicl::ParameterSet const & params) fTimePad = params.get("TimePadding"); fBESOffset = params.get("BESOffset"); fDeviceUsedForTiming = params.get("DeviceUsedForTiming"); - bfp = ifbeam_handle->getBeamFolder(params.get("Bundle"), params.get("URL"), std::stod(params.get("TimeWindow"))); + double const timeWindow = std::stod(params.get("TimeWindow")); + bfp = ifbeam_handle->getBeamFolder(params.get("Bundle"), params.get("URL"), timeWindow); bfp->set_epsilon(0.02); bfp_mwr = ifbeam_handle->getBeamFolder(params.get("MultiWireBundle"), params.get("URL"), std::stod(params.get("MWR_TimeWindow"))); bfp_mwr->set_epsilon(0.5); bfp_mwr->setValidWindow(3605); TotalBeamSpills = 0; + vp873 = ifbeam_handle->getBeamFolder(params.get("VP873Bundle"), params.get("URL"), timeWindow); + vp873->set_epsilon(0.02); + + offsets = ifbeam_handle->getBeamFolder(params.get("OffsetBundle"), params.get("URL"), timeWindow); + offsets->set_epsilon(1000); + } void sbn::SBNDBNBRetriever::produce(art::Event & e) @@ -77,14 +88,14 @@ void sbn::SBNDBNBRetriever::produce(art::Event & e) } mf::LogDebug("SBNDBNBRetriever")<< "ptb_event: " << e.event() << std::endl; - TriggerInfo_t const triggerInfo = extractTriggerInfo(e); + sbn::pot::TriggerInfo_t const triggerInfo = extractTriggerInfo(e); if (triggerInfo.t_previous_event == 0) { return; } TotalBeamSpills += triggerInfo.number_of_gates_since_previous_event; - MWRdata_t const MWRdata = extractSpillTimes(triggerInfo, bfp, bfp_mwr, fTimePad, MWRtoroidDelay, mwrdata); + sbn::pot::MWRdata_t const MWRdata = extractSpillTimes(triggerInfo, bfp, bfp_mwr, fTimePad, MWRtoroidDelay, mwrdata); int const spill_count = matchMultiWireData(e.id(), triggerInfo, MWRdata, fOutbeamInfos); @@ -94,7 +105,7 @@ void sbn::SBNDBNBRetriever::produce(art::Event & e) mf::LogDebug("SBNDBNBRetriever")<< "Event Spills : " << spill_count << ", DAQ Spills : " << triggerInfo.number_of_gates_since_previous_event << std::endl; } -sbn::TriggerInfo_t sbn::SBNDBNBRetriever::extractTriggerInfo(art::Event const& e) const { +sbn::pot::TriggerInfo_t sbn::SBNDBNBRetriever::extractTriggerInfo(art::Event const& e) const { // Using TDC for current event, but PTB for previous event. Exception for case where no TDC. art::InputTag PTB_itag("daq", "ContainerPTB"); auto PTB_cont_frags = e.getHandle(PTB_itag); @@ -102,23 +113,23 @@ sbn::TriggerInfo_t sbn::SBNDBNBRetriever::extractTriggerInfo(art::Event const& e art::InputTag TDC_itag("daq", "ContainerTDCTIMESTAMP"); auto TDC_cont_frags = e.getHandle(TDC_itag); - TriggerInfo_t triggerInfo; - PTBInfo_t PTBInfo = extractPTBInfo(PTB_cont_frags, 2); + sbn::pot::TriggerInfo_t triggerInfo; + sbn::pot::PTBInfo_t PTBInfo = sbn::pot::extractPTBInfo(PTB_cont_frags, 2); if (TDC_cont_frags) { - double TDCTimeStamp = extractTDCTimeStamp(TDC_cont_frags); + double TDCTimeStamp = sbn::pot::extractTDCTimeStamp(TDC_cont_frags); triggerInfo.t_current_event = TDCTimeStamp - fBESOffset; } else{ // If missing TDC, use PTB instead mf::LogDebug("SBNDBNBRetriever") << " Missing TDC Container Fragments!!! " << std::endl; - triggerInfo.t_current_event = PTBInfo.currPTBTimeStamp - fBESOffset; + triggerInfo.t_current_event = PTBInfo.currPTBTimeStamp / 1e9 - fBESOffset; } - triggerInfo.t_previous_event = PTBInfo.prevPTBTimeStamp - fBESOffset; + triggerInfo.t_previous_event = PTBInfo.prevPTBTimeStamp / 1e9 - fBESOffset; triggerInfo.number_of_gates_since_previous_event = PTBInfo.GateCounter; - double PTBandCurrOffset = PTBInfo.currPTBTimeStamp - triggerInfo.t_current_event - fBESOffset; + double PTBandCurrOffset = PTBInfo.currPTBTimeStamp / 1e9 - triggerInfo.t_current_event - fBESOffset; // Catch for an issue seen a few times where PTB off by a second. // Only need to correct prevTS because either currTS is from TDC @@ -134,8 +145,8 @@ sbn::TriggerInfo_t sbn::SBNDBNBRetriever::extractTriggerInfo(art::Event const& e int sbn::SBNDBNBRetriever::matchMultiWireData( art::EventID const& eventID, - TriggerInfo_t const& triggerInfo, - MWRdata_t const& MWRdata, + sbn::pot::TriggerInfo_t const& triggerInfo, + sbn::pot::MWRdata_t const& MWRdata, std::vector< sbn::BNBSpillInfo >& beamInfos ) const { @@ -164,7 +175,7 @@ int sbn::SBNDBNBRetriever::matchMultiWireData( spills_removed++; continue;} - if(BrokenClock(times_temps[i], bfp)){ + if(sbn::pot::BrokenClock(times_temps[i], bfp)){ continue; } @@ -210,8 +221,12 @@ int sbn::SBNDBNBRetriever::matchMultiWireData( }//end loop over MWR devices - sbn::BNBSpillInfo spillInfo = makeBNBSpillInfo(eventID, times_temps[i], MWRdata, matched_MWR, bfp); - + sbn::BNBSpillInfo spillInfo = makeBNBSpillInfo(eventID, times_temps[i], MWRdata, matched_MWR, bfp, offsets, vp873); + std::tuple allFOM = sbn::getBNBqualityFOM(spillInfo); + spillInfo.FOM = std::get<0>(allFOM); + spillInfo.PreFitFOM = std::get<1>(allFOM); + spillInfo.NoMultiWireFOM = std::get<2>(allFOM); + beamInfos.push_back(std::move(spillInfo)); // We do not write these to the art::Events because diff --git a/sbncode/BeamSpillInfoRetriever/SBNDBNBZEROBIASRetriever/CMakeLists.txt b/sbncode/BeamSpillInfoRetriever/SBNDBNBZEROBIASRetriever/CMakeLists.txt index d1ef9e65f..7890fbd93 100644 --- a/sbncode/BeamSpillInfoRetriever/SBNDBNBZEROBIASRetriever/CMakeLists.txt +++ b/sbncode/BeamSpillInfoRetriever/SBNDBNBZEROBIASRetriever/CMakeLists.txt @@ -3,7 +3,8 @@ find_package(ifdh_art) cet_build_plugin(SBNDBNBZEROBIASRetriever art::module LIBRARIES - sbn_SBNDPOTTools + sbn_POTTools + sbn_getFOM ) install_headers() diff --git a/sbncode/BeamSpillInfoRetriever/SBNDBNBZEROBIASRetriever/SBNDBNBZEROBIASRetriever_module.cc b/sbncode/BeamSpillInfoRetriever/SBNDBNBZEROBIASRetriever/SBNDBNBZEROBIASRetriever_module.cc index fa7e8b793..f301eff14 100644 --- a/sbncode/BeamSpillInfoRetriever/SBNDBNBZEROBIASRetriever/SBNDBNBZEROBIASRetriever_module.cc +++ b/sbncode/BeamSpillInfoRetriever/SBNDBNBZEROBIASRetriever/SBNDBNBZEROBIASRetriever_module.cc @@ -8,7 +8,8 @@ #include "art/Framework/Core/EDProducer.h" #include "messagefacility/MessageLogger/MessageLogger.h" #include "sbnobj/Common/POTAccounting/BNBSpillInfo.h" -#include "sbncode/BeamSpillInfoRetriever/SBNDPOTTools.h" +#include "sbncode/BeamSpillInfoRetriever/POTTools.h" +#include "sbncode/BeamSpillInfoRetriever/getFOM.h" namespace sbn { class SBNDBNBZEROBIASRetriever; @@ -34,6 +35,8 @@ class sbn::SBNDBNBZEROBIASRetriever : public art::EDProducer { double fBESOffset; std::string fDeviceUsedForTiming; std::unique_ptr bfp; + std::unique_ptr offsets; + std::unique_ptr vp873; std::unique_ptr bfp_mwr; sbn::MWRData mwrdata; art::ServiceHandle ifbeam_handle; @@ -41,11 +44,11 @@ class sbn::SBNDBNBZEROBIASRetriever : public art::EDProducer { std::vector< sbn::BNBSpillInfo > fOutbeamInfos; std::vector< sbn::BNBSpillInfo > fOutbeamInfosTotal; - TriggerInfo_t extractTriggerInfo(art::Event const& e) const; + sbn::pot::TriggerInfo_t extractTriggerInfo(art::Event const& e) const; void matchMultiWireData( art::EventID const& eventID, - TriggerInfo_t const& triggerInfo, - MWRdata_t const& MWRdata, + sbn::pot::TriggerInfo_t const& triggerInfo, + sbn::pot::MWRdata_t const& MWRdata, std::vector< sbn::BNBSpillInfo >& beamInfos ) const; unsigned int TotalBeamSpills; @@ -56,11 +59,18 @@ sbn::SBNDBNBZEROBIASRetriever::SBNDBNBZEROBIASRetriever(fhicl::ParameterSet cons fTimePad = params.get("TimePadding"); fDeviceUsedForTiming = params.get("DeviceUsedForTiming"); fBESOffset = params.get("BESOffset"); - bfp = ifbeam_handle->getBeamFolder(params.get("Bundle"), params.get("URL"), std::stod(params.get("TimeWindow"))); + const double timeWindow = std::stod(params.get("TimeWindow")); + bfp = ifbeam_handle->getBeamFolder(params.get("Bundle"), params.get("URL"), timeWindow); bfp->set_epsilon(0.02); bfp_mwr = ifbeam_handle->getBeamFolder(params.get("MultiWireBundle"), params.get("URL"), std::stod(params.get("MWR_TimeWindow"))); bfp_mwr->set_epsilon(0.5); bfp_mwr->setValidWindow(3605); + vp873 = ifbeam_handle->getBeamFolder(params.get("VP873Bundle"), params.get("URL"), timeWindow); + vp873->set_epsilon(0.02); + + offsets = ifbeam_handle->getBeamFolder(params.get("OffsetBundle"), params.get("URL"), timeWindow); + offsets->set_epsilon(600); + TotalBeamSpills = 0; produces< std::vector< sbn::BNBSpillInfo >, art::InEvent >(); produces< std::vector< sbn::BNBSpillInfo >, art::InSubRun >(); @@ -79,7 +89,7 @@ void sbn::SBNDBNBZEROBIASRetriever::produce(art::Event & e) return; } - TriggerInfo_t const triggerInfo = extractTriggerInfo(e); + sbn::pot::TriggerInfo_t const triggerInfo = extractTriggerInfo(e); if (triggerInfo.t_previous_event == 0) { auto p = std::make_unique< std::vector< sbn::BNBSpillInfo > >(); @@ -89,7 +99,7 @@ void sbn::SBNDBNBZEROBIASRetriever::produce(art::Event & e) } TotalBeamSpills += triggerInfo.number_of_gates_since_previous_event; - MWRdata_t const MWRdata = extractSpillTimes(triggerInfo, bfp, bfp_mwr, fTimePad, MWRtoroidDelay, mwrdata); + sbn::pot::MWRdata_t const MWRdata = extractSpillTimes(triggerInfo, bfp, bfp_mwr, fTimePad, MWRtoroidDelay, mwrdata); matchMultiWireData(e.id(), triggerInfo, MWRdata, fOutbeamInfos); fOutbeamInfosTotal.insert(fOutbeamInfosTotal.end(),fOutbeamInfos.begin(),fOutbeamInfos.end()); @@ -98,7 +108,7 @@ void sbn::SBNDBNBZEROBIASRetriever::produce(art::Event & e) e.put(std::move(p)); } -sbn::TriggerInfo_t sbn::SBNDBNBZEROBIASRetriever::extractTriggerInfo(art::Event const& e) const { +sbn::pot::TriggerInfo_t sbn::SBNDBNBZEROBIASRetriever::extractTriggerInfo(art::Event const& e) const { // Using TDC for current event, but PTB for previous event art::InputTag PTB_itag("daq", "ContainerPTB"); auto PTB_cont_frags = e.getHandle(PTB_itag); @@ -106,23 +116,23 @@ sbn::TriggerInfo_t sbn::SBNDBNBZEROBIASRetriever::extractTriggerInfo(art::Event art::InputTag TDC_itag("daq", "ContainerTDCTIMESTAMP"); auto TDC_cont_frags = e.getHandle(TDC_itag); - TriggerInfo_t triggerInfo; - PTBInfo_t PTBInfo = extractPTBInfo(PTB_cont_frags, 1); + sbn::pot::TriggerInfo_t triggerInfo; + sbn::pot::PTBInfo_t PTBInfo = sbn::pot::extractPTBInfo(PTB_cont_frags, 1); if (TDC_cont_frags) { - double TDCTimeStamp = extractTDCTimeStamp(TDC_cont_frags); + double TDCTimeStamp = sbn::pot::extractTDCTimeStamp(TDC_cont_frags); triggerInfo.t_current_event = TDCTimeStamp - fBESOffset; } else{ // If missing TDC, use PTB instead mf::LogDebug("SBNDBNBZEROBIASRetriever") << " Missing TDC Container Fragments!!!" << std::endl; - triggerInfo.t_current_event = PTBInfo.currPTBTimeStamp - fBESOffset; + triggerInfo.t_current_event = PTBInfo.currPTBTimeStamp / 1e9 - fBESOffset; } - triggerInfo.t_previous_event = PTBInfo.prevPTBTimeStamp - fBESOffset; + triggerInfo.t_previous_event = PTBInfo.prevPTBTimeStamp / 1e9 - fBESOffset; triggerInfo.number_of_gates_since_previous_event = PTBInfo.GateCounter; - double PTBandCurrOffset = PTBInfo.currPTBTimeStamp - triggerInfo.t_current_event - fBESOffset; + double PTBandCurrOffset = PTBInfo.currPTBTimeStamp / 1e9 - triggerInfo.t_current_event - fBESOffset; // Catch for an issue seen a few times where PTB off by a second. // Only need to correct prevTS because either currTS is from TDC @@ -138,8 +148,8 @@ sbn::TriggerInfo_t sbn::SBNDBNBZEROBIASRetriever::extractTriggerInfo(art::Event void sbn::SBNDBNBZEROBIASRetriever::matchMultiWireData( art::EventID const& eventID, - TriggerInfo_t const& triggerInfo, - MWRdata_t const& MWRdata, + sbn::pot::TriggerInfo_t const& triggerInfo, + sbn::pot::MWRdata_t const& MWRdata, std::vector< sbn::BNBSpillInfo >& beamInfos ) const { @@ -174,7 +184,7 @@ void sbn::SBNDBNBZEROBIASRetriever::matchMultiWireData( spills_removed++; continue;} - if(BrokenClock(times_temps[i], bfp)){ + if(sbn::pot::BrokenClock(times_temps[i], bfp)){ continue; } @@ -217,7 +227,12 @@ void sbn::SBNDBNBZEROBIASRetriever::matchMultiWireData( }//end loop over MWR times }//end loop over MWR devices - sbn::BNBSpillInfo spillInfo = makeBNBSpillInfo(eventID, times_temps[i], MWRdata, matched_MWR, bfp); + sbn::BNBSpillInfo spillInfo = makeBNBSpillInfo(eventID, times_temps[i], MWRdata, matched_MWR, bfp, offsets, vp873); + std::tuple allFOM = sbn::getBNBqualityFOM(spillInfo); + spillInfo.FOM = std::get<0>(allFOM); + spillInfo.PreFitFOM = std::get<1>(allFOM); + spillInfo.NoMultiWireFOM = std::get<2>(allFOM); + beamInfos.push_back(std::move(spillInfo)); // We do not write these to the art::Events because diff --git a/sbncode/BeamSpillInfoRetriever/SBNDPOTTools.h b/sbncode/BeamSpillInfoRetriever/SBNDPOTTools.h deleted file mode 100644 index 46d16c709..000000000 --- a/sbncode/BeamSpillInfoRetriever/SBNDPOTTools.h +++ /dev/null @@ -1,42 +0,0 @@ -#ifndef _SBNDPOTTOOLS_H -#define _SBNDPOTTOOLS_H - -#include "art/Framework/Principal/Event.h" -#include "art/Framework/Principal/Handle.h" -#include "messagefacility/MessageLogger/MessageLogger.h" -#include "ifdh_art/IFBeamService/IFBeam_service.h" - -#include "sbnobj/Common/POTAccounting/BNBSpillInfo.h" -#include "sbndaq-artdaq-core/Overlays/SBND/PTBFragment.hh" -#include "sbndaq-artdaq-core/Overlays/SBND/TDCTimestampFragment.hh" -#include "artdaq-core/Data/ContainerFragment.hh" - -#include "sbncode/BeamSpillInfoRetriever/MWRData.h" -#include "larcorealg/CoreUtils/counter.h" - -namespace sbn{ - typedef struct PTBInfo_t { - double currPTBTimeStamp = 1e20; - double prevPTBTimeStamp = 0; - unsigned int GateCounter = 0; - } PTBInfo_t; - - typedef struct TriggerInfo_t { - double t_current_event = 0; - double t_previous_event = 0; - unsigned int number_of_gates_since_previous_event = 0; - } TriggerInfo_t; - - typedef struct MWRdata_t { - std::vector< std::vector > MWR_times; - std::vector< std::vector< std::vector< int > > > unpacked_MWR; - } MWRdata_t; - - PTBInfo_t extractPTBInfo(art::Handle > cont_frags, int HLT); - double extractTDCTimeStamp(art::Handle > cont_frags); - bool BrokenClock(double time, std::unique_ptr const& bfp); - MWRdata_t extractSpillTimes(TriggerInfo_t const& triggerInfo, std::unique_ptr const& bfp, std::unique_ptr const& bfp_mwr, double fTimePad, double MWRtoroidDelay, sbn::MWRData mwrdata ); - sbn::BNBSpillInfo makeBNBSpillInfo(art::EventID const& eventID, double time, MWRdata_t const& MWRdata, std::vector const& matched_MWR, std::unique_ptr const& bfp); -} - -#endif diff --git a/sbncode/BeamSpillInfoRetriever/getFOM.cpp b/sbncode/BeamSpillInfoRetriever/getFOM.cpp new file mode 100644 index 000000000..e8447cfae --- /dev/null +++ b/sbncode/BeamSpillInfoRetriever/getFOM.cpp @@ -0,0 +1,491 @@ +/** + * @file sbncode/BeamSpillInfoRetriever/getFOM.cpp + * @brief Figure of Merit for BNB Spills using SBND information adapted from MicroBooNE FOM + * @author Max Dubnowski (maxdub@sas.upenn.edu or `@Max Dubnowski` on SBN Slack) + */ +#include "sbncode/BeamSpillInfoRetriever/getFOM.h" +#include +#include "TH1D.h" +#include "TFitResult.h" +#include + + +using namespace std; + + +namespace sbn { + + bool onePlot = true; + + std::tuple getBNBqualityFOM(BNBSpillInfo & spill ) + { + double fom=0; + double prefitfom=0; + double noMWfom=0; + + double hp875_offset= spill.HP875Offset; + double vp875_offset= spill.VP875Offset; + double vp873_offset= spill.VP873Offset; + double hptg1_offset= spill.HPTG1Offset; + //double vptg1_offset= spill.VPTG1Offset; + double hptg2_offset= spill.HPTG2Offset; + double vptg2_offset= spill.VPTG2Offset; + + //Decides which position monitor to check first + int useHTG = 1; + int useVTG = 1; + + //Z Position of the monitors in m + double const vp873_zpos= 191.153656; + double const hp875_zpos= 202.116104; + double const vp875_zpos= 202.3193205; + double const hptg1_zpos= 204.833267; + //double const vptg1_zpos= 204.629608; + double const hptg2_zpos= 205.240662; + double const vptg2_zpos= 205.036835; + double const target_center_zpos= 206.870895; + double const p875x[]={0.431857, 0.158077, 0.00303551}; + double const p875y[]={0.279128, 0.337048, 0}; + double const p876x[]={0.166172, 0.30999, -0.00630299}; + double const p876y[]={0.13425, 0.580862, 0}; + + + std::vector tor860; + std::vector tor875; + std::vector hp875; + std::vector vp875; + std::vector vp873; + std::vector hptg1; + std::vector vptg1; + std::vector hptg2; + std::vector vptg2; + + std::vector m875hs; // Multiwire station after Mag 875, Fit to Horizontal Sigma + std::vector m875hm; // Multiwire station after Mag 875, Fit to Horizontal Mean + std::vector m875vs; // Multiwire station after Mag 875, Fit to Vertical Sigma + std::vector m875vm; // Multiwire station after Mag 875, Fit to Vertical Mean + + std::vector m876hs; // Multiwire station after Mag 876, Fit to Horizontal Sigma + std::vector m876hm; // Multiwire station after Mag 876, Fit to Horizontal Mean + std::vector m876vs; // Multiwire station after Mag 876, Fit to Vertical Sigma + std::vector m876vm; // Multiwire station after Mag 876, Fit to Vertical Mean + + std::vector mw875(spill.M875BB.begin(), spill.M875BB.end()); + std::vector mw876(spill.M876BB.begin(), spill.M876BB.end()); + std::vector mwtgt(spill.MMBTBB.begin(), spill.MMBTBB.end()); + + + tor860.push_back(spill.TOR860); + tor875.push_back(spill.TOR875); + hp875.push_back(spill.HP875); + vp875.push_back(spill.VP875); + vp873.push_back(spill.VP873); + hptg1.push_back(spill.HPTG1); + vptg1.push_back(spill.VPTG1); + hptg2.push_back(spill.HPTG2); + vptg2.push_back(spill.VPTG2); + + m875hs.push_back(spill.M875HS); + m875hm.push_back(spill.M875HM); + m875vs.push_back(spill.M875VS); + m875vm.push_back(spill.M875VM); + + m876hs.push_back(spill.M876HS); + m876hm.push_back(spill.M876HM); + m876vs.push_back(spill.M876VS); + m876vm.push_back(spill.M876VM); + + double tor; + if (!tor860.empty()) + tor=tor860[0]; + else if (!tor875.empty()) + tor=tor875[0]; + else + return {-1,-1,-1}; + + /** + * @brief when creating ntuples for pot counting script the variables are filled with -999 + * this could create a difference when passing events with FOM>1 since + * events with missing BPM data would get FOM=2, while events with BPM set to -999 will get FOM=0 + * bad or missing MWR data gets FOM 4 in either case + */ + if (hptg2.empty()) hptg2.push_back(-999); + if (hptg1.empty()) hptg1.push_back(-999); + if (hp875.empty()) hp875.push_back(-999); + if (vptg2.empty()) vptg2.push_back(-999); + if (vptg1.empty()) vptg1.push_back(-999); + if (vp875.empty()) vp875.push_back(-999); + if (vp873.empty()) vp873.push_back(-999); + double horang; + + auto interpolate_hp875 = [delta_hp875=(hp875[0]-hp875_offset), hp875_zpos, target_center_zpos] + (double delta_value, double zpos) + { + double const ang = (delta_value-delta_hp875)/(zpos-hp875_zpos); + double const pos = delta_hp875+ang*(target_center_zpos-hp875_zpos); + return std::pair(ang, pos); + }; + + // return 2 when missing essential beam horizontal position data: + if (hp875.empty() || (hptg1.empty() && hptg2.empty())) return {2,2,2}; + bool const doUseHTG1 = (useHTG == 1) || hptg2.empty(); + auto const [ Tanhorang, horpos ] = doUseHTG1? + interpolate_hp875(hptg1[0] - hptg1_offset, hptg1_zpos): + interpolate_hp875(hptg2[0] - hptg2_offset, hptg2_zpos); + + + double verang; + auto interpolate_vp875 = [delta_vp875=(vp875[0]-vp875_offset), vp875_zpos, target_center_zpos] + (double delta_value, double zpos) + { + double const ang = (delta_value-delta_vp875)/(zpos-vp875_zpos); + double const pos = delta_vp875+ang*(target_center_zpos-vp875_zpos); + return std::pair(ang, pos); + }; + + // return 3 when missing essential beam horizontal position data: + if (vp875.empty() || (vptg1.empty() && vptg2.empty())) return {3,3,3}; + bool const doUseVTG1 = (useVTG == 1) || vptg2.empty(); + auto const [ Tanverang, verpos ] = doUseVTG1? + interpolate_vp875(vp873[0] - vp873_offset, vp873_zpos): + interpolate_vp875(vptg2[0] - vptg2_offset, vptg2_zpos); + + horang=atan(Tanhorang); + verang=atan(Tanverang); + double xx,yy,sx,sy,chi2x,chi2y; + double tgtsx, tgtsy; + bool good_tgt=false; + bool good_876=false; + bool good_875=false; + constexpr size_t FirstXMWtgt =0; + constexpr size_t FirstYMWtgt =48; + const double smallSigmaX =0.5, largeSigmaX = 10, smallSigmaY = 0.3, largeSigmaY =10, maxChi2X = 20, maxChi2Y = 20; + + // First calculate try the version of the FOM where we fit the width ourselves + if (mwtgt.size()>0) { + processBNBprofile(&mwtgt[FirstXMWtgt], xx, sx,chi2x); + processBNBprofile(&mwtgt[FirstYMWtgt], yy, sy, chi2y); + if (sx>smallSigmaX && sxsmallSigmaY && sy0) { + processBNBprofile(&mw876[FirstXMWtgt], xx,sx,chi2x); + processBNBprofile(&mw876[FirstYMWtgt], yy,sy,chi2y); + double tgtsx876=p876x[0]+p876x[1]*sx+p876x[2]*sx*sx; + double tgtsy876=p876y[0]+p876y[1]*sy+p876y[2]*sy*sy; + if (tgtsx876>smallSigmaX && tgtsx876smallSigmaY && tgtsy8760){ + processBNBprofile(&mw875[FirstXMWtgt], xx,sx,chi2x); + processBNBprofile(&mw875[FirstYMWtgt], yy,sy,chi2y); + double tgtsx875=p875x[0]+p875x[1]*sx+p875x[2]*sx*sx; + double tgtsy875=p875y[0]+p875y[1]*sy+p875y[2]*sy*sy; + if (tgtsx875>smallSigmaX && tgtsx875smallSigmaY && tgtsy875smallSigmaX && tgtsx876smallSigmaY && tgtsy876smallSigmaX && tgtsx875smallSigmaY && tgtsy875SetBinContent(i+1,-mwdata[i]-minx); + if (-mwdata[i]-minx > threshold && first_x==-1) first_x=i; + if (-mwdata[i]-minx > threshold) last_x=i+1; + hProf->SetBinError(i+1,error); + } + if (hProf->GetSumOfWeights()>0) { + TFitResultPtr const fit = hProf->Fit("gaus","QNS","",-12+first_x*0.5,-12+last_x*0.5); + x = fit->Parameter(1); + sx = fit->Parameter(2); + chi2= fit->Chi2() / fit->Ndf(); + delete hProf; + } else { + x=99999; + sx=99999; + chi2=99999; + } + entry += 1; + } + + + double calcFOM(double horpos, double horang, double verpos, double verang, double ppp, double tgtsx, double tgtsy) + { + ppp /= 1e12; //converts to 10^12 POT + + + //code from MiniBooNE AnalysisFramework with the addition of scaling the beam profile to match tgtsx, tgtsy + //form DQ_BeamLine_twiss_init.F + double bx = 4.68; + double ax = 0.0389; + double gx = (1+ax*ax)/bx; + double nx = 0.0958; + double npx = -0.0286; + double by = 59.12; + double ay = 2.4159; + double gy = (1+ay*ay)/by; + double ny = 0.4577; + double npy = -0.0271; + //from DQ_BeamLine_make_tgt_fom2.F + double ex = 0.1775E-06 + 0.1827E-07*ppp; + double ey = 0.1382E-06 + 0.2608E-08*ppp; + double dp = 0.4485E-03 + 0.6100E-04*ppp; + double tex = ex; + double tey = ey; + double tdp = dp; + //from DQ_BeamLine_beam_init.F + double sigma1[6][6]={{0}}; + double centroid1[6]={0}; + centroid1[0] = horpos; + centroid1[1] = horang; + centroid1[2] = verpos; + centroid1[3] = verang; + centroid1[4] = 0.0; + centroid1[5] = 0.0; + sigma1[5][5] = tdp*tdp; + sigma1[0][0] = tex*bx+ nx*nx *tdp*tdp; + sigma1[0][1] = -tex*ax+ nx*npx*tdp*tdp; + sigma1[1][1] = tex*gx+npx*npx*tdp*tdp; + sigma1[0][5] = nx*tdp*tdp; + sigma1[1][5] = npx*tdp*tdp; + sigma1[1][0] = sigma1[0][1]; + sigma1[5][0] = sigma1[0][5]; + sigma1[5][1] = sigma1[1][5]; + sigma1[2][2] = tey*by+ny*ny*tdp*tdp; + sigma1[2][3] = -tey*ay+ny*npy*tdp*tdp; + sigma1[3][3] = tey*gy+npy*npy*tdp*tdp; + sigma1[2][5] = ny*tdp*tdp; + sigma1[3][5] = npy*tdp*tdp; + sigma1[3][2] = sigma1[2][3]; + sigma1[5][2] = sigma1[2][5]; + sigma1[5][3] = sigma1[3][5]; + + + double begtocnt[6][6]={ + { 0.65954, 0.43311, 0.00321, 0.10786, 0.00000, 1.97230}, + { 0.13047, 1.60192, 0.00034, 0.00512, 0.00000, 1.96723}, + {-0.00287, -0.03677, -0.35277, -4.68056, 0.00000, 0.68525}, + {-0.00089, -0.00430, -0.17722, -5.18616, 0.00000, 0.32300}, + {-0.00104, 0.00232, -0.00001, -0.00224, 1.00000, -0.00450}, + { 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 1.00000} + }; + double cnttoups[6][6]={{0}}; + double cnttodns[6][6]={{0}}; + double identity[6][6]={{0}}; + double begtoups[6][6]={{0}}; + double begtodns[6][6]={{0}}; + for (int i=0;i<6;i++) { + for (int j=0;j<6;j++) { + if (i==j) { + cnttoups[i][j] = 1.0; + cnttodns[i][j] = 1.0; + identity[i][j] = 1.0; + } else { + cnttoups[i][j] = 0.0; + cnttodns[i][j] = 0.0; + identity[i][j] = 0.0; + } + } + } + cnttoups[0][1] = -0.35710; + cnttoups[2][3] = -0.35710; + cnttodns[0][1] = +0.35710; + cnttodns[2][3] = +0.35710; + for (int i=0;i<6;i++) { + for (int j=0;j<6;j++) { + for (int k=0;k<6;k++) { + begtoups[i][k] = begtoups[i][k] + cnttoups[i][j]*begtocnt[j][k]; + begtodns[i][k] = begtodns[i][k] + cnttodns[i][j]*begtocnt[j][k]; + } + } + } + //swim to upstream of target + double cx, cy, sx, sy, rho; + sbn::swimBNB(centroid1,sigma1, + cnttoups, begtoups, + cx, cy, sx, sy, rho); + + double scalex, scaley; + if( tgtsx !=-999 && tgtsy!=-999){ + scalex=tgtsx/sx; + scaley=tgtsy/sy; + } + else{ + scalex=1; + scaley=1; + } + double fom_a=sbn::func_intbivar(cx, cy, sx*scalex, sy*scaley, rho); + //swim to center of target + sbn::swimBNB(centroid1,sigma1, + identity, begtocnt, + cx, cy, sx, sy, rho); + double fom_b=sbn::func_intbivar(cx, cy, sx*scalex, sy*scaley, rho); + //swim to downstream of target + sbn::swimBNB(centroid1,sigma1, + cnttodns, begtodns, + cx, cy, sx, sy, rho); + double fom_c=sbn::func_intbivar(cx, cy, sx*scalex, sy*scaley, rho); + // add a guard for double precision + + if(fom_a <= -10000. || fom_b <= -10000. || fom_c <= -10000) return -10000.; + double fom2=fom_a*0.6347 + + fom_b*0.2812 + + fom_c*0.0841; + return fom2; + } + + void swimBNB(const double centroid1[6], const double sigma1[6][6], + const double xferc[6][6], const double xfers[6][6], + double &cx, double& cy, double &sx, double &sy, double &rho) + { + //centroid + double centroid2[6]={0}; + for (int i=0;i<6;i++) { + for (int j=0;j<6;j++) { + centroid2[i] = centroid2[i] + xferc[i][j]*centroid1[j]; + } + } + cx = centroid2[0]; + cy = centroid2[2]; + //sigma + double sigma2[6][6]={{0}}; + for (int i = 0; i<6;i++) { + for (int j = 0;j<6;j++) { + for (int k = 0;k<6;k++) { + for (int m = 0;m<6;m++) { + sigma2[i][m] = sigma2[i][m] + xfers[i][j]*sigma1[j][k]*xfers[m][k]; + } + } + } + } + //get beam sigma + sx = sqrt(sigma2[0][0])*1000.0; + sy = sqrt(sigma2[2][2])*1000.0; + rho = sigma2[0][2]/sqrt(sigma2[0][0]*sigma2[2][2]); + } + + + + double func_intbivar(const double cx, const double cy, const double sx, const double sy, const double rho ) + { + //integrate beam overlap with target cylinder + double x0 = cx; + double y0 = cy; + double dbin = 0.1; + double dx = dbin; + double dy = dbin; + double r = 4.75; + double rr = r*r; + double rho2 = rho*rho; + double xmin = -r; + double ymin = -r; + int imax = round((2.0*r)/dx); + int jmax = round((2.0*r)/dy); + double sum = 0.0; + double x = xmin; + for (int i=0;i<=imax;i++) { + double y = ymin; + for (int j=0;j<=jmax;j++) { + if ( (x*x+y*y)= 1.) return -10000.; + return log10(1-sum); + } + +} diff --git a/sbncode/BeamSpillInfoRetriever/getFOM.h b/sbncode/BeamSpillInfoRetriever/getFOM.h new file mode 100644 index 000000000..78fd0de22 --- /dev/null +++ b/sbncode/BeamSpillInfoRetriever/getFOM.h @@ -0,0 +1,48 @@ +#ifndef SBNCODE_BEAMSPILLRETRIEVER_GETFOM_H +#define SBNCODE_BEAMSPILLRETRIEVER_GETFOM_H + +/** + * @file sbncode/BeamSpillInfoRetriever/getFOM.h + * @brief Beam quality figures of merit. + * @author Max Dubnowski (maxdub@upenn.sas.edu) + */ + +#include "sbnobj/Common/POTAccounting/BNBSpillInfo.h" + + + +namespace sbn +{ + /** + * @brief Returns a Figure of Merit on BNB beam quality. + * + * The figure of merit is described in [SBN DocDB 41901](https://sbn-docdb.fnal.gov/cgi-bin/sso/ShowDocument?docid=41901). + * Inputs the BNBSpillInfo and returns the BNB Quality Metric called FOM, derived from MicroBooNE's FOM + */ + std::tuple getBNBqualityFOM(BNBSpillInfo& spill); + + /** + * @brief Inside the getFOM script, takes the positions and angles of the beam and calculates the BNB FOM + */ + double calcFOM(double horpos,double horang,double verpos,double verang,double tor,double tgtsx=-999,double tgtsy=-999); + + /** + * @brief Takes in the centroid and sigma of the beam, along with transfer matrices, and will determine the beam's + * 2D gaussian position depending where on the target is being measured. The code "swims" up the target to calculate these + */ + void swimBNB(const double centroid1[6], const double sigma1[6][6], + const double xferc[6][6], const double xfers[6][6], + double &cx, double& cy, double &sx, double &sy, double &rho); + + /** + * @brief Integrates the 2D modelled gaussian beam overlapping with the target, and returns the fraction outside the beam + */ + double func_intbivar(const double cx, const double cy, const double sx, const double sy, const double rho ); + + /** + * @brief Inputs the MWR Data and determines the centroid, sigma, and chi2 value of a gaussian fit of the beam + */ + void processBNBprofile(const double* mwdata, double &x, double& sx, double& chi2); + +} +#endif diff --git a/sbncode/BeamSpillInfoRetriever/job/bnbextcountinfo.fcl b/sbncode/BeamSpillInfoRetriever/job/bnbextcountinfo.fcl deleted file mode 100644 index e8191e2dc..000000000 --- a/sbncode/BeamSpillInfoRetriever/job/bnbextcountinfo.fcl +++ /dev/null @@ -1,10 +0,0 @@ - -BEGIN_PROLOG - -bnbextcountinfo: { - - module_type: "BNBEXTRetriever" - raw_data_label: "daq" - -} -END_PROLOG diff --git a/sbncode/BeamSpillInfoRetriever/job/icarusbnbextcountinfo.fcl b/sbncode/BeamSpillInfoRetriever/job/icarusbnbextcountinfo.fcl new file mode 100644 index 000000000..cabe02824 --- /dev/null +++ b/sbncode/BeamSpillInfoRetriever/job/icarusbnbextcountinfo.fcl @@ -0,0 +1,10 @@ + +BEGIN_PROLOG + +icarusbnbextcountinfo: { + + module_type: "ICARUSBNBEXTRetriever" + raw_data_label: "daq" + +} +END_PROLOG diff --git a/sbncode/BeamSpillInfoRetriever/job/bnbspillinfo.fcl b/sbncode/BeamSpillInfoRetriever/job/icarusbnbspillinfo.fcl similarity index 57% rename from sbncode/BeamSpillInfoRetriever/job/bnbspillinfo.fcl rename to sbncode/BeamSpillInfoRetriever/job/icarusbnbspillinfo.fcl index 3aece8658..03c63ab3e 100644 --- a/sbncode/BeamSpillInfoRetriever/job/bnbspillinfo.fcl +++ b/sbncode/BeamSpillInfoRetriever/job/icarusbnbspillinfo.fcl @@ -1,13 +1,16 @@ BEGIN_PROLOG -bnbspillinfo: { +icarusbnbspillinfo: { - module_type: "BNBRetriever" + module_type: "ICARUSBNBRetriever" TimePadding: 0.0333 #unit seconds, Booster Rep Rate is 15 Hz, so the closest spill could be 66ms away + BESOffset: 0.035746 #unit seconds, we expect the BES to be about 35 ms from $1D. Need to adjust search range accordingly. URL: "" #keep this blank and we're good - Bundle: "BoosterNeutrinoBeam_read" + Bundle: "BoosterNeutrinoBeam_read_expanded" MultiWireBundle: "BNBMultiWire" + VP873Bundle: "BNB_monitor" + OffsetBundle: "BNB_BPM_settings" TimeWindow: "700" #seconds MWR_TimeWindow: "3601" #seconds raw_data_label: "daq" diff --git a/sbncode/BeamSpillInfoRetriever/job/run_bnbextinfo_sbn.fcl b/sbncode/BeamSpillInfoRetriever/job/run_bnbextinfo_sbn.fcl deleted file mode 100644 index fbee5871f..000000000 --- a/sbncode/BeamSpillInfoRetriever/job/run_bnbextinfo_sbn.fcl +++ /dev/null @@ -1,32 +0,0 @@ -#include "bnbextcountinfo.fcl" - -process_name: BNBEXTInfoGen - -services:{ - IFBeam:{} -} - - -source: { - -} - -physics: { - producers: { - bnbextinfo: @local::bnbextcountinfo - } - - simulate: [bnbextinfo ] - stream1: [ out1 ] - -} - -outputs: { - out1: { - module_type: RootOutput - fileName: "%ifb_%tc_bnbextinfo.root" - dataTier: "raw" - compressionLevel: 1 - } -} - diff --git a/sbncode/BeamSpillInfoRetriever/job/run_icarusbnbextinfo_sbn.fcl b/sbncode/BeamSpillInfoRetriever/job/run_icarusbnbextinfo_sbn.fcl new file mode 100644 index 000000000..c73bd8fc6 --- /dev/null +++ b/sbncode/BeamSpillInfoRetriever/job/run_icarusbnbextinfo_sbn.fcl @@ -0,0 +1,32 @@ +#include "icarusbnbextcountinfo.fcl" + +process_name: ICARUSBNBEXTInfoGen + +services:{ + IFBeam:{} +} + + +source: { + +} + +physics: { + producers: { + icarusbnbextinfo: @local::icarusbnbextcountinfo + } + + simulate: [icarusbnbextinfo ] + stream1: [ out1 ] + +} + +outputs: { + out1: { + module_type: RootOutput + fileName: "%ifb_%tc_icarusbnbextinfo.root" + dataTier: "raw" + compressionLevel: 1 + } +} + diff --git a/sbncode/BeamSpillInfoRetriever/job/run_bnbinfo_sbn.fcl b/sbncode/BeamSpillInfoRetriever/job/run_icarusbnbinfo_sbn.fcl similarity index 85% rename from sbncode/BeamSpillInfoRetriever/job/run_bnbinfo_sbn.fcl rename to sbncode/BeamSpillInfoRetriever/job/run_icarusbnbinfo_sbn.fcl index 34f8f689d..495b3543b 100644 --- a/sbncode/BeamSpillInfoRetriever/job/run_bnbinfo_sbn.fcl +++ b/sbncode/BeamSpillInfoRetriever/job/run_icarusbnbinfo_sbn.fcl @@ -1,4 +1,4 @@ -#include "bnbspillinfo.fcl" +#include "icarusbnbspillinfo.fcl" process_name: BNBInfoGen @@ -28,10 +28,10 @@ source: { physics: { producers: { - bnbinfo: @local::bnbspillinfo + bnbinfo: @local::icarusbnbspillinfo } - simulate: [bnbinfo ] + simulate: [ bnbinfo ] stream1: [ out1 ] } diff --git a/sbncode/BeamSpillInfoRetriever/job/run_sbndbnbextinfo_sbn.fcl b/sbncode/BeamSpillInfoRetriever/job/run_sbndbnbextinfo_sbn.fcl index ca85d36af..af2f8d1ac 100644 --- a/sbncode/BeamSpillInfoRetriever/job/run_sbndbnbextinfo_sbn.fcl +++ b/sbncode/BeamSpillInfoRetriever/job/run_sbndbnbextinfo_sbn.fcl @@ -1,6 +1,6 @@ #include "sbndbnbdefaults.fcl" -process_name: SBNDBNBEXTInfoGen +process_name: BNBEXTInfoGen services:{ @@ -27,21 +27,21 @@ source: { physics: { producers: { - sbndbnbextinfo: @local::sbndbnbspillinfo + bnbextinfo: @local::sbndbnbspillinfo } - simulate: [sbndbnbextinfo ] + simulate: [bnbextinfo ] stream1: [ out1 ] } -physics.producers.sbndbnbextinfo.module_type: "SBNDBNBEXTRetriever" +physics.producers.bnbextinfo.module_type: "SBNDBNBEXTRetriever" outputs: { out1: { module_type: RootOutput - fileName: "%ifb_%tc_sbndbnbextinfo.root" + fileName: "%ifb_%tc_bnbextinfo.root" dataTier: "raw" compressionLevel: 1 } } -physics.producers.sbndbnbextinfo.fileNames: @local::outputs.out1.fileName +physics.producers.bnbextinfo.fileNames: @local::outputs.out1.fileName diff --git a/sbncode/BeamSpillInfoRetriever/job/run_sbndbnbinfo_sbn.fcl b/sbncode/BeamSpillInfoRetriever/job/run_sbndbnbinfo_sbn.fcl index 66756c41b..602bfe3f3 100644 --- a/sbncode/BeamSpillInfoRetriever/job/run_sbndbnbinfo_sbn.fcl +++ b/sbncode/BeamSpillInfoRetriever/job/run_sbndbnbinfo_sbn.fcl @@ -1,6 +1,6 @@ #include "sbndbnbdefaults.fcl" -process_name: SBNDBNBInfoGen +process_name: BNBInfoGen services:{ @@ -28,21 +28,21 @@ source: { physics: { producers: { - sbndbnbinfo: @local::sbndbnbspillinfo + bnbinfo: @local::sbndbnbspillinfo } - simulate: [sbndbnbinfo ] + simulate: [bnbinfo ] stream1: [ out1 ] } -physics.producers.sbndbnbinfo.module_type: "SBNDBNBRetriever" +physics.producers.bnbinfo.module_type: "SBNDBNBRetriever" outputs: { out1: { module_type: RootOutput - fileName: "%ifb_%tc_sbndbnbinfo.root" + fileName: "%ifb_%tc_bnbinfo.root" dataTier: "raw" compressionLevel: 1 } } -physics.producers.sbndbnbinfo.fileNames: @local::outputs.out1.fileName +physics.producers.bnbinfo.fileNames: @local::outputs.out1.fileName diff --git a/sbncode/BeamSpillInfoRetriever/job/sbndbnbdefaults.fcl b/sbncode/BeamSpillInfoRetriever/job/sbndbnbdefaults.fcl index 26327a100..47f37d9ba 100644 --- a/sbncode/BeamSpillInfoRetriever/job/sbndbnbdefaults.fcl +++ b/sbncode/BeamSpillInfoRetriever/job/sbndbnbdefaults.fcl @@ -6,10 +6,13 @@ sbndbnbspillinfo: { TimePadding: 0.0333 #unit seconds, Booster Rep Rate is 15 Hz, so the closest spill could be 66ms away was 0.0333 BESOffset: 0.035 #unit seconds, we expect the BES to be about 35 ms from $1D. Need to adjust search range accordingly. URL: "" #Default URL having issues Nov 21 2024, used this instead https://dbdata3vm.fnal.gov:9443/ifbeam - Bundle: "BoosterNeutrinoBeam_read" + Bundle: "BoosterNeutrinoBeam_read_expanded" MultiWireBundle: "BNBMultiWire" + VP873Bundle: "BNB_monitor" + OffsetBundle: "BNB_BPM_settings" + TimeWindow: "700" #seconds - MWR_TimeWindow: "3601" #seconds + MWR_TimeWindow: "700" #seconds DeviceUsedForTiming: "E:TOR860" } diff --git a/sbncode/BeamSpillInfoRetriever/job/sbndbnbspillinfo.fcl b/sbncode/BeamSpillInfoRetriever/job/sbndbnbspillinfo.fcl deleted file mode 100644 index 26327a100..000000000 --- a/sbncode/BeamSpillInfoRetriever/job/sbndbnbspillinfo.fcl +++ /dev/null @@ -1,17 +0,0 @@ -BEGIN_PROLOG - -sbndbnbspillinfo: { - module_type: "" - DebugLevel: 0 - TimePadding: 0.0333 #unit seconds, Booster Rep Rate is 15 Hz, so the closest spill could be 66ms away was 0.0333 - BESOffset: 0.035 #unit seconds, we expect the BES to be about 35 ms from $1D. Need to adjust search range accordingly. - URL: "" #Default URL having issues Nov 21 2024, used this instead https://dbdata3vm.fnal.gov:9443/ifbeam - Bundle: "BoosterNeutrinoBeam_read" - MultiWireBundle: "BNBMultiWire" - TimeWindow: "700" #seconds - MWR_TimeWindow: "3601" #seconds - DeviceUsedForTiming: "E:TOR860" -} - -END_PROLOG - diff --git a/sbncode/CAFMaker/CAFMakerParams.h b/sbncode/CAFMaker/CAFMakerParams.h index 2ee91d866..0b226d86f 100644 --- a/sbncode/CAFMaker/CAFMakerParams.h +++ b/sbncode/CAFMaker/CAFMakerParams.h @@ -320,6 +320,12 @@ namespace caf "crthit" // icarus }; + Atom CRTSimChanLabel { + Name("CRTSimChanLabel"), + Comment("Label of AuxDetSimChannels."), + "genericcrt" // icarus + }; + Atom CRTTrackLabel { Name("CRTTrackLabel"), Comment("Label of sbn CRT tracks."), @@ -341,13 +347,13 @@ namespace caf Atom SBNDFrameShiftInfoLabel { Name("SBNDFrameShiftInfoLabel"), Comment("Label of sbnd frame shift."), - "frameshift" // sbnd + "" // sbnd }; Atom SBNDTimingInfoLabel { Name("SBNDTimingInfoLabel"), Comment("Label of sbnd timing shift."), - "frameshift" // sbnd + "" // sbnd }; Atom CRTPMTLabel { @@ -386,24 +392,30 @@ namespace caf "OpFlash" }; - Atom CRTSimT0Offset { - Name("CRTSimT0Offset"), - Comment("start of beam gate/simulation time in the simulated CRT clock"), - 0, - }; - Atom TriggerLabel { Name("TriggerLabel"), Comment("Label of trigger."), "daqTrigger" }; - Atom UnshiftedTriggerLabel { - Name("UnshiftedTriggerLabel"), - Comment("Label of trigger emulation before applying trigger time shifts."), - "emuTriggerUnshifted" + Atom ShiftTimeFromTriggerToBeamGate { + Name("ShiftTimeFromTriggerToBeamGate"), + Comment("Shifts time in branches from trigger time to beam gate time."), + false }; + Atom GlobalTimeReferenceOffset { + Name("GlobalTimeReferenceOffset"), + Comment("Additional offset added to all times [us]"), + 0.0 + }; + + Atom CRTreferenceTimeOffset { + Name("CRTreferenceTimeOffset"), + Comment("Additional reference shift for relative CRT times (not timestamps) [us]"), + 0.0 + }; + Atom FlashTrigLabel { Name("FlashTrigLabel"), Comment("Label of bool of passing flash trigger."), @@ -464,24 +476,6 @@ namespace caf 25. }; - Atom ReferencePMTFromTriggerToBeam { - Name("ReferencePMTFromTriggerToBeam"), - Comment("Whether to switch the reference time of PMT reco from 'trigger' to 'beam spill' time."), - true - }; - - Atom ReferenceCRTT0ToBeam { - Name("ReferenceCRTT0ToBeam"), - Comment("Whether to switch the reference time of CRT T0 reco to the 'beam spill' time."), - true - }; - - Atom ReferenceCRTT1FromTriggerToBeam { - Name("ReferenceCRTT1FromTriggerToBeam"), - Comment("Whether to switch the reference time of CRT T1 reco from 'trigger' to the 'beam spill' time."), - true - }; - Atom CVNLabel { Name("CVNLabel"), Comment("Label of CVN scores."), diff --git a/sbncode/CAFMaker/CAFMaker_module.cc b/sbncode/CAFMaker/CAFMaker_module.cc index 870c813be..5126da544 100644 --- a/sbncode/CAFMaker/CAFMaker_module.cc +++ b/sbncode/CAFMaker/CAFMaker_module.cc @@ -1,15 +1,14 @@ ////////////////////////////////////////////////////////////////// -// \file CAFMaker_module.cc -/// \brief This module creates Common Analysis Files. -// Inspired by the NOvA CAFMaker package -// \author $Author: psihas@fnal.gov +/// \file CAFMaker_module.cc +/// \brief This module creates Common Analysis Files. +/// Inspired by the NOvA CAFMaker package +/// \author psihas@fnal.gov ////////////////////////////////////////////////////////////////// // ---------------- TO DO ---------------- // // - Add in cycle and batch to params // - Move this list some place useful -// - Add reco.CRT branch // --------------------------------------- @@ -19,6 +18,8 @@ #include "sbncode/CAFMaker/FillReco.h" #include "sbncode/CAFMaker/FillExposure.h" #include "sbncode/CAFMaker/FillTrigger.h" +#include "sbncode/CAFMaker/TimeRefShifters.h" +#include "sbncode/CAFMaker/SRDefaults.h" #include "sbncode/CAFMaker/Utils.h" // C/C++ includes @@ -64,9 +65,10 @@ #include "art/Framework/Services/System/TriggerNamesService.h" #include "nurandom/RandomUtils/NuRandomService.h" #include "lardata/DetectorInfoServices/DetectorClocksService.h" -#include "lardataalg/DetectorInfo/DetectorPropertiesStandard.h" +#include "lardataalg/DetectorInfo/DetectorPropertiesData.h" #include "lardata/DetectorInfoServices/DetectorPropertiesService.h" #include "larcore/CoreUtils/ServiceUtil.h" +#include "larcorealg/CoreUtils/DebugUtils.h" // lar::debug #include "larevt/SpaceCharge/SpaceCharge.h" #include "larevt/SpaceChargeServices/SpaceChargeService.h" @@ -92,6 +94,7 @@ #include "lardataobj/RecoBase/PFParticle.h" #include "lardataobj/RecoBase/Slice.h" #include "lardataobj/RecoBase/Track.h" +#include "lardataobj/RecoBase/TrackHitMeta.h" #include "lardataobj/RecoBase/Vertex.h" #include "lardataobj/RecoBase/Shower.h" #include "lardataobj/RecoBase/MCSFitResult.h" @@ -139,6 +142,7 @@ #include "sbnanaobj/StandardRecord/Flat/FlatRecord.h" #include "lardataobj/RawData/ExternalTrigger.h" #include "lardataobj/RawData/TriggerData.h" +#include "lardataobj/Simulation/AuxDetSimChannel.h" // // CAFMaker #include "sbncode/CAFMaker/AssociationUtil.h" @@ -204,6 +208,8 @@ class CAFMaker : public art::EDProducer { std::string fSourceFile; std::uint32_t fSourceFileHash; + + bool fNewInputFile; bool fOverrideRealData; bool fFirstInSubRun; @@ -221,6 +227,7 @@ class CAFMaker : public art::EDProducer { double fTotalEvents; double fBlindEvents; double fPrescaleEvents; + double fTotalGenEvents; std::vector fBNBInfo; ///< Store detailed BNB info to save into the first StandardRecord of the output file std::vector fNuMIInfo; ///< Store detailed NuMI info to save into the first StandardRecord of the output file std::map fBNBInfoEventMap; ///< Store detailed BNB info to save for the particular spills of events @@ -286,6 +293,11 @@ class CAFMaker : public art::EDProducer { std::vector> fTPCVolumes; std::vector fActiveVolumes; + // CRT geometry info + // + // ICARUS + std::map, int> fFEBChannel2AuxDetID; + // random number generator for fake reco CLHEP::HepRandomEngine& fFakeRecoRandomEngine; @@ -314,12 +326,29 @@ class CAFMaker : public art::EDProducer { double GetBlindPOTScale() const; void InitVolumes(); ///< Initialize volumes from Gemotry service - - void FixPMTReferenceTimes(StandardRecord &rec, double PMT_reference_time); - void FixCRTReferenceTimes(StandardRecord &rec, double CRTT0_reference_time, double CRTT1_reference_time); - - void SBNDShiftCRTReference(StandardRecord &rec, double SBNDFrame); - void SBNDShiftPMTReference(StandardRecord &rec, double SBNDFrame); + void InitCRTMapping(); ///< Initialize CRT mapping + + /** + * @brief Returns the absolute timestamps of beam and trigger times + * @param clock_data LArSoft timing information + * @param trigger a trigger object + * @param extra extra information for the same `trigger` + * @return absolute timestamps of beam and trigger times [ns] + * + * The relation between absolute timestamps and relative times is extracted + * from the beam gate time in `trigger` and `extra`. That relation is then + * used to convert the beam and trigger times from the reference time system + * (`detinfo::DetectorClocksData::BeamGateTime()` and + * `detinfo::DetectorClocksData::TriggerTime()`). + */ + std::pair getReferenceTimestamps( + detinfo::DetectorClocksData const& clock_data, + raw::Trigger const& trigger, sbn::ExtraTriggerInfo const& extra + ) const; + + /// Applies a reference time shift (subtracting `refTimeOffset`) + void SBNDShiftCRTReference(StandardRecord &rec, double SBNDFrame) const; + void SBNDShiftPMTReference(StandardRecord &rec, double SBNDFrame) const; /// Equivalent of FindManyP except a return that is !isValid() prints a /// messsage and aborts if StrictMode is true. @@ -343,8 +372,10 @@ class CAFMaker : public art::EDProducer { const art::Event& evt, const art::InputTag& tag) const; - /// \brief Retrieve an object from an association, with error handling + /// \brief Makes a copy of an object from an association, with error handling /// + /// This function attempts to make a copy into `ret` of the first of the + /// objects in the association to `from`. /// This can go wrong in two ways: either the FindManyP itself is /// invalid, or the result for the requested index is empty. In most /// cases these have the same response, so conflating them here @@ -352,23 +383,28 @@ class CAFMaker : public art::EDProducer { /// /// \param fm The FindManyP object describing the association /// \param idx Which element of the FindManyP to look it - /// \param[out] ret The product retrieved + /// \param[out] ret Where to copy the product retrieved /// \return Whether \a ret was filled template bool GetAssociatedProduct(const art::FindManyP& fm, int idx, T& ret) const; - /// Equivalent of evt.getByLabel(label, handle) except failedToGet + /// Equivalent of evt.getByLabel(tag, handle) except failedToGet /// prints a message and aborts if StrictMode is true. template - void GetByLabelStrict(const EvtT& evt, const std::string& label, + void GetByLabelStrict(const EvtT& evt, const art::InputTag& tag, art::Handle& handle) const; - /// Equivalent of evt.getByLabel(label, handle) except failedToGet + /// Equivalent of evt.getByLabel(tag, handle) except failedToGet /// prints a message. - template - void GetByLabelIfExists(const art::Event& evt, const std::string& label, + template + void GetByLabelIfExists(const EvtT& evt, const art::InputTag& tag, art::Handle& handle) const; + /// Equivalent of evt.getHandle(tag) except that if `StrictMode` is set + /// when `failedToGet()` is `true` prints a message and aborts. + template + art::Handle GetHandleStrict(const EvtT& evt, const art::InputTag& tag) const; + /// \param pset The parameter set /// \param name Pass "foo.bar.baz" as {"foo", "bar", "baz"} /// \param[out] ret Value of the key, not set if we return false @@ -422,6 +458,9 @@ class CAFMaker : public art::EDProducer { // setup volume definitions InitVolumes(); + // setup CRT mapping + InitCRTMapping(); + fSaveGENIEEventRecord = fParams.SaveGENIEEventRecord(); } @@ -504,7 +543,35 @@ void CAFMaker::BlindEnergyParameters(StandardRecord* brec) { } } -void CAFMaker::SBNDShiftCRTReference(StandardRecord &rec, double SBNDFrame){ +std::pair CAFMaker::getReferenceTimestamps( + detinfo::DetectorClocksData const& clock_data, + raw::Trigger const& trigger, sbn::ExtraTriggerInfo const& extra +) const { + + double refTime; + std::int64_t refTimestamp = 0; + if (extra.isValidTimestamp(extra.beamGateTimestamp)) { + refTime = trigger.BeamGateTime(); + refTimestamp = extra.beamGateTimestamp; + } + else if (extra.isValidTimestamp(extra.triggerTimestamp)) { // backup + refTime = trigger.TriggerTime(); + refTimestamp = extra.triggerTimestamp; + } + else { // we assign the reference timestamp `0` to the trigger + refTime = trigger.TriggerTime(); + refTimestamp = 0; + } + + auto timeToTS = [refTime, refTimestamp](double time) + { return std::llround((time - refTime) * 1000.0) + refTimestamp; }; + + return + { timeToTS(clock_data.BeamGateTime()), timeToTS(clock_data.TriggerTime()) }; + +} + +void CAFMaker::SBNDShiftCRTReference(StandardRecord &rec, double SBNDFrame) const { //CRT Space Point for (SRCRTSpacePoint &sp: rec.crt_spacepoints){ @@ -517,13 +584,16 @@ void CAFMaker::SBNDShiftCRTReference(StandardRecord &rec, double SBNDFrame){ } //TODO: CRT Space Point and Track Match - //for (SRPFP &pfp: rec.reco.pfp) { - // pfp.trk.crtspacepoint.spacepoint.time += SBNDFrame; - // pfp.trk.crtsbndtrack.track.time += SBNDFrame; - //} + for (SRSlice &slc: rec.slc){ + for (SRPFP &pfp: slc.reco.pfp){ + if(!std::isnan(pfp.trk.crtspacepoint.score)) pfp.trk.crtspacepoint.spacepoint.time += SBNDFrame; + + if(!std::isnan(pfp.trk.crtsbndtrack.score)) pfp.trk.crtsbndtrack.track.time += SBNDFrame; + } + } } -void CAFMaker::SBNDShiftPMTReference(StandardRecord &rec, double SBNDFrame){ +void CAFMaker::SBNDShiftPMTReference(StandardRecord &rec, double SBNDFrame) const { double SBNDFrame_us = SBNDFrame / 1000.0; //convert ns to us @@ -539,57 +609,34 @@ void CAFMaker::SBNDShiftPMTReference(StandardRecord &rec, double SBNDFrame){ } } -void CAFMaker::FixPMTReferenceTimes(StandardRecord &rec, double PMT_reference_time) { - // Fix the flashes - for (SROpFlash &f: rec.opflashes) { - f.time += PMT_reference_time; - f.timemean += PMT_reference_time; - f.firsttime += PMT_reference_time; - } - - // Fix the flash matches - for (SRSlice &s: rec.slc) { - s.fmatch.time += PMT_reference_time; - - s.barycenterFM.flashTime +=PMT_reference_time; - s.barycenterFM.flashFirstHit +=PMT_reference_time; - } - - // TODO: fix more? +void CAFMaker::InitCRTMapping() { -} + // ICARUS + cet::search_path searchPath("FW_SEARCH_PATH"); + std::string fullFileName; + searchPath.find_file("feb_map.txt",fullFileName); + std::ifstream fin; + fin.open(fullFileName, std::ios::in); -void CAFMaker::FixCRTReferenceTimes(StandardRecord &rec, double CRTT0_reference_time, double CRTT1_reference_time) { - // Fix the hits + if (fin.good()) { + std::string line; - double crttime_to_shift = fParams.CRTUseTS0() ? CRTT0_reference_time : CRTT1_reference_time; + while(std::getline(fin,line)) { + std::vector row; + std::string word; - // As discussed/described in https://github.com/SBNSoftware/sbncode/pull/251, - // we added CRTHit::t0 and CRTHit::t1 in addition to CRTHit::time, - // not to break any existing studies that still use "CRTHit::time" - for (SRCRTHit &h: rec.crt_hits) { - h.t0 += CRTT0_reference_time; - h.t1 += CRTT1_reference_time; - h.time += crttime_to_shift; - } - - // Fix the hit matches - for (SRSlice &s: rec.slc) { - for (SRPFP &pfp: s.reco.pfp) { - pfp.trk.crthit.hit.t0 += CRTT0_reference_time; - pfp.trk.crthit.hit.t1 += CRTT1_reference_time; - pfp.trk.crthit.hit.time += crttime_to_shift; + std::stringstream s(line); + while (std::getline(s, word, ',')) { + row.push_back(word); + } + int auxDetID = std::stoi(row[0]); // auxDetID + int mac5 = std::stoi(row[1]); // FEB ID + int chan = std::stoi(row[2]); // FEB channel + fFEBChannel2AuxDetID[std::make_pair(mac5, chan)] = auxDetID; } - } - for (SRPFP &pfp: rec.reco.pfp) { - pfp.trk.crthit.hit.t0 += CRTT0_reference_time; - pfp.trk.crthit.hit.t1 += CRTT1_reference_time; - pfp.trk.crthit.hit.time += crttime_to_shift; - } - - // TODO: fix more? - // Tracks? + fin.close(); + } } void CAFMaker::InitVolumes() { @@ -738,6 +785,7 @@ void CAFMaker::respondToOpenInputFile(const art::FileBlock& fb) { // so should be less than or equal to 32-bit fSourceFileHash = static_cast(fSourceFileHashFull); + fNewInputFile = true; } //...................................................................... @@ -813,6 +861,18 @@ void CAFMaker::beginRun(art::Run& run) { fDet = override; } + if (std::exchange(fNewInputFile, false)){ + for (const art::ProcessConfiguration &process: run.processHistory()) { + std::optional gen_config = run.getProcessParameterSet(process.processName()); + if (gen_config && gen_config->has_key("source") && gen_config->has_key("source.maxEvents") && gen_config->has_key("source.module_type") ) { + int max_events = gen_config->get("source.maxEvents"); + std::string module_type = gen_config->get("source.module_type"); + if (module_type == "EmptyEvent") { + fTotalGenEvents += max_events; + } + } + } + } if(fParams.SystWeightLabels().empty()) return; // no need for globalTree @@ -1091,7 +1151,6 @@ void CAFMaker::InitializeOutfiles() } - if(fParams.CreateFlatCAF()){ mf::LogInfo("CAFMaker") << "Output flat filename is " << fFlatCafFilename; @@ -1168,6 +1227,7 @@ void CAFMaker::InitializeOutfiles() fTotalEvents = 0; fBlindEvents = 0; fPrescaleEvents = 0; + fTotalGenEvents = 0; fIndexInFile = SRHeader::NoSourceIndex; fFirstInSubRun = false; fFirstBlindInSubRun = false; @@ -1186,9 +1246,9 @@ art::FindManyP CAFMaker::FindManyPStrict(const U& from, if (!tag.label().empty() && !ret.isValid() && fParams.StrictMode()) { std::cout << "CAFMaker: No Assn from '" - << cet::demangle_symbol(typeid(from).name()) << "' to '" - << cet::demangle_symbol(typeid(T).name()) - << "' found under label '" << tag << "'. " + << lar::debug::demangle() << "' to '" + << lar::debug::demangle() + << "' found under label '" << tag.encode() << "'. " << "Set 'StrictMode: false' to continue anyway." << std::endl; abort(); } @@ -1205,9 +1265,9 @@ art::FindManyP CAFMaker::FindManyPDStrict(const U& from, if (!tag.label().empty() && !ret.isValid() && fParams.StrictMode()) { std::cout << "CAFMaker: No Assn from '" - << cet::demangle_symbol(typeid(from).name()) << "' to '" - << cet::demangle_symbol(typeid(T).name()) - << "' found under label '" << tag << "'. " + << lar::debug::demangle() << "' to '" + << lar::debug::demangle() + << "' found under label '" << tag.encode() << "'. " << "Set 'StrictMode: false' to continue anyway." << std::endl; abort(); } @@ -1224,9 +1284,9 @@ art::FindOneP CAFMaker::FindOnePStrict(const U& from, if (!tag.label().empty() && !ret.isValid() && fParams.StrictMode()) { std::cout << "CAFMaker: No Assn from '" - << cet::demangle_symbol(typeid(from).name()) << "' to '" - << cet::demangle_symbol(typeid(T).name()) - << "' found under label '" << tag << "'. " + << lar::debug::demangle() << "' to '" + << lar::debug::demangle() + << "' found under label '" << tag.encode() << "'. " << "Set 'StrictMode: false' to continue anyway." << std::endl; abort(); } @@ -1243,9 +1303,9 @@ art::FindOneP CAFMaker::FindOnePDStrict(const U& from, if (!tag.label().empty() && !ret.isValid() && fParams.StrictMode()) { std::cout << "CAFMaker: No Assn from '" - << cet::demangle_symbol(typeid(from).name()) << "' to '" - << cet::demangle_symbol(typeid(T).name()) - << "' found under label '" << tag << "'. " + << lar::debug::demangle() << "' to '" + << lar::debug::demangle() + << "' found under label '" << tag.encode() << "'. " << "Set 'StrictMode: false' to continue anyway." << std::endl; abort(); } @@ -1259,7 +1319,7 @@ bool CAFMaker::GetAssociatedProduct(const art::FindManyP& fm, int idx, T& ret) const { if (!fm.isValid()) return false; - const std::vector> prods = fm.at(idx); + const std::vector>& prods = fm.at(idx); if (prods.empty()) return false; @@ -1270,32 +1330,40 @@ bool CAFMaker::GetAssociatedProduct(const art::FindManyP& fm, int idx, //...................................................................... template -void CAFMaker::GetByLabelStrict(const EvtT& evt, const std::string& label, +void CAFMaker::GetByLabelStrict(const EvtT& evt, const art::InputTag& tag, art::Handle& handle) const { - evt.getByLabel(label, handle); - if (!label.empty() && handle.failedToGet() && fParams.StrictMode()) { - std::cout << "CAFMaker: No product of type '" - << cet::demangle_symbol(typeid(*handle).name()) - << "' found under label '" << label << "'. " - << "Set 'StrictMode: false' to continue anyway." << std::endl; - abort(); - } + + handle = GetHandleStrict(evt, tag); } //...................................................................... -template -void CAFMaker::GetByLabelIfExists(const art::Event& evt, - const std::string& label, +template +void CAFMaker::GetByLabelIfExists(const EvtT& evt, + const art::InputTag& tag, art::Handle& handle) const { - evt.getByLabel(label, handle); - if (!label.empty() && handle.failedToGet() && fParams.StrictMode()) { - std::cout << "CAFMaker: No product of type '" - << cet::demangle_symbol(typeid(*handle).name()) - << "' found under label '" << label << "'. " + handle = evt.template getHandle(tag); + if (!tag.empty() && handle.failedToGet() && fParams.StrictMode()) { + std::cout << "CAFMaker: No product of type '" << lar::debug::demangle() + << "' found under label '" << tag.encode() << "'. " << "Continuing without it." << std::endl; } } +//...................................................................... +template +art::Handle CAFMaker::GetHandleStrict + (const EvtT& evt, const art::InputTag& tag) const +{ + art::Handle handle = evt.template getHandle(tag); + if (!handle.failedToGet() || !fParams.StrictMode() || tag.empty()) + return handle; + std::cout << "CAFMaker: No product of type '" << lar::debug::demangle() + << "' found under label '" << tag.encode() << "'. " + << "Set 'StrictMode: false' to continue anyway." << std::endl; + abort(); +} + + //...................................................................... template bool CAFMaker::GetPsetParameter(const fhicl::ParameterSet& pset, @@ -1427,15 +1495,15 @@ void CAFMaker::produce(art::Event& evt) noexcept { } // Prep truth-to-reco-matching info - std::map>> id_to_ide_map; + std::map> id_to_ide_map; std::map>> id_to_truehit_map; std::map id_to_hit_energy_map; if ( !isRealData ) { art::ServiceHandle bt_serv; - id_to_ide_map = PrepSimChannels(simchannels, wireReadout); - id_to_truehit_map = PrepTrueHits(hits, clock_data, *bt_serv); + id_to_ide_map = sbn::PrepSimChannels(simchannels, wireReadout); + id_to_truehit_map = sbn::PrepTrueHits(hits, clock_data, *bt_serv); id_to_hit_energy_map = SetupIDHitEnergyMap(hits, clock_data, *bt_serv); } @@ -1596,34 +1664,87 @@ void CAFMaker::produce(art::Event& evt) noexcept { //####################################################### // Fill detector & reco //####################################################### - + + // this text will land into Doxygen `produce()` documentation + /** + * ## Time shifts + * + * Depending on the configuration, time shifts may be applied to some of the data. + * + * The following shifts are supported: + * * shift times from trigger to beam gate (`ShiftTimeFromTriggerToBeamGate` flag), + * which assumes the input is on trigger time (no safe way to verify it). + * Beam gate and trigger time are learnt from `DetectorClocksService`, + * which is the service that gives LArSoft modules the time scale. + * Absolute timestamps are not shifted. + * * additional time shift (`GlobalTimeReferenceOffset`); this is removed + * from all time values, since it's a forward shift of the reference time. + * This shift is always applied; when also enabling the shift from trigger + * to beam gate, it can be thought as an addition to the beam gate time. + * Absolute timestamps are not affected by this offset. + * * CRT-specific time reference offset (`CRTreferenceTimeOffset`); this is + * equivalent to `GlobalTimeReferenceOffset` and applied on top of it but + * only on CRT hits (and related). It also moves the (CRT) reference time, + * so it is effectively subtracted from the existing hit time. + * This offset applies with the same scale on all CRT hit times, including + * absolute timestamps. + * + * Currently there is no offset distinguishing between data and simulation: + * to apply specific shifts, two different configurations need to be used. + * + * @note Some time shifting is _always_ applied to CRT timestamps, even when + * `ShiftTimeFromTriggerToBeamGate` is disabled: in that case, the + * reference time is the trigger timestamp. + * + * SBND-specific shifts are applied on top of this reference change. + */ + + auto const trig_handle + = GetHandleStrict>(evt, fParams.TriggerLabel()); + auto const extratrig_handle + = GetHandleStrict(evt, fParams.TriggerLabel()); + bool const isValidTrigger + = trig_handle.isValid() && extratrig_handle.isValid() && !trig_handle->empty(); + + // subtract these offsets from existing times to move to the new reference time + double const triggerToBeamShift = fParams.ShiftTimeFromTriggerToBeamGate() + ? (clock_data.BeamGateTime() - clock_data.TriggerTime()): 0.0; // <= 0.0 + double const refTimeShift + = fParams.GlobalTimeReferenceOffset() + triggerToBeamShift; + + caf::TimeRefShifter const timeShifter{ refTimeShift }; + + // CRTrefTimeShift includes refTimeShift and is used in the same way (subtract) + double const CRTrefTimeShift = refTimeShift + fParams.CRTreferenceTimeOffset(); // [us] + // shift for the time stamps [ns] never includes the shift to beam gate reference + auto const [ beamGateTimestamp, triggerTimestamp ] = isValidTrigger + ? getReferenceTimestamps(clock_data, trig_handle->front(), *extratrig_handle) + : std::pair{}; + std::int64_t const refTimestamp = fParams.ShiftTimeFromTriggerToBeamGate() + ? beamGateTimestamp: triggerTimestamp; + std::int64_t const CRTrefTimeShiftTS = refTimestamp + static_cast( + std::llround(1000.0 + * (fParams.GlobalTimeReferenceOffset() + fParams.CRTreferenceTimeOffset()) + )); + caf::CRTtimeRefShifter const CRTtimeShifter + { fParams.CRTUseTS0(), CRTrefTimeShiftTS, CRTrefTimeShift }; + + if (refTimeShift || CRTrefTimeShift) { + mf::LogInfo("CAFMaker") << "Shifting time reference by " << refTimeShift << " us" + << "\n beam gate time: " << clock_data.BeamGateTime() << " us" + << "\n trigger time: " << clock_data.TriggerTime() << " us" + << "\n reference shift: trigger -> beam: " << triggerToBeamShift + << "\n reference shift: global change: " << fParams.GlobalTimeReferenceOffset() + << "\n CRT reference shift: times: " << CRTrefTimeShift << " us" + << "\n CRT reference shift: timestamps: " << CRTrefTimeShiftTS << " ns" + ; + } + //Beam gate and Trigger info - art::Handle extratrig_handle; - GetByLabelStrict(evt, fParams.TriggerLabel().encode(), extratrig_handle); - - art::Handle> trig_handle; - GetByLabelStrict(evt, fParams.TriggerLabel().encode(), trig_handle); - - art::Handle> unshifted_trig_handle; - if (!isRealData) - GetByLabelStrict(evt, fParams.UnshiftedTriggerLabel().encode(), unshifted_trig_handle); - - const bool isValidTrigger = extratrig_handle.isValid() && trig_handle.isValid() && trig_handle->size() == 1; - const bool isValidUnshiftedTrigger = unshifted_trig_handle.isValid() && unshifted_trig_handle->size() == 1; - - const double triggerShift = (isValidUnshiftedTrigger && isValidTrigger)? - unshifted_trig_handle->at(0).TriggerTime() - trig_handle->at(0).TriggerTime() : 0.; - caf::SRTrigger srtrigger; if (isValidTrigger) { - FillTrigger(*extratrig_handle, trig_handle->at(0), srtrigger, triggerShift); + FillTrigger(*extratrig_handle, trig_handle->at(0), srtrigger, timeShifter); } - // If not real data, fill in enough of the SRTrigger to make (e.g.) the CRT - // time referencing work. TODO: add more stuff to a "MC"-Trigger? - // No longer needed with incorporation of trigger emulation in the MC. - // else if(!isRealData) { - // FillTriggerMC(fParams.CRTSimT0Offset(), srtrigger); - // } // try to find the result of the Flash trigger if it was run bool pass_flash_trig = false; @@ -1634,9 +1755,6 @@ void CAFMaker::produce(art::Event& evt) noexcept { pass_flash_trig = *flashtrig_handle; } - int64_t CRT_T0_reference_time = isRealData ? -srtrigger.beam_gate_time_abs : -fParams.CRTSimT0Offset(); - double CRT_T1_reference_time = isRealData ? srtrigger.trigger_within_gate : -fParams.CRTSimT0Offset(); - // Fill various detector information associated with the event std::vector srcrthits; @@ -1646,16 +1764,36 @@ void CAFMaker::produce(art::Event& evt) noexcept { caf::SRSBNDFrameShiftInfo srsbndframeshiftinfo; caf::SRSBNDTimingInfo srsbndtiminginfo; + // Mapping of (feb, channel) to truth information (AuxDetSimChannel) -- filled for ICARUS + std::map, sim::AuxDetSimChannel> crtsimchanmap; + if(fDet == kICARUS) { art::Handle> crthits_handle; GetByLabelStrict(evt, fParams.CRTHitLabel(), crthits_handle); + + art::Handle> auxdetsimchan_handle; + GetByLabelStrict(evt, fParams.CRTSimChanLabel(), auxdetsimchan_handle); + // fill into event if (crthits_handle.isValid()) { const std::vector &crthits = *crthits_handle; + + std::vector empty; + const std::vector &crtsimchanvec = (auxdetsimchan_handle.isValid()) ? *auxdetsimchan_handle : empty; + // Turn the AuxDetSimChannel's into a map that can be looked up from a CRT Hit + for (const sim::AuxDetSimChannel &crtsimchan: crtsimchanvec) { + for (auto const &map_pair: fFEBChannel2AuxDetID) { + if (map_pair.second == (int)crtsimchan.AuxDetID()) { + crtsimchanmap[map_pair.first] = crtsimchan; + break; + } + } + } + for (unsigned i = 0; i < crthits.size(); i++) { srcrthits.emplace_back(); - FillCRTHit(crthits[i], fParams.CRTUseTS0(), CRT_T0_reference_time, CRT_T1_reference_time, srcrthits.back()); + FillCRTHit(crthits[i], CRTtimeShifter, crtsimchanmap, srcrthits.back()); } } @@ -1666,7 +1804,7 @@ void CAFMaker::produce(art::Event& evt) noexcept { const std::vector &crttracks = *crttracks_handle; for (unsigned i = 0; i < crttracks.size(); i++) { srcrttracks.emplace_back(); - FillCRTTrack(crttracks[i], fParams.CRTUseTS0(), srcrttracks.back()); + FillCRTTrack(crttracks[i], CRTtimeShifter, srcrttracks.back()); } } } @@ -1719,7 +1857,7 @@ void CAFMaker::produce(art::Event& evt) noexcept { const std::vector &crtpmtmatches = *crtpmtmatch_handle; for (unsigned i = 0; i < crtpmtmatches.size(); i++) { srcrtpmtmatches.emplace_back(); - FillCRTPMTMatch(crtpmtmatches[i], srcrtpmtmatches.back()); + FillCRTPMTMatch(crtpmtmatches[i], srcrtpmtmatches.back(), timeShifter, CRTtimeShifter); } } @@ -1744,7 +1882,7 @@ void CAFMaker::produce(art::Event& evt) noexcept { std::vector const& ophits = findManyHits.at(iflash); srflashes.emplace_back(); - FillICARUSOpFlash(flash, ophits, cryostat, srflashes.back()); + FillICARUSOpFlash(flash, ophits, cryostat, srflashes.back(), timeShifter); iflash++; } } @@ -1766,7 +1904,7 @@ void CAFMaker::produce(art::Event& evt) noexcept { for (const recob::OpFlash& flash : opflashes) { std::vector const& ophits = findManyHits.at(iflash); srflashes.emplace_back(); - FillSBNDOpFlash(flash, ophits, tpc, srflashes.back()); + FillSBNDOpFlash(flash, ophits, tpc, srflashes.back(), timeShifter); iflash++; } } @@ -2043,8 +2181,8 @@ void CAFMaker::produce(art::Event& evt) noexcept { FindManyPStrict(fmPFPart, evt, fParams.PFParticleLabel() + slice_tag_suff); - art::FindManyP fmTrackHit = - FindManyPStrict(slcTracks, evt, + art::FindManyP fmTrackHit = + FindManyPDStrict(slcTracks, evt, fParams.RecoTrackLabel() + slice_tag_suff); art::FindManyP fmShowerHit = @@ -2126,21 +2264,21 @@ void CAFMaker::produce(art::Event& evt) noexcept { //####################################################### FillSliceVars(*slice, primary, producer, recslc); FillSliceMetadata(primary_meta, recslc); - FillSliceFlashMatch(fmatch_map["fmatch"], recslc.fmatch); - FillSliceFlashMatch(fmatch_map["fmatchop"], recslc.fmatchop); + FillSliceFlashMatch(fmatch_map["fmatch"], recslc.fmatch, timeShifter); + FillSliceFlashMatch(fmatch_map["fmatchop"], recslc.fmatchop, timeShifter); auto sr_flash = fmatch_map.find("fmatchara"); if(sr_flash!=fmatch_map.end()) { - FillSliceFlashMatch(fmatch_map["fmatchara"], recslc.fmatchara); + FillSliceFlashMatch(fmatch_map["fmatchara"], recslc.fmatchara, timeShifter); } sr_flash = fmatch_map.find("fmatchopara"); if(sr_flash != fmatch_map.end()) { - FillSliceFlashMatch(fmatch_map["fmatchopara"], recslc.fmatchopara); + FillSliceFlashMatch(fmatch_map["fmatchopara"], recslc.fmatchopara, timeShifter); } FillSliceVertex(vertex, recslc); FillSliceCRUMBS(slcCRUMBS, recslc); - FillSliceOpT0Finder(slcOpT0, recslc); + FillSliceOpT0Finder(slcOpT0, recslc, timeShifter); FillSliceBarycenter(slcHits, slcSpacePoints, recslc); - FillTPCPMTBarycenterMatch(barycenterMatch, recslc); + FillTPCPMTBarycenterMatch(barycenterMatch, recslc, timeShifter); FillCVNScores(cvnResult, recslc); // select slice @@ -2298,10 +2436,10 @@ void CAFMaker::produce(art::Event& evt) noexcept { FillTrackDazzle(fmTrackDazzle.at(iPart).front(), trk); } if (fmCalo.isValid()) { - FillTrackCalo(fmCalo.at(iPart), fmTrackHit.at(iPart), + FillTrackCalo(fmCalo.at(iPart), *thisTrack[0], fmTrackHit.at(iPart), fmTrackHit.data(iPart), (fParams.FillHitsNeutrinoSlices() && NeutrinoSlice) || fParams.FillHitsAllSlices(), fParams.TrackHitFillRRStartCut(), fParams.TrackHitFillRREndCut(), - dprop, trk); + dprop, *sce, trk); } if (fmCRTHitMatch.isValid() && fDet == kICARUS) { @@ -2315,11 +2453,11 @@ void CAFMaker::produce(art::Event& evt) noexcept { crthittagginginfo = fmCRTHitMatchInfo.at(iPart); } - FillTrackCRTHit(fmCRTHitMatch.at(iPart), crthitmatch, crthittagginginfo, fParams.CRTUseTS0(), CRT_T0_reference_time, CRT_T1_reference_time, trk); + FillTrackCRTHit(fmCRTHitMatch.at(iPart), crthitmatch, crthittagginginfo, CRTtimeShifter, crtsimchanmap, trk); } // NOTE: SEE TODO AT fmCRTTrackMatch if (fmCRTTrackMatch.isValid() && fDet == kICARUS) { - FillTrackCRTTrack(fmCRTTrackMatch.at(iPart), trk); + FillTrackCRTTrack(fmCRTTrackMatch.at(iPart), trk, CRTtimeShifter); } if(foCRTSpacePointMatch.isValid() && fDet == kSBND) @@ -2416,7 +2554,7 @@ void CAFMaker::produce(art::Event& evt) noexcept { rec.opflashes = srflashes; rec.nopflashes = srflashes.size(); rec.sbnd_frames = srsbndframeshiftinfo; - rec.sbnd_timings = srsbndtiminginfo; + rec.sbnd_timings = srsbndtiminginfo; if (fParams.FillTrueParticles()) { rec.true_particles = true_particles; @@ -2425,50 +2563,26 @@ void CAFMaker::produce(art::Event& evt) noexcept { rec.crtpmt_matches = srcrtpmtmatches; rec.ncrtpmt_matches = srcrtpmtmatches.size(); - // ICARUS: Fix the Reference time - // - // We want MC and Data to have the same reference time. - // In MC/LArSoft the "reference time" is canonically defined - // as the time when the start of the beam spill reaches the detector. - // - // In data it may be defined differently for different subsystems. In - // particular, some sub-systems define the reference time as the time - // of the trigger. We want to correct those to the universal reference - // time from MC. - // - // PMT's: - // - // TW (2024-03-29): In MC, when an event doesn't fire the trigger, the raw::Trigger will be - // filled with the default values, which are set to the numerical limits of double. - // In this case, we should set the PMT_reference_time to 0. - - const bool hasValidTriggerTime = - srtrigger.global_trigger_det_time > - (std::numeric_limits::min() + std::numeric_limits::epsilon()) && - srtrigger.global_trigger_det_time < - (std::numeric_limits::max() - std::numeric_limits::epsilon()); - - double PMT_reference_time = fParams.ReferencePMTFromTriggerToBeam() && hasValidTriggerTime ? triggerShift : 0.; - - mf::LogInfo("CAFMaker") << "Setting PMT reference time to " << PMT_reference_time << " us\n" - << " Trigger Time = " << srtrigger.global_trigger_det_time << " us\n" - << " Beam Gate Time = " << srtrigger.beam_gate_det_time << " us"; + // SBND: Fix the Reference time in data depending on the stream + // For more information, see: + // https://sbn-docdb.fnal.gov/cgi-bin/sso/RetrieveFile?docid=43090 - FixPMTReferenceTimes(rec, PMT_reference_time); - - // TODO: TPC? - - // SBND: Fix the Reference time in data depending on the stream (See FrameShift module on sbndcode repo) - if (isRealData & (fDet == kSBND)) + if (isRealData && (fDet == kSBND)) { - mf::LogInfo("CAFMaker") << "Setting Reference Timing for timing object in SBND \n" - << " Shift Apply At Caf Level = " << rec.sbnd_frames.frameApplyAtCaf << " ns\n"; - - //shift reference frame for CRT objects: crt trk, crt sp, crt sp match, crt trk match - SBNDShiftCRTReference(rec, rec.sbnd_frames.frameApplyAtCaf); + // Update trigger info (on top of the other shifts from the configuration) + FillTriggerSBND(srsbndtiminginfo, srtrigger); - //shift reference frame for PMT objects: opflash, opt0 - SBNDShiftPMTReference(rec, rec.sbnd_frames.frameApplyAtCaf); + // Shift timing reference frame + if (!std::isnan(rec.sbnd_frames.frameApplyAtCaf) && (rec.sbnd_frames.frameApplyAtCaf != 0.0)){ + mf::LogInfo("CAFMaker") << "Setting Reference Timing for timing object in SBND \n" + << " Shift Apply At Caf Level = " << rec.sbnd_frames.frameApplyAtCaf << " ns\n"; + + //shift reference frame for CRT objects: crt trk, crt sp, crt sp match, crt trk match + SBNDShiftCRTReference(rec, rec.sbnd_frames.frameApplyAtCaf); + + //shift reference frame for PMT objects: opflash, opt0 + SBNDShiftPMTReference(rec, rec.sbnd_frames.frameApplyAtCaf); + } } // Get metadata information for header @@ -2631,11 +2745,11 @@ void CAFMaker::endSubRun(art::SubRun& sr) { //...................................................................... void CAFMaker::AddHistogramsToFile(TFile* outfile,bool isBlindPOT = false, bool isPrescalePOT = false) const { - outfile->cd(); TH1* hPOT = new TH1D("TotalPOT", "TotalPOT;; POT", 1, 0, 1); TH1* hEvents = new TH1D("TotalEvents", "TotalEvents;; Events", 1, 0, 1); + TH1* hGen = new TH1D("TotalGenEvents", "TotalGenEvents;; Events", 1, 0, 1); if (isBlindPOT) { hPOT->Fill(0.5,fTotalPOT*(1-(1/fParams.PrescaleFactor()))*GetBlindPOTScale()); @@ -2647,13 +2761,15 @@ void CAFMaker::endSubRun(art::SubRun& sr) { hPOT->Fill(0.5,fTotalPOT); } hEvents->Fill(0.5,fTotalEvents); + hGen->Fill(0.5,fTotalGenEvents); hPOT->Write(); hEvents->Write(); + hGen->Write(); if (fParams.CreateBlindedCAF()) { TH1*hBlindEvents = new TH1D("BlindEvents", "BlindEvents;; Events", 1, 0, 1); - TH1* hPrescaleEvents = new TH1D("PrescaleEvents", "PrescaleEvents;; Events", 1, 0, 1); + TH1*hPrescaleEvents = new TH1D("PrescaleEvents", "PrescaleEvents;; Events", 1, 0, 1); hBlindEvents->Fill(0.5, fBlindEvents); hPrescaleEvents->Fill(0.5, fPrescaleEvents); hBlindEvents->Write(); @@ -2663,29 +2779,26 @@ void CAFMaker::endSubRun(art::SubRun& sr) { //...................................................................... void CAFMaker::endJob() { - if (fTotalEvents == 0) { - std::cerr << "No events processed in this file. Aborting rather than " - "produce an empty CAF." + // Only produce empty recTree/GenieTree since it relies on non-zero art events. + // Still want to keep POT histograms. + if (fTotalEvents == 0) { + std::cerr << "No events processed in this file. Producing empty recTree/GenieTree." << std::endl; - // n.b. changed abort() to return so that eny exceptions thrown during startup - // still get printed to the user by art - return; } - - if(fFile){ - AddHistogramsToFile(fFile); - fRecTree->SetDirectory(fFile); - if(fGenieTree){ - fGenieTree->BuildIndex("SourceFileHash", "GENIEEntry"); - fGenieTree->SetDirectory(fFile); - } - if (fParams.CreateBlindedCAF()) { - fRecTreeb->SetDirectory(fFileb); - fRecTreep->SetDirectory(fFilep); + if (fTotalEvents > 0) { + if(fGenieTree){ + fGenieTree->BuildIndex("SourceFileHash", "GENIEEntry"); + fGenieTree->SetDirectory(fFile); + } + fRecTree->SetDirectory(fFile); + if (fParams.CreateBlindedCAF()) { + fRecTreeb->SetDirectory(fFileb); + fRecTreep->SetDirectory(fFilep); + } } fFile->cd(); fFile->Write(); @@ -2701,17 +2814,19 @@ void CAFMaker::endJob() { } if(fFlatFile){ - AddHistogramsToFile(fFlatFile); - fFlatTree->SetDirectory(fFlatFile); - if(fFlatGenieTree){ - fFlatGenieTree->BuildIndex("SourceFileHash", "GENIEEntry"); - fFlatGenieTree->SetDirectory(fFlatFile); - } - if (fParams.CreateBlindedCAF() && fFlatFileb) { - fFlatTreeb->SetDirectory(fFlatFileb); - fFlatTreep->SetDirectory(fFlatFilep); + if (fTotalEvents > 0) { + if(fFlatGenieTree){ + fFlatGenieTree->BuildIndex("SourceFileHash", "GENIEEntry"); + fFlatGenieTree->SetDirectory(fFlatFile); + } + fFlatTree->SetDirectory(fFlatFile); + if (fParams.CreateBlindedCAF() && fFlatFileb) { + fFlatTreeb->SetDirectory(fFlatFileb); + fFlatTreep->SetDirectory(fFlatFilep); + } } + fFlatFile->cd(); fFlatFile->Write(); if (fParams.CreateBlindedCAF()) { @@ -2754,7 +2869,6 @@ void CAFMaker::endJob() { if(fParams.CreateBlindedCAF() && fFlatFilep) AddMetadataToFile(fFlatFilep, metamap); } - } // end namespace caf DEFINE_ART_MODULE(caf::CAFMaker) //////////////////////////////////////////////////////////////////////// diff --git a/sbncode/CAFMaker/CMakeLists.txt b/sbncode/CAFMaker/CMakeLists.txt index c141efa72..5802cda09 100644 --- a/sbncode/CAFMaker/CMakeLists.txt +++ b/sbncode/CAFMaker/CMakeLists.txt @@ -29,6 +29,7 @@ art_make_library( LIBRARY_NAME sbncode_CAFMaker larcorealg::Geometry larcore::Geometry_Geometry_service larcorealg::GeoAlgo + lardataalg::headers larsim::MCCheater_BackTrackerService_service nusimdata::SimulationBase larsim::MCCheater_ParticleInventoryService_service @@ -66,6 +67,7 @@ cet_build_plugin ( CAFMaker art::module hep_concurrency::hep_concurrency lardataobj::RecoBase nurandom::RandomUtils_NuRandomService_service + lardata::DetectorPropertiesService lardata::DetectorClocksService BASENAME_ONLY ) diff --git a/sbncode/CAFMaker/FillExposure.cxx b/sbncode/CAFMaker/FillExposure.cxx index 01dd11b80..91746f26b 100644 --- a/sbncode/CAFMaker/FillExposure.cxx +++ b/sbncode/CAFMaker/FillExposure.cxx @@ -6,6 +6,29 @@ namespace caf caf::SRBNBInfo makeSRBNBInfo(sbn::BNBSpillInfo const& info) { + float FOM = info.FOM; + float PreFitFOM = info.PreFitFOM; + float NoMultiWireFOM = info.NoMultiWireFOM; + float finalFOM = -999.; + std::cout << "makeSRBNBInfo: Choosing FOM from available" << std::endl; + std::cout << "makeSRBNBInfo: Manually calculated width:" << FOM << std::endl; + std::cout << "makeSRBNBInfo: Pre-Fit width from database:" << PreFitFOM << std::endl; + std::cout << "makeSRBNBInfo: Assuming width of 1.0:" << NoMultiWireFOM << std::endl; + if((FOM > 0.0) & (FOM <= 1.0)){ + finalFOM = FOM; + std::cout << "makeSRBNBInfo: Chose manually calculated width" << std::endl; + } + else if((PreFitFOM > 0.0) & (PreFitFOM <= 1.0)){ + finalFOM = PreFitFOM; + std::cout << "makeSRBNBInfo: Chose pre-fit width" << std::endl; + } + else if((NoMultiWireFOM > 0.0) & (NoMultiWireFOM <= 1.0)) + { + finalFOM = 100.+NoMultiWireFOM; + std::cout << "makeSRBNBInfo: Chose assumed width of 1.0" << std::endl; + std::cout << "makeSRBNBInfo: Note, that this gets prefixed with 100 so it doesn't get automatically included in standard FOM cuts" << std::endl; + } + caf::SRBNBInfo single_store; single_store.spill_time_sec = info.spill_time_s; single_store.spill_time_nsec = info.spill_time_ns; @@ -15,14 +38,33 @@ namespace caf single_store.LM875A = info.LM875A; single_store.LM875B = info.LM875B; single_store.LM875C = info.LM875C; + single_store.HP873 = info.HP873; + single_store.VP873 = info.VP873; single_store.HP875 = info.HP875; single_store.VP875 = info.VP875; single_store.HPTG1 = info.HPTG1; single_store.VPTG1 = info.VPTG1; single_store.HPTG2 = info.HPTG2; single_store.VPTG2 = info.VPTG2; + single_store.FOM = finalFOM; + single_store.VP873 = info.VP873; + single_store.VP873Offset = info.VP873Offset; + single_store.VP875Offset = info.VP875Offset; + single_store.HP875Offset = info.HP875Offset; + single_store.VPTG1Offset = info.VPTG1Offset; + single_store.HPTG1Offset = info.HPTG1Offset; + single_store.VPTG2Offset = info.VPTG2Offset; + single_store.HPTG2Offset = info.HPTG2Offset; single_store.BTJT2 = info.BTJT2; single_store.THCURR = info.THCURR; + single_store.M875HS = info.M875HS; + single_store.M875VS = info.M875VS; + single_store.M875HM = info.M875HM; + single_store.M875VM = info.M875VM; + single_store.M876HS = info.M876HS; + single_store.M876VS = info.M876VS; + single_store.M876HM = info.M876HM; + single_store.M876VM = info.M876VM; single_store.M875BB = info.M875BB; single_store.M876BB = info.M876BB; single_store.MMBTBB = info.MMBTBB; diff --git a/sbncode/CAFMaker/FillExposure.h b/sbncode/CAFMaker/FillExposure.h index ec8def3d2..ffe70e3e8 100644 --- a/sbncode/CAFMaker/FillExposure.h +++ b/sbncode/CAFMaker/FillExposure.h @@ -7,6 +7,7 @@ #include "sbnobj/Common/POTAccounting/NuMISpillInfo.h" #include +#include namespace caf { diff --git a/sbncode/CAFMaker/FillFlashMatch.cxx b/sbncode/CAFMaker/FillFlashMatch.cxx index e6fee07d2..689d5e4f9 100644 --- a/sbncode/CAFMaker/FillFlashMatch.cxx +++ b/sbncode/CAFMaker/FillFlashMatch.cxx @@ -13,6 +13,7 @@ namespace caf //...................................................................... void FillSliceFlashMatch(const sbn::SimpleFlashMatch* fmatch /* can be nullptr */, caf::SRFlashMatch& srflash, + caf::TimeRefShifter<> const& shifter /* = {} */, bool allowEmpty) { if (fmatch == nullptr) { @@ -20,7 +21,7 @@ namespace caf return; } srflash.present = fmatch->present; - srflash.time = fmatch->time; + srflash.time = shifter.shiftedTime(fmatch->time); srflash.chargeQ = fmatch->charge.q; srflash.lightPE = fmatch->light.pe; srflash.score = fmatch->score.total; diff --git a/sbncode/CAFMaker/FillFlashMatch.h b/sbncode/CAFMaker/FillFlashMatch.h index 071f5838f..d2d5de826 100644 --- a/sbncode/CAFMaker/FillFlashMatch.h +++ b/sbncode/CAFMaker/FillFlashMatch.h @@ -2,17 +2,9 @@ #ifndef CAF_FILLFLASHMATCH_H #define CAF_FILLFLASHMATCH_H -#include - -#include "art/Framework/Services/Registry/ServiceHandle.h" - -// LArSoft includes -#include "larcore/Geometry/Geometry.h" -#include "larcorealg/Geometry/GeometryCore.h" - //Include new flash match class +#include "sbncode/CAFMaker/TimeRefShifters.h" #include "sbnobj/Common/Reco/SimpleFlashMatchVars.h" -#include "sbnanaobj/StandardRecord/StandardRecord.h" #include "sbnanaobj/StandardRecord/SRFlashMatch.h" namespace caf @@ -20,6 +12,7 @@ namespace caf void FillSliceFlashMatch(const sbn::SimpleFlashMatch* fmatch, caf::SRFlashMatch& srflash, + caf::TimeRefShifter<> const& shifter = {}, bool allowEmpty = false); } diff --git a/sbncode/CAFMaker/FillReco.cxx b/sbncode/CAFMaker/FillReco.cxx index 94d1f3c9d..6af0bbfe8 100644 --- a/sbncode/CAFMaker/FillReco.cxx +++ b/sbncode/CAFMaker/FillReco.cxx @@ -65,15 +65,13 @@ namespace caf } void FillCRTHit(const sbn::crt::CRTHit &hit, - bool use_ts0, - int64_t CRT_T0_reference_time, // ns, signed - double CRT_T1_reference_time, // us + caf::CRTtimeRefShifter const& shifter, + const std::map, sim::AuxDetSimChannel> &crtsimchanmap, caf::SRCRTHit &srhit, bool allowEmpty) { - srhit.t0 = ( (long long)(hit.ts0()) /*u_int64_t to int64_t*/ + CRT_T0_reference_time )/1000.; - srhit.t1 = hit.ts1()/1000.+CRT_T1_reference_time; // ns -> us - srhit.time = use_ts0 ? srhit.t0 : srhit.t1; + // all times are in us + shifter.fillCRTtimes(hit.ts0(), hit.ts1(), &srhit.t0, &srhit.t1, &srhit.time); srhit.position.x = hit.x_pos; srhit.position.y = hit.y_pos; @@ -86,14 +84,60 @@ namespace caf srhit.pe = hit.peshit; srhit.plane = hit.plane; + // lookup truth matching information + std::map true_energy; + std::set> checked; // keep track of what AuxDetSimChannels we have looked at + for (auto const &mac_to_pes: hit.pesmap) { + int mac = (int)mac_to_pes.first; + for (const std::pair &chan_pe: mac_to_pes.second) { + int chan = chan_pe.first; + + // check for the 3-pos Minos ("m") modules, or the 1-pos other modules + bool is_minos_module = mac < 94; + int pos = is_minos_module ? (chan/10 + 1) : 1; + + std::pair key {mac, pos}; + if (checked.count(key)) continue; // already looked here + + checked.insert(key); + + if (crtsimchanmap.count(key)) { + const sim::AuxDetSimChannel &crtsimchan = crtsimchanmap.at(key); + for (const sim::AuxDetIDE &ide: crtsimchan.AuxDetIDEs()) { + double hit_time_us = srhit.time; + double tru_time_us = ide.entryT/1e3; // ns -> us + + // 1us cut on truth matching + if (abs(hit_time_us - tru_time_us) < 1) { + // std::cout << "Hit at time: " << (srhit.time/1e3) << " " << (srhit.t0/1e3) << " " << (srhit.t1/1e3) << " pes: " << chan_pe.second << " of " << srhit.pe << " on FEB: " << mac << " channel: " << chan << " matched to AuxDetID: " << crtsimchan.AuxDetID() << " G4ID: " << ide.trackID << " E: " << ide.energyDeposited << " T: " << (ide.entryT/1e6) << std::endl; + true_energy[ide.trackID] += ide.energyDeposited; + } + + } + } + } + } + + // sort results by energy + std::vector> true_energy_list; + for (auto const &pair: true_energy) true_energy_list.push_back(std::make_pair(pair.second, pair.first)); + std::sort(true_energy_list.begin(), true_energy_list.end(), std::greater<>()); + + // Save to the SRCRTHit truth + for (auto const &pair: true_energy_list) { + srhit.truth.match_e.push_back(pair.first); + srhit.truth.match_id.push_back(pair.second); + } + if (true_energy_list.size() > 0) srhit.truth.bestmatch_id = true_energy_list[0].second; + } void FillCRTTrack(const sbn::crt::CRTTrack &track, - bool use_ts0, - caf::SRCRTTrack &srtrack, - bool allowEmpty) { + caf::CRTtimeRefShifter const& shifter, + caf::SRCRTTrack &srtrack, + bool allowEmpty) { - srtrack.time = (use_ts0 ? (float)track.ts0_ns : track.ts1_ns) / 1000.; + srtrack.time = shifter.shiftTime(shifter.usingTS0()? track.ts0_ns: track.ts1_ns); srtrack.hita.position.x = track.x1_pos; srtrack.hita.position.y = track.y1_pos; @@ -170,15 +214,17 @@ namespace caf void FillCRTPMTMatch(const sbn::crt::CRTPMTMatching &match, caf::SRCRTPMTMatch &srmatch, + caf::TimeRefShifter<> const& PMTshifter, + caf::CRTtimeRefShifter const& CRTshifter, bool allowEmpty){ // allowEmpty does not (yet) matter here (void) allowEmpty; //srmatch.setDefault(); srmatch.flashID = match.flashID; - srmatch.flashTime_us = match.flashTime; + srmatch.flashTime_us = PMTshifter.shiftedTime(match.flashTime); srmatch.flashGateTime = match.flashGateTime; - srmatch.firstOpHitPeakTime = match.firstOpHitPeakTime; - srmatch.firstOpHitStartTime = match.firstOpHitStartTime; + srmatch.firstOpHitPeakTime = PMTshifter.shiftedTime(match.firstOpHitPeakTime); + srmatch.firstOpHitStartTime = PMTshifter.shiftedTime(match.firstOpHitStartTime); srmatch.flashInGate = match.flashInGate; srmatch.flashInBeam = match.flashInBeam; srmatch.flashPE = match.flashPE; @@ -188,8 +234,8 @@ namespace caf unsigned int topen = 0, topex = 0, sideen = 0, sideex = 0; for(const auto& matchedCRTHit : match.matchedCRTHits){ caf::SRMatchedCRT matchedCRT; - matchedCRT.PMTTimeDiff = matchedCRTHit.PMTTimeDiff; - matchedCRT.time = matchedCRTHit.time; + matchedCRT.PMTTimeDiff = matchedCRTHit.PMTTimeDiff; + matchedCRT.time = CRTshifter.shiftTime(matchedCRTHit.time * 1000); matchedCRT.sys = matchedCRTHit.sys; matchedCRT.region = matchedCRTHit.region; matchedCRT.position = SRVector3D(matchedCRTHit.position.X(), matchedCRTHit.position.Y(), matchedCRTHit.position.Z()); @@ -211,11 +257,12 @@ namespace caf std::vector const& hits, int cryo, caf::SROpFlash &srflash, + caf::TimeRefShifter<> const& shifter, bool allowEmpty) { srflash.setDefault(); - srflash.time = flash.Time(); + srflash.time = shifter.shiftedTime(flash.Time()); srflash.timewidth = flash.TimeWidth(); double firstTime = std::numeric_limits::max(); @@ -224,7 +271,7 @@ namespace caf if (firstTime > hitTime) firstTime = hitTime; } - srflash.firsttime = firstTime; + srflash.firsttime = shifter.shiftedTimeIfNot(firstTime, std::numeric_limits::max()); srflash.cryo = cryo; // 0 in SBND, 0/1 for E/W in ICARUS @@ -259,11 +306,12 @@ namespace caf std::vector const& hits, int tpc, caf::SROpFlash &srflash, + caf::TimeRefShifter<> const& shifter, bool allowEmpty) { srflash.setDefault(); - srflash.time = flash.Time(); + srflash.time = shifter.shiftedTime(flash.Time()); srflash.timewidth = flash.TimeWidth(); double firstTime = std::numeric_limits::max(); @@ -272,7 +320,7 @@ namespace caf if (firstTime > hitTime) firstTime = hitTime; } - srflash.firsttime = firstTime; + srflash.firsttime = shifter.shiftedTimeIfNot(firstTime, std::numeric_limits::max()); srflash.tpc = tpc; srflash.totalpe = flash.TotalPE(); @@ -517,7 +565,7 @@ namespace caf } void FillSliceOpT0Finder(const std::vector> &opt0_v, - caf::SRSlice &slice) + caf::SRSlice &slice, caf::TimeRefShifter<> const& shifter) { if (opt0_v.empty()==false){ unsigned int nopt0 = opt0_v.size(); @@ -538,7 +586,7 @@ namespace caf const sbn::OpT0Finder &maxOpT0 = *opt0_v[max_idx]; slice.opt0.tpc = maxOpT0.tpc; - slice.opt0.time = maxOpT0.time; + slice.opt0.time = shifter.shiftedTime(maxOpT0.time); slice.opt0.score = maxOpT0.score; slice.opt0.measPE = maxOpT0.measPE; slice.opt0.hypoPE = maxOpT0.hypoPE; @@ -556,7 +604,7 @@ namespace caf } const sbn::OpT0Finder &secOpT0 = *opt0_v[sec_idx]; slice.opt0_sec.tpc = secOpT0.tpc; - slice.opt0_sec.time = secOpT0.time; + slice.opt0_sec.time = shifter.shiftedTime(secOpT0.time); slice.opt0_sec.score = secOpT0.score; slice.opt0_sec.measPE = secOpT0.measPE; slice.opt0_sec.hypoPE = secOpT0.hypoPE; @@ -632,9 +680,8 @@ namespace caf void FillTrackCRTHit(const std::vector> &t0match, const std::vector> &hitmatch, const std::vector> &hitmatchinfo, - bool use_ts0, - int64_t CRT_T0_reference_time, // ns, signed - double CRT_T1_reference_time, // us + caf::CRTtimeRefShifter const& shifter, + const std::map, sim::AuxDetSimChannel> &crtsimchanmap, caf::SRTrack &srtrack, bool allowEmpty) { @@ -661,18 +708,19 @@ namespace caf srtrack.crthit.hit.plane = t0match[0]->fID; } if (hitmatch.size()) { - FillCRTHit(*hitmatch[0], use_ts0, CRT_T0_reference_time, CRT_T1_reference_time, srtrack.crthit.hit, allowEmpty); + FillCRTHit(*hitmatch[0], shifter, crtsimchanmap, srtrack.crthit.hit, allowEmpty); } } void FillTrackCRTTrack(const std::vector> &t0match, caf::SRTrack &srtrack, + caf::CRTtimeRefShifter const& timeShifter, bool allowEmpty) { if (t0match.size()) { assert(t0match.size() == 1); srtrack.crttrack.angle = t0match[0]->fTriggerConfidence; - srtrack.crttrack.time = t0match[0]->fTime / 1e3; /* ns -> us */ + srtrack.crttrack.time = timeShifter.shiftTime(t0match[0]->fTime); // ns -> us // TODO/FIXME: FILL MORE ONCE WE HAVE THE CRT HIT!!! @@ -821,10 +869,30 @@ namespace caf } } + // Helper function: get the e field + double GetEfield(const detinfo::DetectorPropertiesData& dprop, spacecharge::SpaceCharge const& sce, const geo::Point_t& loc) { + + double EField = dprop.Efield(); + if (sce.EnableSimEfieldSCE()) { + // Gets fractional E field distortions w.r.t. the nominal field on x-axis + geo::Vector_t EFieldOffsets = sce.GetEfieldOffsets(loc); + // Add 1 in X direction as this is the direction of the drift field, not caring if it is +x or -x direction, since we only want |E| + EFieldOffsets += geo::Vector_t{1, 0, 0}; + // Convert to Absolute E Field from relative + EFieldOffsets *= EField; + // We only care about the magnitude for recombination + EField = EFieldOffsets.r(); + } + return EField; + } + void FillTrackPlaneCalo(const anab::Calorimetry &calo, + const recob::Track& track, const std::vector> &hits, + const std::vector& thms, bool fill_calo_points, float fillhit_rrstart, float fillhit_rrend, const detinfo::DetectorPropertiesData &dprop, + const spacecharge::SpaceCharge &sce, caf::SRTrackCalo &srcalo) { // Collect info from Calorimetry @@ -859,7 +927,8 @@ namespace caf // lookup the wire -- the Calorimery object makes this // __way__ harder than it should be - for (const art::Ptr &h: hits) { + for (unsigned i_hit = 0; i_hit < hits.size(); i_hit++) { + const art::Ptr &h = hits[i_hit]; if (h.key() == tps[i]) { p.wire = h->WireID().Wire; p.tpc = h->WireID().TPC; @@ -871,6 +940,19 @@ namespace caf p.mult = h->Multiplicity(); p.start = h->StartTick(); p.end = h->EndTick(); + + + // Get the trajectory point index from this hit. Again -- this is too hard. + // + // Use this to get the (SCE corrected) efield and the angle to the drift direction + unsigned traj_point_index = thms.at(i_hit)->Index(); + if (track.HasValidPoint(traj_point_index)) { + float costh_drift = track.DirectionAtPoint(traj_point_index).X(); + float phi = acos(abs(costh_drift)); + float efield = GetEfield(dprop, sce, track.LocationAtPoint(traj_point_index)); + p.efield = efield; + p.phi = phi; + } } } @@ -924,9 +1006,12 @@ namespace caf } void FillTrackCalo(const std::vector> &calos, + const recob::Track& track, const std::vector> &hits, + const std::vector& thms, bool fill_calo_points, float fillhit_rrstart, float fillhit_rrend, const detinfo::DetectorPropertiesData &dprop, + const spacecharge::SpaceCharge &sce, caf::SRTrack& srtrack, bool allowEmpty) { @@ -939,7 +1024,7 @@ namespace caf if (calo.PlaneID()) { unsigned plane_id = calo.PlaneID().Plane; assert(plane_id < 3); - FillTrackPlaneCalo(calo, hits, fill_calo_points, fillhit_rrstart, fillhit_rrend, dprop, srtrack.calo[plane_id]); + FillTrackPlaneCalo(calo, track, hits, thms, fill_calo_points, fillhit_rrstart, fillhit_rrend, dprop, sce, srtrack.calo[plane_id]); } } @@ -1131,17 +1216,21 @@ namespace caf } void FillTPCPMTBarycenterMatch(const sbn::TPCPMTBarycenterMatch *matchInfo, - caf::SRSlice& slice) + caf::SRSlice& slice, + caf::TimeRefShifter<> const& shifter) { slice.barycenterFM.setDefault(); if ( matchInfo != nullptr ) { + bool const matched = (matchInfo->radius >= 0.0); slice.barycenterFM.chargeTotal = matchInfo->chargeTotal; slice.barycenterFM.chargeCenterXLocal = matchInfo->chargeCenterXLocal; slice.barycenterFM.chargeCenter = SRVector3D (matchInfo->chargeCenter.x(), matchInfo->chargeCenter.y(), matchInfo->chargeCenter.z()); slice.barycenterFM.chargeWidth = SRVector3D (matchInfo->chargeWidth.x(), matchInfo->chargeWidth.y(), matchInfo->chargeWidth.z()); - slice.barycenterFM.flashFirstHit = matchInfo->flashFirstHit; - slice.barycenterFM.flashTime = matchInfo->flashTime; + slice.barycenterFM.flashFirstHit + = shifter.shiftedTimeIf(matchInfo->flashFirstHit, matched); + slice.barycenterFM.flashTime + = shifter.shiftedTimeIf(matchInfo->flashTime, matched); slice.barycenterFM.flashPEs = matchInfo->flashPEs; slice.barycenterFM.flashCenter = SRVector3D (matchInfo->flashCenter.x(), matchInfo->flashCenter.y(), matchInfo->flashCenter.z()); slice.barycenterFM.flashWidth = SRVector3D (matchInfo->flashWidth.x(), matchInfo->flashWidth.y(), matchInfo->flashWidth.z()); @@ -1154,6 +1243,8 @@ namespace caf slice.barycenterFM.deltaZ_Trigger = matchInfo->deltaZ_Trigger; slice.barycenterFM.deltaY_Trigger = matchInfo->deltaY_Trigger; slice.barycenterFM.radius_Trigger = matchInfo->radius_Trigger; + slice.barycenterFM.score = matchInfo->score; + slice.barycenterFM.chi2 = matchInfo->chi2; } } diff --git a/sbncode/CAFMaker/FillReco.h b/sbncode/CAFMaker/FillReco.h index a79d0185d..2487e0d8a 100644 --- a/sbncode/CAFMaker/FillReco.h +++ b/sbncode/CAFMaker/FillReco.h @@ -5,15 +5,18 @@ #include "art/Framework/Services/Registry/ServiceHandle.h" #include "larsim/MCCheater/ParticleInventoryService.h" -#include "lardataalg/DetectorInfo/DetectorPropertiesStandard.h" +#include "lardataalg/DetectorInfo/DetectorPropertiesData.h" // LArSoft includes #include "larcorealg/Geometry/fwd.h" +#include "larevt/SpaceCharge/SpaceCharge.h" + #include "lardataobj/RecoBase/PFParticle.h" #include "lardataobj/RecoBase/Shower.h" #include "lardataobj/RecoBase/Slice.h" #include "lardataobj/RecoBase/Track.h" +#include "lardataobj/RecoBase/TrackHitMeta.h" #include "lardataobj/RecoBase/Vertex.h" #include "lardataobj/RecoBase/Hit.h" #include "lardataobj/RecoBase/SpacePoint.h" @@ -25,6 +28,7 @@ #include "lardataobj/AnalysisBase/T0.h" #include "lardataobj/RecoBase/PFParticleMetadata.h" #include "lardataobj/RecoBase/MCSFitResult.h" +#include "lardataobj/Simulation/AuxDetSimChannel.h" #include "larrecodnn/CVN/func/Result.h" #include "sbnobj/Common/Reco/Stub.h" #include "sbnobj/Common/Reco/RangeP.h" @@ -51,6 +55,8 @@ #include "sbnanaobj/StandardRecord/SRSlice.h" #include "sbnanaobj/StandardRecord/StandardRecord.h" +#include "sbncode/CAFMaker/TimeRefShifters.h" + namespace caf { @@ -103,7 +109,7 @@ namespace caf bool allowEmpty = false); void FillSliceOpT0Finder(const std::vector> &opt0_v, - caf::SRSlice &slice); + caf::SRSlice &slice, caf::TimeRefShifter<> const& shifter); void FillSliceBarycenter(const std::vector> &inputHits, const std::vector> &inputPoints, @@ -171,14 +177,14 @@ namespace caf void FillTrackCRTHit(const std::vector> &t0match, const std::vector> &hitmatch, const std::vector> &hitmatchinfo, - bool use_ts0, - int64_t CRT_T0_reference_time, // ns, signed - double CRT_T1_reference_time, // us + caf::CRTtimeRefShifter const& shifter, + const std::map, sim::AuxDetSimChannel> &crtsimchanmap, caf::SRTrack &srtrack, bool allowEmpty = false); void FillTrackCRTTrack(const std::vector> &t0match, caf::SRTrack &srtrack, + caf::CRTtimeRefShifter const& timeShifter, bool allowEmpty = false); void FillTrackCRTSpacePoint(const anab::T0 &t0match, @@ -207,9 +213,12 @@ namespace caf bool allowEmpty = false); void FillTrackPlaneCalo(const anab::Calorimetry &calo, + const recob::Track& track, const std::vector> &hits, + const std::vector& thms, bool fill_calo_points, float fillhit_rrstart, float fillhit_rrend, const detinfo::DetectorPropertiesData &dprop, + spacecharge::SpaceCharge const& sce, caf::SRTrackCalo &srcalo); void FillTrackScatterClosestApproach(const art::Ptr closestapproach, @@ -225,9 +234,12 @@ namespace caf bool allowEmpty = false); void FillTrackCalo(const std::vector> &calos, + const recob::Track& track, const std::vector> &hits, + const std::vector& thms, bool fill_calo_points, float fillhit_rrstart, float fillhit_rrend, const detinfo::DetectorPropertiesData &dprop, + spacecharge::SpaceCharge const& sce, caf::SRTrack& srtrack, bool allowEmpty = false); @@ -238,13 +250,12 @@ namespace caf unsigned truth_ind); void FillCRTHit(const sbn::crt::CRTHit &hit, - bool use_ts0, - int64_t CRT_T0_reference_time, // ns, signed - double CRT_T1_reference_time, // us + caf::CRTtimeRefShifter const& shifter, + const std::map, sim::AuxDetSimChannel> &crtsimchanmap, caf::SRCRTHit &srhit, bool allowEmpty = false); void FillCRTTrack(const sbn::crt::CRTTrack &track, - bool use_ts0, + caf::CRTtimeRefShifter const& shifter, caf::SRCRTTrack &srtrack, bool allowEmpty = false); @@ -260,20 +271,25 @@ namespace caf std::vector const& hits, int cryo, caf::SROpFlash &srflash, + caf::TimeRefShifter<> const& shifter = {}, bool allowEmpty = false); void FillSBNDOpFlash(const recob::OpFlash &flash, std::vector const& hits, int tpc, caf::SROpFlash &srflash, + caf::TimeRefShifter<> const& shifter = {}, bool allowEmpty = false); void FillCRTPMTMatch(const sbn::crt::CRTPMTMatching &match, caf::SRCRTPMTMatch &srmatch, + caf::TimeRefShifter<> const& PMTshifter, + caf::CRTtimeRefShifter const& CRTshifter, bool allowEmpty = false); void FillTPCPMTBarycenterMatch(const sbn::TPCPMTBarycenterMatch *matchInfo, - caf::SRSlice& slice); + caf::SRSlice& slice, + caf::TimeRefShifter<> const& shifter); void FillCVNScores(const lcvn::Result *cvnResult, caf::SRSlice& slice); diff --git a/sbncode/CAFMaker/FillTrigger.cxx b/sbncode/CAFMaker/FillTrigger.cxx index 009deccef..5c6644c1e 100644 --- a/sbncode/CAFMaker/FillTrigger.cxx +++ b/sbncode/CAFMaker/FillTrigger.cxx @@ -1,20 +1,22 @@ #include "sbncode/CAFMaker/FillTrigger.h" +#include "sbnobj/Common/Trigger/BeamBits.h" namespace caf { void FillTrigger(const sbn::ExtraTriggerInfo& addltrig_info, const raw::Trigger& trig, caf::SRTrigger& triggerInfo, - const double time_offset = 0.0) + caf::TimeRefShifter<> const& shifter /* = {} */ + ) { triggerInfo.global_trigger_time = addltrig_info.triggerTimestamp; triggerInfo.beam_gate_time_abs = addltrig_info.beamGateTimestamp; - triggerInfo.beam_gate_det_time = trig.BeamGateTime() + time_offset; - triggerInfo.global_trigger_det_time = trig.TriggerTime() + time_offset; + triggerInfo.beam_gate_det_time = shifter.shiftedTime(trig.BeamGateTime()); + triggerInfo.global_trigger_det_time = shifter.shiftedTime(trig.TriggerTime()); double diff_ts = triggerInfo.global_trigger_det_time - triggerInfo.beam_gate_det_time; triggerInfo.trigger_within_gate = diff_ts; - + triggerInfo.prev_global_trigger_time = addltrig_info.previousTriggerTimestamp; triggerInfo.source_type = sbn::bits::value(addltrig_info.sourceType); triggerInfo.trigger_type = sbn::bits::value(addltrig_info.triggerType); @@ -25,14 +27,10 @@ namespace caf triggerInfo.gate_delta = addltrig_info.gateCountFromPreviousTrigger; } - void FillTriggerMC(double absolute_time, caf::SRTrigger& triggerInfo) { - triggerInfo.global_trigger_time = absolute_time; - triggerInfo.beam_gate_time_abs = absolute_time; - - // Set this to 0 since the "MC" trigger is (for now) always at the spill time - triggerInfo.trigger_within_gate = 0.; - - // TODO: fill others? + void FillTriggerSBND(caf::SRSBNDTimingInfo& timingInfo, caf::SRTrigger& triggerInfo){ + // updates the existing triggerInfo record + triggerInfo.global_trigger_time = timingInfo.hltEtrig; + triggerInfo.beam_gate_time_abs = timingInfo.hltBeamGate; } } diff --git a/sbncode/CAFMaker/FillTrigger.h b/sbncode/CAFMaker/FillTrigger.h index b83c2e9f0..2ae879e96 100644 --- a/sbncode/CAFMaker/FillTrigger.h +++ b/sbncode/CAFMaker/FillTrigger.h @@ -1,23 +1,21 @@ #ifndef CAF_FILLTRIGGER_H #define CAF_FILLTRIGGER_H +#include "sbncode/CAFMaker/TimeRefShifters.h" #include "sbnobj/Common/Trigger/ExtraTriggerInfo.h" -#include "sbnobj/Common/Trigger/BeamBits.h" #include "sbnanaobj/StandardRecord/SRTrigger.h" +#include "sbnanaobj/StandardRecord/SRSBNDTimingInfo.h" #include "lardataobj/RawData/TriggerData.h" -#include - namespace caf { void FillTrigger(const sbn::ExtraTriggerInfo& addltrig_info, const raw::Trigger& trig_info, caf::SRTrigger& triggerInfo, - const double time_offset); - - void FillTriggerMC(double absolute_time, caf::SRTrigger& triggerInfo); + caf::TimeRefShifter<> const& shifter = {}); + void FillTriggerSBND(caf::SRSBNDTimingInfo& timingInfo, caf::SRTrigger& triggerInfo); } #endif diff --git a/sbncode/CAFMaker/FillTrue.cxx b/sbncode/CAFMaker/FillTrue.cxx index 491c813c0..b6c78ea06 100644 --- a/sbncode/CAFMaker/FillTrue.cxx +++ b/sbncode/CAFMaker/FillTrue.cxx @@ -5,7 +5,6 @@ #include "larcorealg/Geometry/WireReadoutGeom.h" #include "larevt/SpaceCharge/SpaceCharge.h" #include "larcore/CoreUtils/ServiceUtil.h" -#include "RecoUtils/RecoUtils.h" #include "CLHEP/Random/RandGauss.h" @@ -136,7 +135,7 @@ namespace caf { }//FillTrackTruth // Assumes truth matching and calo-points are filled - void FillTrackCaloTruth(const std::map>> &id_to_ide_map, + void FillTrackCaloTruth(const std::map> &id_to_ide_map, const std::vector &mc_particles, const geo::GeometryCore& geometry, const geo::WireReadoutGeom& wireReadout, @@ -163,10 +162,10 @@ namespace caf { // Load the hits // match on the channel, which is unique - const std::vector> &match_ides = id_to_ide_map.at(srtrack.truth.p.G4ID); + const std::vector &match_ides = id_to_ide_map.at(srtrack.truth.p.G4ID); std::map> chan_2_ides; - for (auto const &ide_pair: match_ides) { - chan_2_ides[wireReadout.PlaneWireToChannel(ide_pair.first)].push_back(ide_pair.second); + for (auto const &ide_p: match_ides) { + chan_2_ides[wireReadout.PlaneWireToChannel(ide_p.wire)].push_back(ide_p.ide); } // pre-compute partial ranges @@ -634,15 +633,15 @@ namespace caf { void FillTrueG4Particle(const simb::MCParticle &particle, const std::vector &active_volumes, const std::vector> &tpc_volumes, - const std::map>> &id_to_ide_map, + const std::map> &id_to_ide_map, const std::map>> &id_to_truehit_map, const cheat::BackTrackerService &backtracker, const cheat::ParticleInventoryService &inventory_service, const std::vector> &neutrinos, caf::SRTrueParticle &srparticle) { - std::vector> empty; - const std::vector> &particle_ides = id_to_ide_map.count(particle.TrackId()) ? id_to_ide_map.at(particle.TrackId()) : empty; + std::vector empty; + const std::vector &particle_ides = id_to_ide_map.count(particle.TrackId()) ? id_to_ide_map.at(particle.TrackId()) : empty; std::vector> emptyHits; const std::vector> &particle_hits = id_to_truehit_map.count(particle.TrackId()) ? id_to_truehit_map.at(particle.TrackId()) : emptyHits; @@ -662,9 +661,9 @@ namespace caf { } } - for (auto const &ide_pair: particle_ides) { - const geo::WireID &w = ide_pair.first; - const sim::IDE *ide = ide_pair.second; + for (auto const &ide_p: particle_ides) { + const geo::WireID &w = ide_p.wire; + const sim::IDE *ide = ide_p.ide; if(w.Plane >= 0 && w.Plane < 3 && w.Cryostat < 2){ srparticle.plane[w.Cryostat][w.Plane].visE += ide->energy / 1000. /* MeV -> GeV*/; @@ -701,7 +700,7 @@ namespace caf { } // get the wall if (entry_point > 0) { - srparticle.wallin = GetWallCross(active_volumes.at(cryostat_index), particle.Position(entry_point).Vect(), particle.Position(entry_point-1).Vect()); + srparticle.wallin = sbn::GetWallCross(active_volumes.at(cryostat_index), particle.Position(entry_point).Vect(), particle.Position(entry_point-1).Vect()); } int exit_point = -1; @@ -777,8 +776,11 @@ namespace caf { if (exit_point < 0 && entry_point >= 0) { exit_point = particle.NumberTrajectoryPoints() - 1; } + if (exit_point >= 0 && entry_point >=0 && exit_point == entry_point && exit_point < static_cast(particle.NumberTrajectoryPoints()) - 1){ + exit_point++; // to avoid exactly the same start and end positions when single index is inside the active volumne + } if (exit_point >= 0 && ((unsigned)exit_point) < particle.NumberTrajectoryPoints() - 1) { - srparticle.wallout = GetWallCross(active_volumes.at(cryostat_index), particle.Position(exit_point).Vect(), particle.Position(exit_point+1).Vect()); + srparticle.wallout = sbn::GetWallCross(active_volumes.at(cryostat_index), particle.Position(exit_point).Vect(), particle.Position(exit_point+1).Vect()); } // other truth information @@ -799,8 +801,8 @@ namespace caf { srparticle.endp = (exit_point >= 0) ? particle.Momentum(exit_point).Vect() : TVector3(-9999, -9999, -9999); srparticle.endE = (exit_point >= 0) ? particle.Momentum(exit_point).E() : -9999.; - srparticle.start_process = GetG4ProcessID(particle.Process()); - srparticle.end_process = GetG4ProcessID(particle.EndProcess()); + srparticle.start_process = sbn::GetG4ProcessID(particle.Process()); + srparticle.end_process = sbn::GetG4ProcessID(particle.EndProcess()); srparticle.G4ID = particle.TrackId(); srparticle.parent = particle.Mother(); @@ -871,37 +873,6 @@ namespace caf { return ret; } - std::map>> PrepTrueHits(const std::vector> &allHits, - const detinfo::DetectorClocksData &clockData, const cheat::BackTrackerService &backtracker) { - std::map>> ret; - for (const art::Ptr h: allHits) { - for (int ID: backtracker.HitToTrackIds(clockData, *h)) { - ret[abs(ID)].push_back(h); - } - } - return ret; - } - - std::map>> PrepSimChannels(const std::vector> &simchannels, const geo::WireReadoutGeom &wireReadout) { - std::map>> ret; - - for (const art::Ptr sc : simchannels) { - // Lookup the wire of this channel - raw::ChannelID_t channel = sc->Channel(); - std::vector maybewire = wireReadout.ChannelToWire(channel); - geo::WireID thisWire; // Default constructor makes invalid wire - if (maybewire.size()) thisWire = maybewire[0]; - - for (const auto &item : sc->TDCIDEMap()) { - for (const sim::IDE &ide: item.second) { - // indexing initializes empty vector - ret[abs(ide.trackID)].push_back({thisWire, &ide}); - } - } - } - return ret; - } - } // end namespace @@ -1253,126 +1224,6 @@ bool FRFillNueCC(const simb::MCTruth &mctruth, return true; } -caf::Wall_t caf::GetWallCross(const geo::BoxBoundedGeo &volume, const TVector3 p0, const TVector3 p1) { - TVector3 direction = (p1 - p0) * ( 1. / (p1 - p0).Mag()); - std::vector intersections = volume.GetIntersections(p0, direction); - - assert(intersections.size() == 2); - - // get the intersection point closer to p0 - int intersection_i = ((intersections[0] - p0).Mag() < (intersections[1] - p0).Mag()) ? 0 : 1; - - double eps = 1e-3; - if (abs(intersections[intersection_i].X() - volume.MinX()) < eps) { - //std::cout << "Left\n"; - return caf::kWallLeft; - } - else if (abs(intersections[intersection_i].X() - volume.MaxX()) < eps) { - //std::cout << "Right\n"; - return caf::kWallRight; - } - else if (abs(intersections[intersection_i].Y() - volume.MinY()) < eps) { - //std::cout << "Bottom\n"; - return caf::kWallBottom; - } - else if (abs(intersections[intersection_i].Y() - volume.MaxY()) < eps) { - //std::cout << "Top\n"; - return caf::kWallTop; - } - else if (abs(intersections[intersection_i].Z() - volume.MinZ()) < eps) { - //std::cout << "Front\n"; - return caf::kWallFront; - } - else if (abs(intersections[intersection_i].Z() - volume.MaxZ()) < eps) { - //std::cout << "Back\n"; - return caf::kWallBack; - } - else assert(false); - //std::cout << "None\n"; - - return caf::kWallNone; -}//GetWallCross - -//------------------------------------------ - -caf::g4_process_ caf::GetG4ProcessID(const std::string &process_name) { -#define MATCH_PROCESS(name) if (process_name == #name) {return caf::kG4 ## name;} -#define MATCH_PROCESS_NAMED(strname, id) if (process_name == #strname) {return caf::kG4 ## id;} - MATCH_PROCESS(primary) - MATCH_PROCESS(CoupledTransportation) - MATCH_PROCESS(FastScintillation) - MATCH_PROCESS(Decay) - MATCH_PROCESS(anti_neutronInelastic) - MATCH_PROCESS(neutronInelastic) - MATCH_PROCESS(anti_protonInelastic) - MATCH_PROCESS(protonInelastic) - MATCH_PROCESS(hadInelastic) - MATCH_PROCESS_NAMED(kaon+Inelastic, kaonpInelastic) - MATCH_PROCESS_NAMED(kaon-Inelastic, kaonmInelastic) - MATCH_PROCESS_NAMED(kaon+Inelastic, kaonpInelastic) - MATCH_PROCESS_NAMED(kaon-Inelastic, kaonmInelastic) - MATCH_PROCESS_NAMED(sigma+Inelastic, sigmapInelastic) - MATCH_PROCESS_NAMED(sigma-Inelastic, sigmamInelastic) - MATCH_PROCESS_NAMED(pi+Inelastic, pipInelastic) - MATCH_PROCESS_NAMED(pi-Inelastic, pimInelastic) - MATCH_PROCESS_NAMED(xi+Inelastic, xipInelastic) - MATCH_PROCESS_NAMED(xi-Inelastic, ximInelastic) - MATCH_PROCESS(kaon0LInelastic) - MATCH_PROCESS(kaon0SInelastic) - MATCH_PROCESS(lambdaInelastic) - MATCH_PROCESS_NAMED(anti-lambdaInelastic, anti_lambdaInelastic) - MATCH_PROCESS(He3Inelastic) - MATCH_PROCESS(ionInelastic) - MATCH_PROCESS(xi0Inelastic) - MATCH_PROCESS(alphaInelastic) - MATCH_PROCESS(tInelastic) - MATCH_PROCESS(dInelastic) - MATCH_PROCESS(anti_neutronElastic) - MATCH_PROCESS(neutronElastic) - MATCH_PROCESS(anti_protonElastic) - MATCH_PROCESS(protonElastic) - MATCH_PROCESS(hadElastic) - MATCH_PROCESS_NAMED(kaon+Elastic, kaonpElastic) - MATCH_PROCESS_NAMED(kaon-Elastic, kaonmElastic) - MATCH_PROCESS_NAMED(pi+Elastic, pipElastic) - MATCH_PROCESS_NAMED(pi-Elastic, pimElastic) - MATCH_PROCESS(conv) - MATCH_PROCESS(phot) - MATCH_PROCESS(annihil) - MATCH_PROCESS(nCapture) - MATCH_PROCESS(nKiller) - MATCH_PROCESS(muMinusCaptureAtRest) - MATCH_PROCESS(muIoni) - MATCH_PROCESS(eBrem) - MATCH_PROCESS(CoulombScat) - MATCH_PROCESS(hBertiniCaptureAtRest) - MATCH_PROCESS(hFritiofCaptureAtRest) - MATCH_PROCESS(photonNuclear) - MATCH_PROCESS(muonNuclear) - MATCH_PROCESS(electronNuclear) - MATCH_PROCESS(positronNuclear) - MATCH_PROCESS(compt) - MATCH_PROCESS(eIoni) - MATCH_PROCESS(muBrems) - MATCH_PROCESS(hIoni) - MATCH_PROCESS(ionIoni) - MATCH_PROCESS(hBrems) - MATCH_PROCESS(muPairProd) - MATCH_PROCESS(hPairProd) - MATCH_PROCESS(LArVoxelReadoutScoringProcess) - MATCH_PROCESS(Transportation) - MATCH_PROCESS(msc) - MATCH_PROCESS(StepLimiter) - MATCH_PROCESS(RadioactiveDecayBase) - std::cerr << "Error: Process name with no match (" << process_name << ")\n"; - assert(false); - return caf::kG4UNKNOWN; // unreachable in debug mode -#undef MATCH_PROCESS -#undef MATCH_PROCESS_NAMED - -}//GetG4ProcessID -//------------------------------------------- - float ContainedLength(const TVector3 &v0, const TVector3 &v1, const std::vector &boxes) { static const geoalgo::GeoAlgo algo; diff --git a/sbncode/CAFMaker/FillTrue.h b/sbncode/CAFMaker/FillTrue.h index 419ed6ee9..d4134d55c 100644 --- a/sbncode/CAFMaker/FillTrue.h +++ b/sbncode/CAFMaker/FillTrue.h @@ -33,6 +33,8 @@ #include "sbnanaobj/StandardRecord/StandardRecord.h" #include "sbnanaobj/StandardRecord/SRMeVPrtl.h" +#include "RecoUtils/RecoUtils.h" + namespace caf { struct HitsEnergy { @@ -40,13 +42,6 @@ namespace caf float totE; }; - // Helpers - caf::Wall_t GetWallCross( const geo::BoxBoundedGeo &volume, - const TVector3 p0, - const TVector3 p1); - - caf::g4_process_ GetG4ProcessID(const std::string &name); - void FillSRGlobal(const sbn::evwgh::EventWeightParameterSet& pset, caf::SRGlobal& srglobal, std::map& weightPSetIndex); @@ -73,7 +68,7 @@ namespace caf void FillTrueG4Particle(const simb::MCParticle &particle, const std::vector &active_volumes, const std::vector> &tpc_volumes, - const std::map>> &id_to_ide_map, + const std::map> &id_to_ide_map, const std::map>> &id_to_truehit_map, const cheat::BackTrackerService &backtracker, const cheat::ParticleInventoryService &inventory_service, @@ -103,7 +98,7 @@ namespace caf caf::SRTrack& srtrack, bool allowEmpty = false); - void FillTrackCaloTruth(const std::map>> &id_to_ide_map, + void FillTrackCaloTruth(const std::map> &id_to_ide_map, const std::vector &mc_particles, const geo::GeometryCore & geometry, const geo::WireReadoutGeom& wireReadout, @@ -133,9 +128,6 @@ namespace caf CLHEP::HepRandomEngine &rand, std::vector &srfakereco); - std::map>> PrepSimChannels(const std::vector> &simchannels, const geo::WireReadoutGeom &wireReadout); - std::map>> PrepTrueHits(const std::vector> &allHits, - const detinfo::DetectorClocksData &clockData, const cheat::BackTrackerService &backtracker); std::map SetupIDHitEnergyMap(const std::vector> &allHits, const detinfo::DetectorClocksData &clockData, const cheat::BackTrackerService &backtracker); diff --git a/sbncode/CAFMaker/RecoUtils/CMakeLists.txt b/sbncode/CAFMaker/RecoUtils/CMakeLists.txt index a992d7c63..6bb5ad1fc 100644 --- a/sbncode/CAFMaker/RecoUtils/CMakeLists.txt +++ b/sbncode/CAFMaker/RecoUtils/CMakeLists.txt @@ -2,6 +2,7 @@ art_make_library( LIBRARY_NAME caf_RecoUtils SOURCE RecoUtils.cc LIBRARIES + sbnanaobj::StandardRecord art::Framework_Core art::Framework_Services_Registry art::Framework_Principal @@ -11,4 +12,6 @@ art_make_library( LIBRARY_NAME caf_RecoUtils larsim::MCCheater_BackTrackerService_service larsim::MCCheater_ParticleInventoryService_service larcorealg::Geometry + sbnanaobj::StandardRecord + sbnanaobj::StandardRecordFlat ) diff --git a/sbncode/CAFMaker/RecoUtils/RecoUtils.cc b/sbncode/CAFMaker/RecoUtils/RecoUtils.cc index d8e9f210f..0c70d25df 100644 --- a/sbncode/CAFMaker/RecoUtils/RecoUtils.cc +++ b/sbncode/CAFMaker/RecoUtils/RecoUtils.cc @@ -107,3 +107,160 @@ int CAFRecoUtils::GetShowerPrimary(const int g4ID) return primary_id; } + +std::map> sbn::PrepSimChannels(const std::vector> &simchannels, const geo::WireReadoutGeom &wireReadout) { + std::map> ret; + + for (const art::Ptr sc : simchannels) { + // Lookup the wire of this channel + raw::ChannelID_t channel = sc->Channel(); + std::vector maybewire = wireReadout.ChannelToWire(channel); + geo::WireID thisWire; // Default constructor makes invalid wire + if (maybewire.size()) thisWire = maybewire[0]; + + for (const auto &item : sc->TDCIDEMap()) { + for (const sim::IDE &ide: item.second) { + // indexing initializes empty vector + ret[abs(ide.trackID)].push_back({thisWire, item.first, &ide}); + } + } + } + return ret; +} + +std::map>> sbn::PrepTrueHits(const std::vector> &allHits, + const detinfo::DetectorClocksData &clockData, const cheat::BackTrackerService &backtracker) { + std::map>> ret; + for (const art::Ptr h: allHits) { + for (int ID: backtracker.HitToTrackIds(clockData, *h)) { + ret[abs(ID)].push_back(h); + } + } + return ret; +} + +caf::Wall_t sbn::GetWallCross(const geo::BoxBoundedGeo &volume, const TVector3 p0, const TVector3 p1) { + TVector3 direction = (p1 - p0) * ( 1. / (p1 - p0).Mag()); + std::vector intersections = volume.GetIntersections(p0, direction); + + /* + * There are either two, or one, or zero intersection points. + * As per larcorealg/Geometry/BoxBoundedGeo.h documentation: + * If the return std::vector is empty the trajectory does not intersect with the box. + * Normally the return value should have one (if the trajectory originates in the box) or two (else) entries. + * If the return value has two entries the first represents the entry point and the second the exit point + */ + + if( intersections.empty() ) return caf::kWallNone; + + // get the intersection point closer to p0 + TVector3 closestIntersection; + double minDistance2 = std::numeric_limits::max(); + + for( TVector3 const & point : intersections ) { + const double d2 = (point - p0).Mag2(); + if( d2 > minDistance2 ) continue; + minDistance2 = d2; + closestIntersection = point; + } + + double eps = 1e-3; + if (abs(closestIntersection.X() - volume.MinX()) < eps) { + return caf::kWallLeft; + } + else if (abs(closestIntersection.X() - volume.MaxX()) < eps) { + return caf::kWallRight; + } + else if (abs(closestIntersection.Y() - volume.MinY()) < eps) { + return caf::kWallBottom; + } + else if (abs(closestIntersection.Y() - volume.MaxY()) < eps) { + return caf::kWallTop; + } + else if (abs(closestIntersection.Z() - volume.MinZ()) < eps) { + return caf::kWallFront; + } + else if (abs(closestIntersection.Z() - volume.MaxZ()) < eps) { + return caf::kWallBack; + } + else assert(false); + + return caf::kWallNone; +}//GetWallCross + +//------------------------------------------ +caf::g4_process_ sbn::GetG4ProcessID(const std::string &process_name) { +#define MATCH_PROCESS(name) if (process_name == #name) {return caf::kG4 ## name;} +#define MATCH_PROCESS_NAMED(strname, id) if (process_name == #strname) {return caf::kG4 ## id;} + MATCH_PROCESS(primary) + MATCH_PROCESS(CoupledTransportation) + MATCH_PROCESS(FastScintillation) + MATCH_PROCESS(Decay) + MATCH_PROCESS(anti_neutronInelastic) + MATCH_PROCESS(neutronInelastic) + MATCH_PROCESS(anti_protonInelastic) + MATCH_PROCESS(protonInelastic) + MATCH_PROCESS(hadInelastic) + MATCH_PROCESS_NAMED(kaon+Inelastic, kaonpInelastic) + MATCH_PROCESS_NAMED(kaon-Inelastic, kaonmInelastic) + MATCH_PROCESS_NAMED(kaon+Inelastic, kaonpInelastic) + MATCH_PROCESS_NAMED(kaon-Inelastic, kaonmInelastic) + MATCH_PROCESS_NAMED(sigma+Inelastic, sigmapInelastic) + MATCH_PROCESS_NAMED(sigma-Inelastic, sigmamInelastic) + MATCH_PROCESS_NAMED(pi+Inelastic, pipInelastic) + MATCH_PROCESS_NAMED(pi-Inelastic, pimInelastic) + MATCH_PROCESS_NAMED(xi+Inelastic, xipInelastic) + MATCH_PROCESS_NAMED(xi-Inelastic, ximInelastic) + MATCH_PROCESS(kaon0LInelastic) + MATCH_PROCESS(kaon0SInelastic) + MATCH_PROCESS(lambdaInelastic) + MATCH_PROCESS_NAMED(anti-lambdaInelastic, anti_lambdaInelastic) + MATCH_PROCESS(He3Inelastic) + MATCH_PROCESS(ionInelastic) + MATCH_PROCESS(xi0Inelastic) + MATCH_PROCESS(alphaInelastic) + MATCH_PROCESS(tInelastic) + MATCH_PROCESS(dInelastic) + MATCH_PROCESS(anti_neutronElastic) + MATCH_PROCESS(neutronElastic) + MATCH_PROCESS(anti_protonElastic) + MATCH_PROCESS(protonElastic) + MATCH_PROCESS(hadElastic) + MATCH_PROCESS_NAMED(kaon+Elastic, kaonpElastic) + MATCH_PROCESS_NAMED(kaon-Elastic, kaonmElastic) + MATCH_PROCESS_NAMED(pi+Elastic, pipElastic) + MATCH_PROCESS_NAMED(pi-Elastic, pimElastic) + MATCH_PROCESS(conv) + MATCH_PROCESS(phot) + MATCH_PROCESS(annihil) + MATCH_PROCESS(nCapture) + MATCH_PROCESS(nKiller) + MATCH_PROCESS(muMinusCaptureAtRest) + MATCH_PROCESS(muIoni) + MATCH_PROCESS(eBrem) + MATCH_PROCESS(CoulombScat) + MATCH_PROCESS(hBertiniCaptureAtRest) + MATCH_PROCESS(hFritiofCaptureAtRest) + MATCH_PROCESS(photonNuclear) + MATCH_PROCESS(muonNuclear) + MATCH_PROCESS(electronNuclear) + MATCH_PROCESS(positronNuclear) + MATCH_PROCESS(compt) + MATCH_PROCESS(eIoni) + MATCH_PROCESS(muBrems) + MATCH_PROCESS(hIoni) + MATCH_PROCESS(ionIoni) + MATCH_PROCESS(hBrems) + MATCH_PROCESS(muPairProd) + MATCH_PROCESS(hPairProd) + MATCH_PROCESS(LArVoxelReadoutScoringProcess) + MATCH_PROCESS(Transportation) + MATCH_PROCESS(msc) + MATCH_PROCESS(StepLimiter) + MATCH_PROCESS(RadioactiveDecayBase) + std::cerr << "Error: Process name with no match (" << process_name << ")\n"; + assert(false); + return caf::kG4UNKNOWN; // unreachable in debug mode +#undef MATCH_PROCESS +#undef MATCH_PROCESS_NAMED +}//GetG4ProcessID diff --git a/sbncode/CAFMaker/RecoUtils/RecoUtils.h b/sbncode/CAFMaker/RecoUtils/RecoUtils.h index a99121d8e..0b43760a4 100644 --- a/sbncode/CAFMaker/RecoUtils/RecoUtils.h +++ b/sbncode/CAFMaker/RecoUtils/RecoUtils.h @@ -24,11 +24,13 @@ #include "lardataobj/RecoBase/Hit.h" #include "lardataobj/RecoBase/Track.h" #include "lardataobj/RecoBase/Shower.h" +#include "sbnanaobj/StandardRecord/SREnums.h" //#include "lardataobj/AnalysisBase/MVAPIDResult.h" //#include "lardataobj/AnalysisBase/ParticleID.h" #include "larsim/MCCheater/BackTrackerService.h" #include "larsim/MCCheater/ParticleInventoryService.h" #include "larcore/Geometry/Geometry.h" +#include "larcorealg/Geometry/WireReadoutGeom.h" // c++ #include @@ -37,6 +39,25 @@ // ROOT #include "TTree.h" +namespace sim { + class IDE; +} + +namespace sbn { + struct ReadoutIDE { + geo::WireID wire; ///< Wire on a given plane closest to the drift path of the charge. + unsigned short tick = 0; ///< Time tick at which the charge passes closest to the wire. + const sim::IDE *ide = nullptr; ///< Deposited charge information. + }; + + // Helpers + std::map> PrepSimChannels(const std::vector> &simchannels, const geo::WireReadoutGeom &wireReadout); + std::map>> PrepTrueHits(const std::vector> &allHits, + const detinfo::DetectorClocksData &clockData, const cheat::BackTrackerService &backtracker); + caf::Wall_t GetWallCross( const geo::BoxBoundedGeo &volume, const TVector3 p0, const TVector3 p1); + caf::g4_process_ GetG4ProcessID(const std::string &name); +} + namespace CAFRecoUtils{ std::vector> AllTrueParticleIDEnergyMatches(const detinfo::DetectorClocksData &clockData, const std::vector >& hits, bool rollup_unsaved_ids=1); @@ -47,4 +68,7 @@ namespace CAFRecoUtils{ int GetShowerPrimary(const int g4ID); } + + + #endif diff --git a/sbncode/CAFMaker/SRDefaults.cxx b/sbncode/CAFMaker/SRDefaults.cxx new file mode 100644 index 000000000..e97f2ebf5 --- /dev/null +++ b/sbncode/CAFMaker/SRDefaults.cxx @@ -0,0 +1,27 @@ +/** + * @file sbncode/CAFMaker/SRDefaults.cxx + * @brief Discovery of default values in Standard Record classes. + * @author Gianluca Petrillo (petrillo@slac.stanford.edu) + * @date May 6, 2026 + * @see sbncode/CAFMaker/SRDefaults.h + */ + +#include "sbncode/CAFMaker/SRDefaults.h" + + +// ----------------------------------------------------------------------------- +namespace { + + template + std::tuple staticInit + (std::tuple objs) + { (std::get(objs).setDefault(), ...); return objs; } + +} // local namespace + + +// ----------------------------------------------------------------------------- +caf::SRDefaults::SupportedTypes const caf::SRDefaults::defaultObjs + = staticInit(caf::SRDefaults::SupportedTypes{}); + +// ----------------------------------------------------------------------------- diff --git a/sbncode/CAFMaker/SRDefaults.h b/sbncode/CAFMaker/SRDefaults.h new file mode 100644 index 000000000..a198f01c5 --- /dev/null +++ b/sbncode/CAFMaker/SRDefaults.h @@ -0,0 +1,103 @@ +/** + * @file sbncode/CAFMaker/SRDefaults.h + * @brief Discovery of default values in Standard Record classes. + * @author Gianluca Petrillo (petrillo@slac.stanford.edu) + * @date May 6, 2026 + * @see sbncode/CAFMaker/SRDefaults.cxx + */ + + +#ifndef SBNCODE_CAFMAKER_SRDEFAULTS_H +#define SBNCODE_CAFMAKER_SRDEFAULTS_H + +// Standard Record objects +#include "sbnanaobj/StandardRecord/SRFlashMatch.h" +#include "sbnanaobj/StandardRecord/SRTPCPMTBarycenterMatch.h" + +// C/C++ standard library +#include +#include // std::decay_t + +// ----------------------------------------------------------------------------- +namespace caf { class SRDefaults; } +/** + * @brief Collection of Standard Record objects with defaulted values. + * + * Example of usage: to test if `caf::SRFlashMatch::time` is still the default + * value: + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp} + * if (match.time == caf::SRDefaults::For().time) { ... } + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * Note that this may be, and usually is, different from the + * default-constructed value (e.g. `caf::SRFlashMatch{}.time`). + * + * + * Extension directions + * --------------------- + * + * Only classes that offer `setDefault()` member function can be included in + * this class. To add such a class: + * 1. include the appropriate header above; + * 2. add the name of the class as a `tuple` type in the definition of + * `SupportedTypes`. + * + * In case a class is used that is not supported, a static error on `std::get()` + * is typically emitted. In GCC 12.1.0: + * ``` + * static assertion failed: the type T in std::get must occur exactly once in the tuple + * ``` + * + */ +class caf::SRDefaults { + + public: + + /// A tuple with all the types included in this object. + using SupportedTypes = std::tuple< + caf::SRFlashMatch, + caf::SRTPCPMTBarycenterMatch + >; + + /// Single copy of defaulted objects. + static SupportedTypes const defaultObjs; + + + /** + * @brief Returns an object of type `SRobj` with its default values set. + * @tparam SRobj type of the Standard Record object to obtain + * @return a static object of type `SRobj` with its default values set + * + * A call of this method must explicitly include the template type `SRobj`, + * e.g. `caf::SRDefaults::For()`. + */ + template + static SRobj const& For(); + + /** + * @brief Returns an object of type `SRobj` with its default values set. + * @tparam SRobj type of the Standard Record object to obtain + * @return a static object of type `SRobj` with its default values set + * + * A call to this method should have as argument an existing object of the + * `SRobj` type, that can be used to omit the explicit type in the call; + * e.g., `caf::SRDefaults::For(track)`. + */ + template + static SRobj const& For(SRobj const&); + +}; + + +// ----------------------------------------------------------------------------- +// --- template implementation +// ----------------------------------------------------------------------------- +template +SRobj const& caf::SRDefaults::For() { return std::get(defaultObjs); } + +template +SRobj const& caf::SRDefaults::For(SRobj const&) + { return For>(); } + +// ----------------------------------------------------------------------------- + +#endif // SBNCODE_CAFMAKER_SRDEFAULTS_H diff --git a/sbncode/CAFMaker/TimeRefShifters.h b/sbncode/CAFMaker/TimeRefShifters.h new file mode 100644 index 000000000..7317abf81 --- /dev/null +++ b/sbncode/CAFMaker/TimeRefShifters.h @@ -0,0 +1,161 @@ +/** + * @file sbncode/CAFMaker/TimeRefShifters.h + * @brief Simple utilities to apply a reference time shift. + * @author Gianluca Petrillo (petrillo@slac.stanford.edu) + * @date July 21, 2026 + * + * The purpose of these utilities is to ensure that the time reference shifts + * are applied in a consistent way. + */ + +#ifndef SBNCODE_CAFMAKER_TIMEREFSHIFTERS_H +#define SBNCODE_CAFMAKER_TIMEREFSHIFTERS_H + + +// C++ standard libraries +#include // std::int64_t + + +// ----------------------------------------------------------------------------- +namespace caf { + template struct TimeRefShifter; + struct CRTtimeRefShifter; +} +/** + * @brief Applies a standard time reference change to a time. + * @tparam RefTime type used to internally store the reference time shift + * + * The shifter object is constructed with a time shift value: the reference time + * is delayed in time by that value. As a consequence, all time points will + * acquire an "earlier" value. + * + * Example: + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp} + * double T1 = 0.5, T2 = 1.0; // [us] + * int T3 = 50; // [ns] + * caf::TimeRefShifter const shifter{ 0.235 }; + * double shiftedT1 = shifter.shiftedTime(T1); + * shifter.shift(T2); + * int shiftedT3 = shifter.shiftedTimeNS(T3); + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * will shift the time reference forward by 235 ns; the value of all shifted + * times will become 235 ns smaller (`0.265` for `shiftedT1`, `0.765` for `T2`, + * `-185` for `T3`; `T1` was not shifted). + * + */ +template +struct caf::TimeRefShifter { + + /// Forward shift of the reference time [us] + RefTime const refTimeShift = RefTime{ 0 }; + /// Returns a time with reference shifted compared to `t` [us] + + /// Returns `t` [us], shifted. + template + constexpr T shiftedTime(T t) const { return t - shiftAs(); } + + /// Returns `t` [us], shifted only if `doShift` is `true`. + template + constexpr T shiftedTimeIf(T t, bool doShift) const + { return doShift? shiftedTime(t): t; } + + /// Returns `t` [us], shifted only if it's not exactly equal to `defVal`. + template + constexpr T shiftedTimeIfNot(T t, T defVal) const + { return shiftedTimeIf(t, t != defVal); } + + /// Returns a time with reference shifted compared to `t` [ns] + template + constexpr T shiftedTimeNS(T t) const { return t - shiftNSas(); } + + /// Changes the value of `t` shifting its reference [us] + template + T& shift(T& t) const { return t -= shiftAs(); } + + /// Changes the value of `t` shifting its reference [ns] + template + T& shiftNS(T& t) const { return t -= shiftNSas(); } + + + private: + + template + constexpr T shiftAs() const { return static_cast(refTimeShift); } + template + constexpr T shiftNSas() const { return static_cast(refTimeShift * 1000); } + +}; // caf::TimeRefShifter + +// template deduction guide: if presented with an argument T... +namespace caf { template TimeRefShifter(T) -> TimeRefShifter; } + +// ----------------------------------------------------------------------------- +/** + * @brief Applies time reference shifts to CRT times and timestamps. + * + * This shifter works similarly to `caf::TimeRefShifter` but it handles relative + * times and absolute timestamps independent shifts. + * + * This object largely adopts the terminology from `sbn::crt::CRTHit`, calling + * time stamps with "TS0" and relative times as "TS1". + * In addition, it supports a "merged" time which is either from TS0 or TS1 + * depending on a fixes parameter. + * + * Note that the convention of `sbn::crt::CRTHit` are: + * * timestamps are expressed as signed 64-bit integers; + * * relative times are expressed as `double`. + * + * They are both expressed in nanoseconds. However, across the CRT-related data + * products conventions do change. + * + * The output follows CAF conventions instead, so it is always in `double` + * precision and in microseconds. That may not play well with actual + * 64-bit timestamps (TS0-style), which may lose precision. + */ +struct caf::CRTtimeRefShifter { + + bool const useTS0 = false; ///< Use TS0 for time? + std::int64_t const refTimeShiftTS = 0; ///< Reference shift for timestamps [ns] + double const refTimeShift = 0.0; ///< Reference shifts for relative times [us] + + + constexpr bool usingTS0() const { return useTS0; } + + /// Returns the timestamp `ts_ns` with shifted reference [us] + constexpr double shiftTimestamp(std::int64_t ts_ns) const + { return (ts_ns - refTimeShiftTS) / 1000.; } + + /// Returns the time `t` [ns] with shifted reference [us] + constexpr double shiftTime(double t_ns) const + { return t_ns / 1000. - refTimeShift; } + + /// Definitions here follow `sbn::crt::CRTHit` (i.e. TS1 is not really a timestamp) + template + void fillCRTtimes( + std::int64_t TS0, std::int64_t TS1, + T* t0 = nullptr, T* t1 = nullptr, T* time = nullptr + ) const + { + double const shT0 = shiftTimestamp(TS0); + double const shT1 = shiftTime(static_cast(TS1)); + if (t0) *t0 = shT0; + if (t1) *t1 = shT1; + if (time) *time = useTS0? shT0: shT1; + } + + + /// Creates a timestamp (of type `TS`) from a second and a nanosecond part. + template + static constexpr TS makeTS(S s, NS ns) + { + return static_cast + (static_cast(s) * 1'000'000'000 + static_cast(ns)); + } + +}; // caf::CRTtimeRefShifter + + +// ----------------------------------------------------------------------------- + + +#endif // SBNCODE_CAFMAKER_TIMEREFSHIFTERS_H diff --git a/sbncode/CMakeLists.txt b/sbncode/CMakeLists.txt index daed2590a..9e8affaa6 100644 --- a/sbncode/CMakeLists.txt +++ b/sbncode/CMakeLists.txt @@ -20,7 +20,9 @@ add_subdirectory(GeometryTools) add_subdirectory(CosmicID) add_subdirectory(DetSim) add_subdirectory(Cluster3D) +add_subdirectory(WireMod) add_subdirectory(HitFinder) +add_subdirectory(Utilities) # Supera # diff --git a/sbncode/Calibration/CMakeLists.txt b/sbncode/Calibration/CMakeLists.txt index 98059edc5..a101a3069 100644 --- a/sbncode/Calibration/CMakeLists.txt +++ b/sbncode/Calibration/CMakeLists.txt @@ -25,6 +25,7 @@ cet_build_plugin( TrackCaloSkimmer art::module sbncode_CAFMaker sbnobj::Common_Calibration_dict larevt::SpaceCharge + caf_RecoUtils ) cet_build_plugin(TrackCaloSkimmerSelectStoppingTrack art::tool @@ -70,6 +71,16 @@ cet_build_plugin(TrackCaloSkimmerSelectAllTrack art::tool ROOT::X3d ) +cet_build_plugin(TrackCaloSkimmerSelectClearCosmicMuon art::tool + LIBRARIES + sbnobj::Common_Calibration_dict + art::Persistency_Common + art::Utilities canvas::canvas + larcore::Geometry_Geometry_service + cetlib::cetlib cetlib_except::cetlib_except + ROOT::X3d +) + cet_build_plugin ( DataSelect art::module LIBRARIES ifdh_art::IFDH_service diff --git a/sbncode/Calibration/TrackCaloSkimmer.h b/sbncode/Calibration/TrackCaloSkimmer.h index f9fb1930e..2cbaf2c7a 100644 --- a/sbncode/Calibration/TrackCaloSkimmer.h +++ b/sbncode/Calibration/TrackCaloSkimmer.h @@ -51,8 +51,8 @@ #include "larevt/SpaceCharge/SpaceCharge.h" #include "larevt/SpaceChargeServices/SpaceChargeService.h" -#include "lardataalg/DetectorInfo/DetectorPropertiesStandard.h" #include "lardata/DetectorInfoServices/DetectorPropertiesService.h" +#include "lardataalg/DetectorInfo/DetectorPropertiesData.h" #include "lardata/DetectorInfoServices/DetectorClocksService.h" #include "larcorealg/Geometry/fwd.h" @@ -63,9 +63,14 @@ #include "sbnobj/Common/Calibration/TrackCaloSkimmerObj.h" #include "sbnobj/Common/CRT/CRTHitT0TaggingInfo.hh" #include "sbnobj/Common/CRT/CRTHitT0TaggingTruthInfo.hh" +#include "sbnobj/SBND/CRT/CRTTrack.hh" +#include "sbnobj/SBND/CRT/CRTSpacePoint.hh" #include "ITCSSelectionTool.h" +// Useful functions +#include "sbncode/CAFMaker/RecoUtils/RecoUtils.h" // sbn::ReadoutIDE, sbn::PrepTrueHits()... + namespace sbn { class TrackCaloSkimmer; enum EDet {kNOTDEFINED, kSBND, kICARUS}; @@ -98,9 +103,12 @@ class sbn::TrackCaloSkimmer : public art::EDAnalyzer { double t0Pandora; double t0CRTTrack; double t0CRTHit; + double t0CRTSpacePoint; bool hasT0Pandora; bool hasT0CRTTrack; bool hasT0CRTHit; + bool hasT0CRTSpacePoint; + double crtMatchingScore; }; // Internal data struct @@ -112,7 +120,6 @@ class sbn::TrackCaloSkimmer : public art::EDAnalyzer { int ID; }; - // Represents a "Snippet" of ADCs shared by a set of hits on a wire struct Snippet { geo::WireID wire; @@ -161,7 +168,7 @@ class sbn::TrackCaloSkimmer : public art::EDAnalyzer { const std::vector> &mcparticles, const std::vector &active_volumes, const std::vector> &tpc_volumes, - const std::map>> id_to_ide_map, + const std::map> id_to_ide_map, const std::map>> id_to_truehit_map, const detinfo::DetectorPropertiesData &dprop, const geo::GeometryCore *geo, @@ -178,7 +185,8 @@ class sbn::TrackCaloSkimmer : public art::EDAnalyzer { const geo::WireReadoutGeom *wireReadout, const detinfo::DetectorClocksData &dclock, const cheat::BackTrackerService *bt_serv, - const detinfo::DetectorPropertiesData &dprop); + const detinfo::DetectorPropertiesData &dprop, + const float &xshift); void FillTrackCRTHitInfo(const std::vector> &tag); // helpers @@ -192,6 +200,7 @@ class sbn::TrackCaloSkimmer : public art::EDAnalyzer { art::InputTag fPFPT0producer; art::InputTag fCRTTrackT0producer; art::InputTag fCRTHitT0producer; + art::InputTag fCRTSpacePointT0producer; art::InputTag fCALOproducer; art::InputTag fTRKproducer; art::InputTag fTRKHMproducer; diff --git a/sbncode/Calibration/TrackCaloSkimmerSelectClearCosmicMuon_tool.cc b/sbncode/Calibration/TrackCaloSkimmerSelectClearCosmicMuon_tool.cc new file mode 100644 index 000000000..9346e953f --- /dev/null +++ b/sbncode/Calibration/TrackCaloSkimmerSelectClearCosmicMuon_tool.cc @@ -0,0 +1,30 @@ +// Framework Includes +#include "art/Utilities/ToolMacros.h" +#include "fhiclcpp/ParameterSet.h" + +#include "ITCSSelectionTool.h" + +namespace sbn { + +class TrackCaloSkimmerSelectClearCosmicMuon: public ITCSSelectionTool { +public: + + TrackCaloSkimmerSelectClearCosmicMuon(const fhicl::ParameterSet &p); + ~TrackCaloSkimmerSelectClearCosmicMuon() {} + + bool Select(const TrackInfo &t) override; + +private: +}; + +TrackCaloSkimmerSelectClearCosmicMuon::TrackCaloSkimmerSelectClearCosmicMuon(const fhicl::ParameterSet &p): + ITCSSelectionTool(p) +{} + +bool TrackCaloSkimmerSelectClearCosmicMuon::Select(const TrackInfo &t) { + return t.clear_cosmic_muon; +} + +DEFINE_ART_CLASS_TOOL(TrackCaloSkimmerSelectClearCosmicMuon) + +} // end namespace sbn diff --git a/sbncode/Calibration/TrackCaloSkimmer_module.cc b/sbncode/Calibration/TrackCaloSkimmer_module.cc index 047f2d019..39d05cc2b 100644 --- a/sbncode/Calibration/TrackCaloSkimmer_module.cc +++ b/sbncode/Calibration/TrackCaloSkimmer_module.cc @@ -12,13 +12,10 @@ #include "TrackCaloSkimmer.h" #include "sbnobj/SBND/CRT/CRTTrack.hh" +#include "sbnanaobj/StandardRecord/SREnums.h" #include "art/Utilities/make_tool.h" -// Useful functions -#include "sbncode/CAFMaker/FillTrue.h" -#include "sbncode/CAFMaker/RecoUtils/RecoUtils.h" - #include "larcore/Geometry/WireReadout.h" #include "larcore/Geometry/Geometry.h" #include "larcore/CoreUtils/ServiceUtil.h" @@ -70,6 +67,7 @@ sbn::TrackCaloSkimmer::TrackCaloSkimmer(fhicl::ParameterSet const& p) fPFPproducer = p.get< art::InputTag > ("PFPproducer","pandoraGausCryo0"); fPFPT0producer = p.get< art::InputTag > ("PFPT0producer", "pandoraGausCryo0"); fCRTTrackT0producer = p.get< art::InputTag >("CRTTrackT0producer", "crttrackmatching"); + fCRTSpacePointT0producer = p.get< art::InputTag >("CRTSpacePointT0producer", "crtspacepointmatching"); fCRTHitT0producer = p.get< art::InputTag >("CRTHitT0producer", "CRTT0Tagging"); fCALOproducer = p.get< art::InputTag > ("CALOproducer"); @@ -231,6 +229,8 @@ void sbn::TrackCaloSkimmer::analyze(art::Event const& e) // // Tracks (SBND style) art::FindManyP fmT0CRTTrack(tracks, e, fCRTTrackT0producer); + // SpacePoints (SBND style) + art::FindManyP fmT0CRTSpacePoint(tracks, e, fCRTSpacePointT0producer); // Hits (ICARUS style) art::FindManyP fmT0CRTHit(tracks, e, fCRTHitT0producer); @@ -288,15 +288,15 @@ void sbn::TrackCaloSkimmer::analyze(art::Event const& e) // Prep truth-to-reco-matching info // - // Use helper functions from CAFMaker/FillTrue - std::map>> id_to_ide_map; + // Use helper functions from CAFMaker/RecoUtils + std::map> id_to_ide_map; std::map>> id_to_truehit_map; const cheat::BackTrackerService *bt = NULL; if (simchannels.size()) { art::ServiceHandle bt_serv; - id_to_ide_map = caf::PrepSimChannels(simchannels, *wireReadout); - id_to_truehit_map = caf::PrepTrueHits(allHits, clock_data, *bt_serv.get()); + id_to_ide_map = sbn::PrepSimChannels(simchannels, *wireReadout); + id_to_truehit_map = sbn::PrepTrueHits(allHits, clock_data, *bt_serv.get()); bt = bt_serv.get(); } @@ -313,6 +313,9 @@ void sbn::TrackCaloSkimmer::analyze(art::Event const& e) for (art::Ptr p_pfp: PFParticleList) { const recob::PFParticle &pfp = *p_pfp; + if(p_pfp->PdgCode() == 11) + continue; + const std::vector> thisTrack = fmTracks.at(p_pfp.key()); if (thisTrack.size() != 1) continue; @@ -345,8 +348,9 @@ void sbn::TrackCaloSkimmer::analyze(art::Event const& e) } // Collect T0s - bool hasT0 = false, hasPFPT0 = false, hasCRTTrackT0 = false, hasCRTHitT0 = false; + bool hasT0 = false, hasPFPT0 = false, hasCRTTrackT0 = false, hasCRTHitT0 = false, hasCRTSpacePointT0 = false; int whicht0 = -1; + float crtMatchingScore = std::numeric_limits::signaling_NaN(); double t0PFP = std::numeric_limits::signaling_NaN(); if (fmT0PFP.isValid() && fmT0PFP.at(p_pfp.key()).size()) { @@ -356,12 +360,23 @@ void sbn::TrackCaloSkimmer::analyze(art::Event const& e) } double t0CRTTrack = std::numeric_limits::signaling_NaN(); + double t0CRTTrackScore = std::numeric_limits::signaling_NaN(); if (fmT0CRTTrack.isValid() && fmT0CRTTrack.at(trkPtr.key()).size()) { t0CRTTrack = fmT0CRTTrack.data(trkPtr.key()).at(0)->Time(); + t0CRTTrackScore = fmT0CRTTrack.data(trkPtr.key()).at(0)->TriggerConfidence(); hasCRTTrackT0 = true; } + double t0CRTSpacePoint = std::numeric_limits::signaling_NaN(); + double t0CRTSpacePointScore = std::numeric_limits::signaling_NaN(); + if (fmT0CRTSpacePoint.isValid() && fmT0CRTSpacePoint.at(trkPtr.key()).size()) { + t0CRTSpacePoint = fmT0CRTSpacePoint.data(trkPtr.key()).at(0)->Time(); + t0CRTSpacePointScore = fmT0CRTSpacePoint.data(trkPtr.key()).at(0)->TriggerConfidence(); + hasCRTSpacePointT0 = true; + } + double t0CRTHit = std::numeric_limits::signaling_NaN(); + double t0CRTHitScore = std::numeric_limits::signaling_NaN(); if (fIncludeCRTHitTagging && fmT0CRTHit.isValid() && fmT0CRTHit.at(trkPtr.key()).size()) { const sbn::crt::CRTHitT0TaggingInfo &tag = *fmCRTHitT0TaggingInfo.at(trkPtr.key()).at(0); double time = fmT0CRTHit.at(trkPtr.key()).at(0)->Time(); @@ -384,23 +399,42 @@ void sbn::TrackCaloSkimmer::analyze(art::Event const& e) if (!crtHitSysRejected && !crtHitDistanceRejected) { t0CRTHit = time; + t0CRTHitScore = tag.Distance; hasCRTHitT0 = true; } } - T0TimingInfo thisTrackTimingInfo = {t0PFP, t0CRTTrack, t0CRTHit, hasPFPT0, hasCRTTrackT0, hasCRTHitT0}; - hasT0 = hasPFPT0 || hasCRTTrackT0 || hasCRTHitT0; + hasT0 = hasPFPT0 || hasCRTTrackT0 || hasCRTHitT0 || hasCRTSpacePointT0; // "whicht0" should reflect the T0 used for the reconstruction of the drift coordinate. - if(!hasT0) whicht0 = -1 ; + if(!hasT0) whicht0 = -1; // In this way, if a track is T0 tagged from PFP and CRT tagged, which T0 reflects the PFP Tag. - else if (hasPFPT0) whicht0 = 0 ; - else if (hasCRTTrackT0) whicht0 = 1 ; - else if (hasCRTHitT0) whicht0 = 2 ; + else if (hasPFPT0) whicht0 = 0; + else if (hasCRTTrackT0) + { + whicht0 = 1; + crtMatchingScore = t0CRTTrackScore; + } + else if (hasCRTHitT0) + { + whicht0 = 2; + crtMatchingScore = t0CRTHitScore; + } + else if (hasCRTSpacePointT0) + { + whicht0 = 3; + crtMatchingScore = t0CRTSpacePointScore; + } + + T0TimingInfo thisTrackTimingInfo = {t0PFP, t0CRTTrack, t0CRTHit, t0CRTSpacePoint, hasPFPT0, hasCRTTrackT0, hasCRTHitT0, hasCRTSpacePointT0, crtMatchingScore}; if (fRequireT0 && !hasT0) continue; - if (fVerbose) std::cout << "Processing new track! ID: " << trkPtr->ID() << " time: " << t0PFP << " timeCRTTrack: " << t0CRTTrack << " timeCRTHit "<ID() << "\n" + << "\tPandora time: " << t0PFP << "\n" + << "\tCRTTrack (SBND) time: " << t0CRTTrack << "\n" + << "\tCRTHit (ICARUS) time: " << t0CRTHit << "\n" + << "\tCRTSpacePoint (SBND) time: " << t0CRTSpacePoint << std::endl; // Reset the track object *fTrack = sbn::TrackInfo(); @@ -545,14 +579,14 @@ geo::Point_t WireToTrajectoryPosition(const geo::Point_t &loc, const geo::TPCID sbn::TrueParticle TrueParticleInfo(const simb::MCParticle &particle, const std::vector &active_volumes, const std::vector> &tpc_volumes, - const std::map>> &id_to_ide_map, + const std::map> &id_to_ide_map, const std::map>> &id_to_truehit_map, const detinfo::DetectorPropertiesData &dprop, const geo::GeometryCore *geo, const geo::WireReadoutGeom *wireReadout) { - std::vector> empty; - const std::vector> &particle_ides = id_to_ide_map.count(particle.TrackId()) ? id_to_ide_map.at(particle.TrackId()) : empty; + std::vector empty; + const std::vector &particle_ides = id_to_ide_map.count(particle.TrackId()) ? id_to_ide_map.at(particle.TrackId()) : empty; std::vector> emptyHits; const std::vector> &particle_hits = id_to_truehit_map.count(particle.TrackId()) ? id_to_truehit_map.at(particle.TrackId()) : emptyHits; @@ -569,9 +603,9 @@ sbn::TrueParticle TrueParticleInfo(const simb::MCParticle &particle, trueparticle.plane0nhit = 0; trueparticle.plane1nhit = 0; trueparticle.plane2nhit = 0; - for (auto const &ide_pair: particle_ides) { - const geo::WireID &w = ide_pair.first; - const sim::IDE *ide = ide_pair.second; + for (auto const &ide_p: particle_ides) { + const geo::WireID &w = ide_p.wire; + const sim::IDE *ide = ide_p.ide; if (w.Plane == 0) { trueparticle.plane0VisE += ide->energy / 1000. /* MeV -> GeV*/; @@ -622,7 +656,7 @@ sbn::TrueParticle TrueParticleInfo(const simb::MCParticle &particle, // get the wall if (entry_point > 0) { - trueparticle.wallin = (int)caf::GetWallCross(active_volumes.at(cryostat_index), particle.Position(entry_point).Vect(), particle.Position(entry_point-1).Vect()); + trueparticle.wallin = (int)sbn::GetWallCross(active_volumes.at(cryostat_index), particle.Position(entry_point).Vect(), particle.Position(entry_point-1).Vect()); } int exit_point = -1; @@ -690,7 +724,7 @@ sbn::TrueParticle TrueParticleInfo(const simb::MCParticle &particle, exit_point = particle.NumberTrajectoryPoints() - 1; } if (exit_point >= 0 && ((unsigned)exit_point) < particle.NumberTrajectoryPoints() - 1) { - trueparticle.wallout = (int)caf::GetWallCross(active_volumes.at(cryostat_index), particle.Position(exit_point).Vect(), particle.Position(exit_point+1).Vect()); + trueparticle.wallout = (int)sbn::GetWallCross(active_volumes.at(cryostat_index), particle.Position(exit_point).Vect(), particle.Position(exit_point+1).Vect()); } // other truth information @@ -711,8 +745,8 @@ sbn::TrueParticle TrueParticleInfo(const simb::MCParticle &particle, trueparticle.endp = ConvertTVector((exit_point >= 0) ? particle.Momentum(exit_point).Vect() : TVector3(-9999, -9999, -9999)); trueparticle.endE = (exit_point >= 0) ? particle.Momentum(exit_point).E() : -9999.; - trueparticle.start_process = (int)caf::GetG4ProcessID(particle.Process()); - trueparticle.end_process = (int)caf::GetG4ProcessID(particle.EndProcess()); + trueparticle.start_process = (int)sbn::GetG4ProcessID(particle.Process()); + trueparticle.end_process = (int)sbn::GetG4ProcessID(particle.EndProcess()); trueparticle.G4ID = particle.TrackId(); trueparticle.parent = particle.Mother(); @@ -720,10 +754,11 @@ sbn::TrueParticle TrueParticleInfo(const simb::MCParticle &particle, // Organize deposition info into per-wire true "Hits" -- key is the Channel Number std::map truehits; - for (auto const &ide_pair: particle_ides) { - const geo::WireID &w = ide_pair.first; + for (auto const &ide_part: particle_ides) { + const geo::WireID &w = ide_part.wire; unsigned c = wireReadout->PlaneWireToChannel(w); - const sim::IDE *ide = ide_pair.second; + const sim::IDE *ide = ide_part.ide; + unsigned short tick = ide_part.tick; // Set stuff truehits[c].cryo = w.Cryostat; @@ -739,6 +774,8 @@ sbn::TrueParticle TrueParticleInfo(const simb::MCParticle &particle, truehits[c].p.y = (truehits[c].p.y*old_elec + ide->y*ide->numElectrons) / new_elec; truehits[c].p.z = (truehits[c].p.z*old_elec + ide->z*ide->numElectrons) / new_elec; + truehits[c].time = (truehits[c].time*old_elec + tick*ide->numElectrons) / new_elec; + // Also get the position with space charge un-done geo::Point_t ide_p(ide->x, ide->y, ide->z); geo::Point_t ide_p_scecorr = WireToTrajectoryPosition(ide_p, w); @@ -754,10 +791,10 @@ sbn::TrueParticle TrueParticleInfo(const simb::MCParticle &particle, } // Compute widths - for (auto const &ide_pair: particle_ides) { - const geo::WireID &w = ide_pair.first; + for (auto const &ide_part: particle_ides) { + const geo::WireID &w = ide_part.wire; unsigned c = wireReadout->PlaneWireToChannel(w); - const sim::IDE *ide = ide_pair.second; + const sim::IDE *ide = ide_part.ide; geo::Point_t ide_p(ide->x, ide->y, ide->z); geo::Point_t ide_p_scecorr = WireToTrajectoryPosition(ide_p, w); @@ -782,8 +819,6 @@ sbn::TrueParticle TrueParticleInfo(const simb::MCParticle &particle, // Compute the time of each hit for (sbn::TrueHit &h: truehits_v) { - h.time = dprop.ConvertXToTicks(h.p.x, h.plane, h.tpc, h.cryo); - double xdrift = abs(h.p.x - wireReadout->Plane(geo::PlaneID(h.cryo, h.tpc, 0)).GetCenter().X()); h.tdrift = xdrift / dprop.DriftVelocity(); } @@ -1010,7 +1045,7 @@ void sbn::TrackCaloSkimmer::FillTrackTruth(const detinfo::DetectorClocksData &cl const std::vector> &mcparticles, const std::vector &active_volumes, const std::vector> &tpc_volumes, - const std::map>> id_to_ide_map, + const std::map> id_to_ide_map, const std::map>> id_to_truehit_map, const detinfo::DetectorPropertiesData &dprop, const geo::GeometryCore *geo, @@ -1124,6 +1159,8 @@ void sbn::TrackCaloSkimmer::FillTrack(const recob::Track &track, fTrack->t0PFP = t0Info.t0Pandora; fTrack->t0CRTTrack = t0Info.t0CRTTrack; fTrack->t0CRTHit = t0Info.t0CRTHit; + fTrack->t0CRTSpacePoint = t0Info.t0CRTSpacePoint; + fTrack->crtMatchingScore = t0Info.crtMatchingScore; fTrack->id = track.ID(); fTrack->clear_cosmic_muon = pfp.Parent() == recob::PFParticle::kPFParticlePrimary; @@ -1135,27 +1172,40 @@ void sbn::TrackCaloSkimmer::FillTrack(const recob::Track &track, fTrack->dir.x = track.StartDirection().X(); fTrack->dir.y = track.StartDirection().Y(); fTrack->dir.z = track.StartDirection().Z(); - //If track is only CRTt0 tagged, undo the assumed trigger correction - if (t0Info.hasT0Pandora) { - fTrack->start.x = track.Start().X(); - fTrack->end.x = track.End().X(); - } else if (t0Info.hasT0CRTTrack) { - const double driftv(dprop.DriftVelocity(dprop.Efield(), dprop.Temperature())); - // Comment from Francesco: I am not sure of the below formula. - // SBND has two TPCs with a common cathode like ICARUS, the driftvelocity - // returns the absolute value, but the displacement (basically the + below) - // depends on the TPC, in one case is + and in the other is negative. - // In this way the displacement is always in the same direction, working for - // one TPC, but not for the other. - fTrack->start.x = track.Start().X() + driftv*t0Info.t0CRTTrack*1e-3; - fTrack->end.x = track.End().X() + driftv*t0Info.t0CRTTrack*1e-3; - } else if (t0Info.hasT0CRTHit){ - // If the track does not have a a Pandora T0, the tracks will always be either on the left or (ex Or) right of the cathode. - int driftDir = geo->TPC(hits[0]->WireID()).DriftDir().X(); - const double driftv(dprop.DriftVelocity(dprop.Efield(), dprop.Temperature())); - fTrack->start.x = track.Start().X() + driftDir*driftv*t0Info.t0CRTHit*1e-3; - fTrack->end.x = track.End().X() + driftDir*driftv*t0Info.t0CRTHit*1e-3; - } + + if(t0Info.hasT0Pandora) + { + fTrack->start.x = track.Start().X(); + fTrack->end.x = track.End().X(); + } + else + { + // If CRT T0 tagged we can shift the x positions appropriately + int driftDir = geo->TPC(hits[0]->WireID()).DriftDir().X(); + const double driftv(dprop.DriftVelocity(dprop.Efield(), dprop.Temperature())); + + if(t0Info.hasT0CRTTrack) + { + const double xshift = driftDir*driftv*t0Info.t0CRTTrack*1e-3; + fTrack->start.x = track.Start().X() + xshift; + fTrack->end.x = track.End().X() + xshift; + fTrack->xShiftCRT = xshift; + } + else if(t0Info.hasT0CRTHit) + { + const double xshift = driftDir*driftv*t0Info.t0CRTHit*1e-3; + fTrack->start.x = track.Start().X() + xshift; + fTrack->end.x = track.End().X() + xshift; + fTrack->xShiftCRT = xshift; + } + else if(t0Info.hasT0CRTSpacePoint) + { + const double xshift = driftDir*driftv*t0Info.t0CRTSpacePoint*1e-3; + fTrack->start.x = track.Start().X() + xshift; + fTrack->end.x = track.End().X() + xshift; + fTrack->xShiftCRT = xshift; + } + } if (hits.size() > 0) { fTrack->cryostat = hits[0]->WireID().Cryostat; @@ -1163,7 +1213,7 @@ void sbn::TrackCaloSkimmer::FillTrack(const recob::Track &track, // Fill each hit for (unsigned i_hit = 0; i_hit < hits.size(); i_hit++) { - sbn::TrackHitInfo hinfo = MakeHit(*hits[i_hit], hits[i_hit].key(), *thms[i_hit], track, t0Info, sps[i_hit], calo, geo, wireReadout, clock_data, bt_serv, dprop); + sbn::TrackHitInfo hinfo = MakeHit(*hits[i_hit], hits[i_hit].key(), *thms[i_hit], track, t0Info, sps[i_hit], calo, geo, wireReadout, clock_data, bt_serv, dprop, fTrack->xShiftCRT); if (hinfo.h.plane == 0) { fTrack->hits0.push_back(hinfo); } @@ -1330,7 +1380,8 @@ sbn::TrackHitInfo sbn::TrackCaloSkimmer::MakeHit(const recob::Hit &hit, const geo::WireReadoutGeom *wireReadout, const detinfo::DetectorClocksData &dclock, const cheat::BackTrackerService *bt_serv, - const detinfo::DetectorPropertiesData &dprop) { + const detinfo::DetectorPropertiesData &dprop, + const float &xshift) { // TrackHitInfo to save sbn::TrackHitInfo hinfo; @@ -1405,13 +1456,29 @@ sbn::TrackHitInfo sbn::TrackCaloSkimmer::MakeHit(const recob::Hit &hit, } } - // This is needed to reconstrut drift coordinate using a different Time - int driftDir = geo->TPC(hit.WireID()).DriftDir().X(); - const double driftv(dprop.DriftVelocity(dprop.Efield(), dprop.Temperature())); - double anodeDistance = std::numeric_limits::signaling_NaN(); - if(t0Info.hasT0CRTHit) anodeDistance = (hit.PeakTime()-dclock.Time2Tick(dclock.TriggerTime())-t0Info.t0CRTHit*1e-3/dclock.TPCClock().TickPeriod())*dclock.TPCClock().TickPeriod()*driftv; - double wirePlaneX = wireReadout->Plane(geo::PlaneID(hit.WireID().Cryostat, hit.WireID().TPC, hit.WireID().Plane)).GetCenter().X(); - double recoX = wirePlaneX - driftDir*anodeDistance; + // If T0 provided CRT then we should appropriately shift the drift position (already done for pandora T0) + double recoX = std::numeric_limits::signaling_NaN(); + + if(!t0Info.hasT0Pandora && (t0Info.hasT0CRTTrack || t0Info.hasT0CRTHit || t0Info.hasT0CRTSpacePoint)) + { + int driftDir = geo->TPC(hit.WireID()).DriftDir().X(); + const double driftv(dprop.DriftVelocity(dprop.Efield(), dprop.Temperature())); + + double time = std::numeric_limits::signaling_NaN(); + + if(t0Info.hasT0CRTTrack) + time = t0Info.t0CRTTrack*1e-3; + else if(t0Info.hasT0CRTHit) + time = t0Info.t0CRTHit*1e-3; + else if(t0Info.hasT0CRTSpacePoint) + time = t0Info.t0CRTSpacePoint*1e-3; + + double anodeDistance = (hit.PeakTime()-dclock.Time2Tick(dclock.TriggerTime())-time/dclock.TPCClock().TickPeriod())*dclock.TPCClock().TickPeriod()*driftv; + double wirePlaneX = wireReadout->Plane(geo::PlaneID(hit.WireID().Cryostat, hit.WireID().TPC, hit.WireID().Plane)).GetCenter().X(); + + recoX = wirePlaneX - driftDir*anodeDistance; + } + // Information from the TrackHitMeta bool badhit = (thm.Index() == std::numeric_limits::max()) || (!trk.HasValidPoint(thm.Index())); @@ -1424,7 +1491,7 @@ sbn::TrackHitInfo sbn::TrackCaloSkimmer::MakeHit(const recob::Hit &hit, // The tp X coordinate is reconstructed only if it does not have a PandoraT0. hinfo.tp.x = loc.X(); - if(t0Info.hasT0CRTHit && !t0Info.hasT0Pandora && !isnan(hinfo.tp.x)) hinfo.tp.x = recoX; + if((t0Info.hasT0CRTTrack || t0Info.hasT0CRTHit || t0Info.hasT0CRTSpacePoint) && !t0Info.hasT0Pandora && !isnan(hinfo.tp.x)) hinfo.tp.x = recoX; hinfo.tp.y = loc.Y(); hinfo.tp.z = loc.Z(); @@ -1456,7 +1523,7 @@ sbn::TrackHitInfo sbn::TrackCaloSkimmer::MakeHit(const recob::Hit &hit, if (sp) { hinfo.h.sp.x = sp->position().x(); // If the track has a Pandora T0, do not displace, track is already reconstructed at the correct position - if(t0Info.hasT0CRTHit && !t0Info.hasT0Pandora) hinfo.h.sp.x = sp->position().x() + driftDir*driftv*t0Info.t0CRTHit*1e-3; + if((t0Info.hasT0CRTTrack || t0Info.hasT0CRTHit || t0Info.hasT0CRTSpacePoint) && !t0Info.hasT0Pandora) hinfo.h.sp.x = sp->position().x() + xshift; hinfo.h.sp.y = sp->position().y(); hinfo.h.sp.z = sp->position().z(); hinfo.h.hasSP = true; diff --git a/sbncode/Calibration/fcl/sbnd/sbnd_trackcalo_skimmer.fcl b/sbncode/Calibration/fcl/sbnd/sbnd_trackcalo_skimmer.fcl index 8735ed405..b510a231f 100644 --- a/sbncode/Calibration/fcl/sbnd/sbnd_trackcalo_skimmer.fcl +++ b/sbncode/Calibration/fcl/sbnd/sbnd_trackcalo_skimmer.fcl @@ -83,6 +83,6 @@ a2c_selection: { } caloskim_nodigits_goldentracks: @local::caloskim_nodigits -caloskim_nodigits_goldentracks.SelectionTools: [@local::stopping_selection, @local::a2c_selection, @local::throughgoing_selection] +caloskim_nodigits_goldentracks.SelectionTools: [ @local::stopping_selection, @local::a2c_selection, @local::throughgoing_selection ] END_PROLOG diff --git a/sbncode/DetSim/AdjustSimForTrigger_module.cc b/sbncode/DetSim/AdjustSimForTrigger_module.cc index 5f6c5eee9..ca625de30 100644 --- a/sbncode/DetSim/AdjustSimForTrigger_module.cc +++ b/sbncode/DetSim/AdjustSimForTrigger_module.cc @@ -1,11 +1,8 @@ -//////////////////////////////////////////////////////////////////////// -// Class: AdjustSimForTrigger -// Plugin Type: producer (Unknown Unknown) -// File: AdjustSimForTrigger_module.cc -// -// Generated at December 2023 by Bruce Howard (howard@fnal.gov) using -// cetskelgen. -//////////////////////////////////////////////////////////////////////// +/** + * @file sbncode/DetSim/AdjustSimForTrigger_module.cc + * @author Bruce Howard (howard@fnal.gov) + * @date December 2023 + */ #include "art/Framework/Core/EDProducer.h" #include "art/Framework/Core/ModuleMacros.h" @@ -13,6 +10,8 @@ #include "art/Framework/Principal/Handle.h" #include "art/Framework/Principal/Run.h" #include "art/Framework/Principal/SubRun.h" +#include "art/Persistency/Common/PtrMaker.h" +#include "canvas/Persistency/Common/Assns.h" #include "canvas/Utilities/InputTag.h" #include "fhiclcpp/ParameterSet.h" #include "messagefacility/MessageLogger/MessageLogger.h" @@ -24,12 +23,207 @@ #include "lardataobj/Simulation/SimEnergyDeposit.h" #include "lardataobj/Simulation/SimEnergyDepositLite.h" #include "lardataobj/Simulation/SimPhotons.h" +#include "sbnobj/ICARUS/PMT/Data/WaveformBaseline.h" +#include "sbnobj/Common/Trigger/ExtraTriggerInfo.h" -#include -#include - -class AdjustSimForTrigger; +#include "lardata/DetectorInfoServices/DetectorClocksService.h" +#include "sbncode/Utilities/AssnsUtils.h" // sbn::RebindAssociatedProducts() +#include // std::boolalpha +#include +#include +#include // std::move() +#include + + +/** + * @brief Applies a time shift to selected data products for simulation. + * + * This module produces a selection of simulation data products shifting their + * time reference to a new "event" (in the sense of something happening, not in + * the DAQ sense). + * + * The time point of the new reference event is specified in + * @ref DetectorClocksElectronicsTime "electronics time scale". + * Technically, this module applies a time shift so that the time of that + * reference event becomes the previous reference value in electronics time scale. + * + * More practically, we start from an electronics time scale where the reference + * time is set at `DetectorClocksData::TriggerTime()` (typically `1500.0` + * µs). + * That time reference may represent a hardware trigger, as the function name + * suggest, or anything else. At this point we introduce a new trigger time + * (from the simulation; e.g. `1502.0` µs) and we want that time to become + * the new reference (and "hardware trigger") of the electronics time scale. + * In this example, this module will add a shift of -2 µs to all the + * selected data products, so that the time of the new trigger is `1500.0` + * and all follows. + * + * @note The new trigger time is going to be whatever value is in + * `DetectorClocksData::TriggerTime()`, rather than a hard-coded value + * like `1500.0`. Therefore some care needs to be taken in the workflow + * to make sure that at the time of the execution of this module + * `DetectorClocksService` is yielding the desired new-reference value. + * Also, after this module is called `DetectorClocksService` itself may + * report obsolete values. Specifically, `DetectorClocksServiceStandard` + * will report the old `BeamGateTime()`, which was from either a + * configuration parameter or from a trigger data product. + * + * This module reads the new reference time from a `raw::Trigger` object + * (assumed to hold a time on the current electronics scale), and, unless + * configured not to (`DropTriggerProduct`) it produces a new trigger data + * product with the shifted trigger information. This data product is suitable + * for configuring services like `DetectorClocksServiceStandard` for the + * following stages of the workflow. + * + * If the reference time is not valid, the applied shift is `0`. + * A reference time is valid if it is neither the maximum nor the minimum value + * of a double (`std::numeric_limits::max()` and + * `std::numeric_limits::min()`). + * + * @note This module does not provide indication on whether a shift was + * performed or not. The only way to know is to check the reference time + * input used and verify whether it's considered valid by this module + * according to the logic described above. + * + * + * ### Optional shift + * + * It is possible to specify a fixed additional shift to the time reference. + * This may be useful for example if the simulation of the trigger yields + * exactly the time at which the triggering activity is detected, but the + * trigger hardware would take still some time in order to tag that time. + * Note, however, that the additional shift is also applied to the beam gate + * time. + * This additional time is specified via the `AdditionalOffset` configuration + * parameter. + * If the new reference time is invalid, no shift is applied and this offset is + * also ignored. + * + * + * ### Multiple shifts on the same event + * + * In general, using as input a sample that has already undergone a previous + * shift is surprisingly robust: as long as the input is all consistently + * shifted, offsets and delays that were already applied are not re-applied + * (this was explicitly verified on `sim::SimPhotons`). + * However, shifting is not "undone": if an input event was shifted and now the + * new proposed time reference is invalid, the event will be applied no + * additional shift, leaving it to the time reference after the first shift. + * + * The chances of the old time reference being completely invalidated depend on + * how the changes intervened between the first and the second shift. + * The module _could_ be adapted to attempt a shifting reversal, if provided + * with the necessary additional information on the first shift. + * + * + * Input + * ------ + * + * * `std::vector` (`InputTriggerLabel`): the first of the + * triggers in the collection will be used as a new reference. + * If the trigger time value is not valid, no shift at all will be performed. + * However, the beam gate time is still expected to be valid. + * An empty collection is not allowed, even when there is no valid trigger. + * + * * `art::Assns` + * (`BindWaveformBaselines`): the association of the original optical detector + * waveforms to their baselines. + * + * + * Output + * ------- + * + * For each enabled data product to be shifted, the corresponding shifted data + * product is produced, with elements in the same order as in the original + * collections. Normally, no associations are ported on. + * + * * `std::vector` (if `ShiftAuxDetIDEs` is set): + * all `sim::AuxDetIDE` entry and exit times are shifted. Order of channels + * and IDE is preserved. + * * `std::vector` (if `ShiftSimEnergyDeposits` is set): + * all `sim::SimEnergyDeposit` entry and exit times are shifted. + * Order of deposits is preserved. + * * `std::vector` (if `ShiftSimEnergyDepositLites` + * is set): all `sim::SimEnergyDepositLite` entry and exit times are shifted. + * Order of deposits is preserved. + * * `std::vector` (if `ShiftSimPhotons` is set): + * all `sim::SimPhotons` times in all channels are shifted. + * Order of channels and photons is preserved. + * * `std::vector` (unless `DropTriggerProduct` is set): + * a collection of shifted triggers is produced; the first one is the shifted + * version of the reference trigger, which can then be used as new trigger + * data product e.g. for `DetectorClocksServiceStandard`. If the reference + * trigger time is not valid, the trigger time will be overwritten: the + * trigger time will be set to the value from `DetectorClocksService`, and the + * beam gate time will be the same as the input trigger object. + * * `sbn::ExtraTriggerInfo` (if `SkipExtraTriggerInfo` is not set): the + * input data product is duplicated in output. The only time information in + * the object, absolute timestamps, is currently not shifted. + * * `std::vector` (if `ShiftBeamGateInfo` is set): + * the beam gate used by the event generator, shifted. Generator times and + * particles from the detector simulation (GEANT4) are not shifted, but pretty + * much everything else may be, including scintillation photons and energy + * depositions. Depending on which aspect of the simulation is being + * investigated, either the unshifted (input) or shifted (output of this + * module) gate needs to be used. + * * `std::vector` (if `ShiftWaveforms` is set): the input + * PMT waveforms, from which presumably the new trigger was extracted, are + * shifted by simply modifying their timestamps. + * * `art::Assns`: enabled only + * if the optical waveforms are being shifted _and_ an association data + * product name is specified (`BindWaveformBaselines`), it rebinds the + * original waveform baselines to the shifted waveforms. Note that neither the + * baseline nor the order nor the content of the waveforms change: only their + * start time (and implicitly the end time) does. + * + * + * Configuration parameters + * ------------------------- + * + * * `InputTriggerLabel` (input tag, mandatory): the tag of the trigger data + * product with the new reference time. It must be available and not empty. + * * `ShiftAuxDetIDE` (bool, default: `false`): enable the shifting of auxiliary + * detector simulation data product at `InitAuxDetSimChannelLabel`. + * * `InitAuxDetSimChannelLabel` (input tag): tag of the auxiliary detector + * simulation `sim::AuxDetSimChannel` product to be shifted. + * * `ShiftBeamGateInfo` (bool, default: `false`): enable the shifting of + * beam gate data product at `InitBeamGateInfoLabel`. + * * `InitBeamGateInfoLabel` (input tag): tag of the simulation beam gate data + * product to be shifted. This can be produced by a LArSoft generation module + * or by a trigger module. + * * `ShiftSimEnergyDeposits` (bool, default: `false`): enable the shifting of + * full energy deposit data product at `InitSimEnergyDepositLabel`. + * * `InitSimEnergyDepositLabel` (input tag): tag of the simulated energy + * deposition data product to be shifted. + * * `ShiftSimEnergyDepositLites` (bool, default: `false`): enable the shifting + * of lightweight energy deposit data product at + * `InitSimEnergyDepositLiteLabel`. + * * `InitSimEnergyDepositLiteLabel` (input tag): tag of the lightweight + * simulated energy deposition data product to be shifted. This reduced + * version is typically kept around for tracking back to truth information. + * * `ShiftSimPhotons` (bool, default: `false`): enable the shifting of + * scintillation photon data product at `InitSimPhotonsLabel`. + * * `InitSimPhotonsLabel` (input tag): tag of the simulated scintillation photon + * data product to be shifted. + * * `ShiftWaveforms` (bool, default: `false`): enable the shifting of optical + * detector waveform data product at `InitWaveformLabel`. + * * `InitWaveformLabel` (input tag): tag of the simulated optical detector + * waveform data product to be shifted. These waveforms may already be + * available if the worflow extracted the trigger time (new reference) out of + * them. + * * `BindWaveformBaselines` (input tag, default: `""`): tag of the association + * between the optical detector waveforms being shifted and their baselines. + * If empty (default), baseline associations will not be produced. + * * `AdditionalOffset` (real value, default: `0`): additional offset in + * microseconds to be added to the new reference trigger. + * * `DropTriggerProduct` (flag, default: `false`): if set, no shifted trigger + * data product will be produced. + * * `SkipExtraTriggerInfo` (flag: default: `false`): if set, the data product + * `sbn::ExtraTriggerInfo` (with the `InputTriggerLabel` tag) is not copied + * and put into the event. Otherwise, it is required to be present. + * + */ class AdjustSimForTrigger : public art::EDProducer { public: explicit AdjustSimForTrigger(fhicl::ParameterSet const& p); @@ -59,13 +253,16 @@ class AdjustSimForTrigger : public art::EDProducer { bool fShiftSimEnergyDepositLites; bool fShiftSimPhotons; bool fShiftWaveforms; + art::InputTag fBindWaveformBaselines; ///< Tag of OpDetWaveform-baseline associations to be rebound. double fAdditionalOffset; + bool fDropTriggerProduct; ///< Do not put the shifted trigger data product into the event. + bool fSkipExtraTriggerInfo; ///< Do not copy input `sbn::ExtraTriggerInfo`. static constexpr auto& kModuleName = "AdjustSimForTrigger"; }; AdjustSimForTrigger::AdjustSimForTrigger(fhicl::ParameterSet const& p) : EDProducer{p} - , fInputTriggerLabel{p.get("InputTriggerLabel", "undefined")} + , fInputTriggerLabel{p.get("InputTriggerLabel")} , fInitAuxDetSimChannelLabel(p.get("InitAuxDetSimChannelLabel", "undefined")) , fInitBeamGateInfoLabel{p.get("InitBeamGateInfoLabel", "undefined")} , fInitSimEnergyDepositLabel{p.get("InitSimEnergyDepositLabel", "undefined")} @@ -78,26 +275,40 @@ AdjustSimForTrigger::AdjustSimForTrigger(fhicl::ParameterSet const& p) , fShiftSimEnergyDepositLites{p.get("ShiftSimEnergyDepositLites", false)} , fShiftSimPhotons{p.get("ShiftSimPhotons", false)} , fShiftWaveforms{p.get("ShiftWaveforms", false)} + , fBindWaveformBaselines{p.get("BindWaveformBaselines", "")} , fAdditionalOffset{p.get("AdditionalOffset", 0.)} + , fDropTriggerProduct{p.get("DropTriggerProduct", false)} + , fSkipExtraTriggerInfo{p.get("SkipExtraTriggerInfo", false)} { if (!(fShiftSimEnergyDeposits || fShiftSimPhotons || fShiftWaveforms || fShiftAuxDetIDEs || fShiftBeamGateInfo || fShiftSimEnergyDepositLites)) { throw art::Exception(art::errors::EventProcessorFailure) << kModuleName << ": NO SHIFTS ENABLED!\n"; } + bool const doWaveformBaselines = fShiftWaveforms && !fBindWaveformBaselines.empty(); mf::LogInfo(kModuleName) << std::boolalpha << "SHIFTING AUXDETIDES? " << fShiftAuxDetIDEs << '\n' << "SHIFTING BEAMGATEINFO? " << fShiftBeamGateInfo << '\n' << "SHIFTING SIMENERGYDEPOSITS? " << fShiftSimEnergyDeposits << '\n' << "SHIFTING SIMENERGYDEPOSITLITES? " << fShiftSimEnergyDepositLites << '\n' << "SHIFTING SIMPHOTONS? " << fShiftSimPhotons << '\n' - << "SHIFTING OPDETWAVEFORMS? " << fShiftWaveforms; + << "SHIFTING OPDETWAVEFORMS? " << fShiftWaveforms << '\n' + << " ASSNS OPDETWAVEFORM-BASELINES? " << doWaveformBaselines + << (doWaveformBaselines? (" ('" + fBindWaveformBaselines.encode() + "')"): ""); + if (!fDropTriggerProduct) { + produces>(); + if (!fSkipExtraTriggerInfo) produces(); + } if (fShiftAuxDetIDEs) { produces>(); } if (fShiftBeamGateInfo) { produces>(); } if (fShiftSimEnergyDeposits) { produces>(); } if (fShiftSimEnergyDepositLites) { produces>(); } if (fShiftSimPhotons) { produces>(); } - if (fShiftWaveforms) { produces>(); } + if (fShiftWaveforms) { + produces>(); + if (!fBindWaveformBaselines.empty()) + produces>(); + } } void AdjustSimForTrigger::produce(art::Event& e) @@ -123,13 +334,39 @@ void AdjustSimForTrigger::produce(art::Event& e) trigger.TriggerTime() < (std::numeric_limits::max() - std::numeric_limits::epsilon()); - const double timeShiftForTrigger_us = - hasValidTriggerTime ? clock_data.TriggerTime() - trigger.TriggerTime() + fAdditionalOffset : 0.; + const double newReferenceTime = + hasValidTriggerTime ? trigger.TriggerTime() - fAdditionalOffset : clock_data.TriggerTime(); + const double timeShiftForTrigger_us = clock_data.TriggerTime() - newReferenceTime; const double timeShiftForTrigger_ns = 1000. * timeShiftForTrigger_us; mf::LogInfo(kModuleName) << "FOR THIS EVENT THE TIME SHIFT BEING ASSUMED IS " << timeShiftForTrigger_ns << " ns ..."; + // Shifted trigger (beam gate info, optional, is later) + // sbn::ExtraTriggerInfo and raw::ExternalTrigger are not shifted (so far); + // it's debatable if they should be, since they only hold absolute timestamps + if (!fDropTriggerProduct) { + auto pShiftedTriggers = std::make_unique>(); + for (raw::Trigger const& unshiftedTrigger: triggers) { // ok, we required there is just one + pShiftedTriggers->emplace_back( + unshiftedTrigger.TriggerNumber(), + hasValidTriggerTime // trigger_time + ? (unshiftedTrigger.TriggerTime() + timeShiftForTrigger_us) + : clock_data.TriggerTime(), + unshiftedTrigger.BeamGateTime() + timeShiftForTrigger_us,// beamgate_time + unshiftedTrigger.TriggerBits() + ); + } // for all triggers + e.put(std::move(pShiftedTriggers)); + + if (!fSkipExtraTriggerInfo) { + auto extraTrigger = e.getProduct(fInputTriggerLabel); + // no shift performed at this time + e.put(std::make_unique(std::move(extraTrigger))); + } + + } // if produce shifted trigger + // Loop over the sim::AuxDetIDE and shift time BACK by the TRIGGER if (fShiftAuxDetIDEs) { auto const& simChannels = @@ -253,6 +490,21 @@ void AdjustSimForTrigger::produce(art::Event& e) waveform.SetTimeStamp(waveform.TimeStamp() + timeShiftForTrigger_us); } e.put(std::move(pWaveforms)); + + if (!fBindWaveformBaselines.empty()) { + // given that the shifting is one-to-one, rebinding is just replacing + // each existing waveform pointer with one to the new waveform in the same position + auto const& waveformBaselineAssns + = e.getProduct>(fBindWaveformBaselines); + art::PtrMaker const makeWaveformPtr{ e }; + + auto pWaveformBaselineAssns + = std::make_unique> + (sbn::RebindAssociatedProducts(waveformBaselineAssns, makeWaveformPtr)); + + e.put(std::move(pWaveformBaselineAssns)); + } // if rebinding associations + } } diff --git a/sbncode/DetSim/CMakeLists.txt b/sbncode/DetSim/CMakeLists.txt index 113aa375a..e718bf218 100644 --- a/sbncode/DetSim/CMakeLists.txt +++ b/sbncode/DetSim/CMakeLists.txt @@ -1,34 +1,12 @@ set( MODULE_LIBRARIES + lardata::DetectorClocksService + larcore::headers + sbncode::Utilities larcorealg::Geometry - larcore::Geometry_Geometry_service - larsim::Simulation - nug4::ParticleNavigation - lardataobj::Simulation - lardata::Utilities - lardataalg::DetectorInfo - larevt::Filters lardataobj::RawData - larevt::CalibrationDBI_Providers - nurandom::RandomUtils_NuRandomService_service - art::Framework_Core - art::Framework_Principal - art::Framework_Services_Registry - art_root_io::tfile_support ROOT::Core - art::Framework_Services_Optional_RandomNumberGenerator_service - art_root_io::TFileService_service - art::Persistency_Common - art::Persistency_Provenance - art::Utilities - canvas::canvas - messagefacility::MF_MessageLogger - messagefacility::headers - fhiclcpp::fhiclcpp - cetlib::cetlib - CLHEP::Random - ROOT::Geom - ROOT::XMLIO - ROOT::Gdml - FFTW3::FFTW3 + lardataobj::Simulation + sbnobj::ICARUS_PMT_Data + sbnobj::Common_Trigger ) cet_build_plugin(AdjustSimForTrigger art::module LIBRARIES ${MODULE_LIBRARIES}) diff --git a/sbncode/DetSim/FilterSimEnergyDeposits_module.cc b/sbncode/DetSim/FilterSimEnergyDeposits_module.cc index 087c7ae96..bfcd191dd 100644 --- a/sbncode/DetSim/FilterSimEnergyDeposits_module.cc +++ b/sbncode/DetSim/FilterSimEnergyDeposits_module.cc @@ -69,7 +69,6 @@ FilterSimEnergyDeposits::FilterSimEnergyDeposits(fhicl::ParameterSet const& p) // Call appropriate produces<>() functions here. // Call appropriate consumes<>() for any products to be retrieved by this module. produces>(); - if (fInitSimEnergyDepositLiteLabel != "undefined") { produces>(); } } diff --git a/sbncode/EventGenerator/CMakeLists.txt b/sbncode/EventGenerator/CMakeLists.txt index b5329a992..292682726 100644 --- a/sbncode/EventGenerator/CMakeLists.txt +++ b/sbncode/EventGenerator/CMakeLists.txt @@ -1,2 +1,3 @@ add_subdirectory(MeVPrtl) add_subdirectory(MultiPart) +add_subdirectory(WireModGen) diff --git a/sbncode/EventGenerator/WireModGen/CMakeLists.txt b/sbncode/EventGenerator/WireModGen/CMakeLists.txt new file mode 100644 index 000000000..c119741b6 --- /dev/null +++ b/sbncode/EventGenerator/WireModGen/CMakeLists.txt @@ -0,0 +1,52 @@ +cet_build_plugin(WireModMuon art::EDProducer + SOURCE CORSIKAGenSBN.cc WireModMuon_module.cc + LIBRARIES PRIVATE + larcore::Geometry_Geometry_service + larcorealg::Geometry + larcoreobj::SummaryData + nurandom::RandomUtils_NuRandomService_service + nusimdata::SimulationBase + art::Framework_Principal + art::Framework_Services_Registry + messagefacility::MF_MessageLogger + fhiclcpp::fhiclcpp + ifdh::ifdh + messagefacility::MF_MessageLogger + ROOT::Core + ROOT::EG + CLHEP::Random + SQLite::SQLite3 + lardata::DetectorInfoServices_DetectorPropertiesServiceStandard_service + lardata::DetectorInfoServices_DetectorClocksServiceStandard_service + lardata::DetectorClocksService +) + +cet_build_plugin(FilterPrimarySimChannel art::EDProducer + LIBRARIES PRIVATE + larsim::Simulation + art::Framework_Principal + art::Framework_Services_Registry + messagefacility::MF_MessageLogger + fhiclcpp::fhiclcpp + messagefacility::MF_MessageLogger +) + +cet_build_plugin(FilterPrimarySimEnergyDepo art::EDProducer + LIBRARIES PRIVATE + art::Framework_Services_Registry + larsim::Simulation + art::Framework_Principal + art::Framework_Services_Registry + messagefacility::MF_MessageLogger + fhiclcpp::fhiclcpp + messagefacility::MF_MessageLogger + larcore::Geometry_Geometry_service + larcorealg::Geometry + lardata::DetectorInfoServices_DetectorPropertiesServiceStandard_service + lardata::DetectorInfoServices_DetectorClocksServiceStandard_service + lardata::DetectorClocksService +) + +install_headers() +install_fhicl() +install_source() diff --git a/sbncode/EventGenerator/WireModGen/CORSIKAGenSBN.cc b/sbncode/EventGenerator/WireModGen/CORSIKAGenSBN.cc new file mode 100644 index 000000000..a9a4be372 --- /dev/null +++ b/sbncode/EventGenerator/WireModGen/CORSIKAGenSBN.cc @@ -0,0 +1,603 @@ +//////////////////////////////////////////////////////////////////////// +/// \file CORSIKAGenSBN_module.cc +/// \brief Generator for cosmic-ray secondaries based on pre-generated CORSIKA shower databases. +/// Overrides the larsim CORSIKAGen module to allow to be inherited by a base class. +/// +/// \author gputnam@fnal.gov, taken from module by: Matthew.Bass@physics.ox.ac.uk +//////////////////////////////////////////////////////////////////////// + +#include "sbncode/EventGenerator/WireModGen/CORSIKAGenSBN.h" + +namespace evgen { + + CORSIKAGenSBN::CORSIKAGenSBN(fhicl::ParameterSet const& p) + : EDProducer{p} + , fProjectToHeight(p.get("ProjectToHeight", 0.)) + , fShowerInputFiles(p.get>("ShowerInputFiles")) + , fShowerCopyType(p.get("ShowerCopyType", "IFDH")) + , fShowerFluxConstants(p.get>("ShowerFluxConstants")) + , fSampleTime(p.get("SampleTime", 0.)) + , fToffset(p.get("TimeOffset", 0.)) + , fBuffBox(p.get>("BufferBox", {0.0, 0.0, 0.0, 0.0, 0.0, 0.0})) + , fShowerAreaExtension(p.get("ShowerAreaExtension", 0.)) + , fRandomXZShift(p.get("RandomXZShift", 0.)) + , fGenEngine(art::ServiceHandle()->registerAndSeedEngine( + createEngine(0, "HepJamesRandom", "gen"), + "HepJamesRandom", + "gen", + p, + {"Seed", "SeedGenerator"})) + , fPoisEngine(art::ServiceHandle()->registerAndSeedEngine( + createEngine(0, "HepJamesRandom", "pois"), + "HepJamesRandom", + "pois", + p, + "SeedPoisson")) + { + if (fShowerInputFiles.size() != fShowerFluxConstants.size() || fShowerInputFiles.size() == 0 || + fShowerFluxConstants.size() == 0) + throw cet::exception("CORSIKAGenSBN") + << "ShowerInputFiles and ShowerFluxConstants have different or invalid sizes!" + << "\n"; + fShowerInputs = fShowerInputFiles.size(); + + if (fSampleTime == 0.) throw cet::exception("CORSIKAGenSBN") << "SampleTime not set!"; + + if (fProjectToHeight == 0.) mf::LogInfo("CORSIKAGenSBN") << "Using 0. for fProjectToHeight!"; + // create a default random engine; obtain the random seed from NuRandomService, + // unless overridden in configuration with key "Seed" + + this->openDBs(p.get("module_label")); + this->populateNShowers(); + this->populateTOffset(); + + produces>(); + produces(); + } + + CORSIKAGenSBN::~CORSIKAGenSBN() + { + for (int i = 0; i < fShowerInputs; i++) { + sqlite3_close(fdb[i]); + } + //cleanup temp files + fIFDH->cleanup(); + } + + void CORSIKAGenSBN::ProjectToBoxEdge(const double xyz[], + const double indxyz[], + const double xlo, + const double xhi, + const double ylo, + const double yhi, + const double zlo, + const double zhi, + double xyzout[]) + { + + //we want to project backwards, so take mirror of momentum + const double dxyz[3] = {-indxyz[0], -indxyz[1], -indxyz[2]}; + + // Compute the distances to the x/y/z walls + double dx = 99.E99; + double dy = 99.E99; + double dz = 99.E99; + if (dxyz[0] > 0.0) { dx = (xhi - xyz[0]) / dxyz[0]; } + else if (dxyz[0] < 0.0) { + dx = (xlo - xyz[0]) / dxyz[0]; + } + if (dxyz[1] > 0.0) { dy = (yhi - xyz[1]) / dxyz[1]; } + else if (dxyz[1] < 0.0) { + dy = (ylo - xyz[1]) / dxyz[1]; + } + if (dxyz[2] > 0.0) { dz = (zhi - xyz[2]) / dxyz[2]; } + else if (dxyz[2] < 0.0) { + dz = (zlo - xyz[2]) / dxyz[2]; + } + + // Choose the shortest distance + double d = 0.0; + if (dx < dy && dx < dz) + d = dx; + else if (dy < dz && dy < dx) + d = dy; + else if (dz < dx && dz < dy) + d = dz; + + // Make the step + for (int i = 0; i < 3; ++i) { + xyzout[i] = xyz[i] + dxyz[i] * d; + } + } + + void CORSIKAGenSBN::openDBs(std::string const& module_label) + { + //choose files based on fShowerInputFiles, copy them with ifdh, open them + // for c2: statement is unused + //sqlite3_stmt *statement; + CLHEP::RandFlat flat(fGenEngine); + + //setup ifdh object + if (!fIFDH) fIFDH = new ifdh_ns::ifdh; + const char* ifdh_debug_env = std::getenv("IFDH_DEBUG_LEVEL"); + if (ifdh_debug_env) { + mf::LogInfo("CORSIKAGenSBN") << "IFDH_DEBUG_LEVEL: " << ifdh_debug_env << "\n"; + fIFDH->set_debug(ifdh_debug_env); + } + + //get ifdh path for each file in fShowerInputFiles, put into selectedflist + //if 1 file returned, use that file + //if >1 file returned, randomly select one file + //if 0 returned, make exeption for missing files + std::vector> selectedflist; + for (int i = 0; i < fShowerInputs; i++) { + if (fShowerInputFiles[i].find("*") == std::string::npos) { + //if there are no wildcards, don't call findMatchingFiles + selectedflist.push_back(std::make_pair(fShowerInputFiles[i], 0)); + mf::LogInfo("CorsikaGen") << "Selected" << selectedflist.back().first << "\n"; + } + else { + //use findMatchingFiles + //default to IFDH approach when wildcards in path + std::vector> flist; + std::string path(gSystem->DirName(fShowerInputFiles[i].c_str())); + std::string pattern(gSystem->BaseName(fShowerInputFiles[i].c_str())); + flist = fIFDH->findMatchingFiles(path, pattern); + unsigned int selIndex = -1; + if (flist.size() == 1) { //0th element is the search path:pattern + selIndex = 0; + } + else if (flist.size() > 1) { + selIndex = (unsigned int)(flat() * (flist.size() - 1) + + 0.5); //rnd with rounding, dont allow picking the 0th element + } + else { + throw cet::exception("CORSIKAGenSBN") + << "No files returned for path:pattern: " << path << ":" << pattern << std::endl; + } + selectedflist.push_back(flist[selIndex]); + mf::LogInfo("CorsikaGen") << "For " << fShowerInputFiles[i] << ":" << pattern << "\nFound " + << flist.size() << " candidate files" + << "\nChoosing file number " << selIndex << "\n" + << "\nSelected " << selectedflist.back().first << "\n"; + } + } + + //do the fetching, store local filepaths in locallist + std::vector locallist; + for (unsigned int i = 0; i < selectedflist.size(); i++) { + mf::LogInfo("CorsikaGen") << "Fetching: " << selectedflist[i].first << " " + << selectedflist[i].second << "\n"; + if (fShowerCopyType == "IFDH") { + std::string fetchedfile(fIFDH->fetchInput(selectedflist[i].first)); + MF_LOG_DEBUG("CorsikaGen") << "Fetched; local path: " << fetchedfile << "; copy type: IFDH"; + locallist.push_back(fetchedfile); + } + else if (fShowerCopyType == "DIRECT") { + std::string fetchedfile(selectedflist[i].first); + MF_LOG_DEBUG("CorsikaGen") + << "Fetched; local path: " << fetchedfile << "; copy type: DIRECT"; + locallist.push_back(fetchedfile); + } + else + throw cet::exception("CORSIKAGenSBN") + << "Error copying shower db: ShowerCopyType must be \"IFDH\" or \"DIRECT\"\n"; + } + + //open the files in fShowerInputFilesLocalPaths with sqlite3 + for (unsigned int i = 0; i < locallist.size(); i++) { + //prepare and execute statement to attach db file + int res = sqlite3_open(locallist[i].c_str(), &fdb[i]); + if (res != SQLITE_OK) + throw cet::exception("CORSIKAGenSBN") + << "Error opening db: (" << locallist[i].c_str() << ") (" << res + << "): " << sqlite3_errmsg(fdb[i]) << "; memory used:<<" << sqlite3_memory_used() << "/" + << sqlite3_memory_highwater(0) << "\n"; + else + mf::LogInfo("CORSIKAGenSBN") << "Attached db " << locallist[i] << "\n"; + } + } + + double CORSIKAGenSBN::wrapvar(const double var, const double low, const double high) + { + //wrap variable so that it's always between low and high + return (var - (high - low) * floor(var / (high - low))) + low; + } + + double CORSIKAGenSBN::wrapvarBoxNo(const double var, const double low, const double high, int& boxno) + { + //wrap variable so that it's always between low and high + boxno = int(floor(var / (high - low))); + return (var - (high - low) * floor(var / (high - low))) + low; + } + + void CORSIKAGenSBN::populateTOffset() + { + //populate TOffset_corsika by finding minimum ParticleTime from db file + + sqlite3_stmt* statement; + const std::string kStatement("select min(t) from particles"); + double t = 0.; + + for (int i = 0; i < fShowerInputs; i++) { + //build and do query to get run min(t) from each db + if (sqlite3_prepare(fdb[i], kStatement.c_str(), -1, &statement, 0) == SQLITE_OK) { + int res = 0; + res = sqlite3_step(statement); + if (res == SQLITE_ROW) { + t = sqlite3_column_double(statement, 0); + mf::LogInfo("CORSIKAGenSBN") + << "For showers input " << i << " found particles.min(t)=" << t << "\n"; + if (i == 0 || t < fToffset_corsika) fToffset_corsika = t; + } + else { + throw cet::exception("CORSIKAGenSBN") + << "Unexpected sqlite3_step return value: (" << res << "); " + << "ERROR:" << sqlite3_errmsg(fdb[i]) << "\n"; + } + } + else { + throw cet::exception("CORSIKAGenSBN") << "Error preparing statement: (" << kStatement << "); " + << "ERROR:" << sqlite3_errmsg(fdb[i]) << "\n"; + } + } + + mf::LogInfo("CORSIKAGenSBN") << "Found corsika timeoffset [ns]: " << fToffset_corsika << "\n"; + } + + void CORSIKAGenSBN::populateNShowers() + { + //populate vector of the number of showers per event based on: + //AREA the showers are being distributed over + //TIME of the event (fSampleTime) + //flux constants that determine the overall normalizations (fShowerFluxConstants) + //Energy range over which the sample was generated (ERANGE_*) + //power spectrum over which the sample was generated (ESLOPE) + + //compute shower area based on the maximal x,z dimensions of cryostat boundaries + fShowerAreaExtension + art::ServiceHandle geom; + for (auto const& cryostat : geom->Iterate()) { + double bounds[6] = {0.}; + cryostat.Boundaries(bounds); + for (unsigned int bnd = 0; bnd < 6; bnd++) { + mf::LogVerbatim("CORSIKAGenSBN") << "Cryo Boundary: " << bnd << "=" << bounds[bnd] + << " ( + Buffer=" << fBuffBox[bnd] << ")\n"; + if (fabs(bounds[bnd]) > fabs(fShowerBounds[bnd])) { fShowerBounds[bnd] = bounds[bnd]; } + } + } + //add on fShowerAreaExtension without being clever + fShowerBounds[0] = fShowerBounds[0] - fShowerAreaExtension; + fShowerBounds[1] = fShowerBounds[1] + fShowerAreaExtension; + fShowerBounds[4] = fShowerBounds[4] - fShowerAreaExtension; + fShowerBounds[5] = fShowerBounds[5] + fShowerAreaExtension; + + double showersArea = (fShowerBounds[1] / 100 - fShowerBounds[0] / 100) * + (fShowerBounds[5] / 100 - fShowerBounds[4] / 100); + + mf::LogInfo("CORSIKAGenSBN") << "Area extended by : " << fShowerAreaExtension + << "\nShowers to be distributed betweeen: x=" << fShowerBounds[0] + << "," << fShowerBounds[1] << " & z=" << fShowerBounds[4] << "," + << fShowerBounds[5] + << "\nShowers to be distributed with random XZ shift: " + << fRandomXZShift << " cm" + << "\nShowers to be distributed over area: " << showersArea << " m^2" + << "\nShowers to be distributed over time: " << fSampleTime << " s" + << "\nShowers to be distributed with time offset: " << fToffset + << " s" + << "\nShowers to be distributed at y: " << fShowerBounds[3] << " cm"; + + //db variables + sqlite3_stmt* statement; + const std::string kStatement("select erange_high,erange_low,eslope,nshow from input"); + double upperLimitOfEnergyRange = 0., lowerLimitOfEnergyRange = 0., energySlope = 0., + oneMinusGamma = 0., EiToOneMinusGamma = 0., EfToOneMinusGamma = 0.; + + for (int i = 0; i < fShowerInputs; i++) { + //build and do query to get run info from databases + // double thisrnd=flat();//need a new random number for each query + if (sqlite3_prepare(fdb[i], kStatement.c_str(), -1, &statement, 0) == SQLITE_OK) { + int res = 0; + res = sqlite3_step(statement); + if (res == SQLITE_ROW) { + upperLimitOfEnergyRange = sqlite3_column_double(statement, 0); + lowerLimitOfEnergyRange = sqlite3_column_double(statement, 1); + energySlope = sqlite3_column_double(statement, 2); + fMaxShowers.push_back(sqlite3_column_int(statement, 3)); + oneMinusGamma = 1 + energySlope; + EiToOneMinusGamma = pow(lowerLimitOfEnergyRange, oneMinusGamma); + EfToOneMinusGamma = pow(upperLimitOfEnergyRange, oneMinusGamma); + mf::LogVerbatim("CORSIKAGenSBN") + << "For showers input " << i << " found e_hi=" << upperLimitOfEnergyRange + << ", e_lo=" << lowerLimitOfEnergyRange << ", slope=" << energySlope + << ", k=" << fShowerFluxConstants[i] << "\n"; + } + else { + throw cet::exception("CORSIKAGenSBN") + << "Unexpected sqlite3_step return value: (" << res << "); " + << "ERROR:" << sqlite3_errmsg(fdb[i]) << "\n"; + } + } + else { + throw cet::exception("CORSIKAGenSBN") << "Error preparing statement: (" << kStatement << "); " + << "ERROR:" << sqlite3_errmsg(fdb[i]) << "\n"; + } + + //this is computed, how? + double NShowers = (M_PI * showersArea * fShowerFluxConstants[i] * + (EfToOneMinusGamma - EiToOneMinusGamma) / oneMinusGamma) * + fSampleTime; + fNShowersPerEvent.push_back(NShowers); + mf::LogVerbatim("CORSIKAGenSBN") + << "For showers input " << i << " the number of showers per event is " << (int)NShowers + << "\n"; + } + } + + void CORSIKAGenSBN::GetSample(simb::MCTruth& mctruth) + { + //for each input, randomly pull fNShowersPerEvent[i] showers from the Particles table + //and randomly place them in time (between -fSampleTime/2 and fSampleTime/2) + //wrap their positions based on the size of the area under consideration + //based on http://nusoft.fnal.gov/larsoft/doxsvn/html/CRYHelper_8cxx_source.html (Sample) + + //query from sqlite db with select * from particles where shower in (select id from showers ORDER BY substr(id*0.51123124141,length(id)+2) limit 100000) ORDER BY substr(shower*0.51123124141,length(shower)+2); + //where 0.51123124141 is a random seed to allow randomly selecting rows and should be randomly generated for each query + //the inner order by is to select randomly from the possible shower id's + //the outer order by is to make sure the shower numbers are ordered randomly (without this, the showers always come out ordered by shower number + //and 100000 is the number of showers to be selected at random and needs to be less than the number of showers in the showers table + + //TDatabasePDG is for looking up particle masses + static TDatabasePDG* pdgt = TDatabasePDG::Instance(); + + //db variables + sqlite3_stmt* statement; + const TString kStatement( + "select shower,pdg,px,py,pz,x,z,t,e from particles where shower in (select id from showers " + "ORDER BY substr(id*%f,length(id)+2) limit %d) ORDER BY substr(shower*%f,length(shower)+2)"); + + CLHEP::RandFlat flat(fGenEngine); + CLHEP::RandPoissonQ randpois(fPoisEngine); + + // get geometry and figure where to project particles to, based on CRYHelper + art::ServiceHandle geom; + double x1, x2; + double y1, y2; + double z1, z2; + geom->WorldBox(&x1, &x2, &y1, &y2, &z1, &z2); + + // make the world box slightly smaller so that the projection to + // the edge avoids possible rounding errors later on with Geant4 + double fBoxDelta = 1.e-5; + x1 += fBoxDelta; + x2 -= fBoxDelta; + y1 += fBoxDelta; + y2 = fProjectToHeight; + z1 += fBoxDelta; + z2 -= fBoxDelta; + + //populate mctruth + int ntotalCtr = 0; //count number of particles added to mctruth + int lastShower = + 0; //keep track of last shower id so that t can be randomized on every new shower + int nShowerCntr = 0; //keep track of how many showers are left to be added to mctruth + int nShowerQry = 0; //number of showers to query from db + int shower, pdg; + double px, py, pz, x, z, tParticleTime, etot, + showerTime = 0., showerTimex = 0., showerTimez = 0., showerXOffset = 0., showerZOffset = 0., + t; + for (int i = 0; i < fShowerInputs; i++) { + nShowerCntr = randpois.fire(fNShowersPerEvent[i]); + mf::LogInfo("CORSIKAGEN") << " Shower input " << i << " with mean " << fNShowersPerEvent[i] + << " generating " << nShowerCntr; + + while (nShowerCntr > 0) { + //how many showers should we query? + if (nShowerCntr > fMaxShowers[i]) { + nShowerQry = fMaxShowers[i]; //take the group size + } + else { + nShowerQry = nShowerCntr; //take the rest that are needed + } + //build and do query to get nshowers + double thisrnd = flat(); //need a new random number for each query + TString kthisStatement = TString::Format(kStatement.Data(), thisrnd, nShowerQry, thisrnd); + MF_LOG_DEBUG("CORSIKAGenSBN") << "Executing: " << kthisStatement; + if (sqlite3_prepare(fdb[i], kthisStatement.Data(), -1, &statement, 0) == SQLITE_OK) { + int res = 0; + //loop over database rows, pushing particles into mctruth object + while (1) { + res = sqlite3_step(statement); + if (res == SQLITE_ROW) { + /* + * Memo columns: + * [0] shower + * [1] particle ID (PDG) + * [2] momentum: x component [GeV/c] + * [3] momentum: y component [GeV/c] + * [4] momentum: z component [GeV/c] + * [5] position: x component [cm] + * [6] position: z component [cm] + * [7] time [ns] + * [8] energy [GeV] + */ + shower = sqlite3_column_int(statement, 0); + if (shower != lastShower) { + //each new shower gets its own random time and position offsets + showerTime = 1e9 * (flat() * fSampleTime); //converting from s to ns + showerTimex = 1e9 * (flat() * fSampleTime); //converting from s to ns + showerTimez = 1e9 * (flat() * fSampleTime); //converting from s to ns + //and a random offset in both z and x controlled by the fRandomXZShift parameter + showerXOffset = flat() * fRandomXZShift - (fRandomXZShift / 2); + showerZOffset = flat() * fRandomXZShift - (fRandomXZShift / 2); + } + pdg = sqlite3_column_int(statement, 1); + //get mass for this particle + double m = 0.; // in GeV + TParticlePDG* pdgp = pdgt->GetParticle(pdg); + if (pdgp) m = pdgp->Mass(); + + //Note: position/momentum in db have north=-x and west=+z, rotate so that +z is north and +x is west + //get momentum components + px = sqlite3_column_double(statement, 4); //uboone x=Particlez + py = sqlite3_column_double(statement, 3); + pz = -sqlite3_column_double(statement, 2); //uboone z=-Particlex + etot = sqlite3_column_double(statement, 8); + + //get/calculate position components + int boxnoX = 0, boxnoZ = 0; + x = wrapvarBoxNo(sqlite3_column_double(statement, 6) + showerXOffset, + fShowerBounds[0], + fShowerBounds[1], + boxnoX); + z = wrapvarBoxNo(-sqlite3_column_double(statement, 5) + showerZOffset, + fShowerBounds[4], + fShowerBounds[5], + boxnoZ); + tParticleTime = sqlite3_column_double( + statement, 7); //time offset, includes propagation time from top of atmosphere + //actual particle time is particle surface arrival time + //+ shower start time + //+ global offset (fcl parameter, in s) + //- propagation time through atmosphere + //+ boxNo{X,Z} time offset to make grid boxes have different shower times + t = tParticleTime + showerTime + (1e9 * fToffset) - fToffset_corsika + + showerTimex * boxnoX + showerTimez * boxnoZ; + //wrap surface arrival so that it's in the desired time window + t = wrapvar(t, (1e9 * fToffset), 1e9 * (fToffset + fSampleTime)); + + simb::MCParticle p(ntotalCtr, pdg, "primary", -200, m, 1); + + //project back to wordvol/fProjectToHeight + /* + * This back propagation goes from a point on the upper surface of + * the cryostat back to the edge of the world, except that that + * world is cut short by `fProjectToHeight` (`y2`) ceiling. + * The projection will most often lie on that ceiling, but it may + * end up instead on one of the side edges of the world, or even + * outside it. + */ + double xyzo[3]; + double x0[3] = {x, fShowerBounds[3], z}; + double dx[3] = {px, py, pz}; + this->ProjectToBoxEdge(x0, dx, x1, x2, y1, y2, z1, z2, xyzo); + + TLorentzVector pos( + xyzo[0], xyzo[1], xyzo[2], t); // time needs to be in ns to match GENIE, etc + TLorentzVector mom(px, py, pz, etot); + p.AddTrajectoryPoint(pos, mom); + mctruth.Add(p); + ntotalCtr++; + lastShower = shower; + } + else if (res == SQLITE_DONE) { + break; + } + else { + throw cet::exception("CORSIKAGenSBN") + << "Unexpected sqlite3_step return value: (" << res << "); " + << "ERROR:" << sqlite3_errmsg(fdb[i]) << "\n"; + } + } + } + else { + throw cet::exception("CORSIKAGenSBN") + << "Error preparing statement: (" << kthisStatement << "); " + << "ERROR:" << sqlite3_errmsg(fdb[i]) << "\n"; + } + nShowerCntr = nShowerCntr - nShowerQry; + } + } + } + + void CORSIKAGenSBN::beginRun(art::Run& run) + { + art::ServiceHandle geo; + run.put(std::make_unique(geo->DetectorName()), art::fullRun()); + } + + void CORSIKAGenSBN::produce(art::Event& evt) + { + std::unique_ptr> truthcol(new std::vector); + + art::ServiceHandle geom; + + simb::MCTruth truth; + truth.SetOrigin(simb::kCosmicRay); + + simb::MCTruth pretruth; + GetSample(pretruth); + mf::LogInfo("CORSIKAGenSBN") << "GetSample number of particles returned: " << pretruth.NParticles() + << "\n"; + // loop over particles in the truth object + for (int i = 0; i < pretruth.NParticles(); ++i) { + simb::MCParticle particle = pretruth.GetParticle(i); + const TLorentzVector& v4 = particle.Position(); + const TLorentzVector& p4 = particle.Momentum(); + double x0[3] = {v4.X(), v4.Y(), v4.Z()}; + double dx[3] = {p4.Px(), p4.Py(), p4.Pz()}; + + // now check if the particle goes through any cryostat in the detector + // if so, add it to the truth object. + for (auto const& cryostat : geom->Iterate()) { + double bounds[6] = {0.}; + cryostat.Boundaries(bounds); + + //add a buffer box around the cryostat bounds to increase the acceptance and account for scattering + //By default, the buffer box has zero size + for (unsigned int cb = 0; cb < 6; cb++) + bounds[cb] = bounds[cb] + fBuffBox[cb]; + + //calculate the intersection point with each cryostat surface + bool intersects_cryo = false; + for (int bnd = 0; bnd != 6; ++bnd) { + if (bnd < 2) { + double p2[3] = {bounds[bnd], + x0[1] + (dx[1] / dx[0]) * (bounds[bnd] - x0[0]), + x0[2] + (dx[2] / dx[0]) * (bounds[bnd] - x0[0])}; + if (p2[1] >= bounds[2] && p2[1] <= bounds[3] && p2[2] >= bounds[4] && + p2[2] <= bounds[5]) { + intersects_cryo = true; + break; + } + } + else if (bnd >= 2 && bnd < 4) { + double p2[3] = {x0[0] + (dx[0] / dx[1]) * (bounds[bnd] - x0[1]), + bounds[bnd], + x0[2] + (dx[2] / dx[1]) * (bounds[bnd] - x0[1])}; + if (p2[0] >= bounds[0] && p2[0] <= bounds[1] && p2[2] >= bounds[4] && + p2[2] <= bounds[5]) { + intersects_cryo = true; + break; + } + } + else if (bnd >= 4) { + double p2[3] = {x0[0] + (dx[0] / dx[2]) * (bounds[bnd] - x0[2]), + x0[1] + (dx[1] / dx[2]) * (bounds[bnd] - x0[2]), + bounds[bnd]}; + if (p2[0] >= bounds[0] && p2[0] <= bounds[1] && p2[1] >= bounds[2] && + p2[1] <= bounds[3]) { + intersects_cryo = true; + break; + } + } + } + + if (intersects_cryo) { + truth.Add(particle); + break; //leave loop over cryostats to avoid adding particle multiple times + } // end if particle goes into a cryostat + } // end loop over cryostats in the detector + + } // loop on particles + + mf::LogInfo("CORSIKAGenSBN") + << "Number of particles from getsample crossing cryostat + bounding box: " + << truth.NParticles() << "\n"; + + truthcol->push_back(truth); + evt.put(std::move(truthcol)); + + return; + } // end produce + +} // end namespace diff --git a/sbncode/EventGenerator/WireModGen/CORSIKAGenSBN.h b/sbncode/EventGenerator/WireModGen/CORSIKAGenSBN.h new file mode 100644 index 000000000..e70d45c53 --- /dev/null +++ b/sbncode/EventGenerator/WireModGen/CORSIKAGenSBN.h @@ -0,0 +1,299 @@ +//////////////////////////////////////////////////////////////////////// +/// \file CORSIKAGenSBN.h +/// \brief Class definition for cosmic-ray generator. +/// +/// \author gputnam@fnal.gov, taken from module by: Matthew.Bass@physics.ox.ac.uk +//////////////////////////////////////////////////////////////////////// +// +// ROOT includes +#include "TDatabasePDG.h" +#include "TString.h" +#include "TSystem.h" //need BaseName and DirName + +// Framework includes +#include "art/Framework/Core/EDProducer.h" +#include "art/Framework/Core/ModuleMacros.h" +#include "art/Framework/Principal/Event.h" +#include "art/Framework/Principal/Run.h" +#include "art/Framework/Services/Registry/ServiceHandle.h" +#include "fhiclcpp/ParameterSet.h" +#include "messagefacility/MessageLogger/MessageLogger.h" + +// art extensions +#include "nurandom/RandomUtils/NuRandomService.h" + +// larsoft includes +#include "larcore/Geometry/Geometry.h" +#include "larcoreobj/SummaryData/RunData.h" +#include "nusimdata/SimulationBase/MCParticle.h" +#include "nusimdata/SimulationBase/MCTruth.h" + +#include "CLHEP/Random/RandFlat.h" +#include "CLHEP/Random/RandPoissonQ.h" +#include "ifdh.h" //to handle flux files +#include + +namespace evgen { + + /** + * @brief LArSoft interface to CORSIKA event generator. + * + * @note A presentation on this module by the original author is archived at: + * https://indico.fnal.gov/event/10893/contribution/3/material/slides + * + * In CORSIKA jargon, a "shower" is the cascade of particles resulting from + * a primary cosmic ray interaction. + * This module creates a single `simb::MCTruth` object (stored as data product + * into a `std::vector` with a single entry) containing all + * the particles from cosmic ray showers crossing a _surface_ above the + * detector. + * + * The generation procedure consists of selecting showers from a database of + * *pregenerated* events, and then to adapt them to the parameters requested + * in the module configuration. Pregenerated showers are "observed" at a + * altitude set in CORSIKA configuration. + * + * Databases need to be stored as files in SQLite3 format. Multiple file + * sources can be specified (`ShowerInputFiles` configuration parameter). + * From each source, one database file is selected and copied locally via + * IFDH. + * From each source, showers are extracted proportionally to the relative flux + * specified in the configuration (specified in `ShowerFluxConstants`, + * see @ref CORSIKAGenSBN_Normalization "normalization" below). + * The actual number of showers per event and per source is extracted + * according to a Poisson distribution around the predicted average number of + * primary cosmic rays for that source. + * + * + * Flux normalization + * ------------------- + * + * @anchor CORSIKAGenSBN_Normalization + * + * CORSIKA generates showers from each specific cosmic ray type @f$ A @f$ + * (e.g. iron, proton, etc.) according to a power law distribution + * @f$ \Phi_{A}(E) \propto E^{-\gamma_{A}} @f$ of the primary + * particle energy @f$ E @f$ [GeV]. When sampling pregenerated events, we + * bypass the normalization imposed by CORSIKA and gain complete + * control on it. + * + * Within CORSIKAGenSBN, for each source (usually each on a different primary + * cosmic ray type, e.g. iron, proton, etc.), the average number of generated + * showers is + * @f$ n_{A} = \pi S T k_{A} \int E^{-\gamma_{A}} dE @f$ with @f$ S @f$ the + * area of the surface the flux passes across, @f$ T @f$ the exposure time, + * the integral defined over the full energy range of the pregenerated showers + * in the source, and @f$ k_{A} @f$ a factor specified in the configuration + * (`ShowerFluxConstants` parameters). + * This is the flux of primary cosmic rays, not of the observed particles + * from their showers. Note that it depends on an area and a time interval, + * but it is uniform with respect to translations and constant in time. + * + * As explained @ref CORSIKAGenSBN_Coverage "below", we consider only the + * secondary particles that cross an "observation" surface. + * After cosmic ray primary particles cross the flux surface (@f$ S_{\Phi} @f$ + * above), they develop into showers of particles that spread across large + * areas. Limiting ourself to the observation of particles on a small surface + * @f$ S_{o} @f$ has two effects. We lose the part of the showers that misses + * that surface @f$ S_{o} @f$. Also, considering a span of time with + * multiple showers, we miss particles from other hypothetical showers + * whose primaries crossed outside the generation surface @f$ S_{\Phi} @f$ + * whose shower development would leak into @f$ S_{o} @f$: we did not simulate + * those showers at all! + * In terms of total flux of observed particles, under the assumption that the + * flux is uniform in space, choosing @f$ S_{\Phi} @f$ the same size as + * @f$ S_{o} @f$ makes the two effects get the same size: just as many + * particles from primaries from @f$ S_{\Phi} @f$ leak out of @f$ S_{o} @f$, + * as many particles from primaries from outside @f$ S_{\Phi} @f$ sneak in + * @f$ S_{o} @f$. + * In that case, counting _all_ the particles from the primaries crossing a + * surface @f$ S_{\Phi} @f$ of area _S_ regardless of where they land + * yields the right amount of cosmic ray secondary particles across an + * observation surface @f$ S_{o} @f$ also of area _S_. Practically, + * the particles landing outside @f$ S_{o} @f$ need to be recovered to + * preserve the correct normalization, which is described in the + * @ref CORSIKAGenSBN_Coverage "next section". + * + * + * Surface coverage, position and timing + * -------------------------------------- + * + * @anchor CORSIKAGenSBN_Coverage + * + * The surface we detect the particles through (let's call it @f$ S_{d} @f$) + * is defined by the smallest _rectangle_ including all cryostats in the + * detector, and located at the height of the ceiling of the tallest cryostat. + * This surface can be increased by specifying a positive value for + * `ShowerAreaExtension` configuration parameter, in which case each side of + * the rectangle will be extended by that amount. + * + * Showers are extracted one by one from the pregenerated samples and treated + * independently. + * Ideally, the detection surface @f$ S_{d} @f$ would be at the same exact + * altitude as the observation surface set in CORSIKA (called @f$ S_{o} @f$ + * above). In practice, we go the other way around, with the assumption that + * the shower observed at @f$ S_{d} @f$ would be very close to the one we + * actually generated at @f$ S_{o} @f$, and teleport the generated particles + * on @f$ S_{d} @f$. Since the cryostats may be just meters from the earth + * surface @f$ S_{o} @f$ lies on, this is an acceptable approximation. + * + * All the particles of one shower are compelled within surface @f$ S_{d} @f$ + * as a first step. As explained when describing the + * @anchor CORSIKAGenSBN_Normalization "normalization", we need to keep all the + * shower particles, one way or the other. + * So, particles of the shower that fell out of @f$ S_{d} @f$ are repackaged + * into other showers and translated back in. This is equivalent to assume the + * primary cosmic rays originating such shower would happen outside + * our generation volume (@f$ S_{\Phi} @f$), and their shower would then spill + * into @f$ S_{d} @f$. Since these repackaged showers are in principle + * independent and uncorrelated, they are assigned a random time different + * than the main shower, leveraging the assumption of constantness of the + * flux. + * + * As for the azimuth, this module uses an approximation by setting north + * direction to match the _z_ axis of LArSoft geometry (typically assumed + * to be the direction of the beam particle). + * + * The particles so manipulated are then back-propagated from the observation + * surface to an absolute height defined by `ProjectToHeight` (although for + * particular combination of position and direction, the particles might be + * propagated back to the edge of the world, or even outside it). + * + * As a final filter, only the particles whose straight projections cross any + * of the cryostats (with some buffer volume around, defined by `BufferBox`) + * are stored, while the other ones are discarded. Note that the actual + * interactions that particles from the generation surface undergo may deviate + * them enough to miss the cryostats anyway, and conversely particles that + * have been filtered out because shooting off the cryostats might have been + * subsequently deviated to actually cross them. This effect is not corrected + * for at this time. + * + * The time of the showers is uniformly distributed within the configured + * time interval, defined by `SampleTime` starting from `TimeOffset`. + * + * + * Configuration parameters + * ========================= + * + * * `ShowerInputFiles` (list of paths; mandatory): a list of file paths to + * pregenerated CORSIKA shower files. Each entry can be a single file or + * use wildcards (`*`) to specify a set of files to choose among. + * Paths and wildcards are processed by IFDH. + * * `ShowerFluxConstants` (list of real numbers; mandatory): for each entry + * @f$ A @f$ in `ShowerInputFiles`, specify the normalization factor + * @f$ K_{A} @f$ of their distribution [@f$ m^{-2}s^{-1} @f$] + * * `ProjectToHeight` (real, default: `0`): the generated particles will + * appear to come from this height [cm] + * * `TimeOffset` (real; default: `0`): start time of the exposure window [s], + * relative to the + * @ref DetectorClocksSimulationTime "simulation time start" + * * `SampleTime` (real; mandatory): duration of the simulated exposure to + * cosmic rays [s] + * * `ShowerAreaExtension` (real; default: `0`): + * extend the size of the observation surface of shower particles (_S_) + * by this much [cm]; e.g. 1000 will extend 10 m on each side + * * `RandomXZShift` (real; default: `0`): the original position of each + * shower is randomly shifted within a square with this length as side + * [cm] + * * `BufferBox` (list of six lengths, all `0` by default): + * extension to the volume of each cryostat for the purpose of filtering + * out the particles which do not cross the detector; each cryostat volume + * is independently extended by the same amount, specified here as + * *shifts* to lower _x_, higher _x_, lower _y_, higher _y_, lower _z_ + * and higher _z_, in that order [cm] (note that to extend e.g. the + * negative _x_ side by 5 meters the parameter value should be -500) + * * `SeedGenerator` (integer): force random number generator for event + * generation to the specified value + * * `SeedPoisson` (integer): force random number generator for number of + * showers to the specified value + * * `Seed`: alias for `SeedGenerator` + * + * + * Random engines + * --------------- + * + * Currently two random engines are used: + * * a generator engine (driven by `SeedGenerator`), of general use + * * a "Poisson" engine (driven by `SeedPoisson`), only used to determine the + * number of showers to be selected on each event + * + */ + class CORSIKAGenSBN : public art::EDProducer { + public: + explicit CORSIKAGenSBN(fhicl::ParameterSet const& pset); + virtual ~CORSIKAGenSBN(); + + void produce(art::Event& evt); + void beginRun(art::Run& run); + + protected: + void openDBs(std::string const& module_label); + void populateNShowers(); + void populateTOffset(); + void GetSample(simb::MCTruth&); + double wrapvar(const double var, const double low, const double high); + double wrapvarBoxNo(const double var, const double low, const double high, int& boxno); + /** + * @brief Propagates a point back to the surface of a box. + * @param xyz coordinates of the point to be propagated (`{ x, y, z }`) + * @param dxyz direction of the point (`{ dx, dy, dz }`) + * @param xlo lower _x_ coordinate of the target box + * @param xhi upper _x_ coordinate of the target box + * @param ylo lower _y_ coordinate of the target box + * @param yhi upper _y_ coordinate of the target box + * @param zlo lower _z_ coordinate of the target box + * @param zhi upper _z_ coordinate of the target box + * @param xyzout _(output, room for at least 3 numbers)_ propagated point + * + * The point `xyz`, assumed to be inside the box, is propagated at the level + * of _the closest among the sides of the box_. Note that this means the + * propagated point might still be not on the surface of the box, even if it + * would later reach it. + * It is anyway guaranteed that `xyzout` is not inside the target box, and + * that at least one of its three coordinates is on the box surface. + */ + void ProjectToBoxEdge(const double xyz[], + const double dxyz[], + const double xlo, + const double xhi, + const double ylo, + const double yhi, + const double zlo, + const double zhi, + double xyzout[]); + + int fShowerInputs = 0; ///< Number of shower inputs to process from + std::vector + fNShowersPerEvent; ///< Number of showers to put in each event of duration fSampleTime; one per showerinput + std::vector fMaxShowers; //< Max number of showers to query, one per showerinput + double fShowerBounds[6] = { + 0., + 0., + 0., + 0., + 0., + 0.}; ///< Boundaries of area over which showers are to be distributed (x(min), x(max), _unused_, y, z(min), z(max) ) + double fToffset_corsika = + 0.; ///< Timing offset to account for propagation time through atmosphere, populated from db + ifdh_ns::ifdh* fIFDH = 0; ///< (optional) flux file handling + + //fcl parameters + double fProjectToHeight = 0.; ///< Height to which particles will be projected [cm] + std::vector fShowerInputFiles; ///< Set of CORSIKA shower data files to use + std::string fShowerCopyType; // ifdh file selection and copying (default) or direct file path + std::vector + fShowerFluxConstants; ///< Set of flux constants to be associated with each shower data file + double fSampleTime = 0.; ///< Duration of sample [s] + double fToffset = 0.; ///< Time offset of sample, defaults to zero (no offset) [s] + std::vector + fBuffBox; ///< Buffer box extensions to cryostat in each direction (6 of them: x_lo,x_hi,y_lo,y_hi,z_lo,z_hi) [cm] + double fShowerAreaExtension = + 0.; ///< Extend distribution of corsika particles in x,z by this much (e.g. 1000 will extend 10 m in -x, +x, -z, and +z) [cm] + sqlite3* fdb[5]; ///< Pointers to sqlite3 database object, max of 5 + double fRandomXZShift = + 0.; ///< Each shower will be shifted by a random amount in xz so that showers won't repeatedly sample the same space [cm] + CLHEP::HepRandomEngine& fGenEngine; + CLHEP::HepRandomEngine& fPoisEngine; + }; +} + diff --git a/sbncode/EventGenerator/WireModGen/FilterPrimarySimChannel_module.cc b/sbncode/EventGenerator/WireModGen/FilterPrimarySimChannel_module.cc new file mode 100644 index 000000000..32c9fd3af --- /dev/null +++ b/sbncode/EventGenerator/WireModGen/FilterPrimarySimChannel_module.cc @@ -0,0 +1,84 @@ +//////////////////////////////////////////////////////////////////////// +// Class: FilterPrimarySimChannel +// Plugin Type: filter (art v3_05_01) +// File: FilterPrimarySimChannel_module.cc +// +// Module for selecting events based on the presence of data products. +// +// Useful for processing data events, which sometimes have missing +// data products. +//////////////////////////////////////////////////////////////////////// + +#include "art/Framework/Core/EDFilter.h" +#include "art/Framework/Core/ModuleMacros.h" +#include "art/Framework/Principal/Event.h" +#include "art/Framework/Principal/Handle.h" +#include "art/Framework/Principal/Run.h" +#include "art/Framework/Principal/SubRun.h" +#include "canvas/Utilities/InputTag.h" +#include "fhiclcpp/ParameterSet.h" +#include "messagefacility/MessageLogger/MessageLogger.h" +#include "lardataobj/Simulation/SimChannel.h" + +#include + +namespace sbn { + class FilterPrimarySimChannel; +} + +class sbn::FilterPrimarySimChannel : public art::EDFilter { +public: + explicit FilterPrimarySimChannel(fhicl::ParameterSet const& p); + // The compiler-generated destructor is fine for non-base + // classes without bare pointers or other resource use. + + // Plugins should not be copied or assigned. + FilterPrimarySimChannel(FilterPrimarySimChannel const&) = delete; + FilterPrimarySimChannel(FilterPrimarySimChannel&&) = delete; + FilterPrimarySimChannel& operator=(FilterPrimarySimChannel const&) = delete; + FilterPrimarySimChannel& operator=(FilterPrimarySimChannel&&) = delete; + + // Required functions. + bool filter(art::Event& e) override; + +private: + + // Declare member data here. + art::InputTag fSimChannelLabel; + std::vector fTrackIDs; + bool fVerbose; +}; + + +sbn::FilterPrimarySimChannel::FilterPrimarySimChannel(fhicl::ParameterSet const& p) + : EDFilter{p}, + fSimChannelLabel(p.get("SimChannelLabel")), + fTrackIDs(p.get>("TrackIDs", {1})), + fVerbose(p.get("Verbose", true)) +{} + +bool sbn::FilterPrimarySimChannel::filter(art::Event& e) { + art::ValidHandle> simchan = e.getValidHandle>(fSimChannelLabel); + + for (const sim::SimChannel &c: *simchan) { + const sim::SimChannel::TDCIDEs_t &tides = c.TDCIDEMap(); + + for (const sim::TDCIDE &tide: tides) { + for (const sim::IDE &ide: tide.second) { + if (fVerbose) std::cout << "ide at tdc: " << tide.first << " id: " << ide.trackID << " x: " << ide.x << " y: " << ide.y << " z: " << ide.z << " nelec: " << ide.numElectrons << std::endl; + for (int tid: fTrackIDs) { + if (ide.trackID == tid) { + if (fVerbose) std::cout << "Found matching sim channel for track ID: " << tid << std::endl; + return true; + } + } + } + } + } + + if (fVerbose) std::cout << "No matching sim channel." << std::endl; + + return false; +} + +DEFINE_ART_MODULE(sbn::FilterPrimarySimChannel) diff --git a/sbncode/EventGenerator/WireModGen/FilterPrimarySimEnergyDepo_module.cc b/sbncode/EventGenerator/WireModGen/FilterPrimarySimEnergyDepo_module.cc new file mode 100644 index 000000000..0ac810055 --- /dev/null +++ b/sbncode/EventGenerator/WireModGen/FilterPrimarySimEnergyDepo_module.cc @@ -0,0 +1,120 @@ +//////////////////////////////////////////////////////////////////////// +// Class: FilterPrimarySimEnergyDepo +// Plugin Type: filter (art v3_05_01) +// File: FilterPrimarySimEnergyDepo_module.cc +// +// Module for selecting events based on the presence of data products. +// +// Useful for processing data events, which sometimes have missing +// data products. +//////////////////////////////////////////////////////////////////////// + +#include "art/Framework/Core/EDFilter.h" +#include "art/Framework/Core/ModuleMacros.h" +#include "art/Framework/Principal/Event.h" +#include "art/Framework/Principal/Handle.h" +#include "art/Framework/Principal/Run.h" +#include "art/Framework/Principal/SubRun.h" +#include "canvas/Utilities/InputTag.h" +#include "fhiclcpp/ParameterSet.h" +#include "messagefacility/MessageLogger/MessageLogger.h" +#include "lardataobj/Simulation/SimEnergyDeposit.h" +#include "larcorealg/Geometry/GeometryCore.h" +#include "larcore/Geometry/Geometry.h" +#include "larcore/CoreUtils/ServiceUtil.h" +#include "lardataalg/DetectorInfo/DetectorPropertiesStandard.h" +#include "lardata/DetectorInfoServices/DetectorPropertiesService.h" +#include "lardataalg/DetectorInfo/DetectorClocksStandard.h" +#include "lardata/DetectorInfoServices/DetectorClocksService.h" + +#include + +namespace sbn { + class FilterPrimarySimEnergyDepo; +} + +class sbn::FilterPrimarySimEnergyDepo : public art::EDFilter { +public: + explicit FilterPrimarySimEnergyDepo(fhicl::ParameterSet const& p); + // The compiler-generated destructor is fine for non-base + // classes without bare pointers or other resource use. + + // Plugins should not be copied or assigned. + FilterPrimarySimEnergyDepo(FilterPrimarySimEnergyDepo const&) = delete; + FilterPrimarySimEnergyDepo(FilterPrimarySimEnergyDepo&&) = delete; + FilterPrimarySimEnergyDepo& operator=(FilterPrimarySimEnergyDepo const&) = delete; + FilterPrimarySimEnergyDepo& operator=(FilterPrimarySimEnergyDepo&&) = delete; + + // Required functions. + bool filter(art::Event& e) override; + +private: + + // Declare member data here. + art::InputTag fSimEnergyDepoLabel; + std::vector fTrackIDs; + bool fVerbose; +}; + + +sbn::FilterPrimarySimEnergyDepo::FilterPrimarySimEnergyDepo(fhicl::ParameterSet const& p) + : EDFilter{p}, + fSimEnergyDepoLabel(p.get("SimEnergyDepoLabel")), + fTrackIDs(p.get>("TrackIDs", {1})), + fVerbose(p.get("Verbose", true)) +{} + +bool sbn::FilterPrimarySimEnergyDepo::filter(art::Event& e) { + art::ValidHandle> simdepo = e.getValidHandle>(fSimEnergyDepoLabel); + + const geo::GeometryCore *geometry = lar::providerFrom(); + auto const &clock_data = art::ServiceHandle()->DataFor(e); + auto const &dprop = + art::ServiceHandle()->DataFor(e, clock_data); + + for (const sim::SimEnergyDeposit &d: *simdepo) { + if (fVerbose) std::cout << "depo at time: " << d.T() << "x: " << d.X() << " y: " << d.Y() << " z: " << d.Z() << " nelec: " << d.NumElectrons() << " id: " << d.TrackID() << std::endl; + + // check if has correct ID, then check if fiducial and in-time + int match_index = -1; + for (unsigned i = 0; i < fTrackIDs.size(); i++) { + if (d.TrackID() == fTrackIDs[i]) { + match_index = (int)i; + } + } + if (match_index == -1) continue; + + for (auto const &cryo: geometry->Iterate()) { + for (auto const& TPC : geometry->Iterate(cryo.ID())) { + if (TPC.ContainsPosition(d.MidPoint())) { + // check drifted point in time + + geo::PlaneID plane(TPC.ID(), 0); // check against front plane + + // get the tick of the point. Subtract out the trigger time, which gets put into t_tick below + double tick = dprop.ConvertXToTicks(d.X(), plane); + + // convert the gen time to a TDC + double t_tdc = clock_data.TPCTick2TDC(clock_data.Time2Tick(d.T()/1e3)); + + // add in the offset from the drift + double x_tick = t_tdc + tick; + + if (fVerbose) std::cout << "Gen time: " << d.T() << " is at TPC tdc: " << t_tdc << ". After drift from X=" << d.X() << " arrival tick is: " << x_tick << ". TPC window is 0 to " << dprop.NumberTimeSamples() << std::endl; + + if ((x_tick > 0) && (x_tick < dprop.NumberTimeSamples())) { + if (fVerbose) std::cout << "Found matching deposit for track ID: " << fTrackIDs[match_index] << std::endl; + return true; + } + + } + } + } + } + + if (fVerbose) std::cout << "No matching deposit." << std::endl; + + return false; +} + +DEFINE_ART_MODULE(sbn::FilterPrimarySimEnergyDepo) diff --git a/sbncode/EventGenerator/WireModGen/WireModMuon_module.cc b/sbncode/EventGenerator/WireModGen/WireModMuon_module.cc new file mode 100644 index 000000000..ae326c1b4 --- /dev/null +++ b/sbncode/EventGenerator/WireModGen/WireModMuon_module.cc @@ -0,0 +1,322 @@ +//////////////////////////////////////////////////////////////////////// +///// \file WireModMuon_module.cc +///// \brief Generates one muon hitting a bounding box, for use by WireMod +///// +///// \author gputnam@fnal.gov +////////////////////////////////////////////////////////////////////////// + +#include "sbncode/EventGenerator/WireModGen/CORSIKAGenSBN.h" + +#include "larcore/CoreUtils/ServiceUtil.h" +#include "larcoreobj/SimpleTypesAndConstants/geo_vectors.h" +#include "lardataalg/DetectorInfo/DetectorPropertiesStandard.h" +#include "lardata/DetectorInfoServices/DetectorPropertiesService.h" +#include "lardataalg/DetectorInfo/DetectorClocksStandard.h" +#include "lardata/DetectorInfoServices/DetectorClocksService.h" + +namespace sbn { + +class WireModMuon: public evgen::CORSIKAGenSBN { + public: + void produce(art::Event& evt) override; + void endRun(art::Run& run) override; + explicit WireModMuon(fhicl::ParameterSet const& pset); + + private: + bool GetMuon(const art::Event &e, const simb::MCTruth &intruth, simb::MCParticle &outparticle); + // Determine if particle ray should be selected + bool SelectRay(const art::Event &e, const TLorentzVector &pos, const TLorentzVector &mom); + // Whether a point is in the TPC readout time + bool PointInTime(const art::Event &e, const geo::TPCID &tpcid, const TVector3 &p, float t); + + struct Plane { + std::array pts; + + bool Intersects(const TVector3 &pos, const TVector3 &dir) const; + bool Intersects(const geo::Point_t &pos, const geo::Vector_t &dir) const; + }; + + // Digested geometry information + std::vector crt_planes; + std::vector tpc_bounds; + + // Enable for debugging output + bool fVerbose; + + // Selection configuration + bool fSelectCXR; + bool fSelectCRT; + unsigned fPrescaleCXR; + unsigned fPrescaleCRT; + unsigned fNMuonPerEvent; + + // Counters + unsigned fNCXR; + unsigned fNCRT; + + unsigned fNSelCXR; + unsigned fNSelCRT; + + unsigned fNCORSIKA; +}; + +} + +sbn::WireModMuon::WireModMuon(fhicl::ParameterSet const& pset): + CORSIKAGenSBN(pset) +{ + + // Load the CRT planes + std::vector crt_x0s = pset.get>("CRTX0", {}); + std::vector crt_x1s = pset.get>("CRTX1", {}); + std::vector crt_y0s = pset.get>("CRTY0", {}); + std::vector crt_y1s = pset.get>("CRTY1", {}); + std::vector crt_z0s = pset.get>("CRTZ0", {}); + std::vector crt_z1s = pset.get>("CRTZ1", {}); + + for (unsigned i_crt = 0; i_crt < crt_x0s.size(); i_crt++) { + std::array ps; + + if (crt_x0s[i_crt] == crt_x1s[i_crt] || crt_y0s[i_crt] == crt_y1s[i_crt]) { + geo::Point_t p1 {crt_x0s[i_crt], crt_y0s[i_crt], crt_z0s[i_crt]}; + geo::Point_t p2 {crt_x1s[i_crt], crt_y1s[i_crt], crt_z0s[i_crt]}; + geo::Point_t p3 {crt_x0s[i_crt], crt_y0s[i_crt], crt_z1s[i_crt]}; + geo::Point_t p4 {crt_x1s[i_crt], crt_y1s[i_crt], crt_z1s[i_crt]}; + ps = {p1, p2, p3, p4}; + } + else { + geo::Point_t p1 {crt_x0s[i_crt], crt_y0s[i_crt], crt_z0s[i_crt]}; + geo::Point_t p2 {crt_x0s[i_crt], crt_y1s[i_crt], crt_z0s[i_crt]}; + geo::Point_t p3 {crt_x1s[i_crt], crt_y0s[i_crt], crt_z1s[i_crt]}; + geo::Point_t p4 {crt_x1s[i_crt], crt_y1s[i_crt], crt_z1s[i_crt]}; + ps = {p1, p2, p3, p4}; + } + + sbn::WireModMuon::Plane crt_plane {ps}; + crt_planes.push_back(crt_plane); + } + + // Load selection config + fSelectCXR = pset.get("SelectCXR", true); + fSelectCRT = pset.get("SelectCRT", true); + + fPrescaleCXR = pset.get("PrescaleCXR", 1); + fPrescaleCRT = pset.get("PrescaleCRT", 1); + + fNMuonPerEvent = pset.get("NMuonPerEvent", 1); + + fNCXR = 0; + fNCRT = 0; + + fNSelCXR = 0; + fNSelCRT = 0; + + fNCORSIKA = 0; + + // debug output config + fVerbose = pset.get("Verbose", true); + +} + +void sbn::WireModMuon::endRun(art::Run& run) { + (void) run; + + mf::LogInfo("WireModMuon") << "Selected " << fNSelCXR << " of " << fNCXR << " cathode crossers and " << fNSelCRT << " of " << fNCRT << " CRT plane crossers. Called CORSIKA " << fNCORSIKA << " times.\n"; +} + +void sbn::WireModMuon::produce(art::Event& evt) { + // output + std::unique_ptr> truthcol(new std::vector); + + simb::MCTruth truth; + truth.SetOrigin(simb::kCosmicRay); + + for (unsigned i_muon = 0; i_muon < fNMuonPerEvent; i_muon++) { + // found a muon + bool foundmuon = false; + simb::MCParticle particle; + + while (!foundmuon) { + simb::MCTruth pretruth; + GetSample(pretruth); + fNCORSIKA ++; + + foundmuon = GetMuon(evt, pretruth, particle); + } + + mf::LogInfo("WireModMuon") << "Selected muon at pos(" << particle.Vx() << ", " << particle.Vy() << ", " << particle.Vz() << ") [cm], dir(" + << particle.Momentum().Vect().Unit().X() << ", " << particle.Momentum().Vect().Unit().Y() << ", " << particle.Momentum().Vect().Unit().Z() + << "), with momentum " << particle.Momentum().Vect().Mag() << " [GeV/c].\n"; + + truth.Add(particle); + } + + // save output + truthcol->push_back(truth); + evt.put(std::move(truthcol)); + + return; +} + +bool sbn::WireModMuon::PointInTime(const art::Event &e, const geo::TPCID &tpcid, const TVector3 &p, float t) { + auto const &clock_data = art::ServiceHandle()->DataFor(e); + auto const &dprop = + art::ServiceHandle()->DataFor(e, clock_data); + + if (fVerbose) std::cout << "Point: " << p.X() << " " << p.Y() << " " << p.Z() << " is in TPC: " << tpcid << std::endl; + + if (!tpcid) return false; + + geo::PlaneID plane(tpcid, 0); // check against front plane + + // get the tick of the point. Subtract out the trigger time, which gets put into t_tick below + double tick = dprop.ConvertXToTicks(p.X(), plane); + + // convert the gen time to a TDC + double t_tdc = clock_data.TPCTick2TDC(clock_data.Time2Tick(t/1e3)); + + // add in the offset from the drift + double x_tick = t_tdc + tick; + + if (fVerbose) std::cout << "Gen time: " << t << " is at TPC tdc: " << t_tdc << ". After drift from X=" << p.X() << " arrival tick is: " << x_tick << ". TPC window is 0 to " << dprop.NumberTimeSamples() << std::endl; + + // this needs to be inside the tpc window + return (x_tick > 0) && (x_tick < dprop.NumberTimeSamples()); +} + +bool sbn::WireModMuon::SelectRay(const art::Event &e, const TLorentzVector &pos, const TLorentzVector &mom) { + // load services + const geo::GeometryCore *geometry = lar::providerFrom(); + + bool CXR = false; + bool CRT = false; + + TVector3 dir = mom.Vect().Unit(); + TVector3 pos3 = pos.Vect(); + const double c_cm_per_ns = 29.9792; + double vel = mom.Beta()*c_cm_per_ns; + const double eps = 1e-1; // wiggle room to ensure inside volume + + for (auto const &cryo: geometry->Iterate()) { + for (auto const& TPC : geometry->Iterate(cryo.ID())) { + std::vector intersections = TPC.ActiveBoundingBox().GetIntersections(pos3, dir); + const double check_eps = 1e-1; + float cathode = (TPC.DriftDir().X() > 0) ? TPC.ActiveBoundingBox().MinX() : TPC.ActiveBoundingBox().MaxX(); + CXR = intersections.size() == 2 && ((abs(intersections[0].X() - cathode) < check_eps) || (abs(intersections[1].X() - cathode) < check_eps)); + + // check if cathode point is in-time + if (CXR) { + TVector3 cathode_point = (abs(intersections[0].X() - cathode) < abs(intersections[1].X() - cathode)) ? (intersections[0] + dir*eps) : (intersections[1] - dir*eps); + + if (fVerbose) std::cout << "Ray with initial time: " << pos.T() << " and arrival time: " << (pos.T() + cathode_point.Mag() / vel) << " with vel: " << vel << std::endl; + if (fVerbose) std::cout << "Checking if cathode intersection point: " << cathode_point.X() << " " << cathode_point.Y() << " " << cathode_point.Z() << " is in time." << std::endl; + CXR = PointInTime(e, TPC.ID(), cathode_point, pos.T() + (cathode_point - pos3).Mag() / vel); + } + if (CXR) break; + } + if (CXR) break; + } + if (fVerbose) std::cout << "CXR: " << CXR << std::endl; + + for (const sbn::WireModMuon::Plane &crt: crt_planes) { + CRT = crt.Intersects(pos3, dir); + if (CRT) { + // require intersect active volume + // + // and require either entrance or exit point to be in-time + for (auto const &cryo: geometry->Iterate()) { + for (auto const& TPC : geometry->Iterate(cryo.ID())) { + std::vector intersections = TPC.ActiveBoundingBox().GetIntersections(pos3, dir); + CRT = intersections.size() == 2; + if (CRT) { + if (fVerbose) std::cout << "Ray with initial time: " << pos.T() << " and arrival time: " << (pos.T() + (intersections[0] - pos3).Mag() / vel) << " with vel: " << vel << std::endl; + CRT = PointInTime(e, TPC.ID(), intersections[0] + dir*eps, pos.T() + (intersections[0] - pos3).Mag() / vel) || + PointInTime(e, TPC.ID(), intersections[1] - dir*eps, pos.T() + (intersections[1] - pos3).Mag() / vel); + } + if (CRT) break; + } + if (CRT) break; + } + } + if (CRT) break; + } + if (fVerbose) std::cout << "CRT: " << CRT << std::endl; + + if (CXR) fNCXR ++; + if (CRT) fNCRT ++; + + if (CXR && fSelectCXR && (fNCXR % fPrescaleCXR == 0)) { + fNSelCXR ++; + return true; + } + + if (CRT && fSelectCRT && (fNCRT % fPrescaleCRT == 0)) { + fNSelCRT ++; + return true; + } + + return false; + +} + +bool sbn::WireModMuon::GetMuon(const art::Event &e, const simb::MCTruth &intruth, simb::MCParticle &outparticle) { + for (int i = 0; i < intruth.NParticles(); ++i) { + const simb::MCParticle &particle = intruth.GetParticle(i); + if (abs(particle.PdgCode()) != 13) continue; // only muons + + if (fVerbose) std::cout << "Found muon at pos(" << particle.Vx() << ", " << particle.Vy() << ", " << particle.Vz() << ") [cm], dir(" + << particle.Momentum().Vect().Unit().X() << ", " << particle.Momentum().Vect().Unit().Y() << ", " << particle.Momentum().Vect().Unit().Z() + << "), with momentum " << particle.Momentum().Vect().Mag() << " [GeV/c].\n"; + + if (SelectRay(e, particle.Position(), particle.Momentum())) { + outparticle = particle; + return true; + } + } + + return false; +} + +bool sbn::WireModMuon::Plane::Intersects(const TVector3 &pos, const TVector3 &dir) const { + geo::Point_t pos_t(pos.X(), pos.Y(), pos.Z()); + geo::Vector_t dir_t(dir.X(), dir.Y(), dir.Z()); + return Intersects(pos_t, dir_t); +} + +bool sbn::WireModMuon::Plane::Intersects(const geo::Point_t &pos, const geo::Vector_t &dir) const { + // Get the u, v vectors + geo::Vector_t u = (geo::Vector_t(pts[1]) - geo::Vector_t(pts[0])).Unit(); + geo::Vector_t v = (geo::Vector_t(pts[2]) - geo::Vector_t(pts[0])).Unit(); + double ulen = (geo::Vector_t(pts[1]) - geo::Vector_t(pts[0])).Dot(u); + double vlen = (geo::Vector_t(pts[2]) - geo::Vector_t(pts[0])).Dot(v); + + // calculate the normal of the plane + geo::Vector_t normal = u.Cross(v); + + double denom = dir.Dot(normal); + + if (abs(denom) < 1e-5) { // parallel to plane + return false; + } + + // point along ray where it intersects plane + double t = (pts[3] - pos).Dot(normal) / denom; + + // intersection happens behind the ray + if (t < 0) { + return false; + } + + geo::Vector_t intersection = pos + dir*t - pts[0]; + + double int_u = intersection.Dot(u); + double int_v = intersection.Dot(v); + + // if (fVerbose) std::cout << "Plane check. ulen: " << ulen << " vlen: " << vlen << ". u: " << int_u << " v: " << int_v << " t: " << t << std::endl; + + return (int_u > 0) && (int_u < ulen) && (int_v > 0) && (int_v < vlen); +} + +namespace sbn { + DEFINE_ART_MODULE(WireModMuon) +} diff --git a/sbncode/EventGenerator/WireModGen/filter_wiremod_sbn.fcl b/sbncode/EventGenerator/WireModGen/filter_wiremod_sbn.fcl new file mode 100644 index 000000000..e04c0f49c --- /dev/null +++ b/sbncode/EventGenerator/WireModGen/filter_wiremod_sbn.fcl @@ -0,0 +1,9 @@ +BEGIN_PROLOG + +filter_primary_simchan: { + module_type: FilterPrimarySimEnergyDepo + SimEnergyDepoLabel: ionization + TrackIDs: [1, 2, 3] +} + +END_PROLOG diff --git a/sbncode/EventGenerator/WireModGen/wiremodmuon_sbn.fcl b/sbncode/EventGenerator/WireModGen/wiremodmuon_sbn.fcl new file mode 100644 index 000000000..9ab3ebf5d --- /dev/null +++ b/sbncode/EventGenerator/WireModGen/wiremodmuon_sbn.fcl @@ -0,0 +1,26 @@ +BEGIN_PROLOG + +# needs the corsika config to be added to the table, inside the experimental codebase +wiremodmuon_icarus: { + module_type: WireModMuon + + # CRT bounds + # + # Each entry (in X0, X1, Y0, Y1, Z0, Z1) is one plane that will be included in the intersection check + # + # listed as: Top horizontal, Top Rim West, Top Rim East, Top Rim South + CRTX0: [-580, 555, -555, -560, -560] + CRTX1: [580, 555, -555, 380, 560] + CRTY0: [617.4, 400, 400, 400, 420] + CRTY1: [617.4, 600, 600, 600, 620] + CRTZ0: [-1180, -1180, -1180, -1143, 1533] + CRTZ1: [1460, 1460, 1460, -1143, 1533] + + # selection config + SelectA2C: true + SelectCRT: true + PrescaleA2C: 1 + PrescaleCRT: 1 + NMuonPerEvent: 3 +} +END_PROLOG diff --git a/sbncode/HitFinder/BadChannelHitCheck_module.cc b/sbncode/HitFinder/BadChannelHitCheck_module.cc new file mode 100644 index 000000000..6ff63fe28 --- /dev/null +++ b/sbncode/HitFinder/BadChannelHitCheck_module.cc @@ -0,0 +1,127 @@ +//////////////////////////////////////////////////////////////////////// +// Class: BadChannelHitCheck +// Plugin Type: analyzer +// File: BadChannelHitCheck_module.cc +// +// Validation analyzer for the GaussHitFinderSBN "ExcludeBadChannels" +// feature. For each configured recob::Hit collection it checks every +// hit's channel against the channel-status database service; if any hit +// sits on a channel the database flags as bad, the art job is failed +// (by throwing). This lets a ctest verify end-to-end that bad channels +// are excluded from the hit finder output. +//////////////////////////////////////////////////////////////////////// + +#include "art/Framework/Core/EDAnalyzer.h" +#include "art/Framework/Core/ModuleMacros.h" +#include "art/Framework/Principal/Event.h" +#include "art/Framework/Services/Registry/ServiceHandle.h" +#include "canvas/Utilities/Exception.h" +#include "canvas/Utilities/InputTag.h" +#include "fhiclcpp/types/Atom.h" +#include "fhiclcpp/types/Sequence.h" +#include "messagefacility/MessageLogger/MessageLogger.h" + +#include "larcoreobj/SimpleTypesAndConstants/RawTypes.h" // raw::ChannelID_t +#include "lardataobj/RecoBase/Hit.h" +#include "larevt/CalibrationDBI/Interface/ChannelStatusService.h" +#include "larevt/CalibrationDBI/Interface/ChannelStatusProvider.h" + +#include + +namespace sbn { + + class BadChannelHitCheck : public art::EDAnalyzer { + public: + struct Config { + fhicl::Sequence HitLabels{ + fhicl::Name("HitLabels"), + fhicl::Comment("recob::Hit collections to validate against the bad-channel database")}; + fhicl::Atom RequirePresent{ + fhicl::Name("RequirePresent"), + fhicl::Comment("Also fail when a hit lands on a channel the database does not list as present"), + false}; + fhicl::Atom ThrowOnFailure{ + fhicl::Name("ThrowOnFailure"), + fhicl::Comment("Throw on the first bad-channel hit; if false, collect and throw in endJob()"), + true}; + }; + using Parameters = art::EDAnalyzer::Table; + + explicit BadChannelHitCheck(Parameters const& p); + + void analyze(art::Event const& e) override; + void endJob() override; + + private: + std::vector const fHitLabels; + bool const fRequirePresent; + bool const fThrowOnFailure; + + unsigned long fNHits{0}; ///< total hits checked + unsigned long fNBad{0}; ///< hits found on bad/not-present channels + bool fReportedStatus{false}; ///< whether the DB bad-channel count was logged + }; + + //------------------------------------------------------------------- + BadChannelHitCheck::BadChannelHitCheck(Parameters const& p) + : art::EDAnalyzer{p} + , fHitLabels{p().HitLabels()} + , fRequirePresent{p().RequirePresent()} + , fThrowOnFailure{p().ThrowOnFailure()} + {} + + //------------------------------------------------------------------- + void BadChannelHitCheck::analyze(art::Event const& e) + { + lariov::ChannelStatusProvider const& chanStatus = + art::ServiceHandle()->GetProvider(); + + if (!fReportedStatus) { + fReportedStatus = true; + mf::LogInfo("BadChannelHitCheck") + << "channel-status database reports " << chanStatus.BadChannels().size() + << " bad and " << chanStatus.NoisyChannels().size() << " noisy channel(s)."; + } + + for (art::InputTag const& tag : fHitLabels) { + auto const& hits = e.getProduct>(tag); + + for (recob::Hit const& hit : hits) { + raw::ChannelID_t const channel = hit.Channel(); + ++fNHits; + + bool const isBad = chanStatus.IsBad(channel); + bool const isAbsent = fRequirePresent && !chanStatus.IsPresent(channel); + if (!isBad && !isAbsent) continue; + + ++fNBad; + mf::LogError("BadChannelHitCheck") + << "recob::Hit on " << (isBad ? "BAD" : "NOT-PRESENT") << " channel " << channel + << " in product '" << tag.encode() << "', event " << e.id(); + + if (fThrowOnFailure) { + throw art::Exception(art::errors::LogicError) + << "BadChannelHitCheck: recob::Hit found on " + << (isBad ? "bad" : "not-present") << " channel " << channel + << " (product '" << tag.encode() << "', event " << e.id() << ")\n"; + } + } + } + } + + //------------------------------------------------------------------- + void BadChannelHitCheck::endJob() + { + mf::LogInfo("BadChannelHitCheck") + << "Checked " << fNHits << " hit(s); " << fNBad << " on bad/not-present channels."; + + if (fNBad > 0) { + throw art::Exception(art::errors::LogicError) + << "BadChannelHitCheck FAILED: " << fNBad << " of " << fNHits + << " hit(s) are on bad/not-present channels.\n"; + } + } + +} // namespace sbn + +DEFINE_ART_MODULE(sbn::BadChannelHitCheck) diff --git a/sbncode/HitFinder/CMakeLists.txt b/sbncode/HitFinder/CMakeLists.txt index 1d4870cef..dc1e13975 100644 --- a/sbncode/HitFinder/CMakeLists.txt +++ b/sbncode/HitFinder/CMakeLists.txt @@ -50,6 +50,7 @@ cet_build_plugin(GaussHitFinderSBN art::module ) cet_build_plugin(ChannelROIToWire art::module LIBRARIES ${MODULE_LIBRARIES}) cet_build_plugin(WireToChannelROI art::module LIBRARIES ${MODULE_LIBRARIES}) +cet_build_plugin(BadChannelHitCheck art::module LIBRARIES ${MODULE_LIBRARIES}) install_headers() diff --git a/sbncode/HitFinder/ChannelROIToWire_module.cc b/sbncode/HitFinder/ChannelROIToWire_module.cc index 42d304537..41d29ccb6 100644 --- a/sbncode/HitFinder/ChannelROIToWire_module.cc +++ b/sbncode/HitFinder/ChannelROIToWire_module.cc @@ -51,6 +51,7 @@ class ChannelROIToWire : public art::EDProducer std::vector fWireModuleLabelVec; ///< vector of modules that made digits std::vector fOutInstanceLabelVec; ///< The output instance labels to apply bool fDiagnosticOutput; ///< secret diagnostics flag + bool fUseFloatPrecision; ///< use SignalROIF() to preserve sub-ADC precision (no rounding) size_t fEventCount; ///< count of event processed const geo::WireReadoutGeom* fChannelMapAlg = &art::ServiceHandle()->Get(); @@ -77,6 +78,7 @@ void ChannelROIToWire::reconfigure(fhicl::ParameterSet const& pset) fWireModuleLabelVec = pset.get>("WireModuleLabelVec", std::vector()={"decon1droi"}); fOutInstanceLabelVec = pset.get> ("OutInstanceLabelVec", {"PHYSCRATEDATA"}); fDiagnosticOutput = pset.get< bool >("DiagnosticOutput", false); + fUseFloatPrecision = pset.get< bool >("UseFloatPrecision", false); if (fWireModuleLabelVec.size() != fOutInstanceLabelVec.size()) { @@ -131,19 +133,26 @@ void ChannelROIToWire::produce(art::Event& evt) // Create an ROI vector for output recob::Wire::RegionsOfInterest_t ROIVec; - - // Loop through the ROIs for this channel - const recob::ChannelROI::RegionsOfInterest_t& channelROIs = channelROI.SignalROI(); - for(const auto& range : channelROIs.get_ranges()) + if (fUseFloatPrecision) + { + const recob::ChannelROI::RegionsOfInterest_f& channelROIsF = channelROI.SignalROIF(); + for(const auto& range : channelROIsF.get_ranges()) + ROIVec.add_range(range.begin_index(), range.data()); + } + else { - size_t startTick = range.begin_index(); + const recob::ChannelROI::RegionsOfInterest_t& channelROIs = channelROI.SignalROI(); + for(const auto& range : channelROIs.get_ranges()) + { + size_t startTick = range.begin_index(); - std::vector dataVec(range.data().size()); + std::vector dataVec(range.data().size()); - for(size_t binIdx = 0; binIdx < range.data().size(); binIdx++) dataVec[binIdx] = std::round(range.data()[binIdx] / ADCScaleFactor); + for(size_t binIdx = 0; binIdx < range.data().size(); binIdx++) dataVec[binIdx] = range.data()[binIdx] / ADCScaleFactor; - ROIVec.add_range(startTick, std::move(dataVec)); + ROIVec.add_range(startTick, std::move(dataVec)); + } } wireCol->push_back(recob::WireCreator(std::move(ROIVec),channel,view).move()); diff --git a/sbncode/HitFinder/GaussHitFinderSBN_module.cc b/sbncode/HitFinder/GaussHitFinderSBN_module.cc index c0a91cc91..8b404146d 100644 --- a/sbncode/HitFinder/GaussHitFinderSBN_module.cc +++ b/sbncode/HitFinder/GaussHitFinderSBN_module.cc @@ -45,6 +45,8 @@ #include "larcoreobj/SimpleTypesAndConstants/RawTypes.h" // raw::ChannelID_t #include "larreco/HitFinder/HitFilterAlg.h" #include "lardataobj/RecoBase/Hit.h" +#include "larevt/CalibrationDBI/Interface/ChannelStatusService.h" +#include "larevt/CalibrationDBI/Interface/ChannelStatusProvider.h" #include "sbnobj/ICARUS/TPC/ChannelROI.h" #include "sbncode/HitFinder/HitFinderUtilities/HitCreator.h" @@ -72,6 +74,7 @@ namespace hit { const bool fFilterHits; const bool fFillHists; + const bool fExcludeBadChannels; ///< skip input ChannelROIs on database-flagged bad channels const std::string fCalDataModuleLabel; const std::string fAllHitsInstanceName; @@ -99,6 +102,9 @@ namespace hit { //HitFilterAlg implementation is threadsafe. std::unique_ptr fHitFilterAlg; ///< algorithm used to filter out noise hits + /// Bad-channel database provider; non-null only when fExcludeBadChannels is true. + const lariov::ChannelStatusProvider* fChannelStatus = nullptr; + //only used when fFillHists is true and in single threaded mode. TH1F* fFirstChi2; TH1F* fChi2; @@ -111,6 +117,7 @@ namespace hit { : SharedProducer{pset} , fFilterHits(pset.get("FilterHits", false)) , fFillHists(pset.get("FillHists", false)) + , fExcludeBadChannels(pset.get("ExcludeBadChannels", false)) , fCalDataModuleLabel(pset.get("CalDataModuleLabel")) , fAllHitsInstanceName(pset.get("AllHitsInstanceName", "")) , fLongMaxHitsVec(pset.get>("LongMaxHits", std::vector() = {25, 25, 25})) @@ -127,6 +134,10 @@ namespace hit { , fPulseRatioCuts( pset.get>("PulseRatioCuts", std::vector() = {0.35, 0.40, 0.20})) { + // Cache the bad-channel database provider when channel exclusion is enabled. + if (fExcludeBadChannels) + fChannelStatus = &art::ServiceHandle()->GetProvider(); + if (fFillHists && art::Globals::instance()->nthreads() > 1u) { throw art::Exception(art::errors::Configuration) << "Cannot fill histograms when multiple threads configured, please set fFillHists to " @@ -223,6 +234,13 @@ namespace hit { auto const& wireReadoutGeom = art::ServiceHandle()->Get(); + // When bad-channel exclusion is enabled, snapshot the set of channels the + // status database flags as bad once here (single-threaded). The parallel + // loop below then cheaply skips any input ChannelROI on those channels. + const lariov::ChannelStatusProvider::ChannelSet_t badChannels = + fExcludeBadChannels ? fChannelStatus->BadChannels() + : lariov::ChannelStatusProvider::ChannelSet_t{}; + // ############################################### // ### Making a ptr vector to put on the event ### // ############################################### @@ -295,6 +313,9 @@ namespace hit { raw::ChannelID_t channel = channelROI->Channel(); + // Skip channels flagged as bad in the channel-status database. + if (fExcludeBadChannels && badChannels.count(channel)) return; + // get the WireID for this hit std::vector wids = wireReadoutGeom.ChannelToWire(channel); // for now, just take the first option returned from ChannelToWire diff --git a/sbncode/SBNEventWeight/App/CMakeLists.txt b/sbncode/SBNEventWeight/App/CMakeLists.txt index 2989659b7..335a19557 100644 --- a/sbncode/SBNEventWeight/App/CMakeLists.txt +++ b/sbncode/SBNEventWeight/App/CMakeLists.txt @@ -1,8 +1,11 @@ cet_build_plugin(SBNEventWeight art::module LIBRARIES - sbnobj::Common_SBNEventWeight sbncode_SBNEventWeight_Base + sbnobj::Common_SBNEventWeight + sbncode_SBNEventWeight_Base sbncode_SBNEventWeight_Calculators_CrossSection - sbncode_SBNEventWeight_Calculators_BNBFlux nusimdata::SimulationBase + sbncode_SBNEventWeight_Calculators_BNBFlux + sbncode_SBNEventWeight_Calculators_Geant4 + nusimdata::SimulationBase ROOT::Geom) cet_build_plugin(SystToolsEventWeight art::module diff --git a/sbncode/SBNEventWeight/CMakeLists.txt b/sbncode/SBNEventWeight/CMakeLists.txt index de59387f6..9f4018c28 100644 --- a/sbncode/SBNEventWeight/CMakeLists.txt +++ b/sbncode/SBNEventWeight/CMakeLists.txt @@ -1,3 +1,6 @@ +include_directories ( $ENV{GEANT4REWEIGHT_INC} ) +link_directories ( $ENV{GEANT4REWEIGHT_LIB} ) + add_subdirectory(Base) add_subdirectory(Calculators) add_subdirectory(App) diff --git a/sbncode/SBNEventWeight/Calculators/CMakeLists.txt b/sbncode/SBNEventWeight/Calculators/CMakeLists.txt index e2080685f..383d41a3a 100644 --- a/sbncode/SBNEventWeight/Calculators/CMakeLists.txt +++ b/sbncode/SBNEventWeight/Calculators/CMakeLists.txt @@ -1,4 +1,4 @@ add_subdirectory(CrossSections) add_subdirectory(BNBFlux) -#add_subdirectory(Geant4) +add_subdirectory(Geant4) diff --git a/sbncode/SBNEventWeight/Calculators/Geant4/BetheBlochForG4ReweightValid.h b/sbncode/SBNEventWeight/Calculators/Geant4/BetheBlochForG4ReweightValid.h new file mode 100644 index 000000000..58cfdc99e --- /dev/null +++ b/sbncode/SBNEventWeight/Calculators/Geant4/BetheBlochForG4ReweightValid.h @@ -0,0 +1,145 @@ + + +double BetheBloch(double energy, double mass){ + + //Need to make this configurable? Or delete... + double K = .307075; + double rho = 1.390; + double Z = 18; + double A = 40; + double I = 188E-6; + double me = .511; + //Need to make sure this is total energy, not KE + double gamma = energy/mass; + double beta = sqrt( 1. - (1. / (gamma*gamma)) ); + double Tmax = 2 * me * beta*beta * gamma*gamma; + + double first = K * (Z/A) * rho / (beta*beta); + double second = .5 * log(Tmax*Tmax/(I*I)) - beta*beta; + + double dEdX = first*second; + return dEdX; +} + +std::vector< std::pair > ThinSliceBetheBloch(G4ReweightTraj * theTraj, double res, double mass, bool isElastic){ + + std::vector< std::pair > result; + + //First slice position +// double sliceEdge = res; +// double lastPos = 0.; +// double nextPos = 0.; +// double px,py,pz; + int interactInSlice = 0; + + //Get total distance traveled in z +// double totalDeltaZ = 0.; + double disp = 0.; +// double oldDisp = 0.; +// int crossedSlices = 0; + + int currentSlice = 0; + int oldSlice = 0; + + double sliceEnergy = theTraj->GetEnergy(); + size_t nSteps = theTraj->GetNSteps(); + for(size_t is = 0; is < nSteps; ++is){ + auto theStep = theTraj->GetStep(is); + + disp += theStep->GetStepLength(); + currentSlice = floor(disp/res); + + std::string theProc = theStep->GetStepChosenProc(); + + //Check to see if in a new slice and it's not the end + if( oldSlice != currentSlice && is < nSteps - 1){ + + + //Save Interaction info of the prev slice + //and reset + result.push_back( std::make_pair(sliceEnergy, interactInSlice) ); + interactInSlice = 0; + + //Update the energy + sliceEnergy = sliceEnergy - res*BetheBloch(sliceEnergy, mass); + if( sliceEnergy - mass < 0.){ + //std::cout << "Warning! Negative energy " << sliceEnergy - mass << std::endl; + //std::cout << "Crossed " << oldSlice - currentSlice << std::endl; + sliceEnergy = 0.0001; + } + //If it's more than 1 slice, add in non-interacting slices + for(int ic = 1; ic < abs( oldSlice - currentSlice ); ++ic){ + //std::cout << ic << std::endl; + + result.push_back( std::make_pair(sliceEnergy, 0) ); + + //Update the energy again + sliceEnergy = sliceEnergy - res*BetheBloch(sliceEnergy, mass); + if( sliceEnergy - mass < 0.){ + //std::cout << "Warning! Negative energy " << sliceEnergy - mass << std::endl; + //std::cout << "Crossed " << oldSlice - currentSlice << std::endl; + sliceEnergy = 0.0001; + } + } + + if ((!isElastic && theProc.find(std::string("Inelastic")) != std::string::npos) || (isElastic && theProc.find(std::string("hadElastic")) != std::string::npos)) { + // std::cout << "found! " << theProc << '\n'; + interactInSlice = 1; + } + } + //It's crossed a slice and it's the last step. Save both info + else if( oldSlice != currentSlice && is == nSteps - 1 ){ + result.push_back( std::make_pair(sliceEnergy, interactInSlice) ); + interactInSlice = 0; + + //Update the energy + sliceEnergy = sliceEnergy - res*BetheBloch(sliceEnergy, mass); + if( sliceEnergy - mass < 0.){ + //std::cout << "Warning! Negative energy " << sliceEnergy - mass << std::endl; + //std::cout << "Crossed " << oldSlice - currentSlice << std::endl; + sliceEnergy = 0.0001; + } + //If it's more than 1 slice, add in non-interacting slices + for(int ic = 1; ic < abs( oldSlice - currentSlice ); ++ic){ + //std::cout << ic << std::endl; + + result.push_back( std::make_pair(sliceEnergy, 0) ); + + //Update the energy again + sliceEnergy = sliceEnergy - res*BetheBloch(sliceEnergy, mass); + if( sliceEnergy - mass < 0.){ + //std::cout << "Warning! Negative energy " << sliceEnergy - mass << std::endl; + //std::cout << "Crossed " << oldSlice - currentSlice << std::endl; + sliceEnergy = 0.0001; + } + } + + //Save the last slice + if ((!isElastic && theProc.find(std::string("Inelastic")) != std::string::npos) || (isElastic && theProc.find(std::string("hadElastic")) != std::string::npos)) { + // std::cout << "found! " << theProc << '\n'; + interactInSlice = 1; + } + result.push_back( std::make_pair(sliceEnergy, interactInSlice) ); + } + //It's the end, so just save this last info + else if( oldSlice == currentSlice && is == nSteps - 1 ){ + if ((!isElastic && theProc.find(std::string("Inelastic")) != std::string::npos) || (isElastic && theProc.find(std::string("hadElastic")) != std::string::npos)) { + // std::cout << "found! " << theProc << '\n'; + interactInSlice = 1; + } + result.push_back( std::make_pair(sliceEnergy, interactInSlice) ); + } + //Same slice, not the end. Check for interactions + else{ + if ((!isElastic && theProc.find(std::string("Inelastic")) != std::string::npos) || (isElastic && theProc.find(std::string("hadElastic")) != std::string::npos)) { + // std::cout << "found! " << theProc << '\n'; + interactInSlice = 1; + } + } + + //Update oldslice + oldSlice = currentSlice; + } + + return result; +} diff --git a/sbncode/SBNEventWeight/Calculators/Geant4/CMakeLists.txt b/sbncode/SBNEventWeight/Calculators/Geant4/CMakeLists.txt new file mode 100644 index 000000000..568ecc7f6 --- /dev/null +++ b/sbncode/SBNEventWeight/Calculators/Geant4/CMakeLists.txt @@ -0,0 +1,24 @@ +include_directories ( $ENV{GEANT4_FQ_DIR}/include ) +include_directories ( $ENV{GEANT4REWEIGHT_INC} ) +link_directories ( $ENV{GEANT4REWEIGHT_LIB} ) + +art_make_library( + LIBRARY_NAME sbncode_SBNEventWeight_Calculators_Geant4 + LIBRARIES + sbncode_SBNEventWeight_Base + nusimdata::SimulationBase + nurandom::RandomUtils_NuRandomService_service + art::Framework_Principal + art::Framework_Services_Registry + art_root_io::TFileService_service + larcore::Geometry_Geometry_service + cetlib_except::cetlib_except + ReweightBaseLib + PropBaseLib + ROOT::Tree +) + +install_headers() +install_fhicl() +install_source() + diff --git a/sbncode/SBNEventWeight/Calculators/Geant4/Geant4WeightCalc.cxx b/sbncode/SBNEventWeight/Calculators/Geant4/Geant4WeightCalc.cxx new file mode 100644 index 000000000..5f45db672 --- /dev/null +++ b/sbncode/SBNEventWeight/Calculators/Geant4/Geant4WeightCalc.cxx @@ -0,0 +1,469 @@ +/** + * \class evwgh::Geant4WeightCalc + * \brief Updated hadron reinteraction event reweighting, using geant4reweight + * \author K. Duffy , 2019/10 + * + * Reweight events based on hadron reinteraction probabilities. + */ + +#include +#include +#include "TDirectory.h" +#include "TFile.h" +#include "TH1D.h" +#include "TTree.h" +#include "Geant4/G4LossTableManager.hh" +#include "Geant4/G4ParticleTable.hh" +#include "Geant4/G4ParticleDefinition.hh" +#include "Geant4/G4Material.hh" +#include "Geant4/G4MaterialCutsCouple.hh" +#include "art_root_io/TFileService.h" +#include "art_root_io/TFileDirectory.h" +#include "art/Framework/Services/Registry/ServiceHandle.h" +#include "art/Framework/Principal/Handle.h" +#include "art/Persistency/Provenance/ModuleContext.h" +#include "art/Framework/Principal/Event.h" +#include "canvas/Persistency/Common/FindManyP.h" +#include "canvas/Persistency/Common/Ptr.h" +#include "nusimdata/SimulationBase/MCParticle.h" +#include "nusimdata/SimulationBase/MCTruth.h" +#include "CLHEP/Random/RandGaussQ.h" +#include "CLHEP/Units/PhysicalConstants.h" +#include "CLHEP/Units/SystemOfUnits.h" +#include "sbncode/SBNEventWeight/Base/WeightCalcCreator.h" +#include "sbncode/SBNEventWeight/Base/WeightCalc.h" +#include "larcore/Geometry/Geometry.h" +#include "geant4reweight/ReweightBase/G4ReweightManager.hh" +#include "geant4reweight/ReweightBase/G4ReweighterFactory.hh" +#include "geant4reweight/ReweightBase/G4Reweighter.hh" +#include "geant4reweight/ReweightBase/G4ReweightTraj.hh" +#include "geant4reweight/ReweightBase/G4ReweightStep.hh" +#include "geant4reweight/PropBase/G4ReweightParameterMaker.hh" + +// local include +#include "BetheBlochForG4ReweightValid.h" + +namespace sbn { + +namespace evwgh { + +class Geant4WeightCalc : public WeightCalc { +public: + Geant4WeightCalc() {} + + void Configure(fhicl::ParameterSet const& p, CLHEP::HepRandomEngine& engine); + + std::vector GetWeight(art::Event& e, size_t inu); + +private: + std::string fMCParticleProducer; //!< Label for the MCParticle producer + std::string fMCTruthProducer; //!< Label for the MCTruth producer + CLHEP::RandGaussQ* fGaussRandom; //!< Random number generator + fhicl::ParameterSet fMaterial; //!< Detector material, i.e. LAr + unsigned fNsims; //!< Number of universes (multisim mode) + unsigned fNsigmas; //!< Number of sigmas (multisigma mode) + int fPdg; //!< PDG value for particles that a given weight calculator should apply to. Note that for now this module can only handle weights for one particle species at a time. + G4ReweighterFactory fRWFactory; //!< Base class to handle all Geant4Reweighters + G4Reweighter *fReweighter; //!< Geant4Reweighter -- this is what provides the weights + G4ReweightParameterMaker *fParMaker; + std::vector> UniverseVals; //!< Vector of maps relating parameter name to value (defines parameter values that will be evaluated in universes). Each map should have one entry per parameter we are considering + + art::ServiceHandle < geo::Geometry > fGeometryService; + + bool fMakeOutputTrees; ///!< Fcl parameter to decide whether to save output tree (useful for validations but not necessary when running in production) + TTree *fOutTree_MCTruth; //!< Output tree for quick validations: on entry per MCTruth object + TTree *fOutTree_Particle; //!< Output tree for quick validations: one entry per neutrino-induced pi+, pi-, or proton + int event_num; //!< Variables for both output trees + int run_num; //!< Variables for both output trees + int subrun_num; //!< Variables for both output trees + double p_track_length; //!< Variables for by-particle output tree + int p_PDG; //!< Variables for by-particle output tree + std::string p_final_proc; //!< Variables for by-particle output tree + double p_init_momentum; //!< Variables for by-particle output tree + double p_final_momentum; //!< Variables for by-particle output tree + std::vector< double > p_energies_el; //!< Variables for by-particle output tree + std::vector< int > p_sliceInts_el; //!< Variables for by-particle output tree + std::vector< double > p_energies_inel; //!< Variables for by-particle output tree + std::vector< int > p_sliceInts_inel; //!< Variables for by-particle output tree + int p_nElasticScatters; //!< Variables for by-particle output tree + std::vector p_weight; //!< Variables for by-particle output tree + + // **IF** I understand correctly, then after commit 11077f2 (el_weight*inel_weight) is returned by GetWeight() + + bool fDebug; //!< print debug statements + + DECLARE_WEIGHTCALC(Geant4WeightCalc) +}; + + +void Geant4WeightCalc::Configure(fhicl::ParameterSet const& p, + CLHEP::HepRandomEngine& engine) +{ + std::cout << "Using Geant4WeightCalc for reinteraction weights" << std::endl; + + // Get configuration for this function + fhicl::ParameterSet const& pset = p.get(GetName()); + fMCParticleProducer = pset.get("MCParticleProducer", "largeant"); + fMCTruthProducer = pset.get("MCTruthProducer", "generator"); + fMakeOutputTrees = pset.get< bool >( "makeoutputtree", false ); + std::string mode = pset.get("mode"); + std::string FracsFileName = pset.get< std::string >( "fracsfile" ); + std::vector< fhicl::ParameterSet > FitParSets = pset.get< std::vector< fhicl::ParameterSet > >("parameters"); + fNsims = pset.get ("number_of_multisims", 0); + fNsigmas = pset.get ("number_of_sigmas", 0); + fPdg = pset.get ("pdg_to_reweight"); + fMaterial = pset.get ("material"); + fDebug = pset.get ("debug",false); + + // Prepare random generator + fGaussRandom = new CLHEP::RandGaussQ(engine); + + // Get input file + TFile FracsFile( FracsFileName.c_str(), "OPEN" ); + + // Configure G4Reweighter + fParMaker = new G4ReweightParameterMaker( FitParSets, true , fPdg ); //TODO:Do we want check_overlap? Maybe a fcl switch? + static G4ReweightManager* RWManager = new G4ReweightManager( {fMaterial} ); //Constructor asks for a vector, but SBN only cares about one material + fReweighter = fRWFactory.BuildReweighter(fPdg, &FracsFile, fParMaker->GetFSHists(), fMaterial, RWManager, fParMaker->GetElasticHist() ); + + // Make output trees to save things for quick and easy validation + art::ServiceHandle tfs; + + if (fMakeOutputTrees){ + fOutTree_Particle = tfs->make(Form("%s_%i","ByParticleValidTree",fPdg),""); + fOutTree_Particle->Branch("event_num",&event_num); + fOutTree_Particle->Branch("run_num",&run_num); + fOutTree_Particle->Branch("subrun_num",&subrun_num); + fOutTree_Particle->Branch("UniverseVals", &UniverseVals); + fOutTree_Particle->Branch("pdg_to_reweight",&fPdg); + + fOutTree_Particle->Branch("weight",&p_weight); + fOutTree_Particle->Branch("track_length",&p_track_length); + fOutTree_Particle->Branch("PDG",&p_PDG); + fOutTree_Particle->Branch("final_proc",&p_final_proc); + fOutTree_Particle->Branch("init_momentum",&p_init_momentum); + fOutTree_Particle->Branch("final_momentum",&p_final_momentum); + fOutTree_Particle->Branch("energies_el",&p_energies_el); + fOutTree_Particle->Branch("sliceInts_el",&p_sliceInts_el); + fOutTree_Particle->Branch("energies_inel",&p_energies_inel); + fOutTree_Particle->Branch("sliceInts_inel",&p_sliceInts_inel); + fOutTree_Particle->Branch("nElasticScatters",&p_nElasticScatters); + + fOutTree_MCTruth = tfs->make(Form("%s_%i","ByMCTruthValidTree",fPdg),""); + fOutTree_MCTruth->Branch("event_num",&event_num); + fOutTree_MCTruth->Branch("run_num",&run_num); + fOutTree_MCTruth->Branch("subrun_num",&subrun_num); + fOutTree_MCTruth->Branch("UniverseVals", &UniverseVals); + fOutTree_MCTruth->Branch("pdg_to_reweight",&fPdg); + } + + // Read input parameter sets and set up universes + size_t n_parsets = FitParSets.size(); + std::vector FitParNames; + std::vector FitParNominals; + std::vector FitParSigmas; + std::map theNominals; + + std::cout << "Configuring parameter: " << GetFullName() << " with mode: " << mode << std::endl; + fParameterSet.Configure(GetFullName(), mode, fNsims); + + for (size_t i_parset=0; i_parset("Name"); + double theNominal = theSet.get("Nominal",1.); + double theSigma = theSet.get("Sigma",0.); + + FitParNames.push_back(theName); + FitParNominals.push_back(theNominal); + FitParSigmas.push_back(theSigma); + + if (fDebug) std::cout << "Name: " << theName << ", Nominal: " << theNominal << ", Sigma: " << theSigma << std::endl; + + theNominals[theName] = theNominal; + + fParameterSet.AddParameter(theName, theSigma); + } + + if (mode=="multisim"){ + // multisim mode: parameter values sample within the given uncertainty for all parameters simultaneously + // Loop over universes j + for (unsigned j=0; j tmp_vals; + for (size_t i_parset=0; i_parsetfire(0.0,1.0); + tmp_vals[FitParNames.at(i_parset)] = FitParNominals.at(i_parset)+(FitParSigmas.at(i_parset)*r); + } // loop over parameters (i_parset) + // Now save this universe + UniverseVals.push_back(tmp_vals); + } // loop over Nsims (j) + } // multisim + + else if (mode=="multisigma"){ + // pm1sigma mode: 0 = +1sigma, 1 = -1sigma of a single parameter. All other parameters at nominal + for (size_t i_parset=0; i_parset tmp_vals(theNominals); + tmp_vals[FitParNames.at(i_parset)] = FitParNominals.at(i_parset) + i_sigma*FitParSigmas.at(i_parset); + UniverseVals.push_back(tmp_vals); + } // ened loop over sigmas + } // end loop over parsets (i) + } // multisigma + + else{ + // Anything else mode: Set parameters to user-defined nominal value + UniverseVals.push_back(theNominals); + } // any other mode + + fNsims = UniverseVals.size(); + if (fDebug) std::cout << "Running mode: " << mode <<". Nsims = " << fNsims << std::endl; +} + + +std::vector Geant4WeightCalc::GetWeight(art::Event& e, size_t itruth ) { + + // Get event/run/subrun numbers for output + run_num = e.run(); + subrun_num = e.subRun(); + event_num = e.id().event(); + + // Get MCParticles for each MCTruth in this event + art::Handle > truthHandle; + e.getByLabel(fMCTruthProducer, truthHandle); + const art::FindManyP truthParticles(truthHandle, e, fMCParticleProducer); + assert(truthParticles.isValid()); + + // Initialize the vector of event weights + std::vector weight; + + // Initialize weight vector for this MCTruth + weight.clear(); + weight.resize(fNsims, 1.0); + + // Loop over MCParticles in the event + auto const& mcparticles = truthParticles.at(itruth); + + for (size_t i=0; i trajpoint_X; + std::vector trajpoint_Y; + std::vector trajpoint_Z; + std::vector trajpoint_PX; + std::vector trajpoint_PY; + std::vector trajpoint_PZ; + std::vector elastic_indices; + + //Get the list of processes from the true trajectory + const std::vector< std::pair< size_t, unsigned char > > & processes = p.Trajectory().TrajectoryProcesses(); + std::map< size_t, std::string > process_map; + for( auto it = processes.begin(); it != processes.end(); ++it ){ + process_map[ it->first ] = p.Trajectory().KeyToProcess( it->second ); + } + + for( size_t i = 0; i < p.NumberTrajectoryPoints(); ++i ){ + double X = p.Position(i).X(); + double Y = p.Position(i).Y(); + double Z = p.Position(i).Z(); + geo::Point_t testpoint1 { X, Y, Z }; + const TGeoMaterial* testmaterial1 = fGeometryService->Material( testpoint1 ); + if( ! testmaterial1 ) { + break; // The trajectory point is outside the world, or the geometry service is unable to handle the point + } + //For now, just going to reweight the points within the LAr of the TPC + // TODO check if this is right + if ( !strcmp( testmaterial1->GetName(), "LAr" ) ){ + trajpoint_X.push_back( X ); + trajpoint_Y.push_back( Y ); + trajpoint_Z.push_back( Z ); + + trajpoint_PX.push_back( p.Px(i) ); + trajpoint_PY.push_back( p.Py(i) ); + trajpoint_PZ.push_back( p.Pz(i) ); + + auto itProc = process_map.find(i); + if( itProc != process_map.end() && itProc->second == "hadElastic" ){ + //Push back the index relative to the start of the reweightable steps + elastic_indices.push_back( trajpoint_X.size() - 1 ); + // if (fDebug) std::cout << "Elastic index: " << trajpoint_X.size() - 1 << std::endl; + } + + } + + } // end loop over trajectory points + + // Now find daughters of the MCP + std::vector daughter_PDGs; + std::vector daughter_IDs; + for( int i_mcp = 0; i_mcp < p.NumberDaughters(); i_mcp++ ){ + int daughterID = p.Daughter(i_mcp); + for (auto test_mcp : mcparticles){ + if (test_mcp->TrackId() == daughterID){ + int pid = test_mcp->PdgCode(); + daughter_PDGs.push_back(pid); + daughter_IDs.push_back( test_mcp->TrackId() ); + break; + } + } + } // end loop over daughters + + // --- Now that we have all the information about the track we need, here comes the reweighting part! --- // + + //Make a G4ReweightTraj -- This is the reweightable object + G4ReweightTraj theTraj(mcpID, p_PDG, 0, event_num, std::make_pair(0,0)); + + //Create its set of G4ReweightSteps and add them to the Traj (note: this needs to be done once per MCParticle but will be valid for all weight calculations) + std::vector< G4ReweightStep* > allSteps; + + size_t nSteps = trajpoint_PX.size(); + + if( nSteps < 2 ) continue; + + p_nElasticScatters = elastic_indices.size(); + for( size_t istep = 1; istep < nSteps; ++istep ){ + + std::string proc = "default"; + if( istep == trajpoint_PX.size() - 1 ) + proc = EndProcess; + else if( std::find( elastic_indices.begin(), elastic_indices.end(), istep ) != elastic_indices.end() ){ + proc = "hadElastic"; + } + + double deltaX = ( trajpoint_X.at(istep) - trajpoint_X.at(istep-1) ); + double deltaY = ( trajpoint_Y.at(istep) - trajpoint_Y.at(istep-1) ); + double deltaZ = ( trajpoint_Z.at(istep) - trajpoint_Z.at(istep-1) ); + + double len = sqrt( + std::pow( deltaX, 2 ) + + std::pow( deltaY, 2 ) + + std::pow( deltaZ, 2 ) + ); + + double preStepP[3] = { + trajpoint_PX.at(istep-1)*1.e3, + trajpoint_PY.at(istep-1)*1.e3, + trajpoint_PZ.at(istep-1)*1.e3 + }; + + double postStepP[3] = { + trajpoint_PX.at(istep)*1.e3, + trajpoint_PY.at(istep)*1.e3, + trajpoint_PZ.at(istep)*1.e3 + }; + + if( istep == 1 ){ + theTraj.SetEnergy( sqrt( preStepP[0]*preStepP[0] + preStepP[1]*preStepP[1] + preStepP[2]*preStepP[2] + mass*mass ) ); + } + + G4ReweightStep * theStep = new G4ReweightStep( mcpID, p_PDG, 0, event_num, preStepP, postStepP, len, proc ); + theStep->SetDeltaX( deltaX ); + theStep->SetDeltaY( deltaY ); + theStep->SetDeltaZ( deltaZ ); + + theTraj.AddStep( theStep ); + + for( size_t k = 0; k < daughter_PDGs.size(); ++k ){ + theTraj.AddChild( new G4ReweightTraj(daughter_IDs[k], daughter_PDGs[k], mcpID, event_num, std::make_pair(0,0) ) ); + } + } // end loop over nSteps (istep) + p_track_length = theTraj.GetTotalLength(); + + p_init_momentum = sqrt( theTraj.GetEnergy()*theTraj.GetEnergy() - mass*mass ); + p_final_momentum = sqrt( + std::pow( theTraj.GetStep( theTraj.GetNSteps() - 1 )->GetPreStepPx(), 2 ) + + std::pow( theTraj.GetStep( theTraj.GetNSteps() - 1 )->GetPreStepPy(), 2 ) + + std::pow( theTraj.GetStep( theTraj.GetNSteps() - 1 )->GetPreStepPz(), 2 ) + ); + + std::vector< std::pair< double, int > > thin_slice_inelastic = ThinSliceBetheBloch( &theTraj, .5, mass , false); + std::vector< std::pair< double, int > > thin_slice_elastic = ThinSliceBetheBloch( &theTraj, .5, mass , true); + + p_energies_inel.clear(); + p_sliceInts_inel.clear(); + for( size_t islice = 0; islice < thin_slice_inelastic.size(); ++islice ){ + p_energies_inel.push_back( thin_slice_inelastic[islice].first ); + p_sliceInts_inel.push_back( thin_slice_inelastic[islice].second ); + } + + p_energies_el.clear(); + p_sliceInts_el.clear(); + for( size_t islice = 0; islice < thin_slice_elastic.size(); ++islice ){ + p_energies_el.push_back( thin_slice_elastic[islice].first ); + p_sliceInts_el.push_back( thin_slice_elastic[islice].second ); + } + + // Loop through universes (j) + for (size_t j=0; jSetNewVals(UniverseVals.at(j)); + fReweighter->SetNewHists(fParMaker->GetFSHists()); + fReweighter->SetNewElasticHists(fParMaker->GetElasticHist()); + //Get the weight from the G4ReweightTraj + w = fReweighter->GetWeight( &theTraj ); + // Total weight is the product of track weights in the event + weight[j] *= std::max((float)0.0, w); + + // just for the output tree + p_weight[j] = w; + + } // loop through universes (j) + + if (fDebug){ + std::cout << "PDG = " << p_PDG << std::endl; + std::cout << "weights by particle: "; + for (unsigned int j=0; jFill(); + } // loop over mcparticles (i) + if (fMakeOutputTrees) fOutTree_MCTruth->Fill(); + +return weight; + +} + +REGISTER_WEIGHTCALC(Geant4WeightCalc) + +} // namespace evwgh + +} // namespace sbn diff --git a/sbncode/SBNEventWeight/jobs/CMakeLists.txt b/sbncode/SBNEventWeight/jobs/CMakeLists.txt index e36b8b0cc..b5aae6724 100644 --- a/sbncode/SBNEventWeight/jobs/CMakeLists.txt +++ b/sbncode/SBNEventWeight/jobs/CMakeLists.txt @@ -5,4 +5,5 @@ install_source() add_subdirectory(SystTools) add_subdirectory(genie) add_subdirectory(flux) +add_subdirectory(geant4) diff --git a/sbncode/SBNEventWeight/jobs/geant4/CMakeLists.txt b/sbncode/SBNEventWeight/jobs/geant4/CMakeLists.txt new file mode 100644 index 000000000..589bc695a --- /dev/null +++ b/sbncode/SBNEventWeight/jobs/geant4/CMakeLists.txt @@ -0,0 +1,6 @@ +install_fhicl() + +FILE(GLOB fcl_files *.fcl) + +install_source( EXTRAS ${fcl_files} ) + diff --git a/sbncode/SBNEventWeight/jobs/geant4/eventweight_geant4_sbn.fcl b/sbncode/SBNEventWeight/jobs/geant4/eventweight_geant4_sbn.fcl new file mode 100644 index 000000000..2431af89d --- /dev/null +++ b/sbncode/SBNEventWeight/jobs/geant4/eventweight_geant4_sbn.fcl @@ -0,0 +1,153 @@ +########################################################## +## Hadron reinteraction uncertainties +## +## References: +## +## * K. Duffy, Pion Secondary Interaction Systematics (from GEANTReweight), DocDB 25379 +## * J. Calcutt, GEANTReweight documentation DocDB 25084, repository https://cdcvs.fnal.gov/redmine/projects/geant4reweight/repository +## +## From MicroBooNE, ubsim UBOONE_SUITE_v08_00_00_69 +## (uBooNE author: Kirsty Duffy (kduffy@fnal.gov)) +## +########################################################## + +#include "piplus_reweight_parameters.fcl" +#include "piminus_reweight_parameters.fcl" +#include "proton_reweight_parameters.fcl" +#include "neutron_reweight_parameters.fcl" +#include "kplus_reweight_parameters.fcl" +#include "kminus_reweight_parameters.fcl" + +#include "material_parameters.fcl" + +BEGIN_PROLOG + +sbn_eventweight_geant4: { + module_type: "SBNEventWeight" + AllowMissingTruth: true + min_weight: 0.0 + max_weight: 100 + + weight_functions_reint: [ reinteractions_piplus , reinteractions_piminus, reinteractions_proton , reinteractions_neutron , reinteractions_kplus , reinteractions_kminus ] + + reinteractions_piplus: { + type: Geant4 + material: @local::LAr + random_seed: 58 + parameters: @local::PiPlusParameters + mode: multisim + number_of_multisims: 1000 + fracsfile: "$SBNDATA_DIR/systematics/reint/g4_fracs_piplus.root" + makeoutputtree: true + pdg_to_reweight: 211 + debug: false + } + + reinteractions_piminus: { + type: Geant4 + material: @local::LAr + random_seed: 59 + parameters: @local::PiMinusParameters + mode: multisim + number_of_multisims: 1000 + fracsfile: "$SBNDATA_DIR/systematics/reint/g4_fracs_piminus.root" + makeoutputtree: true + pdg_to_reweight: -211 + debug: false + } + + reinteractions_proton: { + type: Geant4 + material: @local::LAr + random_seed: 60 + parameters: @local::ProtonParameters + #mode: multisigma + #number_of_sigmas: 3 + mode: multisim + number_of_multisims: 1000 + fracsfile: "$SBNDATA_DIR/systematics/reint/g4_fracs_proton.root" # dummy file, cross section pulled directly from Geant4 + makeoutputtree: true + pdg_to_reweight: 2212 + debug: false + } + + reinteractions_neutron: { + type: Geant4 + material: @local::LAr + random_seed: 61 + parameters: @local::NeutronParameters + #mode: multisigma + #number_of_sigmas: 3 + mode: multisim + number_of_multisims: 1000 + fracsfile: "$SBNDATA_DIR/systematics/reint/g4_fracs_proton.root" # dummy file, cross section pulled directly from Geant4 + makeoutputtree: true + pdg_to_reweight: 2112 + debug: false + } + + reinteractions_kplus: { + type: Geant4 + material: @local::LAr + random_seed: 62 + parameters: @local::KPlusParameters + #mode: pm1sigma + mode: multisim + number_of_multisims: 1000 + fracsfile: "$SBNDATA_DIR/systematics/reint/g4_fracs_proton.root" # dummy file, cross section pulled directly from Geant4 + makeoutputtree: true + pdg_to_reweight: 321 + debug: false + } + + reinteractions_kminus: { + type: Geant4 + material: @local::LAr + random_seed: 63 + parameters: @local::KMinusParameters + #mode: pm1sigma + mode: multisim + number_of_multisims: 1000 + fracsfile: "$SBNDATA_DIR/systematics/reint/g4_fracs_proton.root" # dummy file, cross section pulled directly from Geant4 + makeoutputtree: true + pdg_to_reweight: -321 + debug: false + } + +} + +### +# Reweighting parameters should be defined as +# +# TheParameters: [ +# { +# Cut: "reac" +# Name: "fReacLow" +# Range: [10., 200.] +# Nominal: 1.0 +# Sigma: 0.3 +# }, +# {...} +# ] +# +# - Range defines the energy range over which that parameter has an effect (MeV) +# - Nominal is not used in "multisim" mode. In "pm1sigma" mode it defines the +# nominal (around which you calculate +/- 1 sigma variations). In any other +# mode, the parameter is set to the value under Nominal (so it's not really a +# "nominal" value in that case, just the set value) +# - Sigma defines the 1-sigma range for multisims and unisims. Currently the +# code can only accept a single value for sigma, giving symmetric +# uncertainty bounds: nominal-sigma and nominal+sigma +# - Cut must be one of the following: +# "total" <- total scattering cross section, pulled from Geant4 +# "abs" <- absorption cross section (pi+ and pi- only at the moment -- Oct 2019) +# "cex" <- charge exchange cross section (pi+ and pi- only at the moment -- Oct 2019) +# "dcex" <- double charge exchange cross section (pi+ and pi- only at the moment -- Oct 2019) +# "prod" <- pion production cross section (pi+ and pi- only at the moment -- Oct 2019) +# "inel" <- quasi-elastic inelastic scattering cross section (pi+ and pi- only at the moment -- Oct 2019) +# - Name can be anything you like (but should uniquely identify that parameter/ +# variation) +### + +END_PROLOG + diff --git a/sbncode/SBNEventWeight/jobs/geant4/kminus_reweight_parameters.fcl b/sbncode/SBNEventWeight/jobs/geant4/kminus_reweight_parameters.fcl new file mode 100644 index 000000000..ac330892e --- /dev/null +++ b/sbncode/SBNEventWeight/jobs/geant4/kminus_reweight_parameters.fcl @@ -0,0 +1,15 @@ +BEGIN_PROLOG + +## KMinus available process keys: "total" + +KMinusParameters: [ + { + Name: "fKMinusTotal" + Cut: "total" + Range: [10.0, 2005.0] + Nominal: 1.0 + Sigma: 0.2 + } +] + +END_PROLOG diff --git a/sbncode/SBNEventWeight/jobs/geant4/kplus_reweight_parameters.fcl b/sbncode/SBNEventWeight/jobs/geant4/kplus_reweight_parameters.fcl new file mode 100644 index 000000000..08ec5e9f2 --- /dev/null +++ b/sbncode/SBNEventWeight/jobs/geant4/kplus_reweight_parameters.fcl @@ -0,0 +1,15 @@ +BEGIN_PROLOG + +## KPlus available process keys: "total" + +KPlusParameters: [ + { + Name: "fKPlusTotal" + Cut: "total" + Range: [10.0, 2005.0] + Nominal: 1.0 + Sigma: 0.2 + } +] + +END_PROLOG diff --git a/sbncode/SBNEventWeight/jobs/geant4/material_parameters.fcl b/sbncode/SBNEventWeight/jobs/geant4/material_parameters.fcl new file mode 100644 index 000000000..ed735621f --- /dev/null +++ b/sbncode/SBNEventWeight/jobs/geant4/material_parameters.fcl @@ -0,0 +1,18 @@ +BEGIN_PROLOG + +# LAr parameters for geant4reweight, stolen out of the ND-Lar 2x2 implementation +# A more complete solution would be to get this info out of the geometry instead... + +LAr: { + Name: "liquidArgon" + Density: 1.390 + Components: [ + { + Z: 18 + Mass: 39.95 + Fraction: 1. + } + ] +} + +END_PROLOG diff --git a/sbncode/SBNEventWeight/jobs/geant4/neutron_reweight_parameters.fcl b/sbncode/SBNEventWeight/jobs/geant4/neutron_reweight_parameters.fcl new file mode 100644 index 000000000..65743db98 --- /dev/null +++ b/sbncode/SBNEventWeight/jobs/geant4/neutron_reweight_parameters.fcl @@ -0,0 +1,15 @@ +BEGIN_PROLOG + +## Neutron available process keys: "total" + +NeutronParameters: [ + { + Name: "fNeutronTotal" + Cut: "total" + Range: [100.0, 2005.0] + Nominal: 1.0 + Sigma: 0.2 + } +] + +END_PROLOG diff --git a/sbncode/SBNEventWeight/jobs/geant4/piminus_reweight_parameters.fcl b/sbncode/SBNEventWeight/jobs/geant4/piminus_reweight_parameters.fcl new file mode 100644 index 000000000..fb27aeed4 --- /dev/null +++ b/sbncode/SBNEventWeight/jobs/geant4/piminus_reweight_parameters.fcl @@ -0,0 +1,55 @@ +BEGIN_PROLOG + +## PiMinus available process keys: "abs", "cex", "prod", "dcex", "inel" + +PiMinusParameters: [ + { + Name: "fPiMinusInelLow" + Cut: "inel" + Range: [10., 200.] + Nominal: 1 + Sigma: 0.2 + } , + { + Name: "fPiMinusInelHigh" + Cut: "inel" + Range: [700., 2005.] + Nominal: 1.0 + Sigma: 0.2 + } , + + { + Name: "fPiMinusAbs" + Cut: "abs" + Range: [10.0, 2005.00] + Nominal: 1.0 + Sigma: 0.2 + }, + + { + Name: "fPiMinusCex" + Cut: "cex" + Range: [10.0, 2005.00] + Nominal: 1.0 + Sigma: 0.2 + }, + + { + Name: "fPiMinusDCex" + Cut: "dcex" + Range: [10.0, 2005.00] + Nominal: 1.0 + Sigma: 0.2 + }, + + { + Name: "fPiMinusPiProd" + Cut: "prod" + Range: [10.0, 2005.00] + Nominal: 1.0 + Sigma: 0.2 + } + +] + +END_PROLOG diff --git a/sbncode/SBNEventWeight/jobs/geant4/piplus_reweight_parameters.fcl b/sbncode/SBNEventWeight/jobs/geant4/piplus_reweight_parameters.fcl new file mode 100644 index 000000000..4aeaeb96d --- /dev/null +++ b/sbncode/SBNEventWeight/jobs/geant4/piplus_reweight_parameters.fcl @@ -0,0 +1,55 @@ +BEGIN_PROLOG + +## PiPlus available process keys: "abs", "cex", "prod", "dcex", "inel" + +PiPlusParameters: [ + { + Name: "fPiPlusInelLow" + Cut: "inel" + Range: [10., 200.] + Nominal: 1.0 + Sigma: 0.2 + } , + { + Name: "fPiPlusInelHigh" + Cut: "inel" + Range: [700., 2005.] + Nominal: 1.0 + Sigma: 0.2 + } , + + { + Name: "fPiPlusAbs" + Cut: "abs" + Range: [10.0, 2005.00] + Nominal: 1.0 + Sigma: 0.2 + }, + + { + Name: "fPiPlusCex" + Cut: "cex" + Range: [10.0, 2005.00] + Nominal: 1.0 + Sigma: 0.2 + }, + + { + Name: "fPiPlusDCex" + Cut: "dcex" + Range: [10.0, 2005.00] + Nominal: 1.0 + Sigma: 0.2 + }, + + { + Name: "fPiPlusPiProd" + Cut: "prod" + Range: [10.0, 2005.00] + Nominal: 1.0 + Sigma: 0.2 + } + +] + +END_PROLOG diff --git a/sbncode/SBNEventWeight/jobs/geant4/proton_reweight_parameters.fcl b/sbncode/SBNEventWeight/jobs/geant4/proton_reweight_parameters.fcl new file mode 100644 index 000000000..3dd017d93 --- /dev/null +++ b/sbncode/SBNEventWeight/jobs/geant4/proton_reweight_parameters.fcl @@ -0,0 +1,15 @@ +BEGIN_PROLOG + +## Proton available process keys: "total" + +ProtonParameters: [ + { + Name: "fProtonTotal" + Cut: "total" + Range: [10.0, 2005.0] + Nominal: 1.0 + Sigma: 0.2 + } +] + +END_PROLOG diff --git a/sbncode/Supera b/sbncode/Supera index d84d726d9..93044cecc 160000 --- a/sbncode/Supera +++ b/sbncode/Supera @@ -1 +1 @@ -Subproject commit d84d726d95017f4b86a99537f6d86deeda3216c3 +Subproject commit 93044cecc776662a7ed85157a1e5d5d33ca035df diff --git a/sbncode/Utilities/AssnsUtils.h b/sbncode/Utilities/AssnsUtils.h new file mode 100644 index 000000000..6eb3a1425 --- /dev/null +++ b/sbncode/Utilities/AssnsUtils.h @@ -0,0 +1,144 @@ +/** + * @file sbncode/Utilities/AssnsUtils.h + * @brief Framework-level utilities dealing with associations. + * @date April 2, 2026 + * @author Gianluca Petrillo (petrillo@slac.stanford.edu) + * + * This library is currently header-only. + */ + +#ifndef SBNCODE_UTILITIES_ASSNSUTILS_H +#define SBNCODE_UTILITIES_ASSNSUTILS_H + +// framework libraries +#include "art/Persistency/Common/PtrMaker.h" +#include "canvas/Persistency/Common/Ptr.h" +#include "canvas/Persistency/Common/Assns.h" + +// C/C++ libraries +#include // std::size_t +#include // std::get() +#include // std::is_same_v, ... + + +namespace sbn { + /** + * @brief Creates an association of a new product mirroring an existing one. + * @tparam DiscardData (default: `false`) whether to omit copying the data + * @tparam L type of left-side data in the association + * @tparam R type of right-side data in the association + * @tparam Data type of metadata in the association + * @tparam LorR type whose pointer is being rebound + * @param assns the existing association + * @param reboundPtrMaker tool to create a _art_ pointer to the new product + * @return the newly created association + * + * The existing association `assns` is replicated replacing all the pointers + * in the rebinding side (left or right) with their corresponding pointers + * from another collection of the same type. + * + * The application is for one-to-one processing of data product elements, + * where the element resulting from the processing is still associated to the + * same object as the processed element. + * For example, let's have a reprocessing that modifies the signal on a + * optical detector waveform (`raw::OpDetWaveform`), leaving the baseline + * unchanged. + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp} + * using Assns_t = art::Assns; + * + * Assns_t const& oldAssns = event.getProduct(assnsTag); + * art::PtrMaker const makeWaveformPtr{ event }; + * + * auto newAssns + * = std::make_unique(sbn::RebindAssociatedProducts(oldAssns, makeWaveformPtr)); + * + * event.put(std::move(newAssns)); + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * The alias `Assns_t` was used only to make the example more readable. + * + * As shown in the example, which side to rebind is determined by the type + * of the `art::PtrMaker` object. If its operand type is neither of the + * association types, a compilation error will occur. + * + * Note that the invalid pointers are also rebound, but their rebound pointer + * should be as invalid as the old one. + * + * If metadata is present in the associations, it is _copied_ element by + * element into the new one. It is possible to omit the metadata by calling + * with `DiscardData` set to `true` (`RebindAssociatedProducts(...)`), + * in which case the returned type will always be an association without data. + * + */ + template < + bool DiscardData = false, + typename L, typename R, typename Data, + typename LorR + > + art::Assns> + RebindAssociatedProducts( + art::Assns const& assns, + art::PtrMaker const& reboundPtrMaker + ); + +} // namespace sbn + + +// ----------------------------------------------------------------------------- +// --- Template implementation +// ----------------------------------------------------------------------------- +template < + bool DiscardData /* = false */, + typename L, typename R, typename Data, + typename LorR + > +art::Assns> +sbn::RebindAssociatedProducts( + art::Assns const& assns, + art::PtrMaker const& reboundPtrMaker +) { + static_assert(std::is_same_v || std::is_same_v, + "The PtrMaker tool must operate on either of the association data types."); + + constexpr bool RebindLeft = std::is_same_v; + constexpr bool hasMetadata = !std::is_void_v; + constexpr bool wantsMetadata = hasMetadata && !DiscardData; + + using DestData_t = std::conditional_t; + + /* + * How to deal with invalid pointers? (there should be none though!) + * a) preserve them (drawback: different product IDs are mixed in) + * b) replace them with default-constructed (which is also invalid) + * c) rebind it (drawback: rely on PtrMaker doing the right thing with them) + * Here I chose (c). + */ + auto rebind = [&reboundPtrMaker](auto& ptr) + { /* if (ptr) */ ptr = reboundPtrMaker(ptr.key()); }; + + art::Assns newAssns; + for (auto elements: assns) { + + art::Ptr leftPtr = std::move(std::get<0>(elements)); + art::Ptr rightPtr = std::move(std::get<1>(elements)); + + if constexpr(RebindLeft) { rebind(leftPtr); } + else { rebind(rightPtr); } + + if constexpr(wantsMetadata) { + newAssns.addSingle + (std::move(leftPtr), std::move(rightPtr), std::move(std::get<2>(elements))); + } + else { + newAssns.addSingle(std::move(leftPtr), std::move(rightPtr)); + } + + } // for assn pairs + + return newAssns; + +} // RebindAssociatedProducts() + + +// ----------------------------------------------------------------------------- + +#endif // SBNCODE_UTILITIES_ASSNSUTILS_H diff --git a/sbncode/Utilities/CMakeLists.txt b/sbncode/Utilities/CMakeLists.txt new file mode 100644 index 000000000..a94a8b369 --- /dev/null +++ b/sbncode/Utilities/CMakeLists.txt @@ -0,0 +1,11 @@ +cet_make_library( + INTERFACE + HEADERS_TARGET_ONLY + LIBRARIES + art::Persistency_Common + canvas::canvas + ) + +install_headers() +install_source() + diff --git a/sbncode/WireMod/CMakeLists.txt b/sbncode/WireMod/CMakeLists.txt new file mode 100644 index 000000000..9919ce928 --- /dev/null +++ b/sbncode/WireMod/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory(Utility) diff --git a/sbncode/WireMod/Utility/CMakeLists.txt b/sbncode/WireMod/Utility/CMakeLists.txt new file mode 100644 index 000000000..42a65ae0d --- /dev/null +++ b/sbncode/WireMod/Utility/CMakeLists.txt @@ -0,0 +1,49 @@ +cet_make_library( + LIBRARY_NAME + sbncode_WireMod_Utility + + SOURCE + WireModUtility.cc + + LIBRARIES + ${ART_FRAMEWORK_CORE} + ${MF_MESSAGELOGGER} + ${Boost_SYSTEM_LIBRARY} + ${ROOT_BASIC_LIB_LIST} + ${ROOT_HIST} + art_root_io::TFileService_service + larcorealg::Geometry + larcore::Geometry_Geometry_service + lardataobj::AnalysisBase + lardataobj::RawData + lardataobj::RecoBase + lardataobj::Simulation + nusimdata::SimulationBase + larevt::SpaceCharge + sbnobj::ICARUS_TPC + lardataalg::DetectorInfo + lardata::RecoObjects + lardata::Utilities + larreco::RecoAlg + ${LARRECO_LIB} + ${LARDATA_LIB} + ${ART_FRAMEWORK_CORE} + ${ART_FRAMEWORK_PRINCIPAL} + ${ART_FRAMEWORK_SERVICES_REGISTRY} + ${ART_FRAMEWORK_SERVICES_OPTIONAL} + ${ART_FRAMEWORK_SERVICES_OPTIONAL_RANDOMNUMBERGENERATOR_SERVICE} + ${ART_FRAMEWORK_SERVICES_OPTIONAL_TFILESERVICE_SERVICE} + ${MF_MESSAGELOGGER} + ${FHICLCPP} + ${CLHEP} + ${ROOT_GEOM} + ${ROOT_XMLIO} + ${ROOT_GDML} + ${ROOT_BASIC_LIB_LIST} + ${ROOT_HIST} + BASENAME_ONLY +) + +install_headers() +install_source() + diff --git a/sbncode/WireMod/Utility/WireModUtility.cc b/sbncode/WireMod/Utility/WireModUtility.cc new file mode 100644 index 000000000..001ef7ba0 --- /dev/null +++ b/sbncode/WireMod/Utility/WireModUtility.cc @@ -0,0 +1,1168 @@ +#include "sbncode/WireMod/Utility/WireModUtility.hh" +#include "lardataalg/DetectorInfo/DetectorPropertiesData.h" + +#include +#include +#include +#include "TVector3.h" + +//--- CalcROIProperties --- +sys::WireModUtility::ROIProperties_t sys::WireModUtility::CalcROIProperties(recob::Wire const& wire, size_t const& roi_idx) +{ + // get the ROI + recob::Wire::RegionsOfInterest_t::datarange_t const& roi = wire.SignalROI().get_ranges()[roi_idx]; + + // initialize the return value + ROIProperties_t roi_vals; + roi_vals.channel = wire.Channel(); + roi_vals.view = wire.View(); + roi_vals.begin = roi.begin_index(); + roi_vals.end = roi.end_index(); + roi_vals.center = 0; + roi_vals.total_q = 0; + roi_vals.sigma = 0; + + // loop over the roi and find the charge-weighted center and total charge + auto const& roi_data = roi.data(); + for (size_t i_t = 0; i_t < roi_data.size(); ++i_t) + { + roi_vals.center += roi_data[i_t]*(i_t+roi_vals.begin); + roi_vals.total_q += roi_data[i_t]; + } + roi_vals.center = roi_vals.center/roi_vals.total_q; + + // get the width (again charge-weighted) + // if the ROI is only one tick set the cent to the middle of the tick and width to 0.5 + for (size_t i_t = 0; i_tfloat manually. + auto const& roi_short = chanROI.SignalROI().get_ranges()[roi_idx]; + const float scale = 1.0f / static_cast(chanROI.ADCScaleFactor()); + + ROIProperties_t roi_vals; + roi_vals.channel = chanROI.Channel(); + roi_vals.view = wireReadout->View(chanROI.Channel()); + roi_vals.begin = roi_short.begin_index(); + roi_vals.end = roi_short.end_index(); + roi_vals.center = 0; + roi_vals.total_q = 0; + roi_vals.sigma = 0; + + auto const& short_data = roi_short.data(); + for (size_t i_t = 0; i_t < short_data.size(); ++i_t) + { + float val = static_cast(short_data[i_t]) * scale; + roi_vals.center += val * (i_t + roi_vals.begin); + roi_vals.total_q += val; + } + roi_vals.center = roi_vals.center / roi_vals.total_q; + + for (size_t i_t = 0; i_t < short_data.size(); ++i_t) + { + float val = static_cast(short_data[i_t]) * scale; + roi_vals.sigma += val * (i_t + roi_vals.begin - roi_vals.center) + * (i_t + roi_vals.begin - roi_vals.center); + } + roi_vals.sigma = std::sqrt(roi_vals.sigma / roi_vals.total_q); + if (roi_vals.end - roi_vals.begin == 1) + { + roi_vals.center += 0.5; + roi_vals.sigma = 0.5; + } + + return roi_vals; +} + +//--- GetTargetROIs --- +std::vector> sys::WireModUtility::GetTargetROIs(sim::SimEnergyDeposit const& shifted_edep, double offset) +{ + // initialize return value + // the pairs are + std::vector> target_roi_vec; + + // if the SimEnergyDeposit isn't inside the TPC then it's not going to match a wire + geo::TPCGeo const* curTPCGeomPtr = geometry->PositionToTPCptr(shifted_edep.MidPoint()); + if (curTPCGeomPtr == nullptr) + return target_roi_vec; + + // iterate over planes + for (auto const& plane : wireReadout->Iterate(curTPCGeomPtr->ID())) { + // check the wire exists in this plane + int wireNumber = int(0.5 + plane.WireCoordinate(shifted_edep.MidPoint())); + if ((wireNumber < 0) || (wireNumber >= (int)plane.Nwires())) + continue; + + // reconstruct the wireID from the position + geo::WireID edep_wireID = plane.NearestWireID(shifted_edep.MidPoint()); + + // if the deposition is inside the range where it would leave a signal on the wire, look for the ROI there + if (planeXInWindow(shifted_edep.X(), plane, *curTPCGeomPtr, offset + tickOffset)) + target_roi_vec.emplace_back(wireReadout->PlaneWireToChannel(edep_wireID), std::round(planeXToTick(shifted_edep.X(), plane, *curTPCGeomPtr, offset + tickOffset))); + } + /* + for (size_t i_p = 0; i_p < curTPCGeomPtr->Nplanes(); ++i_p) + { + // check the wire exists in this plane + int wireNumber = int(0.5 + curTPCGeomPtr->Plane(i_p).WireCoordinate(shifted_edep.MidPoint())); + if ((wireNumber < 0) || (wireNumber >= (int) curTPCGeomPtr->Plane(i_p).Nwires())) + continue; + + // reconstruct the wireID from the position + geo::WireID edep_wireID = curTPCGeomPtr->Plane(i_p).NearestWireID(shifted_edep.MidPoint()); + + // if the deposition is inside the range where it would leave a signal on the wire, look for the ROI there + if (planeXInWindow(shifted_edep.X(), i_p, *curTPCGeomPtr, offset + tickOffset)) + target_roi_vec.emplace_back(geometry->PlaneWireToChannel(edep_wireID), std::round(planeXToTick(shifted_edep.X(), i_p, *curTPCGeomPtr, offset + tickOffset))); + } + */ + + return target_roi_vec; +} + +//--- GetHitTargetROIs --- +std::vector> sys::WireModUtility::GetHitTargetROIs(recob::Hit const& hit) +{ + // initialize return value + // the pairs are + std::vector> target_roi_vec; + + int hit_wire = hit.Channel(); + int hit_tick = int(round(hit.PeakTime())); + + if (hit_tick < tickOffset || hit_tick >= readoutWindowTicks + tickOffset) + return target_roi_vec; + + + target_roi_vec.emplace_back((unsigned int) hit_wire, (unsigned int) hit_tick); + + return target_roi_vec; +} + +//--- FillROIMatchedIDEMap --- +// SimChannel/IDE analogue of FillROIMatchedEdepMap. Unlike an edep, a sim::IDE already +// carries its readout channel (SimChannel::Channel()) and time (the TDCIDEMap TDC), so no +// geometric X->tick projection is needed -- the TDC is converted to a readout tick and the +// covering ROI is found directly. Builds the flat fIDEVec and the ROI->index map. +void sys::WireModUtility::FillROIMatchedIDEMap(std::vector const& simchVec, std::vector const& wireVec, detinfo::DetectorClocksData const& clockData, double offset) +{ + ROIMatchedIDEMap.clear(); + fIDEVec.clear(); + + // map channel -> index into wireVec + std::unordered_map wireChannelMap; + for (size_t i_w = 0; i_w < wireVec.size(); ++i_w) + wireChannelMap[wireVec[i_w].Channel()] = i_w; + + for (auto const& simch : simchVec) + { + raw::ChannelID_t channel = simch.Channel(); + auto wcIt = wireChannelMap.find(channel); + if (wcIt == wireChannelMap.end()) + continue; + auto const& target_wire = wireVec.at(wcIt->second); + + // wire closest to the charge (used later for the SCE-undo) + geo::WireID wireID; + auto const wires = wireReadout->ChannelToWire(channel); + if (!wires.empty()) wireID = wires[0]; + + for (auto const& tdcide : simch.TDCIDEMap()) + { + // convert the SimChannel TDC to a readout tick (same frame as the ROI samples) + double tick = clockData.TPCTDC2Tick(tdcide.first) + offset + tickOffset; + if (tick < 0) continue; + unsigned int sample = (unsigned int) std::round(tick); + + if (not target_wire.SignalROI().is_valid() || + target_wire.SignalROI().empty() || + target_wire.SignalROI().n_ranges() == 0 || + target_wire.SignalROI().size() <= sample || + target_wire.SignalROI().is_void(sample) ) + continue; + + auto range_number = target_wire.SignalROI().find_range_iterator(sample) - target_wire.SignalROI().begin_range(); + ROI_Key_t roi_key = std::make_pair(target_wire.Channel(), range_number); + + for (sim::IDE const& ide : tdcide.second) + { + fIDEVec.push_back( MatchedIDE_t{channel, wireID, tick, &ide} ); + ROIMatchedIDEMap[roi_key].push_back(fIDEVec.size() - 1); + } + } + } +} + +//--- FillROIMatchedIDEMap (ChannelROI overload) --- +void sys::WireModUtility::FillROIMatchedIDEMap(std::vector const& simchVec, std::vector const& chanROIVec, detinfo::DetectorClocksData const& clockData, double offset) +{ + ROIMatchedIDEMap.clear(); + fIDEVec.clear(); + + std::unordered_map chanROIChannelMap; + for (size_t i_c = 0; i_c < chanROIVec.size(); ++i_c) + chanROIChannelMap[chanROIVec[i_c].Channel()] = i_c; + + for (auto const& simch : simchVec) + { + raw::ChannelID_t channel = simch.Channel(); + auto ccIt = chanROIChannelMap.find(channel); + if (ccIt == chanROIChannelMap.end()) + continue; + auto const& target_chanROI = chanROIVec.at(ccIt->second); + auto const& roiShort = target_chanROI.SignalROI(); + + geo::WireID wireID; + auto const wires = wireReadout->ChannelToWire(channel); + if (!wires.empty()) wireID = wires[0]; + + for (auto const& tdcide : simch.TDCIDEMap()) + { + double tick = clockData.TPCTDC2Tick(tdcide.first) + offset + tickOffset; + if (tick < 0) continue; + unsigned int sample = (unsigned int) std::round(tick); + + if (not roiShort.is_valid() || + roiShort.empty() || + roiShort.n_ranges() == 0 || + roiShort.size() <= sample || + roiShort.is_void(sample) ) + continue; + + auto range_number = roiShort.find_range_iterator(sample) - roiShort.begin_range(); + ROI_Key_t roi_key = std::make_pair(target_chanROI.Channel(), range_number); + + for (sim::IDE const& ide : tdcide.second) + { + fIDEVec.push_back( MatchedIDE_t{channel, wireID, tick, &ide} ); + ROIMatchedIDEMap[roi_key].push_back(fIDEVec.size() - 1); + } + } + } +} + +//--- FillROIMatchedEdepMap --- +void sys::WireModUtility::FillROIMatchedEdepMap(std::vector const& edepVec, std::vector const& wireVec, double offset) +{ + // clear the map in case it was already set + ROIMatchedEdepMap.clear(); + + // get the channel from each wire and set up a map between them + std::unordered_map wireChannelMap; + for (size_t i_w = 0; i_w < wireVec.size(); ++i_w) + wireChannelMap[wireVec[i_w].Channel()] = i_w; + + // loop over the energy deposits + for (size_t i_e = 0; i_e < edepVec.size(); ++i_e) + { + // get the ROIs + // + std::vector> target_rois = GetTargetROIs(edepVec[i_e], offset); + + // loop over ROI and match the energy deposits with wires + for (auto const& target_roi : target_rois) + { + // if we can't find the wire, skip it + if (wireChannelMap.find(target_roi.first) == wireChannelMap.end()) + continue; + + // get the wire + auto const& target_wire = wireVec.at(wireChannelMap[target_roi.first]); + + // if there are no ticks in in the wire skip it + // likewise if there's nothing in the region of interst + if (not target_wire.SignalROI().is_valid() || + target_wire.SignalROI().empty() || + target_wire.SignalROI().n_ranges() == 0 || + target_wire.SignalROI().size() <= target_roi.second || + target_wire.SignalROI().is_void(target_roi.second) ) + continue; + + // how far into the range is the ROI? + auto range_number = target_wire.SignalROI().find_range_iterator(target_roi.second) - target_wire.SignalROI().begin_range(); + + // popluate the map + ROIMatchedEdepMap[std::make_pair(target_wire.Channel(),range_number)].push_back(i_e); + } + } +} + +//--- FillROIMatchedHitMap --- +void sys::WireModUtility::FillROIMatchedHitMap(std::vector const& hitVec, std::vector const& wireVec) +{ + // clear the map in case it was already set + ROIMatchedHitMap.clear(); + + // get the channel from each wire and set up a map between them + std::unordered_map wireChannelMap; + for (size_t i_w = 0; i_w < wireVec.size(); ++i_w) + wireChannelMap[wireVec[i_w].Channel()] = i_w; + + // loop over hits + for (size_t i_h = 0; i_h < hitVec.size(); ++i_h) + { + // get the ROIs + // + std::vector> target_rois = GetHitTargetROIs(hitVec[i_h]); + + // loop over ROI and match the energy deposits with wires + for (auto const& target_roi : target_rois) + { + // if we can't find the wire, skip it + if (wireChannelMap.find(target_roi.first) == wireChannelMap.end()) + continue; + + auto const& target_wire = wireVec.at(wireChannelMap[target_roi.first]); + + // if there are no ticks in in the wire skip it + // likewise if there's nothing in the region of interst + if (not target_wire.SignalROI().is_valid() || + target_wire.SignalROI().empty() || + target_wire.SignalROI().n_ranges() == 0 || + target_wire.SignalROI().size() <= target_roi.second || + target_wire.SignalROI().is_void(target_roi.second) ) + continue; + + // which range is it? + auto range_number = target_wire.SignalROI().find_range_iterator(target_roi.second) - target_wire.SignalROI().begin_range(); + + // pupluate the map + ROIMatchedHitMap[std::make_pair(target_wire.Channel(),range_number)].push_back(i_h); + } + } +} + +//--- FillROIMatchedEdepMap (ChannelROI overload) --- +void sys::WireModUtility::FillROIMatchedEdepMap(std::vector const& edepVec, std::vector const& chanROIVec, double offset) +{ + ROIMatchedEdepMap.clear(); + + std::unordered_map chanROIChannelMap; + for (size_t i_c = 0; i_c < chanROIVec.size(); ++i_c) + chanROIChannelMap[chanROIVec[i_c].Channel()] = i_c; + + for (size_t i_e = 0; i_e < edepVec.size(); ++i_e) + { + std::vector> target_rois = GetTargetROIs(edepVec[i_e], offset); + + for (auto const& target_roi : target_rois) + { + if (chanROIChannelMap.find(target_roi.first) == chanROIChannelMap.end()) + continue; + + auto const& target_chanROI = chanROIVec.at(chanROIChannelMap[target_roi.first]); + + // SignalROIF() returns by value, so use SignalROI() instead. + auto const& roiShort = target_chanROI.SignalROI(); + if (not roiShort.is_valid() || + roiShort.empty() || + roiShort.n_ranges() == 0 || + roiShort.size() <= target_roi.second || + roiShort.is_void(target_roi.second) ) + continue; + + auto range_number = roiShort.find_range_iterator(target_roi.second) - roiShort.begin_range(); + + ROIMatchedEdepMap[std::make_pair(target_chanROI.Channel(), range_number)].push_back(i_e); + } + } +} + +//--- FillROIMatchedHitMap (ChannelROI overload) --- +void sys::WireModUtility::FillROIMatchedHitMap(std::vector const& hitVec, std::vector const& chanROIVec) +{ + ROIMatchedHitMap.clear(); + + std::unordered_map chanROIChannelMap; + for (size_t i_c = 0; i_c < chanROIVec.size(); ++i_c) + chanROIChannelMap[chanROIVec[i_c].Channel()] = i_c; + + for (size_t i_h = 0; i_h < hitVec.size(); ++i_h) + { + std::vector> target_rois = GetHitTargetROIs(hitVec[i_h]); + + for (auto const& target_roi : target_rois) + { + if (chanROIChannelMap.find(target_roi.first) == chanROIChannelMap.end()) + continue; + + auto const& target_chanROI = chanROIVec.at(chanROIChannelMap[target_roi.first]); + + // SignalROIF() returns by value, so use SignalROI() instead. + auto const& roiShort = target_chanROI.SignalROI(); + if (not roiShort.is_valid() || + roiShort.empty() || + roiShort.n_ranges() == 0 || + roiShort.size() <= target_roi.second || + roiShort.is_void(target_roi.second) ) + continue; + + auto range_number = roiShort.find_range_iterator(target_roi.second) - roiShort.begin_range(); + + ROIMatchedHitMap[std::make_pair(target_chanROI.Channel(), range_number)].push_back(i_h); + } + } +} + +//--- CalcSubROIProperties --- +std::vector sys::WireModUtility::CalcSubROIProperties(sys::WireModUtility::ROIProperties_t const& roi_properties, std::vector const& hitPtrVec) +{ + std::vector subroi_properties_vec; + sys::WireModUtility::SubROIProperties_t subroi_properties; + subroi_properties.channel = roi_properties.channel; + subroi_properties.view = roi_properties.view; + + // if this ROI doesn't contain any hits, define subROI based on ROI properities + // otherwise, define subROIs based on hits + if (hitPtrVec.size() == 0) + { + subroi_properties.key = std::make_pair(roi_properties.key, 0); + subroi_properties.total_q = roi_properties.total_q; + subroi_properties.center = roi_properties.center; + subroi_properties.sigma = roi_properties.sigma; + subroi_properties_vec.push_back(subroi_properties); + } else + { + for (unsigned int i_h=0; i_h < hitPtrVec.size(); ++i_h) + { + auto hit_ptr = hitPtrVec[i_h]; + subroi_properties.key = std::make_pair(roi_properties.key, i_h); + subroi_properties.total_q = hit_ptr->Integral(); + subroi_properties.center = hit_ptr->PeakTime(); + subroi_properties.sigma = hit_ptr->RMS(); + subroi_properties_vec.push_back(subroi_properties); + } + } + + return subroi_properties_vec; +} + +//--- MatchEdepsToSubROIs --- +std::map> sys::WireModUtility::MatchEdepsToSubROIs(std::vector const& subROIPropVec, + std::vector const& edepPtrVec, double offset) +{ + // for each TrackID, which EDeps are associated with it? keys are TrackIDs + std::map> TrackIDMatchedEDepMap; + + // total energy of EDeps matched to the ROI (not strictly necessary, but useful for understanding/development + double total_energy = 0.0; + + // loop over edeps, fill TrackIDMatchedEDepMap and calculate total energy + for (auto const& edep_ptr : edepPtrVec) + { + TrackIDMatchedEDepMap[edep_ptr->TrackID()].push_back(edep_ptr); + total_energy += edep_ptr->E(); + } + + // map it all out + std::map> EDepMatchedSubROIMap; // keys are indexes of edepPtrVec, values are vectors of indexes of subROIPropVec + std::map> TrackIDMatchedSubROIMap; // keys are TrackIDs, values are sets of indexes of subROIPropVec + std::map> SubROIMatchedEDepMap; // keys are indexes of subROIPropVec, values are vectors of indexes of edepPtrVec + std::map> SubROIMatchedTrackEnergyMap; // keys are indexes of subROIPropVec, values are maps of TrackIDs to matched energy (in MeV) + + // loop over EDeps + for (unsigned int i_e = 0; i_e < edepPtrVec.size(); ++i_e) + { + // get EDep properties + auto edep_ptr = edepPtrVec[i_e]; + const geo::TPCGeo& curTPCGeom = geometry->PositionToTPC(edep_ptr->MidPoint()); + std::map planeProjEdepTick; + for (const auto& planeGeom : wireReadout->Iterate(curTPCGeom.ID())) + { + double projTick = detPropData.ConvertXToTicks(edep_ptr->X(), planeGeom.ID()) + offset + tickOffset; + readout::ROPID ropID = wireReadout->WirePlaneToROP(planeGeom.ID()); + if (planeProjEdepTick.count(ropID) == 1) + { + std::cout << "MatchEdepsToSubROIs - Warning, overwriting tick projection at" << ropID.toString() + << "\n Changing from " << planeProjEdepTick[ropID] << " to " << projTick << std::endl; + } + planeProjEdepTick[ropID] = projTick; + } + + // loop over subROIs + unsigned int closest_hit = std::numeric_limits::max(); + float min_dist = std::numeric_limits::max(); + for (unsigned int i_h = 0; i_h < subROIPropVec.size(); ++i_h) + { + auto subroi_prop = subROIPropVec[i_h]; + readout::ROPID subroiROP = wireReadout->ChannelToROP(subroi_prop.channel); + double edep_tick = planeProjEdepTick.at(subroiROP); + if (edep_tick > subroi_prop.center-subroi_prop.sigma && edep_tick < subroi_prop.center+subroi_prop.sigma) + { + EDepMatchedSubROIMap[i_e].push_back(i_h); + TrackIDMatchedSubROIMap[edep_ptr->TrackID()].emplace(i_h); + } + float hit_dist = std::abs(edep_tick - subroi_prop.center) / subroi_prop.sigma; + if (hit_dist < min_dist) + { + closest_hit = i_h; + min_dist = hit_dist; + } + } + + // if EDep is less than 2.5 units away from closest subROI, assign it to that subROI + if (min_dist < 5) // try 5 for testing purposes + { + auto i_h = closest_hit; + SubROIMatchedEDepMap[i_h].push_back(i_e); + SubROIMatchedTrackEnergyMap[i_h][edep_ptr->TrackID()] += edep_ptr->E(); + } + } + + // convert to desired format (possibly a better way to do this...?) + std::map> ReturnMap; + for (auto it_h = SubROIMatchedEDepMap.begin(); it_h != SubROIMatchedEDepMap.end(); ++it_h) + { + auto key = subROIPropVec[it_h->first].key; + for (auto const& i_e : it_h->second) + { + ReturnMap[key].push_back(edepPtrVec[i_e]); + } + } + + return ReturnMap; +} + +//--- CalcPropertiesFromEdeps --- +sys::WireModUtility::TruthProperties_t sys::WireModUtility::CalcPropertiesFromEdeps(std::vector const& edepPtrVec, double offset) +{ + //split the edeps by TrackID + std::map< int, std::vector > edepptrs_by_trkid; + std::map< int, double > energy_per_trkid; + for(auto const& edep_ptr : edepPtrVec) + { + edepptrs_by_trkid[edep_ptr->TrackID()].push_back(edep_ptr); + energy_per_trkid[edep_ptr->TrackID()]+=edep_ptr->E(); + } + + int trkid_max = std::numeric_limits::min(); + double energy_max = std::numeric_limits::min(); + for(auto const& e_p_id : energy_per_trkid) + { + if(e_p_id.second > energy_max) + { + trkid_max = e_p_id.first; + energy_max = e_p_id.second; + } + } + + auto edepPtrVecMaxE = edepptrs_by_trkid[trkid_max]; + + //first, let's loop over all edeps and get an average weight scale... + sys::WireModUtility::TruthProperties_t edep_props; + double total_energy_all = 0.0; + sys::WireModUtility::ScaleValues_t scales_e_weighted[3]; + for(size_t i_p = 0; i_p < 3; ++i_p) + { + scales_e_weighted[i_p].r_Q = 0.0; + scales_e_weighted[i_p].r_sigma = 0.0; + } + + for(auto const edep_ptr : edepPtrVec) + { + if (edep_ptr->StepLength() == 0) + continue; + + geo::TPCID curTPCID; + try { + curTPCID = geometry->PositionToTPC(edep_ptr->MidPoint()).ID(); + } + catch(...) {continue;} // ignore non-active depositions + + edep_props.x = edep_ptr->X(); + edep_props.y = edep_ptr->Y(); + edep_props.z = edep_ptr->Z(); + + edep_props.dxdr = (edep_ptr->EndX() - edep_ptr->StartX()) / edep_ptr->StepLength(); + edep_props.dydr = (edep_ptr->EndY() - edep_ptr->StartY()) / edep_ptr->StepLength(); + edep_props.dzdr = (edep_ptr->EndZ() - edep_ptr->StartZ()) / edep_ptr->StepLength(); + edep_props.dedr = edep_ptr->E() / edep_ptr->StepLength(); + + total_energy_all += edep_ptr->E(); + + for (auto const& plane : wireReadout->Iterate(curTPCID)) { + int i_p = plane.ID().Plane; + auto scales = GetViewScaleValues(edep_props, plane.View()); + scales_e_weighted[i_p].r_Q += edep_ptr->E()*scales.r_Q; + scales_e_weighted[i_p].r_sigma += edep_ptr->E()*scales.r_sigma; + } + } + + for(size_t i_p = 0; i_p < 3; ++i_p) + { + if (total_energy_all > 0) + { + scales_e_weighted[i_p].r_Q = scales_e_weighted[i_p].r_Q / total_energy_all; + scales_e_weighted[i_p].r_sigma = scales_e_weighted[i_p].r_sigma / total_energy_all; + } + if (scales_e_weighted[i_p].r_Q == 0) + scales_e_weighted[i_p].r_Q = 1; + if (scales_e_weighted[i_p].r_sigma == 0) + scales_e_weighted[i_p].r_sigma = 1; + } + + TruthProperties_t edep_col_properties; + + //copy in the scales that were calculated before + for(size_t i_p = 0; i_p < 3; ++i_p) + { + edep_col_properties.scales_avg[i_p].r_Q = scales_e_weighted[i_p].r_Q; + edep_col_properties.scales_avg[i_p].r_sigma = scales_e_weighted[i_p].r_sigma; + } + + // calculations happen here + edep_col_properties.x = 0.; + edep_col_properties.x_rms = 0.; + edep_col_properties.x_rms_noWeight = 0.; + edep_col_properties.x_min = std::numeric_limits::max(); + edep_col_properties.x_max = std::numeric_limits::min(); + + edep_col_properties.y = 0.; + edep_col_properties.z = 0.; + edep_col_properties.dxdr = 0.; + edep_col_properties.dydr = 0.; + edep_col_properties.dzdr = 0.; + edep_col_properties.dedr = 0.; + + double total_energy = 0.0; + for (auto const& edep_ptr : edepPtrVecMaxE) + { + edep_col_properties.x += edep_ptr->X()*edep_ptr->E(); + edep_col_properties.x_min = (edep_ptr->X() < edep_col_properties.x_min) ? edep_ptr->X() : edep_col_properties.x_min; + edep_col_properties.x_max = (edep_ptr->X() > edep_col_properties.x_max) ? edep_ptr->X() : edep_col_properties.x_max; + total_energy += edep_ptr->E(); + edep_col_properties.y += edep_ptr->Y()*edep_ptr->E(); + edep_col_properties.z += edep_ptr->Z()*edep_ptr->E(); + + if (edep_ptr->StepLength() == 0) + continue; + + edep_col_properties.dxdr += edep_ptr->E()*(edep_ptr->EndX() - edep_ptr->StartX()) / edep_ptr->StepLength(); + edep_col_properties.dydr += edep_ptr->E()*(edep_ptr->EndY() - edep_ptr->StartY()) / edep_ptr->StepLength(); + edep_col_properties.dzdr += edep_ptr->E()*(edep_ptr->EndZ() - edep_ptr->StartZ()) / edep_ptr->StepLength(); + edep_col_properties.dedr += edep_ptr->E()*edep_ptr->E() / edep_ptr->StepLength(); + } + + if (total_energy > 0) + { + edep_col_properties.x = edep_col_properties.x / total_energy; + edep_col_properties.y = edep_col_properties.y / total_energy; + edep_col_properties.z = edep_col_properties.z / total_energy; + edep_col_properties.dxdr = edep_col_properties.dxdr / total_energy; + edep_col_properties.dydr = edep_col_properties.dydr / total_energy; + edep_col_properties.dzdr = edep_col_properties.dzdr / total_energy; + edep_col_properties.dedr = edep_col_properties.dedr / total_energy; + } + + for (auto const& edep_ptr : edepPtrVecMaxE) + { + edep_col_properties.x_rms += (edep_ptr->X()-edep_col_properties.x)*(edep_ptr->X()-edep_col_properties.x)*edep_ptr->E(); + edep_col_properties.x_rms_noWeight += (edep_ptr->X()-edep_col_properties.x)*(edep_ptr->X()-edep_col_properties.x); + } + edep_col_properties.x_rms_noWeight = std::sqrt(edep_col_properties.x_rms_noWeight); + + if (total_energy > 0) { + edep_col_properties.x_rms = std::sqrt(edep_col_properties.x_rms/total_energy); + } + + // get ticks, etc. if the deposition is active + geo::TPCID tpcGeomID; + try { + tpcGeomID = geometry->PositionToTPC({edep_col_properties.x, edep_col_properties.y, edep_col_properties.z}).ID(); + } + catch(...) {} + + if (tpcGeomID.isValid) { + // projecting edep to plane0, so be mindful when accessing the tick value + const auto plane0 = wireReadout->FirstPlane(tpcGeomID); + double ticksPercm = detPropData.GetXTicksCoefficient(); // this should be by TPCID, but isn't building right now + edep_col_properties.tick = detPropData.ConvertXToTicks(edep_col_properties.x , plane0.ID()) + offset + tickOffset; + edep_col_properties.tick_rms = ticksPercm*edep_col_properties.x_rms; + edep_col_properties.tick_rms_noWeight = ticksPercm*edep_col_properties.x_rms_noWeight; + edep_col_properties.tick_min = detPropData.ConvertXToTicks(edep_col_properties.x_min, plane0.ID()) + offset + tickOffset; + edep_col_properties.tick_max = detPropData.ConvertXToTicks(edep_col_properties.x_max, plane0.ID()) + offset + tickOffset; + edep_col_properties.total_energy = total_energy; + } + + + return edep_col_properties; +} + +//--- WireToTrajectoryPosition (SCE: at-the-wire -> true trajectory frame) --- +geo::Point_t sys::WireModUtility::WireToTrajectoryPosition(const geo::Point_t& loc, const geo::TPCID& tpc) const +{ + geo::Point_t ret = loc; + if (fSCE && fSCE->EnableSimSpatialSCE()) + { + geo::Vector_t offset = fSCE->GetCalPosOffsets(ret, tpc.TPC); + ret.SetX(ret.X() + offset.X()); + ret.SetY(ret.Y() + offset.Y()); + ret.SetZ(ret.Z() + offset.Z()); + } + return ret; +} + +//--- TrajectoryToWirePosition (SCE: true -> at-the-wire frame) --- +geo::Point_t sys::WireModUtility::TrajectoryToWirePosition(const geo::Point_t& loc, const geo::Vector_t& driftdir) const +{ + geo::Point_t ret = loc; + int corr = driftdir.X(); // returned X offset is the drift; sign it by the drift direction + if (fSCE && fSCE->EnableSimSpatialSCE()) + { + geo::Vector_t offset = fSCE->GetPosOffsets(ret); + ret.SetX(ret.X() + corr * offset.X()); + ret.SetY(ret.Y() + offset.Y()); + ret.SetZ(ret.Z() + offset.Z()); + } + return ret; +} + +//--- MatchIDEsToSubROIs --- +// SimChannel/IDE analogue of MatchEdepsToSubROIs. Each IDE already carries its readout tick +// (no per-plane X->tick projection needed), so it is assigned to the closest sub-ROI whose +// distance |tick - center|/sigma is below the same threshold used for edeps (5 sigma). +std::map> +sys::WireModUtility::MatchIDEsToSubROIs(std::vector const& subROIPropVec, + std::vector const& idePtrVec) +{ + std::map> SubROIMatchedIDEMap; // subROI idx -> ide idxs + + for (unsigned int i_e = 0; i_e < idePtrVec.size(); ++i_e) + { + double ide_tick = idePtrVec[i_e]->tick; + unsigned int closest = std::numeric_limits::max(); + float min_dist = std::numeric_limits::max(); + for (unsigned int i_h = 0; i_h < subROIPropVec.size(); ++i_h) + { + auto const& s = subROIPropVec[i_h]; + if (s.sigma <= 0) continue; + float dist = std::abs((float)(ide_tick - s.center)) / s.sigma; + if (dist < min_dist) { min_dist = dist; closest = i_h; } + } + if (closest != std::numeric_limits::max() && min_dist < 5) + SubROIMatchedIDEMap[closest].push_back(i_e); + } + + std::map> ReturnMap; + for (auto const& kv : SubROIMatchedIDEMap) + { + auto key = subROIPropVec[kv.first].key; + for (auto const& i_e : kv.second) + ReturnMap[key].push_back(idePtrVec[i_e]); + } + return ReturnMap; +} + +//--- CalcPropertiesFromIDEs --- +// SimChannel/IDE analogue of CalcPropertiesFromEdeps. Picks the dominant track (max summed +// energy, abs(trackID) as in TrackCaloSkimmer), builds the numElectrons-weighted centroid in +// the SCE-undone (trajectory) frame, and reads the direction (dxdr/dydr/dzdr) from the matched +// MCParticle's nearest trajectory point. dedr/dqdr use the trajectory-derived pitch. The +// scales_avg pre-pass of the edep version is dropped (those fields are never read downstream). +sys::WireModUtility::TruthProperties_t +sys::WireModUtility::CalcPropertiesFromIDEs(std::vector const& idePtrVec, + std::map const& particleMap) +{ + TruthProperties_t props; + props.x = props.y = props.z = 0.f; + props.x_rms = props.x_rms_noWeight = 0.f; + props.x_min = std::numeric_limits::max(); + props.x_max = std::numeric_limits::lowest(); + props.tick = props.tick_rms = props.tick_rms_noWeight = 0.f; + props.tick_min = std::numeric_limits::max(); + props.tick_max = std::numeric_limits::lowest(); + props.dxdr = props.dydr = props.dzdr = 0.; + props.dedr = props.dqdr = 0.; + props.total_energy = 0.f; + for (size_t i_p = 0; i_p < 3; ++i_p) { props.scales_avg[i_p].r_Q = 1.; props.scales_avg[i_p].r_sigma = 1.; } + if (idePtrVec.empty()) return props; + + // dominant track = max summed energy (abs(trackID), matching TrackCaloSkimmer) + std::map energy_per_trkid; + for (auto const& m : idePtrVec) + energy_per_trkid[std::abs(m->ide->trackID)] += m->ide->energy; + int trkid_max = 0; double energy_max = -1.; + for (auto const& e : energy_per_trkid) + if (e.second > energy_max) { energy_max = e.second; trkid_max = e.first; } + + // numElectrons-weighted centroid in the SCE-undone frame + energy/electron/tick sums + struct Acc { double xtraj, tick, ne; }; + std::vector accs; + double sumNe = 0., sumE = 0., cx = 0., cy = 0., cz = 0., tsum = 0.; + geo::WireID domWire; + for (auto const& m : idePtrVec) + { + if (std::abs(m->ide->trackID) != trkid_max) continue; + double ne = m->ide->numElectrons; + double e = m->ide->energy; + geo::Point_t p_raw(m->ide->x, m->ide->y, m->ide->z); + geo::Point_t p_traj = WireToTrajectoryPosition(p_raw, m->wire); + cx += ne * p_traj.X(); cy += ne * p_traj.Y(); cz += ne * p_traj.Z(); + tsum += ne * m->tick; + sumNe += ne; sumE += e; + accs.push_back({p_traj.X(), m->tick, ne}); + if (p_traj.X() < props.x_min) props.x_min = p_traj.X(); + if (p_traj.X() > props.x_max) props.x_max = p_traj.X(); + if (m->tick < props.tick_min) props.tick_min = m->tick; + if (m->tick > props.tick_max) props.tick_max = m->tick; + domWire = m->wire; + } + if (sumNe <= 0) return props; + cx /= sumNe; cy /= sumNe; cz /= sumNe; + props.x = cx; props.y = cy; props.z = cz; + props.total_energy = sumE; + props.tick = tsum / sumNe; + + double xr = 0., xrnw = 0., tr = 0.; + for (auto const& a : accs) + { + xr += a.ne * (a.xtraj - cx) * (a.xtraj - cx); + xrnw += (a.xtraj - cx) * (a.xtraj - cx); + tr += a.ne * (a.tick - props.tick) * (a.tick - props.tick); + } + props.x_rms = std::sqrt(xr / sumNe); + props.x_rms_noWeight = std::sqrt(xrnw); + props.tick_rms = std::sqrt(tr / sumNe); + props.tick_rms_noWeight = props.x_rms_noWeight; + + // direction / pitch from the matched MCParticle trajectory (nearest point to centroid) + auto pit = particleMap.find(trkid_max); + if (pit != particleMap.end() && pit->second != nullptr) + { + const simb::MCParticle* part = pit->second; + TVector3 hp(cx, cy, cz); + double closest = -1.; TVector3 dir; + for (unsigned int i = 0; i < part->NumberTrajectoryPoints(); ++i) + { + double d = (part->Position(i).Vect() - hp).Mag(); + if (closest < 0. || d < closest) + { + closest = d; + dir = part->Momentum(i).Vect().Unit(); + } + } + if (closest >= 0. && dir.Mag() > 1e-4) + { + props.dxdr = dir.X(); + props.dydr = dir.Y(); + props.dzdr = dir.Z(); + + if (domWire.isValid) + { + geo::PlaneID plane(domWire.Cryostat, domWire.TPC, domWire.Plane); + geo::PlaneGeo const& pg = wireReadout->Plane(plane); + double angletovert = wireReadout->WireAngleToVertical(pg.View(), plane) - 0.5 * ::util::pi<>(); + double cosgamma = std::abs(std::cos(angletovert) * dir.Z() + std::sin(angletovert) * dir.Y()); + double pitch = (cosgamma > 1e-6) ? pg.WirePitch() / cosgamma : pg.WirePitch(); + if (pitch > 0) + { + props.dedr = sumE / pitch; // dE/dx [MeV/cm] + props.dqdr = sumNe / pitch; // dQ/dx [electrons/cm] + } + } + } + } + + return props; +} + +// TGraph2D lookup: interpolate=true uses Delaunay (clamped), false uses nearest bin center. +static double g2dLookup(TGraph2D* g, double x, double y, bool interpolate) +{ + if (interpolate) + return g->Interpolate(std::clamp(x, g->GetXmin(), g->GetXmax()), + std::clamp(y, g->GetYmin(), g->GetYmax())); + + int n = g->GetN(); + double* xs = g->GetX(); + double* ys = g->GetY(); + double* zs = g->GetZ(); + + std::vector ux(xs, xs + n), uy(ys, ys + n); + std::sort(ux.begin(), ux.end()); + ux.erase(std::unique(ux.begin(), ux.end()), ux.end()); + std::sort(uy.begin(), uy.end()); + uy.erase(std::unique(uy.begin(), uy.end()), uy.end()); + + double qx = std::clamp(x, ux.front(), ux.back()); + double qy = std::clamp(y, uy.front(), uy.back()); + + auto findBin = [](std::vector const& c, double q) -> int { + int m = static_cast(c.size()); + if (m == 1) return 0; + double left = c[0] - (c[1] - c[0]) * 0.5; + for (int i = 0; i < m; ++i) { + double right = 2.0 * c[i] - left; + if (q < right || i == m - 1) return i; + left = right; + } + return m - 1; + }; + + double cx = ux[findBin(ux, qx)]; + double cy = uy[findBin(uy, qy)]; + + for (int i = 0; i < n; ++i) + if (std::abs(xs[i] - cx) < 1e-9 && std::abs(ys[i] - cy) < 1e-9) + return zs[i]; + + return 1.0; // bin not populated in the map (sparse grid) — neutral scale +} + +//--- GetScaleValues --- +sys::WireModUtility::ScaleValues_t sys::WireModUtility::GetScaleValues(sys::WireModUtility::TruthProperties_t const& truth_props, sys::WireModUtility::ROIProperties_t const& roi_vals) +{ + sys::WireModUtility::ScaleValues_t scales; + sys::WireModUtility::ScaleValues_t channelScales = GetChannelScaleValues(truth_props, roi_vals.channel); + sys::WireModUtility::ScaleValues_t viewScales = GetViewScaleValues(truth_props, roi_vals.view); + scales.r_Q = channelScales.r_Q * viewScales.r_Q; + scales.r_sigma = channelScales.r_sigma * viewScales.r_sigma; + return scales; +} + +//--- GetChannelScaleValues --- +sys::WireModUtility::ScaleValues_t sys::WireModUtility::GetChannelScaleValues(sys::WireModUtility::TruthProperties_t const& truth_props, raw::ChannelID_t const& channel) +{ + // initialize return + sys::WireModUtility::ScaleValues_t scales; + scales.r_Q = 1.0; + scales.r_sigma = 1.0; + + // try to get geo + // if not in a TPC return default values + double const truth_coords[3] = {truth_props.x, truth_props.y, truth_props.z}; + geo::TPCGeo const* curTPCGeomPtr = geometry->PositionToTPCptr(geo::vect::makePointFromCoords(truth_coords)); + if (curTPCGeomPtr == nullptr) + return scales; + + if (applyChannelScale) + { + if (spline_Charge_Channel == nullptr || + spline_Sigma_Channel == nullptr ) + throw cet::exception("WireModUtility") + << "Tried to apply channel scale factor, but could not find splines. Check that you have set those in the utility."; + scales.r_Q *= spline_Charge_Channel->Eval(channel); + scales.r_sigma *= spline_Sigma_Channel ->Eval(channel); + } + + return scales; +} + +//--- GetViewScaleValues --- +sys::WireModUtility::ScaleValues_t sys::WireModUtility::GetViewScaleValues(sys::WireModUtility::TruthProperties_t const& truth_props, geo::View_t const& view) +{ + // initialize return + sys::WireModUtility::ScaleValues_t scales; + scales.r_Q = 1.0; + scales.r_sigma = 1.0; + + double temp_scale=1.0; + + // try to get geo + // if not in a TPC return default values + double const truth_coords[3] = {truth_props.x, truth_props.y, truth_props.z}; + geo::TPCGeo const* curTPCGeomPtr = geometry->PositionToTPCptr(geo::vect::makePointFromCoords(truth_coords)); + if (curTPCGeomPtr == nullptr) + return scales; + + // get the plane number by the view + auto const& plane_obj = wireReadout->Plane(curTPCGeomPtr->ID(), view); + unsigned int plane = plane_obj.ID().Plane; + + if (applyXScale) + { + if (splines_Charge_X[plane] == nullptr || + splines_Sigma_X [plane] == nullptr ) + throw cet::exception("WireModUtility") + << "Tried to apply X scale factor, but could not find splines. Check that you have set those in the utility."; + scales.r_Q *= splines_Charge_X[plane]->Eval(truth_props.x); + scales.r_sigma *= splines_Sigma_X [plane]->Eval(truth_props.x); + } + + if (applyYZScale) + { + if (graph2Ds_Charge_YZ[plane] == nullptr || + graph2Ds_Sigma_YZ [plane] == nullptr ) + throw cet::exception("WireModUtility") + << "Tried to apply YZ scale factor, but could not find graphs. Check that you have set those in the utility."; + temp_scale = g2dLookup(graph2Ds_Charge_YZ[plane], truth_props.z, truth_props.y, useGraph2DInterpolation); + if(temp_scale>0.001) scales.r_Q *= temp_scale; + + temp_scale = g2dLookup(graph2Ds_Sigma_YZ [plane], truth_props.z, truth_props.y, useGraph2DInterpolation); + if(temp_scale>0.001) scales.r_sigma *= temp_scale; + } + + if (applyXXWScale) + { + if (graph2Ds_Charge_XXW[plane] == nullptr || + graph2Ds_Sigma_XXW [plane] == nullptr ) + throw cet::exception("WireModUtility") + << "Tried to apply XXW scale factor, but could not find graphs. Check that you have set those in the utility."; + double thXW = ThetaXW(truth_props.dxdr, truth_props.dydr, truth_props.dzdr, plane_obj.ThetaZ()); + temp_scale = g2dLookup(graph2Ds_Charge_XXW[plane], truth_props.x, thXW, useGraph2DInterpolation); + if(temp_scale>0.001) scales.r_Q *= temp_scale; + + temp_scale = g2dLookup(graph2Ds_Sigma_XXW [plane], truth_props.x, thXW, useGraph2DInterpolation); + if(temp_scale>0.001) scales.r_sigma *= temp_scale; + } + + if (applyXZAngleScale) + { + if (splines_Charge_XZAngle[plane] == nullptr || + splines_Sigma_XZAngle [plane] == nullptr ) + throw cet::exception("WireModUtility") + << "Tried to apply XZ-angle scale factor, but could not find splines. Check that you have set those in the utility."; + scales.r_Q *= splines_Charge_XZAngle[plane]->Eval(ThetaXZ_PlaneRel(truth_props.dxdr, truth_props.dydr, truth_props.dzdr, plane_obj.ThetaZ())); + scales.r_sigma *= splines_Sigma_XZAngle [plane]->Eval(ThetaXZ_PlaneRel(truth_props.dxdr, truth_props.dydr, truth_props.dzdr, plane_obj.ThetaZ())); + } + if (applyYZAngleScale) + { + if (splines_Charge_YZAngle[plane] == nullptr || + splines_Sigma_YZAngle [plane] == nullptr ) + throw cet::exception("WireModUtility") + << "Tried to apply YZ-angle scale factor, but could not find splines. Check that you have set those in the utility."; + scales.r_Q *= splines_Charge_YZAngle[plane]->Eval(ThetaYZ_PlaneRel(truth_props.dxdr, truth_props.dydr, truth_props.dzdr, plane_obj.ThetaZ())); + scales.r_sigma *= splines_Sigma_YZAngle [plane]->Eval(ThetaYZ_PlaneRel(truth_props.dxdr, truth_props.dydr, truth_props.dzdr, plane_obj.ThetaZ())); + } + + if(applydEdXScale) + { + if (splines_Charge_dEdX[plane] == nullptr || + splines_Sigma_dEdX [plane] == nullptr ) + throw cet::exception("WireModUtility") + << "Tried to apply dEdX scale factor, but could not find splines. Check that you have set those in the utility."; + scales.r_Q *= splines_Charge_dEdX[plane]->Eval(truth_props.dedr); + scales.r_sigma *= splines_Sigma_dEdX [plane]->Eval(truth_props.dedr); + } + + if (applyXXZAngleScale) + { + if (graph2Ds_Charge_XXZAngle[plane] == nullptr || + graph2Ds_Sigma_XXZAngle [plane] == nullptr ) + throw cet::exception("WireModUtility") + << "Tried to apply XXZAngle scale factor, but could not find graphs. Check that you have set those in the utility."; + temp_scale = g2dLookup(graph2Ds_Charge_XXZAngle[plane], truth_props.x, ThetaXZ_PlaneRel(truth_props.dxdr, truth_props.dydr, truth_props.dzdr, plane_obj.ThetaZ()), useGraph2DInterpolation); + if(temp_scale>0.001) scales.r_Q *= temp_scale; + + temp_scale = g2dLookup(graph2Ds_Sigma_XXZAngle [plane], truth_props.x, ThetaXZ_PlaneRel(truth_props.dxdr, truth_props.dydr, truth_props.dzdr, plane_obj.ThetaZ()), useGraph2DInterpolation); + if(temp_scale>0.001) scales.r_sigma *= temp_scale; + } + + if (applyXdQdXScale) + { + if (graph2Ds_Charge_XdQdX[plane] == nullptr || + graph2Ds_Sigma_XdQdX [plane] == nullptr ) + throw cet::exception("WireModUtility") + << "Tried to apply XdQdX scale factor, but could not find graphs. Check that you have set those in the utility."; + temp_scale = g2dLookup(graph2Ds_Charge_XdQdX[plane], truth_props.x, truth_props.dqdr, useGraph2DInterpolation); + if(temp_scale>0.001) scales.r_Q *= temp_scale; + + temp_scale = g2dLookup(graph2Ds_Sigma_XdQdX [plane], truth_props.x, truth_props.dqdr, useGraph2DInterpolation); + if(temp_scale>0.001) scales.r_sigma *= temp_scale; + } + + if (applyXZAngledQdXScale) + { + if (graph2Ds_Charge_XZAngledQdX[plane] == nullptr || + graph2Ds_Sigma_XZAngledQdX [plane] == nullptr ) + throw cet::exception("WireModUtility") + << "Tried to apply XZAngledQdX scale factor, but could not find graphs. Check that you have set those in the utility."; + temp_scale = g2dLookup(graph2Ds_Charge_XZAngledQdX[plane], ThetaXZ_PlaneRel(truth_props.dxdr, truth_props.dydr, truth_props.dzdr, plane_obj.ThetaZ()), truth_props.dqdr, useGraph2DInterpolation); + if(temp_scale>0.001) scales.r_Q *= temp_scale; + + temp_scale = g2dLookup(graph2Ds_Sigma_XZAngledQdX [plane], ThetaXZ_PlaneRel(truth_props.dxdr, truth_props.dydr, truth_props.dzdr, plane_obj.ThetaZ()), truth_props.dqdr, useGraph2DInterpolation); + if(temp_scale>0.001) scales.r_sigma *= temp_scale; + } + + return scales; +} + +//--- ModifyROI --- +void sys::WireModUtility::ModifyROI(std::vector & roi_data, + sys::WireModUtility::ROIProperties_t const& roi_prop, + std::vector const& subROIPropVec, + std::map const& subROIScaleMap, + double sigmaWindow) +{ + // do you want a bunch of messages? + bool verbose = false; + + // initialize some values + double q_orig = 0.0; + double q_mod = 0.0; + double scale_ratio = 1.0; + double sigma_distance = 0.0; + + // loop over the ticks + for(size_t i_t = 0; i_t < roi_data.size(); ++i_t) + { + // reset your values + q_orig = 0.0; + q_mod = 0.0; + scale_ratio = 1.0; + sigma_distance = 0.0; + + double t = i_t + roi_prop.begin; + + // loop over the subs + for (auto const& subroi_prop : subROIPropVec) + { + // get your scale vals + auto scale_vals = subROIScaleMap.find(subroi_prop.key)->second; + + q_orig += gausFunc(i_t + roi_prop.begin, subroi_prop.center, subroi_prop.sigma, subroi_prop.total_q); + q_mod += gausFunc(i_t + roi_prop.begin, subroi_prop.center, scale_vals.r_sigma * subroi_prop.sigma, scale_vals.r_Q * subroi_prop.total_q); + sigma_distance += ((i_t + roi_prop.begin - subroi_prop.center)*(i_t + roi_prop.begin - subroi_prop.center) / (subroi_prop.sigma*subroi_prop.sigma))*\ + gausFunc(i_t + roi_prop.begin, subroi_prop.center, subroi_prop.sigma, subroi_prop.total_q); + + if (verbose) + std::cout << " tick " << t << " subROI center=" << subroi_prop.center + << " dist=" << sigma_distance << "sigma" + << (sigma_distance <= sigmaWindow ? " [scaled]" : " [pass-through]") << '\n' + << " q_orig=" << q_orig << '\n'; + } + + double delta = q_mod - q_orig; + + // do some sanity checks + if (isnan(q_mod)) { + if (verbose) + std::cout << "WARNING: obtained q_mod = NaN... setting scale to 0" << std::endl; + // for additive correction, no cutoffs are applied + } else if (additiveModification) { + roi_data[i_t] += static_cast(delta); + } else if (q_orig < 0.01) { // check that this is a sane limit + if (verbose) std::cout << "WARNING: obtained q_orig < 0.01 ... setting scale to 1" << std::endl; + } else if (sigma_distance > 9.) { + if (verbose) std::cout << "WARNING: sigma_distance > 9 ... setting scale to 1" << std::endl; + } else { + scale_ratio = q_mod / q_orig; + roi_data[i_t] = scale_ratio * roi_data[i_t]; + } + + if (verbose) + std::cout << "\t tick " << i_t << ":" + << " data=" << roi_data[i_t] + << ", q_orig=" << q_orig + << ", q_mod=" << q_mod + << ", scale_ratio=" << scale_ratio << " [multiplicative]" + << ", delta=" << delta << " [additive]" << std::endl; + } + + // we're done now + return; +} diff --git a/sbncode/WireMod/Utility/WireModUtility.hh b/sbncode/WireMod/Utility/WireModUtility.hh new file mode 100644 index 000000000..885a248f0 --- /dev/null +++ b/sbncode/WireMod/Utility/WireModUtility.hh @@ -0,0 +1,332 @@ +#ifndef sbncode_WireMod_Utility_WireModUtility_hh +#define sbncode_WireMod_Utility_WireModUtility_hh + +//std includes +#include + +//ROOT includes +#include "TFile.h" +#include "TSpline.h" +#include "TGraph2D.h" +#include "TNtuple.h" + +//Framework includes +#include "larcorealg/Geometry/GeometryCore.h" +#include "larcorealg/Geometry/WireReadoutGeom.h" +#include "lardataalg/DetectorInfo/DetectorPropertiesData.h" +#include "lardataalg/DetectorInfo/DetectorClocksData.h" +#include "larevt/SpaceCharge/SpaceCharge.h" +#include "lardataobj/RecoBase/Track.h" +#include "lardataobj/RecoBase/TrackHitMeta.h" +#include "lardataobj/RecoBase/Hit.h" +#include "lardataobj/RecoBase/Wire.h" +#include "lardataobj/Simulation/SimEnergyDeposit.h" +#include "lardataobj/Simulation/SimChannel.h" +#include "larcoreobj/SimpleTypesAndConstants/PhysicalConstants.h" +#include "sbnobj/ICARUS/TPC/ChannelROI.h" +#include "nusimdata/SimulationBase/MCParticle.h" +#include "nusimdata/SimulationBase/MCTruth.h" + +#include +#include + +namespace sys { + class WireModUtility{ + // for now make everything public, though it's probably a good idea to think about what doesn't need to be + public: + // detector constants, should be set by geometry service + // the notes at the end refer to their old names in the MircoBooNE code which preceded this + // TODO: how best to initialize the splines/graphs? + const geo::GeometryCore* geometry; // save the TPC geometry + const geo::WireReadoutGeom* wireReadout; // new for LarSoft v10 + const detinfo::DetectorPropertiesData& detPropData; // save the detector property data + bool applyChannelScale; // do we scale with channel? + bool applyXScale; // do we scale with X? + bool applyYZScale; // do we scale with YZ? + bool applyXZAngleScale; // do we scale with XZ angle? + bool applyYZAngleScale; // do we scale with YZ angle? + bool applydEdXScale; // do we scale with dEdx? + bool applyXXZAngleScale; // do we scale with X vs XZ angle? + bool applyXdQdXScale; // do we scale with X vs dQ/dX? + bool applyXZAngledQdXScale; // do we scale with XZ angle vs dQ/dX? + bool applyXXWScale; // do we scale with X vs ThXW? + bool additiveModification; // additive (true) vs multiplicative (false) ROI modification + bool useGraph2DInterpolation; // true = Delaunay interp (default), false = nearest bin center + double readoutWindowTicks; // how many ticks are in the readout window? + double tickOffset; // do we want an offset in the ticks? + + TSpline3* spline_Charge_Channel; // the spline for the charge correction by channel + TSpline3* spline_Sigma_Channel; // the spline for the width correction by channel + std::vector splines_Charge_X; // the splines for the charge correction in X + std::vector splines_Sigma_X; // the splines for the width correction in X + std::vector splines_Charge_XZAngle; // the splines for the charge correction in XZ angle + std::vector splines_Sigma_XZAngle; // the splines for the width correction in XZ angle + std::vector splines_Charge_YZAngle; // the splines for the charge correction in YZ angle + std::vector splines_Sigma_YZAngle; // the splines for the width correction in YZ angle + std::vector splines_Charge_dEdX; // the splines for the charge correction in dEdX + std::vector splines_Sigma_dEdX; // the splines for the width correction in dEdX + std::vector graph2Ds_Charge_YZ; // the graphs for the charge correction in YZ + std::vector graph2Ds_Sigma_YZ; // the graphs for the width correction in YZ + + std::vector graph2Ds_Charge_XXZAngle; // the graphs for the charge correction in X vs XZ angle + std::vector graph2Ds_Sigma_XXZAngle; // the graphs for the width correction in X vs XZ angle + std::vector graph2Ds_Charge_XdQdX; // the graphs for charge correction in X vs dQ/dX + std::vector graph2Ds_Sigma_XdQdX; // the graphs for width correction in X vs dQ/dX + std::vector graph2Ds_Charge_XZAngledQdX; // the graphs for charge correction in XZ angle vs dQ/dX + std::vector graph2Ds_Sigma_XZAngledQdX; // the graphs for width correction in XZ angle vs dQ/dX + std::vector graph2Ds_Charge_XXW; // the graphs for the charge correction in XXW + std::vector graph2Ds_Sigma_XXW; // the graphs for the width correction in XXW + + // lets try making a constructor here + // assume we can get a geometry service, a detector clcok, and a detector properties + // pass the CryoStat and TPC IDs because it's IDs all the way down + // set some optional args fpr the booleans, the readout window, and the offset + WireModUtility(const geo::GeometryCore* geom, + const geo::WireReadoutGeom* wireRead, + const detinfo::DetectorPropertiesData& detProp, + const bool& arg_ApplyChannelScale = false, + const bool& arg_ApplyXScale = true, + const bool& arg_ApplyYZScale = true, + const bool& arg_ApplyXZAngleScale = true, + const bool& arg_ApplyYZAngleScale = true, + const bool& arg_ApplydEdXScale = true, + const bool& arg_ApplyXXZAngleScale = false, + const bool& arg_ApplyXdQdXScale = false, + const bool& arg_ApplyXZAngledQdXScale = false, + const bool& arg_ApplyXXWScale = true, + const double& arg_TickOffset = 0) + : geometry(geom), + wireReadout(wireRead), + detPropData(detProp), + applyChannelScale(arg_ApplyChannelScale), + applyXScale(arg_ApplyXScale), + applyYZScale(arg_ApplyYZScale), + applyXZAngleScale(arg_ApplyXZAngleScale), + applyYZAngleScale(arg_ApplyYZAngleScale), + applydEdXScale(arg_ApplydEdXScale), + applyXXZAngleScale(arg_ApplyXXZAngleScale), + applyXdQdXScale(arg_ApplyXdQdXScale), + applyXZAngledQdXScale(arg_ApplyXZAngledQdXScale), + applyXXWScale(arg_ApplyXXWScale), + additiveModification(false), + useGraph2DInterpolation(true), + readoutWindowTicks(detProp.ReadOutWindowSize()), // the default A2795 (ICARUS TPC readout board) readout window is 4096 samples + tickOffset(arg_TickOffset) // tick offset is for MC truth, default to zero and set only as necessary + { + } + + // typedefs + typedef std::pair ROI_Key_t; + typedef std::pair SubROI_Key_t; + + typedef struct ROIProperties + { + ROI_Key_t key; + raw::ChannelID_t channel; + geo::View_t view; + float begin; + float end; + float total_q; + float center; //charge weighted center of ROI + float sigma; //charge weighted RMS of ROI + } ROIProperties_t; + + typedef struct SubROIProperties + { + SubROI_Key_t key; + raw::ChannelID_t channel; + geo::View_t view; + float total_q; + float center; + float sigma; + } SubROIProperties_t; + + typedef struct ScaleValues + { + double r_Q; + double r_sigma; + } ScaleValues_t; + + typedef struct TruthProperties + { + float x; + float x_rms; + float x_rms_noWeight; + float tick; + float tick_rms; + float tick_rms_noWeight; + float total_energy; + float x_min; + float x_max; + float tick_min; // On Plane0!! + float tick_max; // On Plane0!! + float y; + float z; + double dxdr; + double dydr; + double dzdr; + double dqdr; + double dedr; + ScaleValues_t scales_avg[3]; + } TruthProperties_t; + + // A single SimChannel ionization deposit (sim::IDE) tagged with its readout + // coordinates. This is the SimChannel analogue of a sim::SimEnergyDeposit used by + // the truth-matching path; unlike an edep, a sim::IDE carries only a point position + // (x,y,z) -- direction/step come from the matched simb::MCParticle trajectory. + typedef struct MatchedIDE + { + raw::ChannelID_t channel; // readout channel the charge landed on + geo::WireID wire; // wire closest to the deposit + double tick; // readout tick (TDC converted to tick) + const sim::IDE* ide; // trackID, numElectrons, energy, x, y, z + } MatchedIDE_t; + + // internal containers + std::map< ROI_Key_t, std::vector > ROIMatchedEdepMap; + std::map< ROI_Key_t, std::vector > ROIMatchedHitMap; + + // SimChannel/IDE truth-matching containers (parallel to the Edep versions). + // fIDEVec is the flat owner; ROIMatchedIDEMap stores indices into it, keyed by ROI. + std::vector fIDEVec; + std::map< ROI_Key_t, std::vector > ROIMatchedIDEMap; + + // Space-charge provider, set by the module (lar::providerFrom()). + // Used to map IDE (at-the-wire) positions to/from the true MCParticle trajectory + // frame. When null or SCE disabled, the mappings are the identity. + const spacecharge::SpaceCharge* fSCE = nullptr; + + // some useful functions + // geometries + // TODO is this the most efficient for new v10 iterators? + double planeXToTick(double xPos, const geo::PlaneGeo& plane, const geo::TPCGeo& tpcGeom, double offset = 0) { + return detPropData.ConvertXToTicks(xPos, plane.ID()) + offset; + } + + bool planeXInWindow(double xPos, const geo::PlaneGeo& plane, const geo::TPCGeo& tpcGeom, double offset = 0) + { + double tick = planeXToTick(xPos, plane, tpcGeom, offset); + return (tick > 0 && tick <= detPropData.ReadOutWindowSize()); + } + + // for this function: in the future if we want to use non-gaussian functions make this take a vector of parameters + // the another wiremod utility could overwrite the ``fitFunc'' with some non-standard function + // would require a fair bit of remodling (ie q and sigma would need to be replace with, eg, funcVar[0] and funcVar[1] and probs a bunch of loops) + // so lets worry about that later + double gausFunc(double t, double mean, double sigma, double a = 1.0) + { + return (a / (sigma * std::sqrt(2 * util::pi()))) * std::exp(-0.5 * std::pow((t - mean)/sigma, 2)); + } + + double FoldAngle(double theta) + { + return (std::abs(theta) > 0.5 * util::pi()) ? util::pi() - std::abs(theta) : std::abs(theta); + } + + double ThetaXZ_PlaneRel(double dxdr, double dydr, double dzdr, double planeAngle) + { + double sinPlaneAngle = std::sin(planeAngle); + double cosPlaneAngle = std::cos(planeAngle); + + //double dydrPlaneRel = dydr * cosPlaneAngle - dzdr * sinPlaneAngle; // don't need to rotate Y for this angle + double dzdrPlaneRel = dzdr * cosPlaneAngle + dydr * sinPlaneAngle; + + double theta = std::atan2(dxdr, dzdrPlaneRel); + return FoldAngle(theta); + } + + double ThetaYZ_PlaneRel(double dxdr, double dydr, double dzdr, double planeAngle) + { + double sinPlaneAngle = std::sin(planeAngle); + double cosPlaneAngle = std::cos(planeAngle); + + double dydrPlaneRel = dydr * cosPlaneAngle - dzdr * sinPlaneAngle; + double dzdrPlaneRel = dzdr * cosPlaneAngle + dydr * sinPlaneAngle; + + double theta = std::atan2(dydrPlaneRel, dzdrPlaneRel); + return FoldAngle(theta); + } + // theste are set in the .cc file + + double ThetaXY_PlaneRel(double dxdr, double dydr, double dzdr, double planeAngle) + { + double sinPlaneAngle = std::sin(planeAngle); + double cosPlaneAngle = std::cos(planeAngle); + + double dydrPlaneRel = dydr * cosPlaneAngle - dzdr * sinPlaneAngle; + //double dzdrPlaneRel = dzdr * cosPlaneAngle + dydr * sinPlaneAngle; // don't need to rotate Z for this angle + + double theta = std::atan2(dxdr, dydrPlaneRel); + return FoldAngle(theta); + } + + double ThetaXW(double dxdr, double dydr, double dzdr, double planeAngle) + { + // planeAngle is the wire angle from +z (PlaneGeo::ThetaZ, numerically equal to + // WireReadoutGeom::WireAngleToVertical). The pitch "gamma" angle is measured from the + // wire-normal, i.e. planeAngle - pi/2 -- the same convention as the canonical LArSoft + // pitch (Calorimetry / TrackCaloSkimmer use WireAngleToVertical - pi/2). Subtracting a + // full pi projected the direction onto the wire itself (a cos<->sin swap), yielding the + // wrong angle; subtract pi/2 so cosG is the projection onto the wire-normal (pitch). + double sin = std::sin(planeAngle - 0.5 * ::util::pi<>()); + double cos = std::cos(planeAngle - 0.5 * ::util::pi<>()); + + double cosG = std::abs(dydr * sin + dzdr * cos); + double theta = std::atan(dxdr / cosG); + return std::abs(theta); + } + + // these are set in the .cc file + ROIProperties_t CalcROIProperties(recob::Wire const&, size_t const&); + ROIProperties_t CalcROIProperties(recob::ChannelROI const&, size_t const&); + + std::vector> GetTargetROIs(sim::SimEnergyDeposit const&, double offset); + std::vector> GetHitTargetROIs(recob::Hit const&); + + void FillROIMatchedEdepMap(std::vector const&, std::vector const&, double offset); + void FillROIMatchedHitMap(std::vector const&, std::vector const&); + + // SimChannel/IDE versions of FillROIMatchedEdepMap. The DetectorClocksData is used to + // convert each IDE's TDC to a readout tick (no X->tick projection / tickOffset needed, + // since the SimChannel already carries the readout coordinate). + void FillROIMatchedIDEMap(std::vector const& simchVec, std::vector const& wireVec, detinfo::DetectorClocksData const& clockData, double offset = 0); + void FillROIMatchedIDEMap(std::vector const& simchVec, std::vector const& chanROIVec, detinfo::DetectorClocksData const& clockData, double offset = 0); + + void FillROIMatchedEdepMap(std::vector const&, std::vector const&, double offset); + void FillROIMatchedHitMap(std::vector const&, std::vector const&); + + std::vector CalcSubROIProperties(ROIProperties_t const&, std::vector const&); + + std::map> MatchEdepsToSubROIs(std::vector const&, std::vector const&, double offset); + + // SimChannel/IDE analogue of MatchEdepsToSubROIs. Keyed on the IDE's native readout + // tick (no per-plane X->tick projection), otherwise the same center+/-sigma / closest + // sub-ROI assignment as the edep version. + std::map> MatchIDEsToSubROIs(std::vector const&, std::vector const&); + + TruthProperties_t CalcPropertiesFromEdeps(std::vector const&, double offset); + + // SimChannel/IDE analogue of CalcPropertiesFromEdeps. Fills TruthProperties_t from the + // dominant track's sim::IDEs (charge-weighted position/energy) plus the matched + // simb::MCParticle trajectory (direction / pitch / dedr / dqdr). particleMap maps + // abs(trackID) -> MCParticle so the dominant track's trajectory can be looked up. + TruthProperties_t CalcPropertiesFromIDEs(std::vector const&, std::map const& particleMap); + + // Space-charge coordinate maps (mirroring TrackCaloSkimmer). Identity if fSCE is null + // or SCE disabled. + geo::Point_t WireToTrajectoryPosition(const geo::Point_t& loc, const geo::TPCID& tpc) const; // at-the-wire -> true trajectory frame + geo::Point_t TrajectoryToWirePosition(const geo::Point_t& loc, const geo::Vector_t& driftdir) const; // true -> at-the-wire frame + + ScaleValues_t GetScaleValues(TruthProperties_t const&, ROIProperties_t const&); + ScaleValues_t GetChannelScaleValues(TruthProperties_t const&, raw::ChannelID_t const&); + ScaleValues_t GetViewScaleValues(TruthProperties_t const&, geo::View_t const&); + + void ModifyROI(std::vector &, + ROIProperties_t const &, + std::vector const&, + std::map const&, + double sigmaWindow = std::numeric_limits::infinity()); + }; // end class +} // end namespace + +#endif // sbncode_WireMod_Utility_WireModUtility_hh diff --git a/ups/product_deps b/ups/product_deps index e479198df..4bf1002ef 100644 --- a/ups/product_deps +++ b/ups/product_deps @@ -253,13 +253,14 @@ libdir fq_dir lib #################################### product version qual flags genie_xsec v3_04_00 - -larcv2 v2_2_6 - -larsoft v10_06_00 - -sbnalg v10_06_00_01 - +larcv2 v2_4_1 - +larsoft v10_06_00_02 - +sbnalg v10_06_00_14lf01 - sbndaq_artdaq_core v1_10_06 - -sbndata v01_07 - +sbndata v01_08 - systematicstools v01_04_04 - nusystematics v1_05_07 - +geant4reweight v01_20_11 - cetmodules v3_24_01 - only_for_build end_product_list #################################### @@ -315,12 +316,11 @@ end_product_list # case it is optional. # #################################### - -qualifier larsoft sbnalg sbndaq_artdaq_core genie_xsec sbndata larcv2 systematicstools nusystematics notes -c14:debug c14:debug c14:debug c14:s131:debug AR2320i00000:e1000:k250 -nq- c14:debug:p3915 c14:debug c14:debug -nq- -c14:prof c14:prof c14:prof c14:s131:prof AR2320i00000:e1000:k250 -nq- c14:p3915:prof c14:prof c14:prof -nq- -e26:debug e26:debug e26:debug e26:s131:debug AR2320i00000:e1000:k250 -nq- debug:e26:p3915 e26:debug e26:debug -nq- -e26:prof e26:prof e26:prof e26:s131:prof AR2320i00000:e1000:k250 -nq- e26:p3915:prof e26:prof e26:prof -nq- +qualifier larsoft sbnalg sbndaq_artdaq_core genie_xsec sbndata larcv2 systematicstools nusystematics geant4reweight notes +c14:debug c14:debug c14:debug c14:s131:debug AR2320i00000:e1000:k250 -nq- c14:debug:p3915 c14:debug c14:debug c14:debug:s131 -nq- +c14:prof c14:prof c14:prof c14:s131:prof AR2320i00000:e1000:k250 -nq- c14:p3915:prof c14:prof c14:prof c14:prof:s131 -nq- +e26:debug e26:debug e26:debug e26:s131:debug AR2320i00000:e1000:k250 -nq- debug:e26:p3915 e26:debug e26:debug e26:debug:s131 -nq- +e26:prof e26:prof e26:prof e26:s131:prof AR2320i00000:e1000:k250 -nq- e26:p3915:prof e26:prof e26:prof e26:prof:s131 -nq- end_qualifier_list ####################################