/// <summary> /// Save a detector and its related class instances on the in-memory collections to the database /// </summary> /// <param name="newdet">The detector to persist</param> public static void PersistDetectorAndAssociations(Detector newdet) { // write detector essentials to DB, detector must be in DB before related insertions try { CentralizedState.App.DB.UpdateDetector(newdet); // write detector and related sr_parms to database BackgroundParameters bp = CentralizedState.App.DB.BackgroundParameters.Get(newdet); CentralizedState.App.DB.BackgroundParameters.Set(newdet, bp); NormParameters np = CentralizedState.App.DB.NormParameters.Get(newdet); CentralizedState.App.DB.NormParameters.Set(newdet, np); UnattendedParameters unp = CentralizedState.App.DB.UnattendedParameters.Get(newdet); CentralizedState.App.DB.UnattendedParameters.Set(newdet, unp); AddASourceSetup aass = CentralizedState.App.DB.AASSParameters.Get(newdet); CentralizedState.App.DB.AASSParameters.Set(newdet, aass); HVCalibrationParameters hv = CentralizedState.App.DB.HVParameters.Get(newdet); CentralizedState.App.DB.HVParameters.Set(newdet, hv); } catch (Exception) { } }
/// <summary> /// Create a new detector in-memory, with the related classes. /// Emulates the INCC relationship constructor, caller must insert new det into global list, then update to DB later for persistence /// </summary> /// <param name="model">detector to copy</param> /// <param name="newId">New detector name</param> /// <param name="elecId">Electronics id (just a string)</param> /// <param name="typeDesc">Type description (just a string)</param> /// <param name="srType">Actual instrument type</param> /// <returns>The newly created in-memory Detector class instance</returns> static public Detector CreateDetectorWithAssociations(Detector model, string newId, string elecId, string typeDesc, InstrType srType = InstrType.AMSR) { if (model != null) { Detector det = new Detector(model); // copies the SR too det.Id.SetIdDetails(newId, elecId, typeDesc, model.Id.SRType); // copy the model detector's related parameters (skips stratum) NormParameters n = CentralizedState.App.DB.NormParameters.Get(model); NormParameters Norm = new NormParameters(n); BackgroundParameters b = CentralizedState.App.DB.BackgroundParameters.Get(model); BackgroundParameters Background = new BackgroundParameters(b); UnattendedParameters u = CentralizedState.App.DB.UnattendedParameters.Get(model); UnattendedParameters Unatt = new UnattendedParameters(u); AddASourceSetup a = CentralizedState.App.DB.AASSParameters.Get(model); AddASourceSetup Aass = new AddASourceSetup(a); HVCalibrationParameters h = CentralizedState.App.DB.HVParameters.Get(model); HVCalibrationParameters Hv = new HVCalibrationParameters(h); // add copied param instances to in-memory maps CentralizedState.App.DB.NormParameters.Map.Add(det, Norm); CentralizedState.App.DB.UnattendedParameters.Map.Add(det, Unatt); CentralizedState.App.DB.BackgroundParameters.Map.Add(det, Background); CentralizedState.App.DB.AASSParameters.Map.Add(det, Aass); CentralizedState.App.DB.HVParameters.Map.Add(det, Hv); CentralizedState.App.DB.Detectors.Add(det); // add detector to in-memory list return(det); } else { Detector det = new Detector(); det.Id.SetIdDetails(newId, elecId, typeDesc, srType); if (srType.IsListMode()) { det.Id.FullConnInfo = new LMConnectionInfo(); } // add fresh param instances to in-memory maps CentralizedState.App.DB.NormParameters.Map.Add(det, new NormParameters()); CentralizedState.App.DB.UnattendedParameters.Map.Add(det, new UnattendedParameters()); CentralizedState.App.DB.BackgroundParameters.Map.Add(det, new BackgroundParameters()); CentralizedState.App.DB.AASSParameters.Map.Add(det, new AddASourceSetup()); CentralizedState.App.DB.HVParameters.Map.Add(det, new HVCalibrationParameters()); CentralizedState.App.DB.Detectors.Add(det); // add detector to in-memory list return(det); } /* * todo: create analysis selector (or it happens automatically when first referenced?) * creating a stratum association * */ }
public static AddASourceSetup GetCurrentAASSParams(Detector det) { AddASourceSetup aass = new AddASourceSetup(); if ((det != null) && CentralizedState.App.DB.AASSParameters.Map.ContainsKey(det)) { aass.Copy(CentralizedState.App.DB.AASSParameters.Map[det]); } return(aass); }
void TestDataAssay() { List <string> exts = new List <string>() { ".dat", ".cnn" }; FileList <TestDataFile> hdlr = new FileList <TestDataFile>(); hdlr.Init(exts, ctrllog); FileList <TestDataFile> files = null; // initialize operation timer here NC.App.Opstate.ResetTimer(filegather, files, 170, (int)NC.App.AppContext.StatusTimerMilliseconds); FireEvent(EventType.ActionPrep, this); NC.App.Opstate.StampOperationStartTime(); if (NC.App.AppContext.FileInputList == null) { files = (FileList <TestDataFile>)hdlr.BuildFileList(NC.App.AppContext.FileInput, NC.App.AppContext.Recurse, true); } else { files = (FileList <TestDataFile>)hdlr.BuildFileList(NC.App.AppContext.FileInputList); } if (files == null || files.Count < 1) { NC.App.Opstate.StopTimer(); NC.App.Opstate.StampOperationStopTime(); FireEvent(EventType.ActionStop, this); ctrllog.TraceEvent(LogLevels.Warning, 33085, "No usable Test data/Disk .dat or .cnn files found"); return; } AcquireParameters orig_acq = new AcquireParameters(NC.App.Opstate.Measurement.AcquireState); Detector curdet = NC.App.Opstate.Measurement.Detector; AssaySelector.MeasurementOption mo = NC.App.Opstate.Measurement.MeasOption; if (curdet.AB.Unset) { ABKey abkey = new ABKey(curdet.MultiplicityParams, 512); // NEXT: maxbins is arbitrary, just the first of many for VSR LMRawAnalysis.SDTMultiplicityCalculator.SetAlphaBeta(abkey, curdet.AB); } // Each .dat file is a separate measurement foreach (TestDataFile td in files) { Measurement meas = null; ResetMeasurement(); try { if (!td.OpenForReading()) { continue; } if (NC.App.Opstate.IsQuitRequested) { break; } uint run_seconds; ushort number_good_runs = 0; ushort total_number_runs = 0; double run_count_time = 0; double total_good_count_time = 0; if (mo != AssaySelector.MeasurementOption.holdup) { /* number of good runs */ string l = td.reader.ReadLine(); ushort.TryParse(l, out number_good_runs); if (number_good_runs == 0) { ctrllog.TraceEvent(LogLevels.Error, 440, "This measurement has no good cycles."); continue; } /* run count time */ l = td.reader.ReadLine(); double.TryParse(l, out run_count_time); if (run_count_time <= 0.0) { ctrllog.TraceEvent(LogLevels.Error, 441, "Count time is <= 0."); continue; } } // update acq and then meas here AcquireParameters newacq = ConfigureAcquireState(curdet, orig_acq, DateTimeOffset.Now, number_good_runs, td.Filename); IntegrationHelpers.BuildMeasurement(newacq, curdet, mo); meas = NC.App.Opstate.Measurement; meas.MeasDate = newacq.MeasDateTime; meas.Persist(); // preserve the basic results record /* convert to total count time */ total_number_runs = number_good_runs; total_good_count_time = number_good_runs * run_count_time; meas.RequestedRepetitions = total_number_runs; // update the acq status and do the persist /* read in run data */ for (int i = 0; i < number_good_runs; i++) { /* run date and time (IAEA format) */ run_seconds = (uint)(i * (ushort)run_count_time); // from start time AddTestDataCycle(i, run_seconds, run_count_time, meas, td); if (i % 8 == 0) { FireEvent(EventType.ActionInProgress, this); } } FireEvent(EventType.ActionInProgress, this); if (meas.MeasOption == AssaySelector.MeasurementOption.verification && meas.INCCAnalysisState.Methods.Has(AnalysisMethod.AddASource) && meas.AcquireState.well_config == WellConfiguration.Passive) { AddASourceSetup aass = IntegrationHelpers.GetCurrentAASSParams(meas.Detector); for (int n = 1; n <= aass.number_positions; n++) { /* number of good runs */ string l = td.reader.ReadLine(); if (td.reader.EndOfStream) { ctrllog.TraceEvent(LogLevels.Error, 440, "No add-a-source data found in disk file. AAS p" + n.ToString()); } ushort.TryParse(l, out number_good_runs); if (number_good_runs == 0) { ctrllog.TraceEvent(LogLevels.Error, 440, "This measurement has no good cycles. AAS p" + n.ToString()); continue; } /* run count time */ l = td.reader.ReadLine(); double.TryParse(l, out run_count_time); if (run_count_time <= 0.0) { ctrllog.TraceEvent(LogLevels.Error, 441, "Count time is <= 0. AAS p" + n.ToString()); continue; } /* read in run data */ for (int i = 0; i < number_good_runs; i++) { /* run date and time (IAEA format) */ run_seconds = (uint)((n + 1) * (i + 1) * (ushort)run_count_time); // from start time AddTestDataCycle(i, run_seconds, run_count_time, meas, td, " AAS p" + n.ToString(), n); if (i % 8 == 0) { FireEvent(EventType.ActionInProgress, this); } } FireEvent(EventType.ActionInProgress, this); } } } catch (Exception e) { NC.App.Opstate.SOH = OperatingState.Trouble; ctrllog.TraceException(e, true); ctrllog.TraceEvent(LogLevels.Error, 437, "Test data file processing stopped with error: '" + e.Message + "'"); } finally { td.CloseReader(); NC.App.Loggers.Flush(); } FireEvent(EventType.ActionInProgress, this); ComputeFromINCC5SRData(meas); FireEvent(EventType.ActionInProgress, this); } NC.App.Opstate.ResetTokens(); NC.App.Opstate.SOH = OperatingState.Stopping; NC.App.Opstate.StampOperationStopTime(); FireEvent(EventType.ActionFinished, this); }
public static AddASourceSetup GetCurrentAASSParams(Detector det) { AddASourceSetup aass = new AddASourceSetup(); if ((det != null) && CentralizedState.App.DB.AASSParameters.Map.ContainsKey(det)) aass.Copy(CentralizedState.App.DB.AASSParameters.Map[det]); return aass; }
/// <summary> /// Create a new detector in-memory, with the related classes. /// Emulates the INCC relationship constructor, caller must insert new det into global list, then update to DB later for persistence /// </summary> /// <param name="model">detector to copy</param> /// <param name="newId">New detector name</param> /// <param name="elecId">Electronics id (just a string)</param> /// <param name="typeDesc">Type description (just a string)</param> /// <param name="srType">Actual instrument type</param> /// <returns>The newly created in-memory Detector class instance</returns> public static Detector CreateDetectorWithAssociations(Detector model, string newId, string elecId, string typeDesc, InstrType srType = InstrType.AMSR) { if (model != null) { Detector det = new Detector(model); // copies the SR too det.Id.SetIdDetails(newId, elecId, typeDesc, model.Id.SRType); // copy the model detector's related parameters (skips stratum) NormParameters n = CentralizedState.App.DB.NormParameters.Get(model); NormParameters Norm = new NormParameters(n); BackgroundParameters b = CentralizedState.App.DB.BackgroundParameters.Get(model); BackgroundParameters Background = new BackgroundParameters(b); UnattendedParameters u = CentralizedState.App.DB.UnattendedParameters.Get(model); UnattendedParameters Unatt = new UnattendedParameters(u); AddASourceSetup a = CentralizedState.App.DB.AASSParameters.Get(model); AddASourceSetup Aass = new AddASourceSetup(a); HVCalibrationParameters h = CentralizedState.App.DB.HVParameters.Get(model); HVCalibrationParameters Hv = new HVCalibrationParameters(h); // add copied param instances to in-memory maps CentralizedState.App.DB.NormParameters.Map.Add(det, Norm); CentralizedState.App.DB.UnattendedParameters.Map.Add(det, Unatt); CentralizedState.App.DB.BackgroundParameters.Map.Add(det, Background); CentralizedState.App.DB.AASSParameters.Map.Add(det, Aass); CentralizedState.App.DB.HVParameters.Map.Add(det, Hv); CentralizedState.App.DB.Detectors.Add(det); // add detector to in-memory list return det; } else { Detector det = new Detector(); det.Id.SetIdDetails(newId, elecId, typeDesc,srType); if (srType.IsListMode()) det.Id.FullConnInfo = new LMConnectionInfo(); // add fresh param instances to in-memory maps CentralizedState.App.DB.NormParameters.Map.Add(det, new NormParameters()); CentralizedState.App.DB.UnattendedParameters.Map.Add(det, new UnattendedParameters()); CentralizedState.App.DB.BackgroundParameters.Map.Add(det, new BackgroundParameters()); CentralizedState.App.DB.AASSParameters.Map.Add(det, new AddASourceSetup()); CentralizedState.App.DB.HVParameters.Map.Add(det, new HVCalibrationParameters()); CentralizedState.App.DB.Detectors.Add(det); // add detector to in-memory list return det; } /* * todo: create analysis selector (or it happens automatically when first referenced?) * creating a stratum association * */ }
public unsafe void BuildDetector(INCCInitialDataDetectorFile iddf, int num) { INCCDB DB = NC.App.DB; detector_rec d = iddf.Detector[0]; sr_parms_rec sr = iddf.SRParms[0]; bkg_parms_rec bkh = iddf.BKGParms[0]; norm_parms_rec norm = iddf.NormParms[0]; tm_bkg_parms_rec tmbkg = iddf.TMBKGParms[0]; add_a_source_setup_rec aass = iddf.AASParms[0]; InstrType srtype = (InstrType)sr.sr_type; bool overwrite = NC.App.AppContext.OverwriteImportedDefs; mlogger.TraceEvent(LogLevels.Verbose, 34100, "Building '{0}' detector '{1}' from {2} {3}", srtype.ToString(), TransferUtils.str(d.detector_id, INCC.MAX_DETECTOR_ID_LENGTH), num, iddf.Path); // if the detector is not known internally, then // add the detector to the list of detectors in memory, and // associate a new set of SR, Bkg and Norm and AB params with the new detector // todo: What are the HV Params from INCC5, and once that is known, can they be transferred to the INCC6 HV Param and results tables Detector det = new Detector(); if (srtype.IsListMode()) det.Id.FullConnInfo = new LMConnectionInfo(); try { // this transfer should be a method in this class, it will be used elsewhere too det.Id.DetectorId = TransferUtils.str(d.detector_id, INCC.MAX_DETECTOR_ID_LENGTH); det.Id.SRType = srtype; det.Id.Type = TransferUtils.str(d.detector_type, INCC.DETECTOR_TYPE_LENGTH); det.Id.ElectronicsId = TransferUtils.str(d.electronics_id, INCC.ELECTRONICS_ID_LENGTH); det.Id.ConnInfo = sr.sr_port_number.ToString(); det.Id.source = ConstructedSource.INCCTransferCopy; det.MultiplicityParams.FA = srtype.DefaultFAFor(); det.MultiplicityParams.gateWidthTics = (ulong)(sr.gate_length * 10.0); // shift down to tics from microseconds det.SRParams.deadTimeCoefficientAinMicroSecs = sr.coeff_a_deadtime; det.SRParams.deadTimeCoefficientBinPicoSecs = sr.coeff_b_deadtime; det.SRParams.deadTimeCoefficientCinNanoSecs = sr.coeff_c_deadtime; det.SRParams.deadTimeCoefficientMultiplicityinNanoSecs = sr.multiplicity_deadtime; det.SRParams.dieAwayTime = sr.die_away_time * 10.0; // shift down to tics from microseconds det.SRParams.doublesGateFraction = sr.doubles_gate_fraction; det.SRParams.efficiency = sr.efficiency; det.SRParams.gateLength = (ulong)(sr.gate_length * 10.0); // shift down to tics from microseconds //det.SRParams.gateLength2 = sr.gate_length2; det.SRParams.highVoltage = sr.high_voltage; det.SRParams.predelay = (ulong)(sr.predelay * 10.0); // shift down to tics from microseconds det.SRParams.triplesGateFraction = sr.triples_gate_fraction; // = sr.sr_type , sr.sr_port_number, sr.sr_detector_id these are in the Id now, not the SRparams, but they travel together. if (NC.App.AppContext.OverwriteImportedDefs) DB.Detectors.Replace(det); else DB.Detectors.AddOnlyIfNotThere(det); DetectorIndex = DB.Detectors.Count - 1; BackgroundParameters bkg = new BackgroundParameters(); bkg.DeadtimeCorrectedRates.Singles.v = bkh.curr_passive_bkg_singles_rate; bkg.DeadtimeCorrectedRates.Doubles.v = bkh.curr_passive_bkg_doubles_rate; bkg.DeadtimeCorrectedRates.Triples.v = bkh.curr_passive_bkg_triples_rate; bkg.DeadtimeCorrectedRates.Singles.err = bkh.curr_passive_bkg_singles_err; bkg.DeadtimeCorrectedRates.Doubles.err = bkh.curr_passive_bkg_doubles_err; bkg.DeadtimeCorrectedRates.Triples.err = bkh.curr_passive_bkg_triples_err; bkg.Scaler1.v = bkh.curr_passive_bkg_scaler1_rate; bkg.Scaler2.v = bkh.curr_passive_bkg_scaler2_rate; bkg.INCCActive.Singles.v = bkh.curr_active_bkg_singles_rate; bkg.INCCActive.Singles.err = bkh.curr_active_bkg_singles_err; bkg.INCCActive.Scaler1Rate = bkh.curr_active_bkg_scaler1_rate; bkg.INCCActive.Scaler2Rate = bkh.curr_active_bkg_scaler2_rate; bkg.TMBkgParams.Singles.v = tmbkg.tm_singles_bkg; bkg.TMBkgParams.Ones.v = tmbkg.tm_ones_bkg; bkg.TMBkgParams.Twos.v = tmbkg.tm_twos_bkg; bkg.TMBkgParams.Zeros.v = tmbkg.tm_zeros_bkg; bkg.TMBkgParams.Singles.err = tmbkg.tm_singles_bkg_err; bkg.TMBkgParams.Ones.err = tmbkg.tm_ones_bkg_err; bkg.TMBkgParams.Twos.err = tmbkg.tm_twos_bkg_err; bkg.TMBkgParams.Zeros.err = tmbkg.tm_zeros_bkg_err; bkg.TMBkgParams.ComputeTMBkg = (tmbkg.tm_bkg == 0 ? false : true); if (DB.BackgroundParameters.Get(det.Id.DetectorName) == null) { DB.BackgroundParameters.GetMap().Add(det, bkg); // saved to DB at below } else if (overwrite) { bkg.modified = true; NC.App.DB.BackgroundParameters.GetMap().Remove(det); NC.App.DB.BackgroundParameters.GetMap().Add(det, bkg); NC.App.DB.BackgroundParameters.Set(det, bkg); } // save the params listed here using the detector as the key NormParameters normp = new NormParameters(); normp.acceptanceLimitPercent = norm.acceptance_limit_percent; normp.acceptanceLimitStdDev = norm.acceptance_limit_std_dev; normp.amliRefSinglesRate = norm.amli_ref_singles_rate; normp.biasMode = OldToNewBiasTestId(norm.bias_mode); normp.biasPrecisionLimit = norm.bias_precision_limit; normp.biasTestAddasrcPosition = norm.bias_test_addasrc_position; normp.biasTestUseAddasrc = (norm.bias_test_use_addasrc == 0 ? false : true); normp.cf252RefDoublesRate.v = norm.cf252_ref_doubles_rate; normp.currNormalizationConstant.v = norm.curr_normalization_constant; normp.currNormalizationConstant.err = norm.curr_normalization_constant_err; normp.initSrcPrecisionLimit = norm.init_src_precision_limit; normp.measRate.v = norm.meas_rate; normp.measRate.err = norm.meas_rate_err; normp.refDate = INCC.DateFrom(TransferUtils.str(norm.ref_date, INCC.DATE_TIME_LENGTH)); normp.sourceId = TransferUtils.str(norm.source_id, INCC.SOURCE_ID_LENGTH); normp.yieldRelativeToMrc95 = norm.yield_relative_to_mrc_95; if (DB.NormParameters.Get(det.Id.DetectorName) == null) { DB.NormParameters.GetMap().Add(det, normp); // saved to DB at end } else if (overwrite) { normp.modified = true; DB.NormParameters.GetMap().Remove(det); DB.NormParameters.GetMap().Add(det, normp); // the in-memory map DB.NormParameters.Set(det, normp); // the DB table } AddASourceSetup aassp = new AddASourceSetup(); aassp.type = OldToNewAASId(aass.ad_type); aassp.port_number = aass.ad_port_number; aassp.forward_over_travel = aass.ad_forward_over_travel; aassp.reverse_over_travel = aass.ad_reverse_over_travel; aassp.number_positions = aass.ad_number_positions; aassp.dist_to_move = TransferUtils.Copy(aass.ad_dist_to_move, INCC.MAX_ADDASRC_POSITIONS); aassp.cm_steps_per_inch = aass.cm_steps_per_inch; aassp.cm_forward_mask = aass.cm_forward_mask; aassp.cm_reverse_mask = aass.cm_reverse_mask; aassp.cm_axis_number = aass.cm_axis_number; aassp.cm_over_travel_state = aass.cm_over_travel_state; aassp.cm_step_ratio = aass.cm_step_ratio; aassp.cm_slow_inches = aass.cm_slow_inches; aassp.plc_steps_per_inch = aass.plc_steps_per_inch; aassp.scale_conversion_factor = aass.scale_conversion_factor; aassp.cm_rotation = (aass.cm_rotation == 0 ? false : true); if (!DB.AASSParameters.GetMap().ContainsKey(det)) { DB.AASSParameters.GetMap().Add(det, aassp); } else if (overwrite) { aassp.modified = true; DB.AASSParameters.GetMap().Remove(det); DB.AASSParameters.GetMap().Add(det, aassp); // todo: in-memory and db } if (!DB.UnattendedParameters.GetMap().ContainsKey(det)) { DB.UnattendedParameters.GetMap().Add(det, new UnattendedParameters()); } else if (overwrite) { DB.UnattendedParameters.GetMap().Remove(det); DB.UnattendedParameters.GetMap().Add(det, new UnattendedParameters()); } // the alpha beta arrays must be sized here, for first use by the calc_alpha_beta code in the cycle conditioning code Multiplicity mkey = new Multiplicity(srtype.DefaultFAFor()); mkey.SR = new ShiftRegisterParameters(det.SRParams); MultiplicityCountingRes mcr = new MultiplicityCountingRes(srtype.DefaultFAFor(), 0); ABKey abkey = new ABKey(mkey, 512); // NEXT: maxbins is arbitrary LMRawAnalysis.SDTMultiplicityCalculator.SetAlphaBeta(abkey, det.AB); mcr.AB.TransferIntermediates(det.AB); } catch (Exception e) { mlogger.TraceEvent(LogLevels.Warning, 34064, "Detector transfer processing error {0} {1} ({2})", det.Id.DetectorName, e.Message, System.IO.Path.GetFileName(iddf.Path)); } }