running same script for 2 different files (input & output) ??? /ROOT/C++ - c++

I have a script that reads a tree and creates some plots.
I have two files I need to run, so I need to run the same script once for file1 and once for file2, storing results in a different output file each time.
How can I tell my program what file to run each time and where to save the results?
file1 is: flatTree_jetHT
outputfile1 is: flatTree_JetHT_output.root
file2 is: flatTree_jetHT2
outputfile2 is: flatTree_JetHT2_output.root
I need to write this down using just one void and telling which file to run from terminal (.x flatTree_jetHt_read.C)
Here is my code:
#include <iostream>
void flatTree_jetHT_read()
{
gROOT->Reset();
gROOT->SetStyle("Plain");
gStyle->SetOptStat(1);
gStyle->SetOptFit(0);
gStyle->SetPadColor(0);
gStyle->SetPalette(1);
TFile *f = TFile::Open("flatTree_JetHT.root", "READ");
TTree *tree = (TTree*)f->Get("boosted/events");
TFile *outf = TFile::Open("flatTree_JetHT_output.root", "RECREATE");
//more code....
}

Have you tried just passing the input/output as arguments of the function? (see the Getting Started section of the user guide).
void two_args(const char* input_file, const char* output_file)
{
printf("Input: '%s'\n", input_file);
printf("Output: '%s'\n", output_file);
}
then run as
$ root -l -x -q '/tmp/two_args.C+("in.root", "out.root")'

Related

System() from c++ does not execute ImageMagick command correctly

As a part of my studies I am working on a command line tool to compute distortions on images. Currently, I am facing following problem:
I want to call an ImageMagick command via system(). The output I get is following:
convert: command not found.
However, calling the exactly same function directly on the terminal works perfectly.
The command I am calling is
convert building.jpg -matte -virtual-pixel transparent \
-distort Perspective \
'7,40 4,30 4,124 4,123 85,122 100,123 85,2 100,30' \
building_pers.png
From http://www.imagemagick.org/Usage/distorts/#perspective_projection
Of course the name of the image is different, this is just an example:
My code is the following:
int main(int argc, const char * argv[]) {
enterPoints();
std::string cmd;
cmd = create_cmd_l1();
cmd += create_cmd_l2();
cmd += create_cmd_l3();
cmd += create_cmd_l4();
system(cmd.c_str());
return 0;
}
The method enterPoints() just collects the reference points, there is no problem with this part.
The create_cmd-commands create the 4 lines of the whole ImageMagick-command.
Calling all lines separately produces the same error:
system(create_cmd_l1().c_str());
system(create_cmd_l2().c_str());
system(create_cmd_l3().c_str());
system(create_cmd_l4().c_str());

Redirect ffmpeg console output to a string or a file in C++

I'm trying to use ffmpeg to do some operations for me. It's really simple for now. I want to omit the ffmpeg output in my console, either redirecting them to strings or a .txt file that I can control. I'm on Windows 10.
I have tried _popen (with and "r" and "w") and system("ffmpeg command > output.txt")', with no success.
#include <iostream>
#include <stdio.h>
using namespace std;
#define BUFSIZE 256
int main()
{
/* 1.
x = system("ffmpeg -i video.mp4 -i audio.mp4 -c copy output.mp4 > output.txt");
*/
/* 2.
FILE* p;
p = _popen("ffmpeg -i video.mp4 -i audio.mp4 -c copy output.mp4", "w");
_pclose(p);
*/
/* 3.
char cmd[200] = { "ffmpeg -i video.mp4 -i audio.mp4 -c copy output.mp4" };
char buf[BUFSIZE];
FILE* fp;
if ((fp = _popen(cmd, "r")) == NULL) {
printf("Error opening pipe!\n");
return -1;
}
while (fgets(buf, BUFSIZE, fp) != NULL) {
// Do whatever you want here...
// printf("OUTPUT: %s", buf);
}
if (_pclose(fp)) {
printf("Command not found or exited with error status\n");
return -1;
}
*/
return 0;
}
Further in the development, I would like to know when the ffmpeg process finished (maybe I can monitor the ffmpeg return value?) or to display only the last line if the some error occurred.
I have made it to work.
In the solution 1, I added " 2>&1" to the end of the string.
Found it here: ffmpeg command line write output to a text file
output-to-a-text-file
Thanks!

