Skip to content

Instantly share code, notes, and snippets.

View mastbaum's full-sized avatar

Andy Mastbaum mastbaum

  • Rutgers University
  • New Brunswick, NJ
View GitHub Profile
@mastbaum
mastbaum / keys.cpp
Created April 22, 2014 21:06
Creating a list of keys from an STL map.
/**
* Creating a list of keys from an STL map.
*
* Build: g++ -o keys keys.cpp
* Run: ./keys
*/
#include <iostream>
#include <map>
#include <string>
@mastbaum
mastbaum / time_residuals.py
Created May 13, 2014 20:47
Plot PMT hit time residuals from the RAT DS
'''Plot PMT hit time residuals from the RAT DS.
Usage:
$ python time_residuals.py "filenames_*.root"
With one filename or wildcards (use quotes when using wildcards).
The histogram opens in an interactive ROOT window for editing.
'''
/**
* Plot ES scattering angle for 8B solar neutrinos vs. recoil energy,
* weighted by the neutrino spectrum.
*
* This uses the 8B spectrum and ES cross section code in RAT.
*
* Build:
*
* g++ -g -o es es.cpp -L$RATROOT/lib -I$RATROOT/include \
* -lrat_Linux `root-config --libs --cflags` \
@mastbaum
mastbaum / overlay.C
Created June 20, 2014 16:37
Example of drawing overlaid histograms from two TTrees in two TFiles
// Draw overlaid histograms from a RAT ntuple (for example).
//
// Usage:
// $ root -l -x 'overlay.C("filename1.root", "filename2.root")'
//
// The trick is that histograms are associated with a TDirectory
// upon creation, which is most likely the open file. When the
// directory changes when a new file is opened, the pointer to
// the histogram is invalidated. TH1::SetDirectory is used to
// detach a histogram from a file.
@mastbaum
mastbaum / orca_db_example.py
Created June 25, 2014 18:17
A minimal example of how Orca might interact with the CouchDB database
'''A minimal example of how Orca might interact with the CouchDB database
to load settings from the DB at startup and write settings to the DB only
when they change.
Because Orca is the only *producer* of the database data, it can keep track
internally of whether the detector configuration has changed.
NO: Checking in-memory settings against values in the DB
for fec in fecs:
@mastbaum
mastbaum / check_smart_status.py
Created January 9, 2015 15:05
Nagios plugin to monitor the SMART status of hard disks
#!/usr/bin/env python
'''Report the SMART status of hard disks using a log file.'''
import argparse
import subprocess
import sys
# Nagios status codes
OK, WARNING, CRITICAL, UNKNOWN = range(4)
@mastbaum
mastbaum / mcphoton.cpp
Created June 10, 2015 17:24
Plot RAT-PAC MC photon data
/**
* Example script for drawing MCPhoton information from RAT-PAC ROOT files.
*
* Usage:
*
* root[0] .L mcphoton.cpp+
* root[1] plotMCData("myfile.root");
*
* A. Mastbaum <mastbaum@hep.upenn.edu>, June 2015
*/
@mastbaum
mastbaum / time_example.py
Created July 22, 2015 01:49
Python dates & times example
from datetime import datetime
import pytz # Probably need to install this one on your system
import dateutil.parser
# An example timestamp string
timestamp = "2014-12-12T08:51:06.082327-05:00"
# Convert the string to a datetime object
timestamp_obj = dateutil.parser.parse(timestamp)
@mastbaum
mastbaum / stl_bounds_at.cpp
Created June 2, 2011 16:00
A comparison of using brackets or "at" to access elements of an STL vector
#include<iostream>
#include<vector>
/** There are two ways to access the ith element of an STL vector, the usual
* v[i] syntax or using v.at(i).
*
* The former doesn't check array boundaries, so something like v[v.size()+1]
* works and gives you whatever happens to be sitting in that memory location.
*
* v.at() has the same purpose, but actually throws an exception
@mastbaum
mastbaum / bad_ptr.cpp
Created June 2, 2011 16:34
Invalid pointer dereference in C++
/** A simple example of an invalid pointer dereference.
*
* a is not a valid pointer until initialized, so dereferencing it (here,
* setting it to 37) is bad news.
*
* Note that C++ doesn't necessarily initialize pointers to NULL, so it is
* possible that this will "work" and not cause a segmentation fault. This
* could be really bad, trampling some other memory. We're lucky to get a
* segfault.
*/