00001
00002
00003
00004
00005
00009
00015
00021
00031
00032
00033 #include <stdio.h>
00034 #include <stdlib.h>
00035 #include <string.h>
00036 #include <time.h>
00037 #include <signal.h>
00038
00039
00040 #include "midas.h"
00041
00042
00043 #ifdef OS_WINNT
00044 #define VISUAL_CPLUSPLUS
00045 #endif
00046 #ifdef __linux__
00047 #define f2cFortran
00048 #endif
00049
00050 #ifdef HAVE_HBOOK
00051 #include <cfortran.h>
00052 #include <hbook.h>
00053
00054 PAWC_DEFINE(1000000);
00055 #endif
00056
00057
00058 #include "TGlobalData.h"
00059 #include "TSetupData.h"
00060 #include "TVacuumData.h"
00061
00062
00063
00064
00065 char *analyzer_name = "Analyzer";
00066
00067
00068 INT analyzer_loop_period = 0;
00069
00070
00071 INT odb_size = DEFAULT_ODB_SIZE;
00072
00076 TGlobalData* gData;
00080 TSetupData* gSetup;
00083 TVacuumData* gVacuum;
00084
00085 TSetupData* TSetupData::Instance()
00086 {
00087 return gSetup;
00088 }
00089
00090 TGlobalData* TGlobalData::Instance()
00091 {
00092 return gData;
00093 }
00094
00095 bool UpdateDetectorBankNameMap(TSetupData *gSetup);
00096
00097
00098
00099 #include "MODULES.h"
00100
00101
00102
00103 BANK_LIST ana_trigger_bank_list[] = {
00104
00105 { "" },
00106 };
00107
00108 BANK_LIST ana_vacuum_bank_list[] = {
00109
00110 { "" },
00111 };
00112
00113
00114 extern ANA_MODULE MVacuumHisto_module;
00115 ANA_MODULE *Vacuum_module[] = {
00116 &MVacuumHisto_module,
00117 NULL };
00118
00119 ANALYZE_REQUEST analyze_request[] = {
00120
00121 { "Vacuum",
00122 { 24,
00123 TRIGGER_ALL,
00124 GET_SOME,
00125 "SYSTEM",
00126 TRUE,
00127 "", "", },
00128 NULL,
00129 Vacuum_module,
00130 ana_vacuum_bank_list,
00131 1000,
00132 TRUE,
00133 },
00134
00135 { "Trigger",
00136 { 1,
00137 TRIGGER_ALL,
00138 GET_SOME,
00139 "SYSTEM",
00140 TRUE,
00141 "", "", },
00142 NULL,
00143 trigger_module,
00144 ana_trigger_bank_list,
00145 1000,
00146 TRUE,
00147 },
00148
00149 { "" },
00150 };
00151
00152
00153
00154 static void catastrophe(int signum)
00155 {
00156 #if 0
00157 static bool previously_caught_signal = false;
00158 #endif
00159
00160 printf("Caught signal %d, exiting...", signum);
00161
00162 #if 0
00163 if(!previously_caught_signal) {
00164 previously_caught_signal = true;
00165
00166 #ifdef HAVE_ROOT
00167 printf("Attempting to save histograms...\n");
00168 extern void CloseRootOutputFile();
00169 CloseRootOutputFile();
00170 #endif
00171 }
00172 #endif
00173
00174 exit(-signum);
00175 }
00176
00177 INT analyzer_init()
00178 {
00179 bool ret;
00180
00181
00182 gData = new TGlobalData();
00183
00184
00185 gSetup = new TSetupData();
00186 ret = UpdateDetectorBankNameMap(gSetup);
00187 if (!ret) {
00188 printf("THERE WAS AN ERROR!\n");
00189 return 0;
00190 }
00191
00192 signal(SIGHUP , catastrophe);
00193 signal(SIGINT , catastrophe);
00194 signal(SIGQUIT , catastrophe);
00195 signal(SIGILL , catastrophe);
00196 signal(SIGABRT , catastrophe);
00197 signal(SIGFPE , catastrophe);
00198 signal(SIGKILL , catastrophe);
00199 signal(SIGSEGV , catastrophe);
00200 signal(SIGPIPE , catastrophe);
00201 signal(SIGTERM , catastrophe);
00202
00203
00204 gVacuum = new TVacuumData();
00205
00206 return SUCCESS;
00207 }
00208
00209
00210
00211 INT analyzer_exit()
00212 {
00213 if(gData) {
00214 delete gData;
00215 gData = NULL;
00216 }
00217
00218 if(gSetup) {
00219 delete gSetup;
00220 gSetup = NULL;
00221 }
00222
00223 if(gVacuum) {
00224 delete gVacuum;
00225 gVacuum = NULL;
00226 }
00227
00228 return CM_SUCCESS;
00229 }
00230
00231
00232
00233 INT ana_begin_of_run(INT run_number, char *error)
00234 {
00235 printf("Analyzer saw beginning of run %d\n", run_number);
00236 return CM_SUCCESS;
00237 }
00238
00239
00240
00241 INT ana_end_of_run(INT run_number, char *error)
00242 {
00243 printf("Analyzer saw end of run %d\n", run_number);
00244 return CM_SUCCESS;
00245 }
00246
00247
00248
00249 INT ana_pause_run(INT run_number, char *error)
00250 {
00251 return CM_SUCCESS;
00252 }
00253
00254
00255
00256 INT ana_resume_run(INT run_number, char *error)
00257 {
00258 return CM_SUCCESS;
00259 }
00260
00261
00262
00263 INT analyzer_loop()
00264 {
00265 return CM_SUCCESS;
00266 }
00267
00268
00269
00270
00271 bool UpdateDetectorBankNameMap(TSetupData *gSetup){
00272
00273 bool ret = true;
00274 HNDLE hDB, hKey;
00275 char keyName[200];
00276
00277 if(cm_get_experiment_database(&hDB, NULL) != CM_SUCCESS){
00278 printf("Warning: Could not connect to ODB database!\n");
00279 return false;
00280 }
00281
00282 sprintf(keyName, "/Analyzer/WireMap/BankName");
00283 if(db_find_key(hDB,0,keyName, &hKey) != SUCCESS){
00284 printf("Warning: Could not find key %s\n", keyName);
00285 return false;
00286 }
00287 KEY bk_key;
00288 if(db_get_key(hDB, hKey, &bk_key) != DB_SUCCESS){
00289 printf("Warning: Could not find key %s\n", keyName);
00290 return false;
00291 }
00292 char BankNames[bk_key.num_values][bk_key.item_size];
00293 int size = sizeof(BankNames);
00294 if(db_get_value(hDB, 0, keyName , BankNames, &size, TID_STRING, 0) != DB_SUCCESS){
00295 printf("Warning: Could not retrieve values for key %s\n", keyName);
00296 ret = false;
00297 }
00298
00299 sprintf(keyName, "/Analyzer/WireMap/DetectorName");
00300 if(db_find_key(hDB,0,keyName, &hKey) != SUCCESS){
00301 printf("Warning: Could not find key %s\n", keyName);
00302 return false;
00303 }
00304 KEY det_key;
00305 if(db_get_key(hDB, hKey, &det_key) != DB_SUCCESS){
00306 printf("Warning: Could not find key %s\n", keyName);
00307 return false;
00308 }
00309 char DetectorNames[det_key.num_values][det_key.item_size];
00310 size = sizeof(DetectorNames);
00311 if(db_get_value(hDB, 0, keyName , DetectorNames, &size, TID_STRING, 0) != DB_SUCCESS){
00312 printf("Warning: Could not retrieve values for key %s\n", keyName);
00313 ret = false;
00314 }
00315
00316 sprintf(keyName, "/Analyzer/WireMap/TimeShift");
00317 if(db_find_key(hDB,0,keyName, &hKey) != SUCCESS){
00318 printf("Warning: Could not find key %s\n", keyName);
00319 return false;
00320 }
00321 KEY timeshift_key;
00322 if(db_get_key(hDB, hKey, ×hift_key) != DB_SUCCESS){
00323 printf("Warning: Could not find key %s\n", keyName);
00324 return false;
00325 }
00326 float TimeShifts[timeshift_key.num_values];
00327 size = sizeof(TimeShifts);
00328 if(db_get_value(hDB, 0, keyName, TimeShifts, &size, TID_FLOAT, 0) != DB_SUCCESS){
00329 printf("Warning: Could not retrieve values for key %s\n", keyName);
00330 ret = false;
00331 }
00332
00333 sprintf(keyName, "/Analyzer/WireMap/Enabled");
00334 if(db_find_key(hDB,0,keyName, &hKey) != SUCCESS){
00335 printf("Warning: Could not find key %s\n", keyName);
00336 return false;
00337 }
00338 KEY enablebit_key;
00339 if (db_get_key(hDB, hKey, &enablebit_key) != DB_SUCCESS)
00340 {
00341 printf("Warning: Could not find key %s\n", keyName);
00342 return false;
00343 }
00344 int EnableBitsBuffer[enablebit_key.num_values];
00345 size = sizeof(EnableBitsBuffer);
00346
00347 if(db_get_value(hDB, 0, keyName , EnableBitsBuffer, &size, TID_BOOL, 0) != DB_SUCCESS){
00348 printf("Warning: Could not retrieve values for key %s\n", keyName);
00349 ret = false;
00350 }
00351
00352 bool EnableBits[enablebit_key.num_values];
00353
00354 int jj = 0;
00355 for (jj = 0; jj < enablebit_key.num_values; ++jj)
00356 {
00357 EnableBits[jj] = EnableBitsBuffer[jj] == 1 ? 1:0;
00358
00359 }
00360
00361
00362 sprintf(keyName, "/Analyzer/WireMap/TriggerPolarity");
00363 if(db_find_key(hDB,0,keyName, &hKey) != SUCCESS){
00364 printf("Warning: Could not find key %s\n", keyName);
00365 return false;
00366 }
00367 KEY pol_key;
00368 if(db_get_key(hDB, hKey, &pol_key) != DB_SUCCESS){
00369 printf("Warning: Could not find key %s\n", keyName);
00370 return false;
00371 }
00372 int TriggerPolarities[pol_key.num_values];
00373 size = sizeof(TriggerPolarities);
00374 if(db_get_value(hDB, 0, keyName , TriggerPolarities, &size, TID_INT, 0) != DB_SUCCESS){
00375 printf("Warning: Could not retrieve values for key %s\n", keyName);
00376 ret = false;
00377 }
00378
00379 sprintf(keyName, "/Analyzer/WireMap/Pedestal");
00380 if(db_find_key(hDB,0,keyName, &hKey) != SUCCESS){
00381 printf("Warning: Could not find key %s\n", keyName);
00382 return false;
00383 }
00384 KEY ped_key;
00385 if(db_get_key(hDB, hKey, &ped_key) != DB_SUCCESS){
00386 printf("Warning: Could not find key %s\n", keyName);
00387 return false;
00388 }
00389 int Pedestals[ped_key.num_values];
00390 size = sizeof(Pedestals);
00391 if(db_get_value(hDB, 0, keyName , Pedestals, &size, TID_INT, 0) != DB_SUCCESS){
00392 printf("Warning: Could not retrieve values for key %s\n", keyName);
00393 ret = false;
00394 }
00395
00396 if(det_key.num_values != bk_key.num_values){
00397 printf("Warning: Key sizes are not equal for banks and detectors in /Analyzer/WireMap/\n");
00398 ret = false;
00399 }
00400 else printf("sizes are %d\n", det_key.num_values);
00401
00402 std::vector<bool> duplicate_dets(det_key.num_values, false);
00403 for(int i=0; i<det_key.num_values; i++){
00404 if(strcmp(BankNames[i], "") == 0) {
00405 printf("Found bank name that is empty! (Index %d)\n", i);
00406 ret = false;
00407 continue;
00408 }
00409 if(strcmp(DetectorNames[i], "") == 0) printf("Warning: No detector name associated with bank %s!\n", BankNames[i]);
00411
00412
00413
00414 std::string bank_name(BankNames[i]), detector(DetectorNames[i]);
00415 if(detector != "blank" && !(gSetup->SetDetectorName(bank_name,detector))) {
00416 printf("WARNING: Detector %s listed multiple times in ODB! (Duplicate index %d)\n", detector.c_str(), i);
00417 duplicate_dets.at(i) = true;
00418 }
00419 gSetup->SetTriggerPolarity(bank_name,TriggerPolarities[i]);
00420 gSetup->SetPedestal(bank_name,Pedestals[i]);
00421 gSetup->SetTimeShift(bank_name,TimeShifts[i]);
00422 gSetup->SetEnableBit(bank_name,EnableBits[i]);
00423
00424
00426
00427 int DCMPhase = 1;
00428 if(TSetupData::IsFADC(bank_name)){
00429 int iChn = (int)(bank_name[1] - 97);
00430 std::string iAddr = bank_name.substr(2, 2);
00431
00432 sprintf(keyName, "/Equipment/Crate 9/Settings/NFADC %s/Channel %d/DCM phase", iAddr.c_str(), iChn);
00433 if(db_find_key(hDB,0,keyName, &hKey) == SUCCESS){
00434 db_get_key(hDB, hKey, &bk_key);
00435
00436 int size = sizeof(DCMPhase);
00437 if(db_get_value(hDB, 0, keyName , &DCMPhase, &size, TID_INT, 0) == DB_SUCCESS){
00438
00439 }
00440 }
00441
00443
00444 sprintf(keyName, "/Analyzer/WireMap/SamplingFrequency");
00445 if(db_find_key(hDB, 0, keyName, &hKey) == SUCCESS){
00446 std::string bank_name(BankNames[i]);
00447 float frequency = 170E6 / DCMPhase;
00448 int size = sizeof(float);
00449 db_set_data_index(hDB, hKey, &frequency, size, i, TID_FLOAT);
00450 }
00451 }
00452
00453 sprintf(keyName, "/Analyzer/WireMap/SamplingFrequency");
00454 if(db_find_key(hDB,0,keyName, &hKey) != SUCCESS){
00455 printf("Warning: Could not find key %s\n", keyName);
00456 return false;
00457 }
00458 KEY freq_key;
00459 if(db_get_key(hDB, hKey, &freq_key) != DB_SUCCESS){
00460 printf("Warning: Could not find key %s\n", keyName);
00461 return false;
00462 }
00463 float SamplingFrequencies[freq_key.num_values];
00464 size = sizeof(SamplingFrequencies);
00465 if(db_get_value(hDB, 0, keyName , SamplingFrequencies, &size, TID_FLOAT, 0) != DB_SUCCESS){
00466 printf("Warning: Could not retrieve values for key %s\n", keyName);
00467 ret = false;
00468 }
00469
00470 double clockTickInNs = (1/SamplingFrequencies[i]) * 1e9;
00471
00472 gSetup->SetClockTick(bank_name,clockTickInNs);
00473
00475
00476 sprintf(keyName, "/Analyzer/WireMap/ADCSlopeCalib");
00477 if(db_find_key(hDB,0,keyName, &hKey) != SUCCESS){
00478 printf("Warning: Could not find key %s\n", keyName);
00479 return false;
00480 }
00481 KEY adc_slope_calib_key;
00482 if(db_get_key(hDB, hKey, &adc_slope_calib_key) != DB_SUCCESS){
00483 printf("Warning: Could not find key %s\n", keyName);
00484 return false;
00485 }
00486 float ADCSlopeCalibs[adc_slope_calib_key.num_values];
00487 size = sizeof(ADCSlopeCalibs);
00488 if(db_get_value(hDB, 0, keyName , ADCSlopeCalibs, &size, TID_FLOAT, 0) != DB_SUCCESS){
00489 printf("Warning: Could not retrieve values for key %s\n", keyName);
00490 ret = false;
00491 }
00492
00493 sprintf(keyName, "/Analyzer/WireMap/ADCOffsetCalib");
00494 if(db_find_key(hDB,0,keyName, &hKey) != SUCCESS){
00495 printf("Warning: Could not find key %s\n", keyName);
00496 return false;
00497 }
00498 KEY adc_offset_calib_key;
00499 if(db_get_key(hDB, hKey, &adc_offset_calib_key) != DB_SUCCESS){
00500 printf("Warning: Could not find key %s\n", keyName);
00501 return false;
00502 }
00503 float ADCOffsetCalibs[adc_offset_calib_key.num_values];
00504 size = sizeof(ADCOffsetCalibs);
00505 if(db_get_value(hDB, 0, keyName , ADCOffsetCalibs, &size, TID_FLOAT, 0) != DB_SUCCESS){
00506 printf("Warning: Could not retrieve values for key %s\n", keyName);
00507 ret = false;
00508 }
00509
00510
00511 float adcSlopeValue = ADCSlopeCalibs[i];
00512 float adcOffsetValue = ADCOffsetCalibs[i];
00513 gSetup->SetADCSlopeCalib(bank_name,adcSlopeValue);
00514 gSetup->SetADCOffsetCalib(bank_name,adcOffsetValue);
00515
00517
00518 if(TSetupData::IsFADC(bank_name )) {
00519 gSetup->SetNBits(bank_name,12);
00520 }
00521 else if(TSetupData::IsHoustonCAEN(bank_name)) {
00522 gSetup->SetNBits(bank_name,14);
00523 }
00524 else if (TSetupData::IsBostonCAEN(bank_name)) {
00525 gSetup->SetNBits(bank_name,12);
00526 } else if (bank_name == "ZZZZ") {
00527 gSetup->SetNBits(bank_name, 0);
00528 } else {
00529 printf("Cannot determine digitizer type of bank %s!\n", bank_name.c_str());
00530 ret = false;
00531 }
00532
00534
00535 if(TSetupData::IsFADC(bank_name)) {
00536 std::string iAddr(bank_name);
00537 int iChn = (int)(iAddr[1] - 97);
00538 iAddr = iAddr.substr(2, 2);
00539
00540 char wireKey[100];
00541 BOOL enabled = false;
00542 int size = sizeof(enabled);
00543 sprintf(wireKey, "/Analyzer/WireMap/Enabled");
00544 if(db_find_key(hDB,0,wireKey, &hKey) == SUCCESS){
00545
00546 db_set_data_index(hDB, hKey, &enabled, size, i, TID_BOOL);
00547
00548 sprintf(keyName, "/Equipment/Crate 9/Settings/NFADC %s/Enabled", iAddr.c_str());
00549 if(db_get_value(hDB, 0, keyName , &enabled, &size, TID_BOOL, 0) == DB_SUCCESS) {
00550 if(enabled){
00551 sprintf(keyName, "/Equipment/Crate 9/Settings/NFADC %s/Channel %d/Trigger mask", iAddr.c_str(), iChn);
00552 int trigger_mask;
00553 size = sizeof(trigger_mask);
00554 if(db_get_value(hDB, 0, keyName , &trigger_mask, &size, TID_INT, 0) == DB_SUCCESS) {
00555 if (trigger_mask == 1){
00556 db_set_data_index(hDB, hKey, &enabled, size, i, TID_BOOL);
00557 }
00558 } else {
00559 printf("Could not find Trigger Mask key in ODB!\n");
00560 ret = false;
00561 }
00562 } else {
00563 if (duplicate_dets.at(i)) {
00564 printf("ATTENTION: Duplicate detector at index %d resolved (not enabled).\n", i);
00565 duplicate_dets.at(i) = false;
00566 }
00567 }
00568 }
00569
00570 } else {
00571 printf("Could not find Enabled key in ODB!\n");
00572 ret = false;
00573 }
00574 } else if (TSetupData::IsHoustonCAEN(bank_name)) {
00575 BOOL enabled = true;
00576 int size;
00577 char aCh = bank_name[1];
00578 int iCh = aCh - 'a';
00579
00580 size = sizeof(enabled);
00581 sprintf(keyName, "/Equipment/Crate 4/Settings/CAEN0/Ch%1d/Enabled", iCh);
00582 if (db_get_value(hDB, 0, keyName, &enabled, &size, TID_BOOL, 0) == DB_SUCCESS) {
00583 if (!enabled && duplicate_dets.at(i)){
00584 duplicate_dets[i] = false;
00585 printf("ATTENTION: Duplicate detector at index %d resolved (not enabled).\n", i);
00586 }
00587 } else {
00588 printf("Could not find UH CAEN Enabled key in ODB!\n");
00589 ret = false;
00590 }
00591 } else if (TSetupData::IsBostonCAEN(bank_name)) {
00592 BOOL enabled = true;
00593 int size;
00594 char aCh = bank_name[1];
00595 int iCh = aCh - 'a';
00596
00597 size = sizeof(enabled);
00598 sprintf(keyName, "/Equipment/Crate 5/Settings/CAEN/Ch%02d/enable", iCh);
00599 if (db_get_value(hDB, 0, keyName, &enabled, &size, TID_BOOL, 0) == DB_SUCCESS) {
00600 if (!enabled && duplicate_dets.at(i)) {
00601 printf("ATTENTION: Duplicate detector at index %d resolved (not enabled).\n", i);
00602 duplicate_dets[i] = false;
00603 }
00604 } else {
00605 printf("Could not find BU CAEN Enabled key in ODB!\n");
00606 ret = false;
00607 }
00608 }
00609 }
00610
00611
00612 for (int i = 0; i < duplicate_dets.size(); ++i) {
00613 if (duplicate_dets.at(i)) {
00614 printf("ERROR: Multiple detectors enabled! (Index %d)\n", i);
00615 ret = false;
00616 }
00617 }
00618
00619 return ret;
00620 }
00621