Skip to content

File npconversion.cxx

File List > src > utility > npconversion.cxx

Go to the documentation of this file

#include "NPApplication.h"
#include "NPDetectorManager.h"
#include "NPException.h"
#include "NPFunction.h"
#include "NPProgressDisplay.h"
#include "NPTerminalColor.h"
#include "NPVDataInput.h"
#include "NPVDataOutput.h"
#include <chrono>
#include <iomanip>
#include <iostream>
using namespace nptool;

int main(int argc, char** argv) {
  try {
    // instantiate an apppication
    auto app = nptool::Application::InitApplication(argc, argv, 1);
    app->Start();

    // For Progress Display
    long long event_processed = 0;
    long long event_to_process = -1;
    long long size_to_read = -1;
    // Data Input
    std::shared_ptr<nptool::VDataInput> input;
    if (app->HasFlag("--input")) {
      auto input_arg = app->GetVectorArg("--input");
      if (input_arg.size()) {
        input = app->ConstructDataInput(input_arg[0]);
        if (input) {
          input->Init(input_arg);
        }
        else {
          throw(nptool::Error("npconversion", "Fail to construct data input : " + input_arg[0]));
        }
        if (input->GetEntries() == 0) {
          throw(nptool::Error("npconversion", "No entries to analyse"));
        }
        else {
          nptool::message("green", "core", "npconversion",
                          "Data input: " + nptool::itoa(input->GetEntries()) + " entries loaded ", true);
          event_to_process = input->GetEntries();
          size_to_read = input->GetFileSize();
        }
      }
      else
        throw(nptool::Error("npconversion", "--input flag provided with no arguments, at least one needed"));
    }
    else
      throw(nptool::Error("npconversion", "--input flag required"));

    // Data Output
    std::shared_ptr<nptool::VDataOutput> output;
    if (app->HasFlag("--output")) {
      auto output_arg = app->GetVectorArg("--output");
      if (output_arg.size()) {
        output = app->ConstructDataOutput(output_arg[0]);
        if (output) {
          output->Init(output_arg);
        }
        else {
          throw(nptool::Error("core", "npconversion", "Fail to construct data output " + output_arg[0]));
        }
      }
      else
        throw(nptool::Error("npconversion", "--output flag provided with no arguments, at least one needed"));
    }
    else
      throw(nptool::Error("npconversion", "--output flag required"));

    // Detector Configuration
    app->InitializeDataInputConversion(input);
    app->InitializeDataOutputRaw(output);
    app->AttachASCIIFiles();
    nptool::ProgressDisplay progress(event_to_process, size_to_read);

    // Main loop
    while (input->GetNextEntry()) {
      event_processed++;
      output->Fill();
      app->ClearEventData();
      progress.AttemptDisplay(event_processed, input->GetReadSize());
    }

    progress.ForceDisplay(event_processed, input->GetReadSize());

    // Stop the app
    app->Stop();
  }
  catch (nptool::Warning& ex) {
    nptool::DisplayException(ex);
  }
  catch (nptool::Error& ex) {
    nptool::DisplayException(ex);
    return 1;
  }

  // any other exception
  catch (...) {
    return 1;
  }
  return 0;
}