MY LOGBOOK FOR THE SPRING SEMESTER IS HERE: https://sites.google.com/a/physics.umd.edu/cmsanalysis/diana
- anything in brackets [ ] is variable user input. The rest is commands/options.
Useful commands:
touch [fileName] - makes a file with name "fileName" or updates its timestamp to the current time if the file already exists
find [filePath] -name ["string"] - finds all files with "string" in their name at location filePath
ls [filePath] - seemingly goes to the given location and lists the contents
- '-slrt' - shows contents in order by creation date
whoami - prints your username. Your home folder will be in /home/[your username]
- The '~' character references this home folder. Can use it as shorthand for its file path. (ex: "echo ~" "ls ~" "cd~")
rm [fileName] - permanently deletes the file
rmdir [dirName] - permanently deletes the directory; only works if directory is empty
cat [fileName] - displays contents of file to the terminal as one block; you scroll through them
more [fileName] - displays contents of file to the terminal in pages; you go through pg by pg
`` - putting a command in backticks lets you input it as a string into another command
chmod +x [scriptname.tcsh] - lets you run the script without using 'tcsh' beforehand
head -[integer] [fileName] - shows only the first [integer] lines of fileName
sed -i -e 's/[word1]/[word2]/g' [fileName] - replaces every instance of word1 with word2 in fileName
- 'sed' stands for Stream EDitor
- the 's/[word1]/[word2]/g' part is what actually does the replacing.
- '-i' removes the automatic display of the new, edited file contents
- '-e' apparently acts as a new line character. Seems like you don't need it if you're only running one command with sed.
- Could NOT get this to work with variable inputs. NEED HELP HERE!!
- variable input: sed -i -e 's/'$WORD1'/'$WORD2'/g' $FILE
more [fileName] - prints contents of fileName to the terminal.
tar -xvzf [folderName] - command to unzip .gz and .tzg files. Probably others, too. the letters in -xvzf each stand for a way the command is executed. See HW7 for explanation of letters.
tar -cvzf [archiveName] [folderName] - command to zip .gz and .tzg files. Puts folderName in archive called archiveName. If no such archive exists, creates new one.
gunzip [fileName] - command to unzip file
./[someFile] - runs someFile
Opening ROOT:
cd to your CMSSW-.... /src directory on the VM
cmsenv
root [someFile] - opens file in root
TBrowser t - opens GUI for root
Exit by typing ".quit"
Bash scripting:
***NEED TO HAVE '#!/bin/tcsh' AS FIRST LINE OF CODE!!! It's called a "shebang". It tells the executor how to interpret the file. It's only relevant if you have execute permissions, since otherwise, you use that same last word (here, 'tcsh', but it can be any file type) as the command to execute the file (ex: 'tcsh ./myfile.tcsh' vs 'chmod +x myfile.tcsh; ./myfile.tcsh')
- The '#!' is the character combination signaling a shebang. No, it's not commented out!!!
From HW2...
‘man’ commandName - like ‘help’ command. Gives you information on commandName, including what it does, its options, and its targets.
The command before pressing enter.
After pressing enter, a new information page pops up.
The information page was closed by pressing the ‘q’ key.
‘date’ - prints time and date
‘cp’ oldLocation/filename newLocation/newFileName - copies filename to newLocation and renames it to newFileName
Here, file1 was copied to /home/cms-opendata/TEST from /home/cms-opendata/TEST/testInTest. No file path was given for file1, so it was assumed to be in the working directory. No new name was given for the copied file, so it kept the name ‘file1’.
‘mv’ oldLocation/filename newLocation/newFileName - moves filename to newLocation and renames to newFileName
log.txt was moved from the TEST folder to the testInTest folder. No comprehensive file paths were used because everything was happening inside the TEST folder.
‘grep’ myString fileLocation - returns lines containing myString
I added several lines to file1, then used grep to print the lines with the given strings. Grep treats the target string as if it is preceded and succeeded by ‘*’, meaning it will find any occurrence of the target string in the target file regardless of the surrounding characters.
‘xdg-open’ url - opens url in your default browser
‘kill’ pid - ends the process with given process ID (PID)
This session of firefox, with PID 10925, was closed using the ‘kill’ command.
‘pkill’ processName - kills the process with “processName”
This session of Firefox was killed using its name rather than its PID.
‘ps’ - prints PID’s and running time of all current processes
‘ps -A’ | ‘grep -i’ processName - can get PID of specific process by combining ps and grep:
‘alias’ newName=“commandNameAndOptionsAndTargets” - reassigns newName to commandNameAndOptionsAndTargets; useful for frequently-used, long commands.
‘Unalias’ newName - removes the alias! ‘l’ was aliased to represent the ‘ls -l’ command. Then, the shortcut was removed using the ‘unalias’ command.
From HW3...
"#" is comment out character
The script (written in emacs; saved as "testscript.tcsh"):
# here we create an environmental variable ARG and assign to it the value of the first input. '$' indicates user input; order of declaration dictates input order
set ARG=$1
# here as a bug statement we echo out the value to the screen
echo $ARG
# here we have it look for all the files in / and its subdirectories that
# contain somewhere in the file name the string we inputted
find / -name "*$ARG*" -print
It's just terminal commands written in a re-runnable file.
As you can tell from the code, it takes the user's input and finds all files on the machine with the input in their name. Then, it prints those outputs.
The 'echo' statement is for debugging.
The 'ls' statements seen in the screenshot below are just for learning about backticks.
To run the script:
Type into terminal: "tcsh ./testscript.tcsh <argument>" ("<argument>" is a fill-in-the-blank; replace with "word you're looking for")
Visually:
ARG vs $ARG - '$' indicates user input. ARG is just a variable; "$ARG" is a user-input variable
'print "$ARG"' prints the variable; same as 'print $ARG'. But, 'print "$ARG*"' is NOT the same as 'print $ARG*'. Although double quotes don't cancel out the variable's value, they do cancel out the '*' command.
Regarding the 'ls' commands:
`` - putting a command in backticks lets you input it as a string into another command (so, the 'output' of ls is input into 'echo')
'/' - refers to home directory in the file system
File system home vs cms-opendata (user) home???
'./' - refers to the current directory
'../' - refers to the previous directory
Difference between '..' and '../'? Does '/' just turn it into a file path, while without it, it would just refer to the folder without giving a path?
Changing the script to run without the 'tcsh' command:
'chmod +x testscript.tcsh' lets you run the script without using 'tcsh' beforehand
Now, as an exercise, try using emacs to write a simple script based on testscript.tcsh called homesearch.tcsh which takes one input, <input>. homesearch.tcsh should search your home directory and its subdirectories for any files which contain <input> somewhere in their name. Finally, homesearch.tcsh should log the results of the search to a file called homesearch_<input>.txt. (Don't forget to 'chmod +x' your new script!) Try:
touch testfile
./homesearch.tcsh testfile
cat homesearch_testfile.txt
What is the output of this script?
The output of the script is the paths within the home directory that have the input (“testscript”) in their names. Because homesearch.tcsh writes this information into a file, we can use ‘cat’ to access it.
***If I call it without the ‘tcsh’ command, even though I used ‘chmod +x’, it can’t access the user input variable with echo statements, so I don’t know how to put it together so that it can make the new text file and use user input at the same time without using ‘tcsh’.
Example of code not working if I don’t use ‘tcsh’. I previously called ‘chmod +x homesearch.tcsh’.
Piping - the process of taking input/output data and redirecting it (ie taking inputs not from the command line or outputting not to the command line)
Redirecting (used for FILES ONLY!)
That's what the '>' symbol has been doing all along! It takes the usual output and writes it to the file following '>' instead of spitting it out to the command line
STDIN/STDOUT/STDERR - input/output/error
Can put number before '<' or '>' to direct to specific stream (defaults to 1)
line 4: writes the error message to errors.txt
line 1 in 2nd block: says to write all of STDOUT to myoutput, then says to write all of STDERR into STDOUT
Uses "&" to specify that it means the symbolic value of 1, not a file named '1'
Can also use '<' to input the file on the right into command on the left
Ex: wc -l < myoutput - applies wc command to myoutput
Gets rid of extra name data for reason specified bellow
When using '<' or '>', data is sent anonymously--you don't get the name of the file involved
This is why '<' can be useful, even if many functions already take file names as inputs
Can use the operators in one line!
Ex: 'wc -l < barry.txt > myoutput' - 'wc' is word count function. We input 'barry.txt'. Then, we output this to 'myoutput'
Piping (for commands as well as files!)
Piping is when you us the result of one command as the input for another command. The format is ‘[command and modifications] | [next command and modifications]’. It can be done as many times as desired, even on the same line (although that isn’t recommended for debugging purposes).
See https://ryanstutorials.net/linuxtutorial/piping.php#tofile for more info + examples
See sed notes in command reference at top of pg
HW4 notes:
C++ program:
#include <iostream> //makes compiler include iostream library (needed to use 'cout')
using namespace std; //lets you type 'cout' instead of 'std.cout'
int main() { //declares the 'main' function. Needed in every file of c++.
cout <<"Hello World!" << endl; //Print hello world to screen followed by end line (endl)
return 0; //Ends 'main' function by returning 0.
}
Calling it in terminal:
g++ main.cpp //compiles main.cpp as a c++ file. 'g++' is our system's compiler.
Need to do each time you edit main.cpp!
./a.out //creates and runs executable for everything you've compiled.
More terminal commands:
find /usr/include/c++ -name "4*"
finds what versions of c++ you have downloaded. They're stored in the given file path and are all of the 4th release (that is, all names are 4.x.y, where x and y are numbers specifying the version)
g++ -dumpspecs
Lists specs for your compiler. Lets you see exactly what version you're using!
Finding and reading header files:
Header file - a file storing usable functions for programming. Ends in .h.
Examples for finding h files:
ls /usr/include/c++/4.4.4/ //lists all files in the 4.4.4 c++ folder, including ones that aren't header files.
ls /usr/include/math.h //returns the file path, since it only specified one file.
ls /usr/include/bits/*math*.h //returns any header files with "math" in the name that are in the given directory.
'more [fileName]' - prints contents of file to terminal. Works for programming files.
Another file:
#include <iostream>
using namespace std;
int main() {
cout << "hello world" << end;
int i=2; //declares variable 'i' that's an int with value = 2
cout << "i = " <<i<<endl; // prints i
double a=3.3; //declares variable 'a' that's a double with value = 3.3
cout << "a = " <<a<<endl; //prints a
int j = a*i; //declares variable 'j' that's an int with value = a*i
cout << "a*i = "<<j<<endl; //prints j. j will be truncated after the decimal point because j is of type int.
return 0;
}
Another file:
#include <iostream>
using namespace std;
int main() {
int n=10;
cout << "n is "<<n<<endl;
n--; //decreases n by 1
cout<<"n is now "<<n<<endl;
n++; //increases n by 1
cout<<n is now "<<n<<endl;
return 0;
}
Another file: non-numerical variables
#include <iostream>
using namespace std;
int main() {
bool prop; //makes boolean var called "prop"
prop = (5>1); //prop is set to true (1)
cout<<"prop is "<<prop<<endl; //displays prop
prop = (1>5); //prop is set to false (0)
cout<<"prop is "<<prop<<endl;
prop = (1 != 5); //prop is set to true (1)
cout << "prop is " <<prop<<endl;
return 0;
}
Another file: loops: while, for, change from while to for
#include <iostream>
using namespace std;
int main() {
int n=10;
while(n>0) {
cout<<"n is "<<n<<endl;
n--;
} //displays then decrements n while n > 0. Will not display "n is 0".
return 0;
}
#include <iostream>
using namespace std;
int main() {
for (int n=10; n>0; n--) {
cout<<"n is "<<n<<endl;
} //for each n, decremented by 1, while n is greater than 0, print out n.
return 0;
}
#include <iostream>
using namespace std;
int main() {
int n=0, m=0;
for(n; n<10; n++) {
cout << "n is " << n << ": ";
m=0;
for(m; m<=n; m++ {
cout << m; //I'm out of comments to make. It's just nested loops. The inner one repeats each time the outer one runs.
}
cout << endl;
}
return 0 ;
}
Debugging with gdb!
It's a useful framework that lets you print out variables, check certain lines, and step through your code without manually typing in these features.
See debugging section under "C++ notes" for more.
Ex:
#include <iostream>
using namespace std;
int main() {
int i = 1;
while (i<1.5e9) {
i *= 10;
}
cout << "The final value of 'i' was: " << i << endl;
return 0;
}
Debugging this with "wa i" lets you see how i is incremented, and when everything starts breaking down:
(only shows small part of debugging output.)
after i = 10^9, the int type cannot store any more values, meaning when you try to multiply it again, it starts storing gibberish. Can fix this by making i of type "double"!
HW5: (10/16/18)
#include <iostream>
using namespace std;
int main() {
int n = 10;
while (n>=10) { //loops while n is bigger than 9
if(n>5) {
cout<<"n is "<<n<<endl; //will always print this statement bc if n>9 then it must be >5
}
else {
cout<<"n = "<<n<<endl; //will never print this statement
n--;
}
return 0;
}
}
#include <iostream>
using namespace std;
int main() {
int i = 10;
cout << "The memory address of i is " << &i << "\n"; //use '&[varName]' to get memory location slot number ("memory address") of varName
cout << "The data stored at memory address " << &i << " is " << i << "\n";
int* p = &i; //this var equals the location of i
cout << "The value of p is " << p << "\n";
cout << "We say that p 'points at' the memory location referenced by address " << p << "\n";
cout << "The data stored at memory address " << p << " is " << *p << "\n"; //To get data stored at location of someIndex, type '*[someIndex]'
return 0;
}
Pointer - variable storing memory address
&[varName] - spits out memory address of varName
[varType]* pointerName = &[varName] - makes pointer variable storing location of varName
*[pointerName] - returns value of variable at location stored in pointerName
/*PROGRAM 1*/
#include <iostream>
using namespace std;
int main(){
int i = 10;
int j = i; //j is independent variable; can equal anything, independent of i. Here, it just happens to be set to equal i, but it can be changed any time without changing i.
cout << "i= " << i << " and j= " << j << "\n"; //i and j are equal
i=5;
cout << "i= " << i << " and j= " << j << "\n"; //i = 5, but j = 10. This is bc j = i is a one-time-only declaration; it doesn't set up an eternal relationship.
j=1;
cout << "i= " << i << " and j= " << j << "\n"; //i = 5; j = 1
return 0;
}
/*PROGRAM 2*/
#include <iostream>
using namespace std;
int main(){
int i = 10;
int* p = &i; //p points to location of i
cout << "i= " << i << " and *p= " << *p << "\n";
i=5; //because p points to location of i, *p will always give the value stored at that location, which is changed whenever i is changed.
cout << "i= " << i << " and *p= " << *p << "\n"; //unlike in PROGRAM 1, *p is changed when i is changed. So, i = 5 = *p.
*p=1; //this changes the variable stored at address p to be 1. This means that i = 1.
cout << "i= " << i << " and *p= " << *p << "\n"; //*p = 1 = i
return 0;
}
Example of difference between using pointers vs vars when coding
Pointers without pre-existing variables:
#include <iostream>
using namespace std;
int main(){
int* p = new int(5); //can have p point to memory address that hasn't been used before. ie, to a memory address that has no regular, non-pointer variable assigned to it. This stores an int = 5 at address p. MUST USE 'new' KEYWORD!
cout << "p points at address " << p << "\n"; //gives address (NOT value stored at address! So, does NOT return 5!)
cout << "The data stored in address " << p << " is " << *p << "\n"; //*p = 5
*p = 10; //change value at p from 5 to 10
cout << "Now the data stored in address " << p << " is " << *p << "\n"; //works the same as a regular pointer. Now, *p = 10
return 0;
}
My own program:
p1 and p2 point to the same location, so *p1 = *p2, and when you change *p1, *p2 changes as well.
HW problem 1:
HW problem 2:
Original: lists all files in current directory that were edited on the cu
#!/bin/tcsh //sets up the bash environment
set month = `date | awk '{print $2}'` //pipes output of "date" into "awk", meaning "date" is broken up into lines that are each assigned a numbered variable name. The second line happens to be the month.
echo $month
set tmpday = `date | awk '{print $3}'` //like above, except it uses the third line. This line happens to be the date.
echo $tmpday
if ($tmpday < 10) then
set today = " $tmpday" //putting something in double quotes means it won’t be parsed into fields.
else
set today = $tmpday //not putting something in double quotes means it will be parsed into fields. But, here, it doesn’t matter bc the only thing stored in $tmpday is ‘16’. If change the if statement to <20, "echo $today" still prints out the same value as before, '16'.
endif
echo $today
ls -lat | grep "$month $today" //
awk someInput - parses someInput into individual lines (whenever there’s a line break, it starts a new one) called “fields”. The fields are numbered in order; “$1” refers to the first line; “$2” to the second; and so on.
New: lists all files in the current directory that were edited within the current month
To find files by month, you don’t need any of the code that gets the numerical date; you only need the month. Then, you can run the line, ‘ls -lat | grep “$month”’, which lists the files in the current directory with date information, then uses grep to parse that list for the lines that contain the string stored in “$month” (that is, “Oct”, the current month).
The final, commented out line does the same thing, but for all files on the computer. It was not demonstrated because the output is too long and takes a while to find any files I can actually access. It uses the “find” command, goes to the root directory (“/”), and lists all files of the regular file type (-ls, -type f). Then it pipes this list into grep by month, then it pipes that list into grep by year (this is to make sure only files edited this month rather than Octobers of past years appear).
HW 6: (10/25/18)
Arrays:
#include <iostream>
using namespace std;
int main() {
int ii[3] = {1,2,3}; //array with 3 slots storing integers 1 through 3
int j=0;
while (j<3) { //Array length is hard-coded in; you'd have to change the bound for j if you changed the length of ii
cout <<" ii of "<<j<<" is "<<ii[j]<<endl; //prints each element of ii in order.
j++;
}
int LL[2][3] = {1,2,3,4,5,6}; //2D array. Row-major order; ie, it has 2 rows and 3 cols, not other way around. The first 3 numbers are in the first row; the next 3 are in the 2nd.
j=0;
int k;
while (j<2) {
k=0; //resets col count for each consecutive row
while (k<3) {
cout<<" LL of "<<j<<" "<<k<<" is "<<LL[j][k]<<endl; //prints all elements in 2D array using nested loops. Outer loop is for rows, inner loop is for cols.
k++;
}
j++;
}
return 0;
}
Comments:
The comment-out-one-line character is "//"
The comment-out-multiple-lines character is "/*" to start the comment and "*/" to close it.
#include <iostream>
using namespace std;
/************************************************\ // first chars start the comment: "/*"
* *
* Arrays *
* This program demonstrates arrays *
* *
\************************************************/ //last chars end the comment: "*/"
int main() {
// a loop to demonstrate 1D arrays
int ii[3] = {1,2,3};
int j=0;
while (j<3) {
cout <<" ii of "<<j<<" is "<<ii[j]<<endl;
j++;
}
// a loop ot demonstrate 2D arrays //oh look it's a comment. Hey they use the "//" I've been using all along because I already know C++
int LL[2][3] = {1,2,3,4,5,6};
j=0;
int k;
while (j<2) {
k=0; // do not forget to initialize k here
while (k<3) {
cout<<" LL of "<<j<<" "<<k<<" is "<<LL[j][k]<<endl;
k++;
}
j++;
}
return 0;
}
File IO:
#include <iostream> //this is for cin/cout. Misleadingly named; it makes me think of file IO.
#include <fstream> //this is for reading/writing to files
using namespace std;
int main() {
ofstream myfile; //creates new class of type ofstream; lets you use fstream commands to open/edit files.
myfile.open("example.txt"); //calls the "open" function in class type ofstream. You use "myfile" and not "ofstream" because this class has to be intantiated to use its functions. This line opens the file "example.txt" and creates it if it does not exist.
myfile<<"write some junk."; //writes this to the file. That's what the '<<' character does.
myfile.close(); //closes and saves the file
return 0;
}
Using multiple files:
/* MAIN.CPP */
#include <iostream>
#include <fstream>
// include the program dotprod.cpp so that we can find the dot_prod function
#include "dotprod.cpp" //"include" statement for the file who's functions you'll be using
using namespace std;
int main () {
// declare the vectors
double vector1[3];
double vector2[3];
// open the input file
ifstream infile;
infile.open("vectors.txt"); //we'll be using input from a text file
// store the input in the vectors and print the vectors for the user
infile>>vector1[0]>>vector1[1]>>vector1[2]; //each consecutive ">>" reads another double in vectors.txt
cout<<" Vector 1 is ("<<vector1[0]<<","<<vector1[1]<<","<<vector1[2]<<")"<<endl; //displays the vectors
infile>>vector2[0]>>vector2[1]>>vector2[2]; //repeat for vector2
cout<<" Vector 2 is ("<<vector2[0]<<","<<vector2[1]<<","<<vector2[2]<<")"<<endl;
// close the input file
infile.close();
// call the dot_prod function from dotprod.cpp
dot_prod(vector1,vector2); //don't need any special formatting here! Just call the function like normal!
return 0;
}
This is dotprod.cpp:
/* DOTPROD.CPP */
#include <iostream>
using namespace std;
double dot_prod(double v1[3],double v2[3]) { //this function takes two vectors of length 3 and type "double"
double dotdot;
dotdot = v1[0]*v2[0]+v1[1]*v2[1]+v1[2]*v2[2]; //computes the dot prod
cout<<" The dot product is "<<dotdot<<endl; //prints the dot prod
return 0;
}
This is vectors.txt:
1 2 3
4 5 6
7
My mods:
Added to main.cpp (in spot right after it reads in the vector vals) (new code is in red):
infile>>scalar;
cout<<"The scalar is "<<scalar<<endl;
// close the input file \
infile.close();
// call the dot_prod function from dotprod.cpp \
dot_prod(vector1,vector2);
scalar_mult(vector1,scalar);
scalar_mult(vector2,scalar);
Created scalarmult.cpp:
include <iostream>
using namespace std;
double scalar_mult(double v1[3],double s) {
double mult[3];
mult = {v1[0]*s,v1[1]*s,v1[2]*s};
cout<<"The scalar multiple is {"<<mult[0]<<","<<mult[1]<<","<<mult[2]<<"}"<<e\
ndl;
return 0;
}
Random numbers:
#include <iostream>
#include <math.h>
using namespace std;
// this function is the actual random number generator
// this code is stolen from the book numerical recipes for fortran
// it relies on random generated from overflow of memory locations
// and is a pseudo random number generator
dconst int a = 7141;
const int c = 54773;
const int mmod=256200;
double getFlatRandom(int& inew) { //"&" passes inew by reference. That is, when inew is changed inside the function, it is changed outside of the function as well.
double mranflat = 0.;
//manipulate inew in a way that gives pseudorandom numbers
inew = inew%mmod;
double aa = double(inew)/double(mmod);
mranflat=aa;
inew = a*inew+c;
return mranflat;
}
// in this code, we will call the pseudo-random number generator and learn some things
// about its properties by filling and then displaying a histogram
int main() {
int num;
cout << "Enter the number of loop iterations: ";
cin >> num;
int inew = 2345; // This is the "seed" for the random number generator
// we will put the results from the call into a histogram that we can look at, to learn some of its
// properties. This histogram has 10 bins.
int histo[10] = {0,0,0,0,0,0,0,0,0,0};
double atmp; // this will be your random number
// call the random number generator num times and fill a histogram
for (int i = 0; i<num; i++){
atmp=getFlatRandom(inew); // call the random number generator
histo[int(atmp*10)]++; // increment the histogram bin the number falls within
}
// print the histogram to the screen
for (int i = 0; i<10; i++){
cout<<i<<": ";
for (int j=0; j<int((double(100*histo[i])/double(num))+0.5); j++) { //scales histogram to display 100 data points, regardless of how many there actually are
cout << "=";
}
cout << endl;
}
return 0;
}
How people actually do random numbers:
#include <iostream>
#include <stdlib.h> //need this for srand() and rand()
#include <time.h> //need this for time(NULL)
using namespace std;
int main(){
srand(time(NULL)); //seeds random generator with current time, ensuring a unique input that's constantly changing
cout << rand()%100 << endl; /* This will print a random n\
umber between [0,100). */
return 0;
}
HW7 (11/6/18, 11/12/18)
MadGraph - Monte Carlo simulation for CMS events
tar -xvzf [folderName] - command to unzip folder
See below for letter meanings.
tar -cvzf [archiveName] [folderName] - command to zip folderName into archive called archiveName
c/x - create/extract
v - verbose. Displays process of creating the archive while it's being created. If you don't have v, you won't get extra outputs after you call the command.
z - compress with gzip
f - allows you to specify archiveName
./[someFile] - runs someFile
Madgraph commands:
generate [someProcess] ~ - generate process
Ex: generate p p > t t~
makes proton-proton collision that decays into a top quark and an anti-top quark
~ - signifies antiparticle
add process [someProcess] - adds process
Ex: add process p p > W+ j, W+ > l+ vl
adds another process to the current "generation" of processes
"W+ decaying leptonically"
j is a "jet multiparticle" - means "to sum over all of the same gluon, quark, and antiquark partons"
W is W boson
l+ is a "lepton multiplarticle" - means to sum over all positrons and anti-muons
l- is a "lepton multiparticle" - means to sum over all electrons and muons
"The multiparticle "l-" means to sum over electrons and muons, and the multiparticle "l+" means to sum over their antiparticles"
vl "multiparticle" - neutrino
display processes - displays all processes
display particles - display particles
display multiparticles - display multiparticles
output [outputName] - creates output of name outputName
Ex: output MY_FIRST_MG5_RUN
launch [outputName] - runs output of name outputName
Ex: launch MY_FIRST_MG5_RUN
.lhe files store event info
Montecarlo ID - standardized integer ID that corresponds to a specific physical particle.
For example, the MC ID of the top quark is 6.
Used in MadGraph to simulate events.
iSpy WebGL - notes in HW submission.
HW8 (11/8/18)
HZZ4LeptonsAnalysisReduced->MakeClass("HiggsAnalysis") - makes C++ files with classes for higgs analysis
Higgs analysis code:
#define HiggsAnalysis_cxx
#include "HiggsAnalysis.h"
#include <TH2.h>
#include <TStyle.h>
#include <TCanvas.h>
void HiggsAnalysis::Loop()
{
// In a ROOT session, you can do: // used commands in red to run this code in ROOT
// Root > .L HiggsAnalysis.C
// Root > HiggsAnalysis t
// Root > t.GetEntry(12); // Fill t data members with entry number 12
// Root > t.Show(); // Show values of entry 12
// Root > t.Show(16); // Read and show values of entry 16
// Root > t.Loop(); // Loop on all entries
//
// This is the loop skeleton where:
// jentry is the global entry number in the chain
// ientry is the entry number in the current Tree
// Note that the argument to GetEntry must be:
// jentry for TChain::GetEntry
// ientry for TTree::GetEntry and TBranch::GetEntry
//
// To read only selected branches, Insert statements like:
// METHOD1:
// fChain->SetBranchStatus("*",0); // disable all branches
// fChain->SetBranchStatus("branchname",1); // activate branchname
// METHOD2: replace line
// fChain->GetEntry(jentry); //read all branches
//by b_branchname->GetEntry(ientry); //read only this branch
if (fChain == 0) return;
Long64_t nentries = fChain->GetEntriesFast();
Long64_t nbytes = 0, nb = 0;
TFile* output = TFile::Open("Dielectron_MC.root", "RECREATE"); // "RECREATE" would produce a new root file with name Dielectron_MC.root every time you run the code
TH1F* Z_ee = new TH1F("Z_ee", "Di-electron candidate invariant mass", 200, 0, 200); // creates new data set that we can plot from. Will store Z boson candidates.
double el1mt = 0.0;
double el1pt = 0.0;
double el1eta = 0.0;
for (Long64_t jentry=0; jentry<nentries;jentry++) {
Long64_t ientry = LoadTree(jentry);
if (ientry < 0) break;
nb = fChain->GetEntry(jentry); nbytes += nb;
// if (Cut(ientry) < 0) continue;
TLorentzVector el1, el2; // makes relativistic vectors el1 and el2 (4 components). Has a bunch of methods you can use
el1.SetPtEtaPhiM(f_lept1_pt, f_lept1_eta, f_lept1_phi, 0.0); // transverse momentun, eta, phi, M. Numbers come from HiggsAnalysis.h
el2.SetPtEtaPhiM(f_lept2_pt, f_lept2_eta, f_lept2_phi, 0.0);
TLorentzVector zCandidate = el1 + el2; // reconstructs Z boson from our leptons
Z_ee->Fill(zCandidate.M()); // fills Z_ee plot with reconstructed Z
el1mt = el1.Mt();
cout << el1mt << endl;
}
Z_ee->Write(); // writes Z_ee data to visual plot
output->Close();
}
To find the Higgs, just duplicated the Z_ee graph and methods in the code. In new code, used two reconstructed Z bosons to reconstruct the Higgs, just like the old code used two directly detected leptons to reconstruct a Z boson.
HW9: Python (11/19/18)
Basic syntax:
Adding variables:
pandas=10 // declares numerical variable called "pandas" and sets equal to 10. Can declare and define in same line. Note the lack of semicolons; whitespace, not semicolons and brackets, defines lines and functions. Also note the lack of type casting. Pandas is not specified as an int, double, float, etc--Python makes that call based on how it's used later on in the program.
zebras=20 // see above
total_animals_in_zoo=pandas+zebras // adds two variables and stores them in a third one
print(total_animals_in_zoo) // prints the third variable; outputs the sum of "pandas" and "zebras"
Multiplication, exponentiation:
pi=3.14159 #Approx. // stores value of pi for use in later calculation
diameter=10 // stores value of diameter for use in later calculation
######YOUR CODE HERE#### // calculates area using predefined variables. Note the exponential operator: [base]**[exponent]. Also note order of operations. As expected, order is parenthesis, exponent, multiplication, addition.
print(area) // prints area to console
Switching variables:
Approach 1 (doesn't work):
a = [1, 2, 3] // declares list a
b = ['cat', 'dog', 'fish'] // declares list b
a=b // sets a equal to b; loses value of a
b=a // sets b equal to a. But, a stores the value originally assigned to b, so this is like saying "b=b". The switch fails.
print('Method 1')
print(a) // prints original value of b
print(b) // prints original value of b
Approach 2 (works!):
a = [1, 2, 3]
b = ['cat', 'dog', 'fish']
temp = a // makes variable to store original value of a so it is not lost when a is overwritten
a = b // overwrites a; a now stores original value of b
b = temp // overwrites b with temp, which stores the original value of a. b now stores the original value of a. The switch succeeds!
print('Method 2')
print(a) // prints original value of b
print(b) // prints original value of a
My own application:
Uses temp variable to prevent data loss, just like in previous bullet points.
Functions:
Example:
def round_to_two_places(num): // keyword "def" defines function. argument is in parenthesis. Again, no type needed for function or argument.
return round(num, 2) // whitespace (tabbed in) defines content of function. Note: round() returns truly rounded number, not just truncated!
print(round_to_two_places(4.325456463)) // inputs a number into the function, then prints the result. Shows how function works. Returns 4.33.
My function:
Like above, but can also specify number of places to round to.
Predefined functions:
Some examples:
Using predefined functions (my code):
Booleans, conditionals:
Example:
print(3==10) // == operator, same as other languages. Outputs false.
print(len('abc')==3) // using some different inputs. Still comparing integers, though, so statement still stands. Outputs true.
print(max([10,40,53]) < 25) // prints false because the max of the list is greater than 25
print (5 != 3) // prints true because 5 != 3
My code:
In a function:
def longer_string(string1, string2): // function header. Two arguments.
if(len(string1) > len(string2)): // if statement
return string1 // return the longer string
elif(len(string1) < len(string2)): // alternate condition
return string2 // return the longer string
else: // if neither string is longer
return // then don't return anything
print(longer_string('abc', 'jfkdla;s')) // returns second string
print(longer_string('abcfdsafdasg', 'jfkdla;s')) // returns first string
print(longer_string('abc', 'def')) // returns nothing because neither string is longer
"and" and "or" operators:
Unlike most languages, just uses the English words for these operators; no fancy symbols
def can_make_cake(eggs, flour, milk, almond_milk):
#I can make cake if I have some kind of milk AND flour AND eggs
return (milk or almond_milk) and flour and eggs > 0
print('Can I make cake?')
print(can_make_cake(10, True, False, True)) // true bc have a type of milk, some eggs, and some flour
Modulus operator (%):
Returns remainder of division operation.
My code:
If modulo 2 is 1, remainder when divide by 2 is 1, meaning number being divided is odd. Otherwise, number is even.
Lists:
Start at 0, just like most programming languages
Example (with 2D):
My code (sports):
My code (Mario):
Loops:
For loop example:
def list_contains_seven(my_list): // function that takes one argument. If list passed in contains 7, return true. Otherwise, return false.
for element in my_list: // for each element in the list
if element ==7: // if it equals 7
return True // return true
return False // otherwise, return false.
print(list_contains_seven([6,14,5,7])) // true
print(list_contains_seven([6,14,5,9])) // false
While loop example:
def count_to_10(num): // function with numerical input. Counts from that input up to 10 by 1, printing out the number with each iteration.
while(num<10): // while the number is less than 10
num=num+1 // add 1 each time
print(num) // print the number each time
count_to_10(0) // prints 1 to 10
count_to_10(5) // prints 6 to 10
count_to_10(11) // prints nothing because number is already greater than 10
My code (prints number of elements divisible by 7):
Dictionaries:
Example:
capitals={'United States': 'DC', 'France': 'Paris', 'Mexico': 'DF'} // pairs each "key" with its "definition". Key is country, definition is capital
populations={'UMD':40000, 'Towson': 30000, 'UMBC': 20000} // can pair values of different types (string and int in this case)
print(populations.get('UMD')) // print the definition associated with UMD
Some methods:
.items() - prints all keys and values
.keys() - prints all keys
.values() - prints all values
.pop() - removes entry with given key
.get() - returns entry with given key
.update({'key':'value'}) - adds given entry
.clear() - deletes all entries in dictionary
External libraries:
Example with math, numpy, matplotlib:
import math as m #I use the as m part so when I refer to the library I can just type m instead of math. Its just an abbreviation.
print(m.pi, m.log(32, 2)) # can get auto values of pi as well as log function from math library
print(m.gcd(75348597,979531683)) # greatest common denominator
print(m.cos(10)) # cos() is also a math function!
Here's a more advanced example.
import matplotlib # imports some more libraries. These are used for plotting visible graphs.
import matplotlib.pyplot as plt
import numpy as np # numpy is like the math library, except it uses arrays and is much faster.
# Data for plotting
t = np.arange(0.0, 2.0, 0.01) # t goes from 0 to 2 in increments of .01
s = 1 + np.sin(2 * np.pi * t) # s is a function of t
fig, ax = plt.subplots() # new figure
ax.plot(t, s) # plots s vs t
ax.set(xlabel='time (s)', ylabel='voltage (mV)',
title='About as simple as it gets, folks') # labels the graph
ax.grid() # toggles grid. Default is on, so turns it off.
plt.show() # shows plot
Output:
Another example:
My Example:
C++ notes:
g++ main.cpp //compiles main.cpp as a c++ file. 'g++' is our system's compiler.
Need to do each time you edit main.cpp!
./a.out //creates and runs executable for everything you've compiled.
Debugging with gdb!
To use, implement after saving file.
Setup (all terminal commands):
g++ -g file.cpp - compiles file with markers for debugging (that's what the '-g' does)
gdb - opens gdb ("puts you in the gdb shell")
file a.out - loads file
Can combine commands: "gdb a.out" loads file in gdb
break 1 (or, 'b 1') - puts break point at line 1
watch [varName] (or, 'wa [varName]') - makes it track and display varName every time it's changed
continue (or, 'c') - steps through code until specified breakpoint (either a line or a variable)
step - runs next line
info locals - displays all vars and their values in the function
q - quits gdb
Pointer - variable storing memory address
&[varName] - spits out memory address of varName
[varType]* pointerName = &[varName] - makes pointer variable storing location of varName
*[pointerName] - returns value of variable at location stored in pointerName
To point to a memory address with no regular, non-pointer assigned to it:
[varType]* pointerName = new varType(varValue)
Special characters:
~ - detailed above
. - references current directory
.. - references immediate parent directory
* - "wildcard" character. Can stand for anything. Useful for text searches.
> - can use to overwrite a file. (ex: 'ls -1 > log.txt')
>> - can use with "echo" to append text to a file. (ex: 'echo "these are words" >> log.txt')
`` - run the code inside the backticks then converts output to a string. Lets you input result into another function.
Emacs notes:
- Emacs is a text editor used to edit simple text files in unicode format
emacs -nw [fileName] - opens/creates file with name fileName
emacs [fileName] & - opens file in NEW WINDOW!!!
- If you want to get to a file not in your folder, you can use its relative or absolute path
- '-nw' means 'no window.' That is, it doesn't open the GUI, just the CLI.
SOME KEYBOARD COMMANDS IN EMACS:
- ctrl + x ctrl + c ctrl + y - saves and closes file
- ctrl + x ctrl + s - saves file w/o closing
- Fun fact: each time you edit a file, the old version is copied to a backup file called "name.txt~", which is overwritten every time you start a new editing session. But, it can be useful if you need to go one generation back.
WebGL notes:
- WRITE INSTRUCTIONS FOR WEBGL USE HERE