#include "TRuntimeParameters.hxx" #include //#include #include #include #include //#include "HEPUnits.hxx" //#include "HEPConstants.hxx" //#include "TND280Log.hxx" using namespace std; TRuntimeParameters* TRuntimeParameters::fTRuntimeParameters = NULL; TRuntimeParameters::TRuntimeParameters() { std::cout << "*** Initializing TRuntimeParameters" << std::endl; // Build the set of units for Geant4. fUnitsTableParser = new TUnitsTableParser(); // We do not open any parameter files by default in the constructor. // Instead, parameter files are open automatically when an entry // in the file is requested by the user. } TRuntimeParameters::~TRuntimeParameters() { } /// Read in each parameter file. void TRuntimeParameters::ReadInputFile(TString fileName, TString dirName, bool tryFile, bool fixParameters) { TString fullFileName; // Make full file name. if(!dirName.CompareTo("")) fullFileName = fileName.Data(); else fullFileName = dirName.Data() + TString("/") + fileName.Data(); std::cout << "Opening parameter file: " << fullFileName.Data() << std::endl; ifstream inputFile(fullFileName.Data(), ios::in); // If input file doesn't exist. if (!inputFile){ // Just return if 'tryFile' is specified. if(tryFile){ std::cout << "\n***** TRuntimeParameters::ReadInputFile *****\n" << "Cannot open input file '" << fullFileName.Data() << "'." << std::endl; return; }else{ // Otherwise, throw exception. std::cerr << "\n***** TRuntimeParameters::ReadInputFile *****\n" << "Cannot open input file '" << fullFileName.Data() << "'." << std::endl; //throw EBadParameterFile(); exit(7); } } int inputState = 0; string inputString; string parameterName; string parameterValueUnit; string parameterValue; while (inputFile >> inputString) { if (inputState == 0) { if (inputString == "<") inputState = 1; } else if (inputState == 1) { parameterName = inputString; inputState = 2; } else if (inputState == 2) { if (inputString == "=") inputState = 3; else { std::cerr << "\n***** TRuntimeParameters::ReadInputFile *****\n" << "Input file '" << fileName << "'. Last parameter '" << parameterName << "'.\n" << "Cannot find symbol '='.\n" << "Badly formatted parameters file." << std::endl; //throw EBadParameterFile(); exit(7); } } else if (inputState == 3) { // parameterValue = atof(inputString.c_str()); parameterValue = inputString.c_str(); parameterValueUnit = inputString; inputState = 4; } else if (inputState == 4) { if (inputString == ">"){ // Finished reading. Save parameter; but only if the parameter // isn't already 'fixed' if(fixedParameters.find(parameterName) == fixedParameters.end()){ mapOfTRuntimeParameters[parameterName] = parameterValue; // If fixParameters bool is set, fix this parameter now. if(fixParameters) fixedParameters.insert(parameterName); } inputState = 0; } else if (inputString == "<") { std::cerr << "\n***** TRuntimeParameters::ReadInputFile *****\n" << "Input file '" << fileName << "'. Last parameter '" << parameterName << "'.\n" << "Unexpected symbol '<'.\n" << "Badly formatted parameters file." << std::endl; //throw EBadParameterFile(); exit(7); } else { // The parameter must have a unit. Resave the value with the correct unit. parameterValueUnit.append(" "); parameterValueUnit.append(inputString); // Use TUnitsTableParser class to convert string of value+unit to // a double with value+unit. parameterValue = fUnitsTableParser->Convert2DoubleWithUnit(parameterValueUnit); } } } if (inputState != 0) { std::cerr << "\n***** TRuntimeParameters::ReadInputFile *****\n" << "Input file '" << fileName << "'. Last parameter '" << parameterName << "'.\n" << "Cannot find symbol '>' at the end of file.\n" << "Badly formatted parameters file." << std::endl; //throw EBadParameterFile(); exit(7); } inputFile.close(); } void TRuntimeParameters::PrintListOfParameters() { std::cout << "***** TRuntimeParameters::PrintListOfParameters *****" << std::endl; std::cout << "List of parameters:" << std::endl; for (mapIterator i = mapOfTRuntimeParameters.begin(); i != mapOfTRuntimeParameters.end(); i++) std::cout << " " << (*i).first << " = " << (*i).second << std::endl; } int TRuntimeParameters::TryLoadingParametersFile(std::string parameterName){ std::cout << "Trying to load parameters file for parameter: " << parameterName << std::endl; // Figure out the name of the package int pos = parameterName.find("."); TString packageName(parameterName.c_str(), pos); // and the file name for parameters file TString fileName = packageName + ".parameters.dat"; TString dirName; char *ctmp = getenv("FITQUN_ROOT"); if (ctmp!=NULL) { dirName=ctmp; dirName+="/"; } else { dirName = getenv("ATMPD_ROOT") + TString("/src/recon/fitqun"); } // Now try reading in this file. Last input variable is set to true, // indicating that we don't want to throw exception if a file is not found. ReadInputFile(fileName,dirName,true); // Now try to find this parameter again mapIterator i = mapOfTRuntimeParameters.find(parameterName); if(i != mapOfTRuntimeParameters.end()) return 1; else{ return 0; } } void TRuntimeParameters::ReadParamOverrideFile(TString filename){ std::cout << "Using TRuntimeParameters override file = " << filename << std::endl; // Setting final input variable to true forces the parameters // that are loaded to be 'fixed'; ie immutable. ReadInputFile(filename,"",false,true); } bool TRuntimeParameters::HasParameter(string parameterName) { mapIterator i = mapOfTRuntimeParameters.find(parameterName); if(i != mapOfTRuntimeParameters.end()) return 1; else{ // Okay, we didn't find this parameter on the first try. // Let's see if we can try loading the parameters file. // The function will return whether this parameter // was found afterwards. return TryLoadingParametersFile(parameterName); } } int TRuntimeParameters::GetParameterI(string parameterName) { if(HasParameter(parameterName)) { return atoi(mapOfTRuntimeParameters[parameterName].c_str()); }else{ std::cerr << "\n***** TRuntimeParameters::GetParameterAsInteger *****\n" << "Cannot find parameter '" << parameterName << "'." << std::endl; //throw ENonexistantDatabaseParameter(); exit(7); } return -1; } double TRuntimeParameters::GetParameterD(string parameterName) { if(HasParameter(parameterName)) { return atof(mapOfTRuntimeParameters[parameterName].c_str()); }else{ std::cerr << "\n***** TRuntimeParameters::GetParameterAsDouble *****\n" << "Cannot find parameter '" << parameterName << "'." << std::endl; //throw ENonexistantDatabaseParameter(); exit(7); } return -1; } std::string TRuntimeParameters::GetParameterS(string parameterName) { if(HasParameter(parameterName)) { return mapOfTRuntimeParameters[parameterName]; }else{ std::cerr << "\n***** TRuntimeParameters::GetParameterAsString *****\n" << "Cannot find parameter '" << parameterName << "'." << std::endl; //throw ENonexistantDatabaseParameter(); exit(7); } return std::string(); } void TRuntimeParameters::ClearMapOfTRuntimeParameters() { mapOfTRuntimeParameters.clear(); } void TRuntimeParameters::PrintCommandLineUsage(std::string programName) { std::cout << "Usage: " << programName << " [options] [input-file] ..." << std::endl << std::endl; std::cout << "Options:" << std::endl; // std::cout << " -f Look for an event number" // << std::endl; std::cout << " -l Set the name of an event list file." << std::endl; std::cout << " -n Only read events"; //if (readCount>0) std::cout << " [Default: " << readCount << "]"; std::cout << std::endl; std::cout << " -o Set the name of an output zbs file." << std::endl; std::cout << " -p Set the name of a fiTQun parameter-override file" << std::endl; std::cout << " -a or" << std::endl; std::cout << " --copy Copy tree1 and tree2 from the input file to the output file.\n\ To copy multiple trees list their names separated by colons.\n\ If the argument 'all' is listed then all trees in the file will be copied.\n\ To copy all trees except for those listed put 'nocopy' as the first tree name. E.g -a nocopy:tree1:tree2 will copy all trees except tree1 and tree2" << std::endl; std::cout << " -c Set the name of a spliTChan parameter-override file" << std::endl; std::cout << " -r Set the name of an output root file." << std::endl; std::cout << " -s Skip events" << std::endl; std::cout << " -h 0/1 (0)1: (Do not) write debugging histograms to tree" << std::endl; std::cout << " -e or" << std::endl; std::cout << " --nosubevents Turn off sub-event algorithm to pass clusters to fiTQun" << std::endl; std::cout << std::endl; } void TRuntimeParameters::ParseOptions(int argc, char* argv[]) { std::string programName = argv[0]; outZBSFileName = ""; outROOTFileName = ""; eventListFileName = ""; fqParOverrideFileName = ""; copyTreeList = ""; scParOverrideFileName = ""; fqKFileName = ""; readCount = 0; skipCount = 0; writeHistos = 0; useSubEvents = 1; copyTrees = 0; doTOFind = 0; //do not run TOF method by default doMSFit = 0; //do not run MS fit by default const struct option longopts[] = { {"nevents", required_argument, 0, 'n'}, {"skip", required_argument, 0, 's'}, {"nosubevents", no_argument, 0, 'e'}, {"copy", required_argument, 0, 'a'}, {0,0,0,0}, }; int indexptr; // Process the options. extern char *optarg; extern int optind, optopt; for (;;) { int c = getopt_long(argc, argv, ":o:f:l:n:r:s:h:p:e:t:c:k:a:",longopts,&indexptr); if (c<0) break; switch (c) { // case 'f': // { // // Look for a particular run and event number in the file. // std::string arg(optarg); // if (arg.find(',')!=std::string::npos) { // std::istringstream input(arg); // char c; // input >> targetRun; // input >> c; // input >> targetEvent; // } // else { // std::istringstream input(arg); // input >> targetEvent; // } // break; // } case 'l': { eventListFileName = optarg; break; } case 'n': { std::istringstream tmp(optarg); tmp >> readCount; break; } case 'o': { outZBSFileName = optarg; break; } case 'p': { fqParOverrideFileName = optarg; break; } case 'c': { scParOverrideFileName = optarg; break; } case 'k': { fqKFileName = optarg; break; } case 'r': { outROOTFileName = optarg; break; } case 's': { std::istringstream tmp(optarg); tmp >> skipCount; break; } case 'h': { std::istringstream tmp(optarg); tmp >> writeHistos; break; } case 'a': { std::cout << "Copying input trees" << std::endl; copyTrees = 1; copyTreeList = optarg; break; } case 'e': { std::cout << "Turning off subevents" << std::endl; useSubEvents = 0;; break; } case 't': { std::cout << "finding clusters using TOF method" << std::endl; doTOFind = 1; break; } // case 'm': // { // std::cout<<"running MS muon fit on MER"< 0) { std::cerr << "ERROR: only 1 input file is supported at this time" << std::endl; exit(1); } // Store the input the file name inFileName = argv[optind++]; nInFiles++; } std::cout << "inFileName = " << inFileName << std::endl; std::cout << "outZBSFileName = " << outZBSFileName << std::endl; std::cout << "outROOTFileName = " << outROOTFileName << std::endl; std::cout << "fqParOverrideFileName = " << fqParOverrideFileName << std::endl; std::cout << "scParOverrideFileName = " << scParOverrideFileName << std::endl; std::cout << "readCount = " << readCount << std::endl; std::cout << "skipCount = " << skipCount << std::endl; std::cout << "writeHistos = " << writeHistos << std::endl; std::cout << "useSubEvents = " << useSubEvents << std::endl; std::cout << "copyTrees = " << copyTrees << std::endl; std::cout << "copyTreeList = " << copyTreeList << std::endl; std::cout << "doTOFind = " << doTOFind << std::endl; std::cout << "doMSFit = " <