How to draw a TH1F in the terminal without opening TBrowser in ROOT? - root-framework

I have a root directory, when I list the files, I can see there is a file
KEY: TH1F h_met;1
I am trying to draw this histogram through a terminal command, and do not want to open TBrowser since it is SUPER slow for me.
Is there a terminal command that will draw this?
I have tried
TCanvas *c1 = new TCanvas("c1","c1"); c1->Draw("h_met")
but that didn't work.

You should first get the histogram, then plot it. Assuming your file is opened as f:
TH1F *h1 = (TH1F*)f.Get("h_met");
h1->Draw();

Assuming you are on ROOT 6 and assuming your file is called f.root, just do
root -l f.root -e "h_met->Draw()"

Yes, sometimes with the graphics it can become slow,
I recommend to use the option
-b Run in batch mode without graphics
So, you can do
root -l -q
root [1] TFile f("yourfile.root");
root [2] TH1F *h1 = (TH1F*)f.Get("h_met");
root [3] h1->Draw()
Info in <TCanvas::MakeDefCanvas>: created default TCanvas with name c1
root [4] c1->SaveAs("save_it_for_open_later.png")
As you can see with Draw, the TCanvas c1 is created by default,and with SaveAs, you can save de histogram h1 in png or other format, to open it later with other application.

Just for fun, you can also actually 'draw' it in terminal, see: https://root-forum.cern.ch/t/tbrowser-alternative-for-non-x11-mode/20754/27

Related

org-mode inline images not working (remotely with TRAMP)

I am working with emacs org-mode on a remote machine using TRAMP. I connect code cells to a jupyter server (on that remote machine) where I start a python 3 kernel. Code execution works perfectly fine, I can also create plots with matplotlib. While a .png is generated in the right temp file location, the output of the code cell is a (relative) link to the file without displaying it inline as expected.
An example code-block look like this:
#+BEGIN_SRC jupyter-python :session /jpy:localhost#9090:TEST
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(range(10), range(10))
#+END_SRC
#+RESULTS:
:RESULTS:
| <matplotlib.lines.Line2D | at | 0x7f1c43a289a0> |
[[file:./.ob-jupyter/e1eecf5d59de9bfa1d3468867a64aadf4b1a6261.png]]
:END:
C-c C-x C-v gives the message: 'No images to display inline'
C-c o opens and displays the file in a different buffer correctly.
I would expect the file to display correctly inline in the org-mode buffer.
I tried to change the link manually to a TRAMP path, looking something like this:
[[file:/ssh:MYSERVER:/PATH_TO_TEMP_FOLDER/.ob-jupyter/e1eecf5d59de9bfa1d3468867a64aadf4b1a6261.png]]
which also allows me to open the file with C-c o, but won't display the file inline (Same behavior as described above)
If I open the jupyter-repl session directly and type fig (after executing the above code block) The figure displays as expected in the jupyter-repl buffer
If I run the jupyter session locally, inline plotting works as expected
Update:
I realized if I C-f on the link to open the file, the link expands to an invalid tramp link, which throws the following error message:
File is missing: /ssh:bih:/PATH_TO_CORRECT_FOLDER/00_test/file:./.ob-jupyter/
Note the file:./ at the end of the link that doesn't belong there. So I think something is going wrong somewhere between TRAMP and org-mode (or emacs-jupyter). Any ideas how to fix this?
C-h v org-display-remote-inline-images says:
org-display-remote-inline-images is a variable defined in ‘org.el’.
Its value is ‘skip’
How to display remote inline images.
Possible values of this option are:
skip Don’t display remote images.
download Always download and display remote images.
cache Display remote images, and open them in separate buffers
for caching. Silently update the image buffer when a file
change is detected.
Check the value and maybe customize the variable to do something other than skip.

Unison: sync only in one direction ...option selected in profile

I'm effectively asking this question:
Unison: sync only in one direction
but I'd like to apply the accepted answer to a profile script. Can I do that?
unison /src/dir /dest/dir -force /src/dir -nodeletion /dest/dir
Cheers!
Yes, of course you can write that command line to the profile script.
Locate the corresponding .prf unison preferences/profile file (usually under $HOME/.unison, but please double check, this may vary depending on your OS or Unison installation)
Edit the .prf file and add the following:
# Unison preferences
label = Sync in only one direction (mirror)
root = /src/dir
root = /dest/dir
force = /src/dir # force changes from this replica to the other
nodeletion = /dest/dir # prevent file deletions on this replica
noupdate = /src/dir # prevent file updates on this replica
You can also replace step #1 above by creating a new preferences profile using the Profile Editor in the GUI, or your text editor of choice. Please remember the name and location of this file (e.g. $HOME/.unison/sync-one-direction.prf). Then you can call unison from the command line like this:
unison $HOME/.unison/sync-one-direction.prf
or
unison -i
The latter will start an interactive profile selection.
HTH

Blender Command line importing files

