From e63fcaf54e5473134eff78fb1224cca5d4cf3ab5 Mon Sep 17 00:00:00 2001 From: cmorgoth Date: Sat, 2 Dec 2017 13:39:34 -0800 Subject: [PATCH] adding more clear command line inputs --- Makefile | 12 +- app/{dat2root.cc => ConvertDat2Root.cc} | 120 +++++++++++--- ...Pixels.cc => ConvertDat2RootWithPixels.cc} | 150 +++++++++++------- app/{datroot2root.cc => Rereco.cc} | 16 -- include/Aux.hh | 2 +- src/Aux.cc | 15 ++ 6 files changed, 213 insertions(+), 102 deletions(-) rename app/{dat2root.cc => ConvertDat2Root.cc} (82%) rename app/{dat2rootPixels.cc => ConvertDat2RootWithPixels.cc} (84%) rename app/{datroot2root.cc => Rereco.cc} (98%) diff --git a/Makefile b/Makefile index a252c8d..c741161 100644 --- a/Makefile +++ b/Makefile @@ -8,14 +8,14 @@ CPPFLAGS := $(shell root-config --cflags) -I$(INC)/include LDFLAGS := $(shell root-config --glibs) CPPFLAGS += -g -std=c++14 -TARGET = dat2root -SRC = app/dat2root.cc src/Aux.cc src/Config.cc +TARGET = ConvertDat2Root +SRC = app/ConvertDat2Root.cc src/Aux.cc src/Config.cc TARGET2 = Rereco -SRC2 = app/datroot2root.cc src/Aux.cc src/Config.cc +SRC2 = app/Rereco.cc src/Aux.cc src/Config.cc -TARGET3 = dat2rootPixels -SRC3 = app/dat2rootPixels.cc src/Aux.cc src/Config.cc +TARGET3 = ConvertDat2RootWithPixels +SRC3 = app/ConvertDat2RootWithPixels.cc src/Aux.cc src/Config.cc TARGET4 = SkimTree SRC4 = app/SkimTree.cc @@ -48,4 +48,4 @@ $(TARGET4) : $(OBJ4) @echo $@ $(CXX) $(CPPFLAGS) -o $@ -c $< clean : - rm -f *.o src/*.o $(Aux)/src/*.o $(TARGET) $(TARGET2) $(TARGET3) $(TARGET4) *~ + rm -f *.o app/*.o src/*.o $(Aux)/src/*.o $(TARGET) $(TARGET2) $(TARGET3) $(TARGET4) *~ diff --git a/app/dat2root.cc b/app/ConvertDat2Root.cc similarity index 82% rename from app/dat2root.cc rename to app/ConvertDat2Root.cc index 852441d..8aaf8ea 100644 --- a/app/dat2root.cc +++ b/app/ConvertDat2Root.cc @@ -21,21 +21,6 @@ TStyle* style; int graphic_init(); -std::string ParseCommandLine( int argc, char* argv[], std::string opt ) -{ - for (int i = 1; i < argc; i++ ) - { - std::string tmp( argv[i] ); - if ( tmp.find( opt ) != std::string::npos ) - { - if ( tmp.find( "=" ) != std::string::npos ) return tmp.substr( tmp.find_last_of("=") + 1 ); - if ( tmp.find( "--" ) != std::string::npos ) return "yes"; - } - } - - return ""; -}; - int main(int argc, char **argv) { gROOT->SetBatch(); @@ -53,8 +38,99 @@ int main(int argc, char **argv) { std::cerr << "Usage: dat2root in_file.dat num_events" << std::endl; return -1; } + + std::cout << "\n=== Beginning program ===\n" << std::endl; + //**************************************** + // Getting Input FileName (CAEN DRS4 file) + //**************************************** + std::string inputFileName = ParseCommandLine( argc, argv, "--inputFileName=" ); + if ( inputFileName == "" ) + { + std::cerr << "[ERROR]: please provide a valid inputFileName. Use: --inputFileName= " << std::endl; + exit(0); + } + + //******************************* + // Getting Output FileName (ROOT) + //******************************* + std::string outputFileName = ParseCommandLine( argc, argv, "--outputFileName=" ); + if ( outputFileName == "" ) + { + std::cerr << "[ERROR]: please provide a valid outputFileName. Use: --outputFileName= " << std::endl; + exit(0); + } + + //***************************************** + // Getting number of events to be processed + //***************************************** + std::string nEvents = ParseCommandLine( argc, argv, "--nEvents=" ); + if ( nEvents == "" ) + { + std::cerr << "[ERROR]: please provide a valid nEvents. Use: --nEvents= " << std::endl; + exit(0); + } + int nevents = atoi(nEvents.c_str()); + + std::cout << "[INFO]: input file --> " << inputFileName << std::endl; + std::cout << "[INFO]: output file --> " << outputFileName << std::endl; + std::cout << "[INFO]: processing --> " << nEvents << " events" << std::endl; + + //**************************** + // Getting channel config file + //**************************** + std::string configName = ParseCommandLine( argc, argv, "--config=" ); + if ( configName == "" ) + { + configName = "config/15may2017.config"; + std::cout << "[INFO]: using default config file: " << configName << ". Use: --config= to set your config" << std::endl; + } + + //********************************************************* + // Check if has valid input file, otherwise exit with error + //********************************************************* + ifstream ifile(inputFileName); + if (!ifile) + { + std::cerr << "[ERROR]: !USAGE! Input file does not exist. Please enter valid file name" << std::endl; + exit(0); + } + + //****************************************************************** + // Board number is fixed at 1 for now because we only have one board + //****************************************************************** + std::string boardNumber = "1"; + std::cout << "[INFO]: will use calibration files for board number " << boardNumber << "\n"; + + //********************************************************* + // Check if has valid input file, otherwise exit with error + //********************************************************* + bool saveRaw = false; + std::string _saveRaw = ParseCommandLine( argc, argv, "--saveRaw" ); + if ( _saveRaw == "yes" ) + { + saveRaw = true; + std::cout << "[INFO]: Will save raw pulses" << std::endl; + } + + bool drawDebugPulses = false; + std::string _drawDebugPulses = ParseCommandLine( argc, argv, "--debug" ); + if ( _drawDebugPulses == "yes" ) + { + drawDebugPulses = true; + std::cout << "[INFO]: Will draw pulse to check procedure" << std::endl; + } + + std::cout << "\n=== Parsing configuration file " << configName << " ===\n" << std::endl; + Config config(configName); + if ( !config.hasChannels() || !config.isValid() ) { + std::cerr << "\nFailed to load channel information from config " << configName << std::endl; + return -1; + } + + + /* std::string inputFilename = argv[1]; std::string outputFilename = argv[2]; std::cout << "Input file: " << inputFilename << std::endl; @@ -101,7 +177,9 @@ int main(int argc, char **argv) { std::cerr << "\nFailed to load channel information from config " << configName << std::endl; return -1; } + */ + //************************************** // Load Voltage Calibration //************************************** @@ -155,7 +233,7 @@ int main(int argc, char **argv) { // Define output //************************************** - TFile* file = new TFile( outputFilename.c_str(), "RECREATE", "CAEN V1742"); + TFile* file = new TFile( outputFileName.c_str(), "RECREATE", "CAEN V1742"); TTree* tree = new TTree("pulse", "Digitized waveforms"); int event; @@ -219,7 +297,7 @@ int main(int argc, char **argv) { // Open Input File //************************* - FILE* fpin = fopen( inputFilename.c_str(), "r" ); + FILE* fpin = fopen( inputFileName.c_str(), "r" ); //************************* //Event Loop @@ -227,13 +305,13 @@ int main(int argc, char **argv) { std::cout << "\n=== Processing input data ===\n" << std::endl; int nGoodEvents = 0; - int maxEvents = nEvents; - if (nEvents < 0) maxEvents = 999999; + int maxEvents = nevents; + if (nevents < 0) maxEvents = 999999; for( int iEvent = 0; iEvent < maxEvents; iEvent++){ if ( iEvent % 100 == 0 ) { - if (nEvents >= 0) { - std::cout << "Event " << iEvent << " of " << nEvents << std::endl; + if (nevents >= 0) { + std::cout << "Event " << iEvent << " of " << nevents << std::endl; } else { std::cout << "Event " << iEvent << "\n"; } diff --git a/app/dat2rootPixels.cc b/app/ConvertDat2RootWithPixels.cc similarity index 84% rename from app/dat2rootPixels.cc rename to app/ConvertDat2RootWithPixels.cc index 07c8135..8f60b3f 100644 --- a/app/dat2rootPixels.cc +++ b/app/ConvertDat2RootWithPixels.cc @@ -33,22 +33,6 @@ TStyle* style; int graphic_init(); -std::string ParseCommandLine( int argc, char* argv[], std::string opt ) -{ - for (int i = 1; i < argc; i++ ) - { - std::string tmp( argv[i] ); - if ( tmp.find( opt ) != std::string::npos ) - { - if ( tmp.find( "=" ) != std::string::npos ) return tmp.substr( tmp.find_last_of("=") + 1 ); - if ( tmp.find( "--" ) != std::string::npos ) return "yes"; - } - } - - return ""; -}; - - int main(int argc, char **argv) { gROOT->SetBatch(); @@ -67,47 +51,96 @@ int main(int argc, char **argv) { } std::cout << "\n=== Beginning program ===\n" << std::endl; - std::string inputFilename = argv[1]; - std::string pixelInputFilename = argv[2]; - std::string outputFilename = argv[3]; - std::cout << "Input file: " << inputFilename << std::endl; - std::cout << "Pixel Input file: " << pixelInputFilename << std::endl; - std::cout << "Output file: " << outputFilename << std::endl; - - // Check if has valid input file, otherwise exit with error - ifstream ifile(inputFilename); - if (!ifile) { - printf("!USAGE! Input file does not exist. Please enter valid file name"); - exit(0); - } + //**************************************** + // Getting Input FileName (CAEN DRS4 file) + //**************************************** + std::string inputFileName = ParseCommandLine( argc, argv, "--inputFileName=" ); + if ( inputFileName == "" ) + { + std::cerr << "[ERROR]: please provide a valid inputFileName. Use: --inputFileName= " << std::endl; + exit(0); + } + + //***************************** + // Getting Pixel Input FileName + //***************************** + std::string pixelInputFileName = ParseCommandLine( argc, argv, "--pixelInputFileName=" ); + if ( pixelInputFileName == "" ) + { + std::cerr << "[ERROR]: please provide a valid pixelInputFileName. Use: --pixelInputFileName= " << std::endl; + exit(0); + } + + //******************************* + // Getting Output FileName (ROOT) + //******************************* + std::string outputFileName = ParseCommandLine( argc, argv, "--outputFileName=" ); + if ( outputFileName == "" ) + { + std::cerr << "[ERROR]: please provide a valid outputFileName. Use: --outputFileName= " << std::endl; + exit(0); + } - int nEvents = atoi(argv[4]); - std::cout << "Will process " << nEvents << " events" << std::endl; + //***************************************** + // Getting number of events to be processed + //***************************************** + std::string nEvents = ParseCommandLine( argc, argv, "--nEvents=" ); + if ( nEvents == "" ) + { + std::cerr << "[ERROR]: please provide a valid nEvents. Use: --nEvents= " << std::endl; + exit(0); + } + int nevents = atoi(nEvents.c_str()); + + std::cout << "[INFO]: input file --> " << inputFileName << std::endl; + std::cout << "[INFO]: pixel input file --> " << pixelInputFileName << std::endl; + std::cout << "[INFO]: output file --> " << outputFileName << std::endl; + std::cout << "[INFO]: processing --> " << nEvents << " events" << std::endl; + + //**************************** + // Getting channel config file + //**************************** + std::string configName = ParseCommandLine( argc, argv, "--config=" ); + if ( configName == "" ) + { + configName = "config/15may2017.config"; + std::cout << "[INFO]: using default config file: " << configName << ". Use: --config= to set your config" << std::endl; + } + //********************************************************* + // Check if has valid input file, otherwise exit with error + //********************************************************* + ifstream ifile(inputFileName); + if (!ifile) + { + std::cerr << "[ERROR]: !USAGE! Input file does not exist. Please enter valid file name" << std::endl; + exit(0); + } + + //****************************************************************** // Board number is fixed at 1 for now because we only have one board + //****************************************************************** std::string boardNumber = "1"; - std::cout << "Will use calibration files for board number " << boardNumber << "\n"; + std::cout << "[INFO]: will use calibration files for board number " << boardNumber << "\n"; + //********************************************************* + // Check if has valid input file, otherwise exit with error + //********************************************************* bool saveRaw = false; std::string _saveRaw = ParseCommandLine( argc, argv, "--saveRaw" ); - if ( _saveRaw == "yes" ) { + if ( _saveRaw == "yes" ) + { saveRaw = true; - std::cout << "Will save raw pulses\n"; - } + std::cout << "[INFO]: Will save raw pulses" << std::endl; + } bool drawDebugPulses = false; std::string _drawDebugPulses = ParseCommandLine( argc, argv, "--debug" ); - if ( _drawDebugPulses == "yes" ) { - drawDebugPulses = true; - std::cout << "draw: " << drawDebugPulses << std::endl; - } - - std::string configName = "config/15may2017.config"; - //std::string configName = "alignmentTestConfig.config"; - std::string _configName = ParseCommandLine( argc, argv, "--config" ); - if ( _configName != "" ) { - configName = _configName; - } + if ( _drawDebugPulses == "yes" ) + { + drawDebugPulses = true; + std::cout << "[INFO]: Will draw pulse to check procedure" << std::endl; + } std::cout << "\n=== Parsing configuration file " << configName << " ===\n" << std::endl; Config config(configName); @@ -169,7 +202,7 @@ int main(int argc, char **argv) { // Define output //************************************** - TFile* file = new TFile( outputFilename.c_str(), "RECREATE", "CAEN V1742"); + TFile* file = new TFile( outputFileName.c_str(), "RECREATE", "CAEN V1742"); TTree* tree = new TTree("pulse", "Digitized waveforms"); int event; @@ -255,11 +288,12 @@ int main(int argc, char **argv) { //************************* // Open Pixel Tree //************************* - TFile *pixelDataFile = TFile::Open( pixelInputFilename.c_str(),"READ"); - if (!pixelDataFile) { - cout << "Error: Pixel file not found\n"; - return 0; - } + TFile *pixelDataFile = TFile::Open( pixelInputFileName.c_str(),"READ"); + if (!pixelDataFile) + { + std::cout << "[ERROR]: Pixel file not found" << std::endl; + return -1; + } TTree *pixelTree = (TTree*)pixelDataFile->Get("T1037"); if (!pixelTree) { cout << "Error: Pixel Tree not found\n"; @@ -279,7 +313,7 @@ int main(int argc, char **argv) { // Open Input File //************************* - FILE* fpin = fopen( inputFilename.c_str(), "r" ); + FILE* fpin = fopen( inputFileName.c_str(), "r" ); //************************* //Event Loop @@ -287,9 +321,9 @@ int main(int argc, char **argv) { std::cout << "\n=== Processing input data ===\n" << std::endl; int nGoodEvents = 0; - int maxEvents = nEvents; - if (nEvents < 0) maxEvents = 999999; - else maxEvents = nEvents; + int maxEvents = nevents; + if (nevents < 0) maxEvents = 999999; + else maxEvents = nevents; for( int iEvent = 0; iEvent < maxEvents; iEvent++){ @@ -326,8 +360,8 @@ int main(int argc, char **argv) { if ( iEvent % 100 == 0 ) { - if (nEvents >= 0) { - std::cout << "Event " << iEvent << " of " << nEvents << std::endl; + if (nevents >= 0) { + std::cout << "Event " << iEvent << " of " << nevents << std::endl; } else { std::cout << "Event " << iEvent << "\n"; } diff --git a/app/datroot2root.cc b/app/Rereco.cc similarity index 98% rename from app/datroot2root.cc rename to app/Rereco.cc index cf9db92..e166534 100644 --- a/app/datroot2root.cc +++ b/app/Rereco.cc @@ -22,22 +22,6 @@ TStyle* style; int graphic_init(); -std::string ParseCommandLine( int argc, char* argv[], std::string opt ) -{ - for (int i = 1; i < argc; i++ ) - { - std::string tmp( argv[i] ); - if ( tmp.find( opt ) != std::string::npos ) - { - if ( tmp.find( "=" ) != std::string::npos ) return tmp.substr( tmp.find_last_of("=") + 1 ); - if ( tmp.find( "--" ) != std::string::npos ) return "yes"; - } - } - - return ""; -}; - - int main(int argc, char **argv) { gROOT->SetBatch(); diff --git a/include/Aux.hh b/include/Aux.hh index 7bc0db4..4c1255e 100644 --- a/include/Aux.hh +++ b/include/Aux.hh @@ -46,6 +46,6 @@ float GetPulseIntegral(int peak, short *a, std::string option = "", int nsamples float GetPulseIntegral(int peak, int nsamplesL, int nsamplesR, short *a, float *t); float ConstantThresholdTime(TGraphErrors * pulse, const float threshold); bool isRinging( int peak, short *a ); - +std::string ParseCommandLine( int argc, char* argv[], std::string opt ); #endif diff --git a/src/Aux.cc b/src/Aux.cc index d6c8194..16f6195 100644 --- a/src/Aux.cc +++ b/src/Aux.cc @@ -882,3 +882,18 @@ bool isRinging( int peak, short *a ) if ( right_max > 0.5*fabs(a[peak]) ) return true; return false; }; + +std::string ParseCommandLine( int argc, char* argv[], std::string opt ) +{ + for (int i = 1; i < argc; i++ ) + { + std::string tmp( argv[i] ); + if ( tmp.find( opt ) != std::string::npos ) + { + if ( tmp.find( "=" ) != std::string::npos ) return tmp.substr( tmp.find_last_of("=") + 1 ); + if ( tmp.find( "--" ) != std::string::npos && tmp.find( "=" ) == std::string::npos ) return "yes"; + } + } + + return ""; +};