/usr/bin/env: node: No such file or directory , c++

i am writing some functions on c++ for compiler less to css.
i installed nodejs, less.
i created a less file test.less
#color: red;
a{color:#color;}
when i run command on terminal:
lessc test.less test.css
it created a files css with name is test.css, but when i run this command via c++, it return a error. please help me. this is my c++ function:
std::string shell_exec( std::string cmd )
{
std::string result = "";
FILE* pipe = popen(cmd.c_str(), "r");
if (pipe == NULL)
{
return result;
}
char buffer[128];
while(!feof(pipe))
{
if(fgets(buffer, 128, pipe) != NULL)
{
result += buffer;
}
}
pclose(pipe);
return result;
}
shell_exec("lessc test.less test.css");
i got a error:
/usr/bin/env: node: No such file or directory
/usr/bin/node is existed.
================ UPDATE: Fixed==================
Thank you #Bass Jobsen , #Lightness Races in Orbit
i fixed by add absolute path to lessc and nodejs
shell_exec("/usr/bin/node /usr/bin/lessc test.less test.css");
From: https://unix.stackexchange.com/a/29620
The advantage of #!/usr/bin/env python is that it will use whatever
python executable appears first in the user's $PATH.
So you should add node to the $PATH of the user that runs your script, see: https://stackoverflow.com/a/13210246/1596547
Notice that i can not compile your code, but i can when using the following code:
int main()
{
std::string r = shell_exec("lessc test.less test.css");
}
Probably also use using namespace std and string instead of std:string.

extracting output of system command using popen

I am using following code for extracting output of system command .
I have not set path for "pic" in PATH variable. and i want to store
output of command "which pic" and do not want to display it on console.
Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
int main ()
{
FILE *fp;
int status;
char path[1035];
char *command = "which pic";
/* Open the command for reading. */
fp = popen(command, "r");
if (fp == NULL) {
printf("Failed to run command\n" );
exit(0);
}
/* Read the output a line at a time - output it. */
while (fgets(path, sizeof(path)-1, fp) != NULL) {
cout<<"<<<<<<<<<<,"<<endl;
printf("%s", path);
}
/* close */
pclose(fp);
return 0;
}
but it displaying following output in console :
which: no pic in(/usr/kerberos/bin:/usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin)
Run "which pic 2>&1" as your command. You want to capture all output from which, including its errors (which are sent to stderr).

stat() doesn't find a file in c++

on Linux 12.04
I have an executable file located in say:
/a/b/exe
and a config file on
/a/b/config
when doing:
cd /a/b/
./exe
everything's ok and the stat function finds the file config on /a/b/
HOWEVER,when running from root
/a/b/exe
the stat doesn't find the config file
any idea why?
it makes it impossible to run the binary using a script that isn't ran from the folder of the exe.
Edit
The call looks like this:
struct stat stFileInfo;
bool blnReturn;
int intStat;
// Attempt to get the file attributes
intStat = stat(strFilename.c_str(),&stFileInfo);
if(intStat == 0) {
// We were able to get the file attributes
// so the file obviously exists.
blnReturn = true;
} else {
// We were not able to get the file attributes.
// This may mean that we don't have permission to
// access the folder which contains this file. If you
// need to do that level of checking, lookup the
// return values of stat which will give you
// more details on why stat failed.
blnReturn = false;
}
In first case cd ..., run exe you change current working directory before executing the program, in second case you launch exe without changing current working directory, and I think in your program you use a relative path to open your config(for example ./config or just config) and it can't find it from current working directory. easiest workaround is to change working directory at start of your app:
int main(int argc, char** argv) {
std::string s( argv[0] ); // path to the program
std::string::size_type n = s.rfind( '/' );
if( n != std::string::npos ) {
std::system( ("cd " + s.substr(0, n)).c_str() );
}
// rest of your code
}