INT analyzer_init() { HNDLE hDB, hKey; char str[80]; RUNINFO_STR(runinfo_str); EXP_PARAM_STR(exp_param_str); GLOBAL_PARAM_STR(global_param_str); TRIGGER_SETTINGS_STR(trigger_settings_str); /* open ODB structures */ cm_get_experiment_database(&hDB, NULL); db_create_record(hDB, 0, "/Runinfo", strcomb((const char **)runinfo_str)); db_find_key(hDB, 0, "/Runinfo", &hKey); if (db_open_record(hDB, hKey, &runinfo, sizeof(runinfo), MODE_READ, NULL, NULL) != DB_SUCCESS) { cm_msg(MERROR, "analyzer_init", "Cannot open \"/Runinfo\" tree in ODB"); return 0; } db_create_record(hDB, 0, "/Experiment/Run Parameters", strcomb((const char **)exp_param_str)); db_find_key(hDB, 0, "/Experiment/Run Parameters", &hKey); if (db_open_record(hDB, hKey, &exp_param, sizeof(exp_param), MODE_READ, NULL, NULL) != DB_SUCCESS) { cm_msg(MERROR, "analyzer_init", "Cannot open \"/Experiment/Run Parameters\" tree in ODB"); return 0; } sprintf(str, "/%s/Parameters/Global", analyzer_name); db_create_record(hDB, 0, str, strcomb((const char **)global_param_str)); db_find_key(hDB, 0, str, &hKey); if (db_open_record (hDB, hKey, &global_param, sizeof(global_param), MODE_READ, NULL, NULL) != DB_SUCCESS) { cm_msg(MERROR, "analyzer_init", "Cannot open \"%s\" tree in ODB", str); return 0; } db_create_record(hDB, 0, "/Equipment/Trigger/Settings", strcomb((const char **)trigger_settings_str)); db_find_key(hDB, 0, "/Equipment/Trigger/Settings", &hKey); if (db_open_record (hDB, hKey, &trigger_settings, sizeof(trigger_settings), MODE_READ, NULL, NULL) != DB_SUCCESS) { cm_msg(MERROR, "analyzer_init", "Cannot open \"/Equipment/Trigger/Settings\" tree in ODB"); return 0; } //Initialize gData gData = new TEnvi(); return SUCCESS; }
/*! * <p>Establishes a hot-link between a local data structure and the ODB, i.e. * everytime this structure is changed in the ODB, it is automatically * propageted to the structure and vice versa. Further is starts the cyclic * timer needed for the MIDAS watch-dog process. * * <p><b>Return:</b> * - DB_SUCCESS on successful completion. * - DB_INVALID_HANDLE if ODB or key is invalid. * - DB_STRUCT_SIZE_MISMATCH if the structure size doesn't match the sub-tree size * - DB_NO_ACCESS if there is no READ/WRITE access to the sub-tree * * \param addr Address of the local structure to which a hot-link shall be established. * \param size size of the calling structure * \param access Access flags for this hot-link. * \param dispatcher Callback function which is called, if the entries in the ODB have * changed. * \param obj Pointer to the calling object */ INT PKey::HotLink(void *addr, int size, WORD access, void (*dispatcher)(INT,INT,void*), void *obj) { fExperiment->StartTimer(); // start a cylcic timer event // establishes a hotlink return db_open_record(fHDB, fHKey, addr, size, access, dispatcher, obj); }
/*-- Analyzer Init -------------------------------------------------*/ INT analyzer_init() { HNDLE hDB, hKey; char str[80]; RUNINFO_STR(runinfo_str); // RUNINFO_STR in experim.h rn: not true !?!?! EXP_PARAM_STR(exp_param_str); // EXP_PARAM_STR in experim.h rn: not true !!?!?! //GLOBAL_PARAM_STR(global_param_str); // GLOBAL_PARAM_STR in experim.h /* open ODB structures */ cm_get_experiment_database(&hDB, NULL); db_create_record(hDB, 0, "/Runinfo", strcomb(runinfo_str)); db_find_key(hDB, 0, "/Runinfo", &hKey); if (db_open_record(hDB, hKey, &runinfo, sizeof(runinfo), MODE_READ, NULL, NULL) != DB_SUCCESS) { cm_msg(MERROR, "analyzer_init", "Cannot open \"/Runinfo\" tree in ODB"); return 0; } db_create_record(hDB, 0, "/Experiment/Run Parameters", strcomb(exp_param_str)); db_find_key(hDB, 0, "/Experiment/Run Parameters", &hKey); if (db_open_record(hDB, hKey, &exp_param, sizeof(exp_param), MODE_READ, NULL, NULL) != DB_SUCCESS) { cm_msg(MERROR, "analyzer_init", "Cannot open \"/Experiment/Run Parameters\" tree in ODB"); return 0; } //printf("\n---testing testing 123----------------------------------------------- %i \n\n\n\n ",runinfo.run_number); //runnr=runinfo.run_number; ParameterInit(); return SUCCESS; }
int main() { int status, size; HNDLE hDB, hKey; /* connect to experiment */ status = cm_connect_experiment("", "", "ODBTest", NULL); if (status != CM_SUCCESS) return 1; /* get handle to online database */ cm_get_experiment_database(&hDB, &hKey); /* read key "runinfo/run number" */ size = sizeof(run_number); status = db_get_value(hDB, 0, "/runinfo/run number", &run_number, &size, TID_INT, TRUE); if (status != DB_SUCCESS) { printf("Cannot read run number"); return 0; } printf("Current run number is %d\n", run_number); /* set new run number */ run_number++; db_set_value(hDB, 0, "/runinfo/run number", &run_number, size, 1, TID_INT); /* now open run_number with automatic updates */ db_find_key(hDB, 0, "/runinfo/run number", &hKey); db_open_record(hDB, hKey, &run_number, sizeof(run_number), MODE_READ, run_number_changed, NULL); printf("Waiting for run number to change. Hit RETURN to abort\n"); do { cm_yield(1000); } while (!ss_kbhit()); db_close_record(hDB, hKey); /* disconnect from experiment */ cm_disconnect_experiment(); return 1; }
INT master_ODB_init(void) { INT ret = SUCCESS; INT status; char str[MAX_ODB_PATH]; // char str_aux[MAX_ODB_PATH]; HNDLE hKey; sprintf(master_settings_odb.trigger_source, "Fake"); //Default to self-generated fake trigger signals master_settings_odb.rate = 12; sprintf(master_settings_odb.readout_name, "AMC13"); sprintf(master_settings_odb.sim_name, "CaloSimulatorAMC13"); dbprintf(str,"/Equipment/MasterGM2/Settings/Globals"); sprintf(str,"/Equipment/MasterGM2/Settings/Globals"); // create ODB structure if it doesn't exist status = db_check_record(hDB, 0, str, MASTER_SETTINGS_ODB_STR, TRUE); if( status != DB_SUCCESS ) { cm_msg(MERROR, __FILE__, "Cannot create [%s] entry in ODB, err = %i\n",str,status); return FE_ERR_ODB; } // returns key handle "hDB" to ODB name "str" for fast access status = db_find_key(hDB, 0, str, &hKey); if ( status != DB_SUCCESS ) { cm_msg(MERROR, __FILE__, "Cannot find [%s] key in ODB, err = %i",str,status); return FE_ERR_ODB; } // creates hotlink of ODB subtree to structure master_settings_odb that's automatically updated if (db_open_record(hDB, hKey, &master_settings_odb, sizeof(MASTER_SETTINGS_ODB), MODE_READ, master_ODB_update, NULL) != DB_SUCCESS) { cm_msg(MERROR, __FILE__, "Cannot open [%s] settings in ODB",str); return FE_ERR_ODB; } print(str); return ret; }
INT fgd_init(EQUIPMENT * pequipment) { int status, size, i, j, index, offset; char str[256]; HNDLE hDB, hKey, hNames, hThreshold; FGD_INFO *fgd_info; /* allocate private data */ pequipment->cd_info = calloc(1, sizeof(FGD_INFO)); fgd_info = (FGD_INFO *) pequipment->cd_info; /* get class driver root key */ cm_get_experiment_database(&hDB, NULL); sprintf(str, "/Equipment/%s", pequipment->name); db_create_key(hDB, 0, str, TID_KEY); db_find_key(hDB, 0, str, &fgd_info->hKeyRoot); /* save event format */ size = sizeof(str); db_get_value(hDB, fgd_info->hKeyRoot, "Common/Format", str, &size, TID_STRING, TRUE); if (equal_ustring(str, "Fixed")) fgd_info->format = FORMAT_FIXED; else if (equal_ustring(str, "MIDAS")) fgd_info->format = FORMAT_MIDAS; else if (equal_ustring(str, "YBOS")) fgd_info->format = FORMAT_YBOS; /* count total number of channels */ for (i = 0, fgd_info->num_channels = 0; pequipment->driver[i].name[0]; i++) { if (pequipment->driver[i].channels == 0) { cm_msg(MERROR, "fgd_init", "Driver with zero channels not allowed"); return FE_ERR_ODB; } fgd_info->num_channels += pequipment->driver[i].channels; } if (fgd_info->num_channels == 0) { cm_msg(MERROR, "fgd_init", "No channels found in device driver list"); return FE_ERR_ODB; } /* Allocate memory for buffers */ fgd_info->names = (char *) calloc(fgd_info->num_channels, NAME_LENGTH); fgd_info->demand = (float *) calloc(fgd_info->num_channels, sizeof(float)); fgd_info->measured = (float *) calloc(fgd_info->num_channels, sizeof(float)); fgd_info->temp1 = (float *) calloc(fgd_info->num_channels, sizeof(float)); fgd_info->temp2 = (float *) calloc(fgd_info->num_channels, sizeof(float)); fgd_info->temp3 = (float *) calloc(fgd_info->num_channels, sizeof(float)); fgd_info->update_threshold = (float *) calloc(fgd_info->num_channels, sizeof(float)); fgd_info->demand_mirror = (float *) calloc(fgd_info->num_channels, sizeof(float)); fgd_info->measured_mirror = (float *) calloc(fgd_info->num_channels, sizeof(float)); fgd_info->channel_offset = (INT *) calloc(fgd_info->num_channels, sizeof(INT)); fgd_info->driver = (void *) calloc(fgd_info->num_channels, sizeof(void *)); if (!fgd_info->driver) { cm_msg(MERROR, "hv_init", "Not enough memory"); return FE_ERR_ODB; } /*---- Initialize device drivers ----*/ /* call init method */ for (i = 0; pequipment->driver[i].name[0]; i++) { sprintf(str, "Settings/Devices/%s", pequipment->driver[i].name); status = db_find_key(hDB, fgd_info->hKeyRoot, str, &hKey); if (status != DB_SUCCESS) { db_create_key(hDB, fgd_info->hKeyRoot, str, TID_KEY); status = db_find_key(hDB, fgd_info->hKeyRoot, str, &hKey); if (status != DB_SUCCESS) { cm_msg(MERROR, "hv_init", "Cannot create %s entry in online database", str); free_mem(fgd_info); return FE_ERR_ODB; } } status = device_driver(&pequipment->driver[i], CMD_INIT, hKey); if (status != FE_SUCCESS) { free_mem(fgd_info); return status; } } /* compose device driver channel assignment */ for (i = 0, j = 0, index = 0, offset = 0; i < fgd_info->num_channels; i++, j++) { while (j >= pequipment->driver[index].channels && pequipment->driver[index].name[0]) { offset += j; index++; j = 0; } fgd_info->driver[i] = &pequipment->driver[index]; fgd_info->channel_offset[i] = offset; } /*---- create demand variables ----*/ /* get demand from ODB */ status = db_find_key(hDB, fgd_info->hKeyRoot, "Variables/Demand", &fgd_info->hKeyDemand); if (status == DB_SUCCESS) { size = sizeof(float) * fgd_info->num_channels; db_get_data(hDB, fgd_info->hKeyDemand, fgd_info->demand, &size, TID_FLOAT); } /* let device driver overwrite demand values, if it supports it */ for (i = 0; i < fgd_info->num_channels; i++) { if (fgd_info->driver[i]->flags & DF_PRIO_DEVICE) { device_driver(fgd_info->driver[i], CMD_GET_DEMAND, i - fgd_info->channel_offset[i], &fgd_info->demand[i]); fgd_info->demand_mirror[i] = fgd_info->demand[i]; } else fgd_info->demand_mirror[i] = -12345.f; /* use -12345 as invalid value */ } /* write back demand values */ status = db_find_key(hDB, fgd_info->hKeyRoot, "Variables/Demand", &fgd_info->hKeyDemand); if (status != DB_SUCCESS) { db_create_key(hDB, fgd_info->hKeyRoot, "Variables/Demand", TID_FLOAT); db_find_key(hDB, fgd_info->hKeyRoot, "Variables/Demand", &fgd_info->hKeyDemand); } size = sizeof(float) * fgd_info->num_channels; db_set_data(hDB, fgd_info->hKeyDemand, fgd_info->demand, size, fgd_info->num_channels, TID_FLOAT); db_open_record(hDB, fgd_info->hKeyDemand, fgd_info->demand, fgd_info->num_channels * sizeof(float), MODE_READ, fgd_demand, pequipment); /*---- create measured variables ----*/ db_merge_data(hDB, fgd_info->hKeyRoot, "Variables/Current Measured", fgd_info->measured, sizeof(float) * fgd_info->num_channels, fgd_info->num_channels, TID_FLOAT); db_find_key(hDB, fgd_info->hKeyRoot, "Variables/Current Measured", &fgd_info->hKeyMeasured); memcpy(fgd_info->measured_mirror, fgd_info->measured, fgd_info->num_channels * sizeof(float)); /*---- create Temp1 measured variables ----*/ db_merge_data(hDB, fgd_info->hKeyRoot, "Variables/Temp1", fgd_info->temp1, sizeof(float) * fgd_info->num_channels, fgd_info->num_channels, TID_FLOAT); db_find_key(hDB, fgd_info->hKeyRoot, "Variables/Temp1", &fgd_info->hKeyTemp1); /*---- create Temp2 measured variables ----*/ db_merge_data(hDB, fgd_info->hKeyRoot, "Variables/Temp2", fgd_info->temp2, sizeof(float) * fgd_info->num_channels, fgd_info->num_channels, TID_FLOAT); db_find_key(hDB, fgd_info->hKeyRoot, "Variables/Temp2", &fgd_info->hKeyTemp2); /*---- create Temp3 measured variables ----*/ db_merge_data(hDB, fgd_info->hKeyRoot, "Variables/Temp3", fgd_info->temp3, sizeof(float) * fgd_info->num_channels, fgd_info->num_channels, TID_FLOAT); db_find_key(hDB, fgd_info->hKeyRoot, "Variables/Temp3", &fgd_info->hKeyTemp3); /*---- get default names from device driver ----*/ for (i = 0; i < fgd_info->num_channels; i++) { sprintf(fgd_info->names + NAME_LENGTH * i, "Default%%CH %d", i); device_driver(fgd_info->driver[i], CMD_GET_LABEL, i - fgd_info->channel_offset[i], fgd_info->names + NAME_LENGTH * i); } db_merge_data(hDB, fgd_info->hKeyRoot, "Settings/Names", fgd_info->names, NAME_LENGTH * fgd_info->num_channels, fgd_info->num_channels, TID_STRING); /*---- set labels form midas SC names ----*/ for (i = 0; i < fgd_info->num_channels; i++) { fgd_info = (FGD_INFO *) pequipment->cd_info; device_driver(fgd_info->driver[i], CMD_SET_LABEL, i - fgd_info->channel_offset[i], fgd_info->names + NAME_LENGTH * i); } /* open hotlink on channel names */ if (db_find_key(hDB, fgd_info->hKeyRoot, "Settings/Names", &hNames) == DB_SUCCESS) db_open_record(hDB, hNames, fgd_info->names, NAME_LENGTH*fgd_info->num_channels, MODE_READ, fgd_update_label, pequipment); /*---- get default update threshold from device driver ----*/ for (i = 0; i < fgd_info->num_channels; i++) { fgd_info->update_threshold[i] = 1.f; /* default 1 unit */ device_driver(fgd_info->driver[i], CMD_GET_THRESHOLD, i - fgd_info->channel_offset[i], &fgd_info->update_threshold[i]); } db_merge_data(hDB, fgd_info->hKeyRoot, "Settings/Update Threshold Measured", fgd_info->update_threshold, sizeof(float)*fgd_info->num_channels, fgd_info->num_channels, TID_FLOAT); /* open hotlink on update threshold */ if (db_find_key(hDB, fgd_info->hKeyRoot, "Settings/Update Threshold Measured", &hThreshold) == DB_SUCCESS) db_open_record(hDB, hThreshold, fgd_info->update_threshold, sizeof(float)*fgd_info->num_channels, MODE_READ, NULL, NULL); /*---- set initial demand values ----*/ // fgd_demand(hDB, fgd_info->hKeyDemand, pequipment); /* initially read all channels */ for (i = 0; i < fgd_info->num_channels; i++) fgd_read(pequipment, i); return FE_SUCCESS; }
INT amc13simulator_ODB_init(void) { INT ret = SUCCESS; INT status; char str[MAX_ODB_PATH]; char str_aux[MAX_ODB_PATH]; HNDLE hKey; /* reset channel info information */ int i; amc13simulator_settings_odb.sync = 1; amc13simulator_settings_odb.write_root = 1; amc13simulator_settings_odb.rider_header = 0; amc13simulator_settings_odb.n_seg_x = 9; amc13simulator_settings_odb.n_seg_y = 6; amc13simulator_settings_odb.seg_size = 3; amc13simulator_settings_odb.waveform_length = 589824; amc13simulator_settings_odb.n_muons_mean = 400; amc13simulator_settings_odb.Emax = 52.8; amc13simulator_settings_odb.Elab_max = 3.1; amc13simulator_settings_odb.omega_a = 1.438e6; amc13simulator_settings_odb.repeat_first_event = 1; amc13simulator_settings_odb.laser_pulse = 0; amc13simulator_settings_odb.send_to_event_builder = 0; /* for (i=0; i<AMC13SIMULATOR_NUM; i++){ amc13simulator_channel_odb[i].enabled = 0; amc13simulator_channel_odb[i].port_no = 0; sprintf(amc13simulator_channel_odb[i].ip_addr,"000.000.0.000"); sprintf(amc13simulator_channel_odb[i].host_name,""); } */ dbprintf(str,"/Equipment/AMC13Simulator%02d/Settings/Globals",frontend_index); sprintf(str,"/Equipment/AMC13Simulator%02d/Settings/Globals",frontend_index); // create ODB structure /Equipment/%s/Settings/Globals if doesn't exist status = db_check_record(hDB, 0, str, AMC13SIMULATOR_SETTINGS_ODB_STR, TRUE); if ( status != DB_SUCCESS ) { cm_msg(MERROR, __FILE__, "Cannot create [%s] entry in ODB, err = %i",str,status); return FE_ERR_ODB; } // returns key handle "hDB" to ODB name "str" for fast access status = db_find_key(hDB, 0, str, &hKey); if ( status != DB_SUCCESS ) { cm_msg(MERROR, __FILE__, "Cannot find [%s] key in ODB, err = %i",str,status); return FE_ERR_ODB; } // creates hotlink of ODB subtree to structure amc13simulator_settings_odb that's automatically updated if (db_open_record(hDB, hKey, &amc13simulator_settings_odb, sizeof(AMC13SIMULATOR_SETTINGS_ODB), MODE_READ, amc13simulator_ODB_update, NULL) != DB_SUCCESS) { cm_msg(MERROR, __FILE__, "Cannot open [%s] settings in ODB",str); return FE_ERR_ODB; } dbprintf("%s(%d): %s sync %d\n", __func__, __LINE__, str, amc13simulator_settings_odb.sync ); dbprintf("%s(%d): %s write_root %d\n", __func__, __LINE__, str, amc13simulator_settings_odb.write_root ); dbprintf("%s(%d): %s rider_header %d\n", __func__, __LINE__, str, amc13simulator_settings_odb.rider_header ); dbprintf("%s(%d): %s n_seg_x %d\n", __func__, __LINE__, str, amc13simulator_settings_odb.n_seg_x ); dbprintf("%s(%d): %s n_seg_y %d\n", __func__, __LINE__, str, amc13simulator_settings_odb.n_seg_y ); dbprintf("%s(%d): %s seg_size %d\n", __func__, __LINE__, str, amc13simulator_settings_odb.seg_size ); dbprintf("%s(%d): %s waveform length %d\n", __func__, __LINE__, str, amc13simulator_settings_odb.waveform_length ); dbprintf("%s(%d): %s n_muons_mean %d\n", __func__, __LINE__, str, amc13simulator_settings_odb.n_muons_mean ); dbprintf("%s(%d): %s Emax %f\n", __func__, __LINE__, str, amc13simulator_settings_odb.Emax ); dbprintf("%s(%d): %s Elab_max %f\n", __func__, __LINE__, str, amc13simulator_settings_odb.Elab_max ); dbprintf("%s(%d): %s omega_a %f\n", __func__, __LINE__, str, amc13simulator_settings_odb.omega_a ); dbprintf("%s(%d): %s repeat_first_event %d\n",__func__,__LINE__,str,amc13simulator_settings_odb.repeat_first_event); dbprintf("%s(%d): %s laser_pulse %d\n",__func__,__LINE__,str,amc13simulator_settings_odb.laser_pulse); /* for (i=0; i<AMC13SIMULATOR_NUM; i++){ sprintf(str,"/Equipment/AMC13Simulator%02d/Settings/Channel%02d",frontend_index,i+1); // create ODB structure /Equipment/%s/Settings/Channel%02d if doesn't exist status = db_check_record(hDB, 0, str, AMC13SIMULATOR_CHANNEL_ODB_STR, TRUE); if ( status != DB_SUCCESS ) { cm_msg(MERROR, __FILE__, "Cannot create [%s] entry in ODB, err = %i",str,status); return FE_ERR_ODB; } // returns key handle "hDB" to ODB name "str" for fast access status = db_find_key(hDB, 0, str, &hKey); if ( status != DB_SUCCESS ) { cm_msg(MERROR, __FILE__, "Cannot find [%s] key in ODB, err = %i",str,status); return FE_ERR_ODB; } // creates hotlink of ODB subtree to structure amc13simulator_channel_odb[i] that's automatically update if (db_open_record(hDB, hKey, &amc13simulator_channel_odb[i], sizeof(AMC13SIMULATOR_CHANNEL_ODB), MODE_READ, amc13simulator_ODB_update, NULL) != DB_SUCCESS) { cm_msg(MERROR, __FILE__, "Cannot open [%s] settings in ODB",str); return FE_ERR_ODB; } dbprintf("%s(%d): %s enabled %d\n", __func__, __LINE__, str, amc13simulator_channel_odb[i].enabled ); dbprintf("%s(%d): %s port no %d\n", __func__, __LINE__, str, amc13simulator_channel_odb[i].port_no ); dbprintf("%s(%d): %s ip addr %s\n", __func__, __LINE__, str, amc13simulator_channel_odb[i].ip_addr ); } */ return ret; }
INT multi_init(EQUIPMENT * pequipment) { int status, size, i, j, index, ch_offset; char str[256]; HNDLE hDB, hKey, hNamesIn, hNamesOut; MULTI_INFO *m_info; BOOL partially_disabled; /* allocate private data */ pequipment->cd_info = calloc(1, sizeof(MULTI_INFO)); m_info = (MULTI_INFO *) pequipment->cd_info; /* get class driver root key */ cm_get_experiment_database(&hDB, NULL); sprintf(str, "/Equipment/%s", pequipment->name); db_create_key(hDB, 0, str, TID_KEY); db_find_key(hDB, 0, str, &m_info->hKeyRoot); /* save event format */ size = sizeof(str); db_get_value(hDB, m_info->hKeyRoot, "Common/Format", str, &size, TID_STRING, TRUE); if (equal_ustring(str, "Fixed")) m_info->format = FORMAT_FIXED; else if (equal_ustring(str, "MIDAS")) m_info->format = FORMAT_MIDAS; else if (equal_ustring(str, "YBOS")) m_info->format = FORMAT_YBOS; /* count total number of channels */ for (i = m_info->num_channels_input = m_info->num_channels_output = 0; pequipment->driver[i].name[0]; i++) { if (pequipment->driver[i].flags & DF_INPUT) m_info->num_channels_input += pequipment->driver[i].channels; if (pequipment->driver[i].flags & DF_OUTPUT) m_info->num_channels_output += pequipment->driver[i].channels; } if (m_info->num_channels_input == 0 && m_info->num_channels_output == 0) { cm_msg(MERROR, "multi_init", "No channels found in device driver list"); return FE_ERR_ODB; } /* Allocate memory for buffers */ if (m_info->num_channels_input) { m_info->names_input = (char *) calloc(m_info->num_channels_input, NAME_LENGTH); m_info->var_input = (float *) calloc(m_info->num_channels_input, sizeof(float)); m_info->update_threshold = (float *) calloc(m_info->num_channels_input, sizeof(float)); m_info->offset_input = (float *) calloc(m_info->num_channels_input, sizeof(float)); m_info->factor_input = (float *) calloc(m_info->num_channels_input, sizeof(float)); m_info->input_mirror = (float *) calloc(m_info->num_channels_input, sizeof(float)); m_info->channel_offset_input = (INT *) calloc(m_info->num_channels_input, sizeof(INT)); m_info->driver_input = (void *) calloc(m_info->num_channels_input, sizeof(void *)); } if (m_info->num_channels_output) { m_info->names_output = (char *) calloc(m_info->num_channels_output, NAME_LENGTH); m_info->var_output = (float *) calloc(m_info->num_channels_output, sizeof(float)); m_info->offset_output = (float *) calloc(m_info->num_channels_output, sizeof(float)); m_info->factor_output = (float *) calloc(m_info->num_channels_output, sizeof(float)); m_info->output_mirror = (float *) calloc(m_info->num_channels_output, sizeof(float)); m_info->channel_offset_output = (INT *) calloc(m_info->num_channels_output, sizeof(DWORD)); m_info->driver_output = (void *) calloc(m_info->num_channels_output, sizeof(void *)); } /*---- Create/Read settings ----*/ if (m_info->num_channels_input) { /* Update threshold */ for (i = 0; i < m_info->num_channels_input; i++) m_info->update_threshold[i] = 0.1f; /* default 0.1 */ db_merge_data(hDB, m_info->hKeyRoot, "Settings/Update Threshold", m_info->update_threshold, m_info->num_channels_input * sizeof(float), m_info->num_channels_input, TID_FLOAT); db_find_key(hDB, m_info->hKeyRoot, "Settings/Update Threshold", &hKey); db_open_record(hDB, hKey, m_info->update_threshold, m_info->num_channels_input * sizeof(float), MODE_READ, NULL, NULL); /* Offset */ for (i = 0; i < m_info->num_channels_input; i++) m_info->offset_input[i] = 0.f; /* default 0 */ db_merge_data(hDB, m_info->hKeyRoot, "Settings/Input Offset", m_info->offset_input, m_info->num_channels_input * sizeof(float), m_info->num_channels_input, TID_FLOAT); db_find_key(hDB, m_info->hKeyRoot, "Settings/Input Offset", &hKey); db_open_record(hDB, hKey, m_info->offset_input, m_info->num_channels_input * sizeof(float), MODE_READ, NULL, NULL); } for (i = 0; i < m_info->num_channels_output; i++) m_info->offset_output[i] = 0.f; if (m_info->num_channels_output) { db_merge_data(hDB, m_info->hKeyRoot, "Settings/Output Offset", m_info->offset_output, m_info->num_channels_output * sizeof(float), m_info->num_channels_output, TID_FLOAT); db_find_key(hDB, m_info->hKeyRoot, "Settings/Output Offset", &hKey); db_open_record(hDB, hKey, m_info->offset_output, m_info->num_channels_output * sizeof(float), MODE_READ, NULL, NULL); } /* Factor */ for (i = 0; i < m_info->num_channels_input; i++) m_info->factor_input[i] = 1.f; /* default 1 */ if (m_info->num_channels_input) { db_merge_data(hDB, m_info->hKeyRoot, "Settings/Input Factor", m_info->factor_input, m_info->num_channels_input * sizeof(float), m_info->num_channels_input, TID_FLOAT); db_find_key(hDB, m_info->hKeyRoot, "Settings/Input factor", &hKey); db_open_record(hDB, hKey, m_info->factor_input, m_info->num_channels_input * sizeof(float), MODE_READ, NULL, NULL); } if (m_info->num_channels_output) { for (i = 0; i < m_info->num_channels_output; i++) m_info->factor_output[i] = 1.f; db_merge_data(hDB, m_info->hKeyRoot, "Settings/Output Factor", m_info->factor_output, m_info->num_channels_output * sizeof(float), m_info->num_channels_output, TID_FLOAT); db_find_key(hDB, m_info->hKeyRoot, "Settings/Output factor", &hKey); db_open_record(hDB, hKey, m_info->factor_output, m_info->num_channels_output * sizeof(float), MODE_READ, NULL, NULL); } /*---- Create/Read variables ----*/ /* Input */ if (m_info->num_channels_input) { db_merge_data(hDB, m_info->hKeyRoot, "Variables/Input", m_info->var_input, m_info->num_channels_input * sizeof(float), m_info->num_channels_input, TID_FLOAT); db_find_key(hDB, m_info->hKeyRoot, "Variables/Input", &m_info->hKeyInput); memcpy(m_info->input_mirror, m_info->var_input, m_info->num_channels_input * sizeof(float)); } /* Output */ if (m_info->num_channels_output) { db_merge_data(hDB, m_info->hKeyRoot, "Variables/Output", m_info->var_output, m_info->num_channels_output * sizeof(float), m_info->num_channels_output, TID_FLOAT); db_find_key(hDB, m_info->hKeyRoot, "Variables/Output", &m_info->hKeyOutput); } /*---- Initialize device drivers ----*/ /* call init method */ partially_disabled = FALSE; for (i = 0; pequipment->driver[i].name[0]; i++) { sprintf(str, "Settings/Devices/%s", pequipment->driver[i].name); status = db_find_key(hDB, m_info->hKeyRoot, str, &hKey); if (status != DB_SUCCESS) { db_create_key(hDB, m_info->hKeyRoot, str, TID_KEY); status = db_find_key(hDB, m_info->hKeyRoot, str, &hKey); if (status != DB_SUCCESS) { cm_msg(MERROR, "multi_init", "Cannot create %s entry in online database", str); free_mem(m_info); return FE_ERR_ODB; } } /* check enabled flag */ size = sizeof(pequipment->driver[i].enabled); pequipment->driver[i].enabled = 1; sprintf(str, "Settings/Devices/%s/Enabled", pequipment->driver[i].name); status = db_get_value(hDB, m_info->hKeyRoot, str, &pequipment->driver[i].enabled, &size, TID_BOOL, TRUE); if (status != DB_SUCCESS) return FE_ERR_ODB; if (pequipment->driver[i].enabled) { status = device_driver(&pequipment->driver[i], CMD_INIT, hKey); if (status != FE_SUCCESS) { free_mem(m_info); return status; } } else partially_disabled = TRUE; } /* compose device driver channel assignment */ for (i = 0, j = 0, index = 0, ch_offset = 0; i < m_info->num_channels_input; i++, j++) { while (pequipment->driver[index].name[0] && (j >= pequipment->driver[index].channels || (pequipment->driver[index].flags & DF_INPUT) == 0)) { ch_offset += j; index++; j = 0; } m_info->driver_input[i] = &pequipment->driver[index]; m_info->channel_offset_input[i] = ch_offset; } for (i = 0, j = 0, index = 0, ch_offset = 0; i < m_info->num_channels_output; i++, j++) { while (pequipment->driver[index].name[0] && (j >= pequipment->driver[index].channels || (pequipment->driver[index].flags & DF_OUTPUT) == 0)) { ch_offset += j; index++; j = 0; } m_info->driver_output[i] = &pequipment->driver[index]; m_info->channel_offset_output[i] = ch_offset; } /*---- get default names from device driver ----*/ if (m_info->num_channels_input) { for (i = 0; i < m_info->num_channels_input; i++) { sprintf(m_info->names_input + NAME_LENGTH * i, "Input Channel %d", i); device_driver(m_info->driver_input[i], CMD_GET_LABEL, i - m_info->channel_offset_input[i], m_info->names_input + NAME_LENGTH * i); /* merge existing names with labels from driver */ status = db_find_key(hDB, m_info->hKeyRoot, "Settings/Names Input", &hKey); if (status != DB_SUCCESS) { db_create_key(hDB, m_info->hKeyRoot, "Settings/Names Input", TID_STRING); db_find_key(hDB, m_info->hKeyRoot, "Settings/Names Input", &hKey); db_set_data(hDB, hKey, m_info->names_input, NAME_LENGTH, 1, TID_STRING); } else { size = sizeof(str); db_get_data_index(hDB, hKey, str, &size, i, TID_STRING); if (!str[0]) db_set_data_index(hDB, hKey, m_info->names_input+NAME_LENGTH*i, NAME_LENGTH, i, TID_STRING); } } } if (m_info->num_channels_output) { for (i = 0; i < m_info->num_channels_output; i++) { sprintf(m_info->names_output + NAME_LENGTH * i, "Output Channel %d", i); device_driver(m_info->driver_output[i], CMD_GET_LABEL, i - m_info->channel_offset_output[i], m_info->names_output + NAME_LENGTH * i); /* merge existing names with labels from driver */ status = db_find_key(hDB, m_info->hKeyRoot, "Settings/Names Output", &hKey); if (status != DB_SUCCESS) { db_create_key(hDB, m_info->hKeyRoot, "Settings/Names Output", TID_STRING); db_find_key(hDB, m_info->hKeyRoot, "Settings/Names Output", &hKey); db_set_data(hDB, hKey, m_info->names_input, NAME_LENGTH, 1, TID_STRING); } else { size = sizeof(str); db_get_data_index(hDB, hKey, str, &size, i, TID_STRING); if (!str[0]) db_set_data_index(hDB, hKey, m_info->names_input+NAME_LENGTH*i, NAME_LENGTH, i, TID_STRING); } } } /*---- set labels from midas SC names ----*/ if (m_info->num_channels_input) { for (i = 0; i < m_info->num_channels_input; i++) { device_driver(m_info->driver_input[i], CMD_SET_LABEL, i - m_info->channel_offset_input[i], m_info->names_input + NAME_LENGTH * i); } /* open hotlink on input channel names */ if (db_find_key(hDB, m_info->hKeyRoot, "Settings/Names Input", &hNamesIn) == DB_SUCCESS) db_open_record(hDB, hNamesIn, m_info->names_input, NAME_LENGTH * m_info->num_channels_input, MODE_READ, multi_update_label, pequipment); } for (i = 0; i < m_info->num_channels_output; i++) { device_driver(m_info->driver_output[i], CMD_SET_LABEL, i - m_info->channel_offset_output[i], m_info->names_output + NAME_LENGTH * i); } /* open hotlink on output channel names */ if (m_info->num_channels_output) { if (db_find_key(hDB, m_info->hKeyRoot, "Settings/Names Output", &hNamesOut) == DB_SUCCESS) db_open_record(hDB, hNamesOut, m_info->names_output, NAME_LENGTH * m_info->num_channels_output, MODE_READ, multi_update_label, pequipment); /* open hot link to output record */ db_open_record(hDB, m_info->hKeyOutput, m_info->var_output, m_info->num_channels_output * sizeof(float), MODE_READ, multi_output, pequipment); } /* set initial demand values */ for (i = 0; i < m_info->num_channels_output; i++) { if (pequipment->driver[index].flags & DF_PRIO_DEVICE) { /* read default value directly from device bypassing multi-thread buffer */ device_driver(m_info->driver_output[i], CMD_GET_DEMAND, i - m_info->channel_offset_output[i], &m_info->output_mirror[i]); } else { /* use default value from ODB */ m_info->output_mirror[i] = m_info->var_output[i] * m_info->factor_output[i] - m_info->offset_output[i]; device_driver(m_info->driver_output[i], CMD_SET, i - m_info->channel_offset_output[i], m_info->output_mirror[i]); } } if (m_info->num_channels_output) db_set_record(hDB, m_info->hKeyOutput, m_info->output_mirror, m_info->num_channels_output * sizeof(float), 0); /* initially read all input channels */ if (m_info->num_channels_input) multi_read(pequipment, -1); if (partially_disabled) return FE_PARTIALLY_DISABLED; return FE_SUCCESS; }
INT amc13simulator_ODB_init(void) { INT ret = SUCCESS; INT status; char str[MAX_ODB_PATH]; char str_aux[MAX_ODB_PATH]; HNDLE hKey; /* reset channel info information */ int i; amc13simulator_settings_odb.sync = 0; for (i=0; i<AMC13SIMULATOR_NUM; i++){ amc13simulator_channel_odb[i].enabled = 0; amc13simulator_channel_odb[i].port_no = 0; sprintf(amc13simulator_channel_odb[i].ip_addr,"000.000.0.000"); sprintf(amc13simulator_channel_odb[i].host_name,""); } dbprintf(str,"/Equipment/AMC13Simulator%02d/Settings/Globals",frontend_index); sprintf(str,"/Equipment/AMC13Simulator%02d/Settings/Globals",frontend_index); // create ODB structure /Equipment/%s/Settings/Globals if doesn't exist status = db_check_record(hDB, 0, str, AMC13SIMULATOR_SETTINGS_ODB_STR, TRUE); if ( status != DB_SUCCESS ) { cm_msg(MERROR, __FILE__, "Cannot create [%s] entry in ODB, err = %i",str,status); return FE_ERR_ODB; } // returns key handle "hDB" to ODB name "str" for fast access status = db_find_key(hDB, 0, str, &hKey); if ( status != DB_SUCCESS ) { cm_msg(MERROR, __FILE__, "Cannot find [%s] key in ODB, err = %i",str,status); return FE_ERR_ODB; } // creates hotlink of ODB subtree to structure amc13simulator_settings_odb that's automatically updated if (db_open_record(hDB, hKey, &amc13simulator_settings_odb, sizeof(AMC13SIMULATOR_SETTINGS_ODB), MODE_READ, amc13simulator_ODB_update, NULL) != DB_SUCCESS) { cm_msg(MERROR, __FILE__, "Cannot open [%s] settings in ODB",str); return FE_ERR_ODB; } dbprintf("%s(%d): %s sync %d\n", __func__, __LINE__, str, amc13simulator_settings_odb.sync ); for (i=0; i<AMC13SIMULATOR_NUM; i++){ sprintf(str,"/Equipment/AMC13Simulator%02d/Settings/Channel%02d",frontend_index,i+1); // create ODB structure /Equipment/%s/Settings/Channel%02d if doesn't exist status = db_check_record(hDB, 0, str, AMC13SIMULATOR_CHANNEL_ODB_STR, TRUE); if ( status != DB_SUCCESS ) { cm_msg(MERROR, __FILE__, "Cannot create [%s] entry in ODB, err = %i",str,status); return FE_ERR_ODB; } // returns key handle "hDB" to ODB name "str" for fast access status = db_find_key(hDB, 0, str, &hKey); if ( status != DB_SUCCESS ) { cm_msg(MERROR, __FILE__, "Cannot find [%s] key in ODB, err = %i",str,status); return FE_ERR_ODB; } // creates hotlink of ODB subtree to structure amc13simulator_channel_odb[i] that's automatically update if (db_open_record(hDB, hKey, &amc13simulator_channel_odb[i], sizeof(AMC13SIMULATOR_CHANNEL_ODB), MODE_READ, amc13simulator_ODB_update, NULL) != DB_SUCCESS) { cm_msg(MERROR, __FILE__, "Cannot open [%s] settings in ODB",str); return FE_ERR_ODB; } dbprintf("%s(%d): %s enabled %d\n", __func__, __LINE__, str, amc13simulator_channel_odb[i].enabled ); dbprintf("%s(%d): %s port no %d\n", __func__, __LINE__, str, amc13simulator_channel_odb[i].port_no ); dbprintf("%s(%d): %s ip addr %s\n", __func__, __LINE__, str, amc13simulator_channel_odb[i].ip_addr ); } return ret; }
INT register_equipment(void) { INT index, size, status; char str[256]; EQUIPMENT_INFO *eq_info; EQUIPMENT_STATS *eq_stats; HNDLE hKey; /* get current ODB run state */ size = sizeof(run_state); run_state = STATE_STOPPED; db_get_value(hDB, 0, "/Runinfo/State", &run_state, &size, TID_INT, TRUE); size = sizeof(run_number); run_number = 1; status = db_get_value(hDB, 0, "/Runinfo/Run number", &run_number, &size, TID_INT, TRUE); assert(status == SUCCESS); /* scan EQUIPMENT table from mevb.C */ for (index = 0; equipment[index].name[0]; index++) { eq_info = &equipment[index].info; eq_stats = &equipment[index].stats; if (eq_info->event_id == 0) { printf("\nEvent ID 0 for %s not allowed\n", equipment[index].name); cm_disconnect_experiment(); ss_sleep(5000); exit(0); } /* init status */ equipment[index].status = EB_SUCCESS; sprintf(str, "/Equipment/%s/Common", equipment[index].name); /* get last event limit from ODB */ if (eq_info->eq_type != EQ_SLOW) { db_find_key(hDB, 0, str, &hKey); size = sizeof(double); if (hKey) db_get_value(hDB, hKey, "Event limit", &eq_info->event_limit, &size, TID_DOUBLE, TRUE); } /* Create common subtree */ status = db_check_record(hDB, 0, str, EQUIPMENT_COMMON_STR, TRUE); if (status != DB_SUCCESS) { printf("Cannot check equipment record, status = %d\n", status); ss_sleep(3000); } db_find_key(hDB, 0, str, &hKey); if (equal_ustring(eq_info->format, "FIXED")) equipment[index].format = FORMAT_FIXED; else /* default format is MIDAS */ equipment[index].format = FORMAT_MIDAS; gethostname(eq_info->frontend_host, sizeof(eq_info->frontend_host)); strcpy(eq_info->frontend_name, frontend_name); strcpy(eq_info->frontend_file_name, frontend_file_name); /* set record from equipment[] table in frontend.c */ db_set_record(hDB, hKey, eq_info, sizeof(EQUIPMENT_INFO), 0); /* get record once at the start equipment info */ size = sizeof(EQUIPMENT_INFO); db_get_record(hDB, hKey, eq_info, &size, 0); /*---- Create just the key , leave it empty ---------------------------------*/ sprintf(str, "/Equipment/%s/Variables", equipment[index].name); db_create_key(hDB, 0, str, TID_KEY); db_find_key(hDB, 0, str, &hKey); equipment[index].hkey_variables = hKey; /*---- Create and initialize statistics tree -------------------*/ sprintf(str, "/Equipment/%s/Statistics", equipment[index].name); status = db_check_record(hDB, 0, str, EQUIPMENT_STATISTICS_STR, TRUE); if (status != DB_SUCCESS) { printf("Cannot create/check statistics record, error %d\n", status); ss_sleep(3000); } status = db_find_key(hDB, 0, str, &hKey); if (status != DB_SUCCESS) { printf("Cannot find statistics record, error %d\n", status); ss_sleep(3000); } eq_stats->events_sent = 0; eq_stats->events_per_sec = 0; eq_stats->kbytes_per_sec = 0; /* open hot link to statistics tree */ status = db_open_record(hDB, hKey, eq_stats, sizeof(EQUIPMENT_STATS) , MODE_WRITE, NULL, NULL); if (status != DB_SUCCESS) { cm_msg(MERROR, "register_equipment", "Cannot open statistics record, error %d. Probably other FE is using it", status); ss_sleep(3000); } /*---- open event buffer ---------------------------------------*/ if (eq_info->buffer[0]) { status = bm_open_buffer(eq_info->buffer, 2 * max_event_size, &equipment[index].buffer_handle); if (status != BM_SUCCESS && status != BM_CREATED) { cm_msg(MERROR, "register_equipment", "Cannot open event buffer. Try to reduce EVENT_BUFFER_SIZE in midas.h \ and rebuild the system."); return 0; } if (1) { int level = 0; bm_get_buffer_level(equipment[index].buffer_handle, &level); printf("Buffer %s, level %d, info: \n", eq_info->buffer, level); } /* set the default buffer cache size */ bm_set_cache_size(equipment[index].buffer_handle, 0, SERVER_CACHE_SIZE); } else {