I will run the script on the Blender command line. All I want to do is run the same script for several files. I have completed the steps to run a background file (.blend) and run a script in Blender, but since I have just loaded one file, I can not run the script on another file.
I looked up the Blender manual, but I could not find the command to import the file.
I proceeded to creating a .blend file and running the script.
blender -b background.blend -P pythonfile.py
In addition, if possible, I would appreciate it if you could tell me how to script the camera and track axes to track to contraint (Ctrl + T -> Track to constraint).
really thank you for reading my ask.
Blender can only have one blend file open at a time, any open scripts are cleared out when a new file is opened. What you want is a loop that starts blender for each blend file using the same script file.
On *nix systems you can use a simple shell script
#!/bin/sh
for BF in $(ls *.blend)
do
blender -b ${BF} -P pythonfile.py
done
A more cross platform solution is to use python -
from glob import glob
from subprocess import call
for blendFile in glob('*.blend'):
call([ 'blender',
'-b', blendFile,
'--python', 'pythonfile.py' ])
To add a Track-to constraint to Camera pointing it at Cube -
camera = bpy.data.objects['Camera']
c = camera.constraints.new('TRACK_TO')
c.target = bpy.data.objects['Cube']
c.track_axis = 'TRACK_NEGATIVE_Z'
c.up_axis = 'UP_Y'
This is taken from my answer here which also animates the camera going around the object.
bpy.context.view_layer.objects.active = CameraObject
bpy.ops.object.constraint_add(type='TRACK_TO')
CameraObject.constraints["Track To"].target = bpy.data.objects['ObjectToTrack']

GIMP Script.Fu script to batch convert JPEG to PNG

Can someone give me the script I would need to run to batch convert many *.jpeg files to *.png in Script.Fu in GIMP?
Currently I am spending way too much time manually exporting every image and it's a waste of time.
I can't install anything right now so can't use alternative applications.
Alright, after a lot of trials and errors I finally figured out how to convert one file format to another using only GIMP.
This is the Script-Fu script for conversion to PNG:
(
let* ((filename "{{filename}}")
(output "{{output}}")
(image (car (gimp-file-load 1 filename filename)))
(drawable (car (gimp-image-get-active-layer image))))
(file-png-save-defaults 1 image drawable output output)
)
Where {{filename}} is input file that needs to be converted (a jpeg file, for example), {{output}} is the output file that you need (it can be simply the same file name but with PNG extension)
How to run it: it can probably be improved
gimp -i -n -f -d --batch "{{one-line script-fu}}"
More about command line options you can find in GIMP online documentation.
The place that needs to be changed is {{one-line script-fu}} and it has to be... one-line! You can probably do all of this in one file using cmd (in case if you use Windows), but for me it was easier to use Python, so here's the script for it:
import subprocess, os
def convert_to_png(file_dds):
#Loads the command to run gimp cli (second code block)
#Note: remove "{{one-line script-fu}}" and leave one space after the --batch
with open("gimp-convert.bat", "r") as f:
main_script = f.read()
#Prepares the Script-Fu script to be run, replacing necessary file names and makes it one-line (the firs code block)
with open("gimp-convert-png.fu", "r") as f:
script = f.read().replace("\n", " ").replace("{{filename}}", file_dds) \
.replace("{{output}}", file_dds[:-3]+"PNG").replace("\\", "\\\\").replace("\"", "\\\"")
subprocess.run(main_script + " \"" + script + "\" --batch \"(gimp-quit 1)\"",
cwd = os.getcwd(),
shell = True)
And you should get your file converted to PNG!
I needed this for my texture upscale project, all of the code below you can find here.
Tested with GIMP 2.10
The real solution is to use ImageMagicks convert, as simple as magick convert some.jpeg some.png. There must be a "portable" version somewhere that you can use off a USB key.
Otherwise with Gimp, a much less manual way that doesn't need for a new script, since it uses an existing script:
get/install ofn-export-layers
File>Open the first JPEG
File>Open as layers more Jpegs. You can select several/all jpegs in one call (actual number limited by available RAM mostly). Once this is done you have many Jpegs stacked in the same image
File>Export all layers, making sure the name pattern you use ends in .png (the doc that comes with the script explains how that works).

Open txt file and draw histogram in root

I have a txt file which contains numbers. I want to draw a histogram according to this file in root. But I can't do this.
{
TFile *f = new TFile("myfile.root");
f.ls();
TH1F * h1 = (TH1F*)f.Get("h1");
h1->Draw();
}
Have a look at this tutorial from the ROOT website. As far as I know ROOT can't open a plain text file directly. The TFile("myfile.root") call can be used to open a ROOT file (i.e. a binary file created by ROOT containing persistified ROOT objects such as histograms), which as you point out causes errors if you try to open a plain text file using it. The approach shown in the tutorial page is to create a TH1, then use standard C++ I/O streams to read from your text fiule and fill the histogram in the normal way.