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 / nested_class.cpp
Created July 31, 2013 19:03
A nested C++ class "using private members of its parent"
#include <iostream>
class Foo {
public:
class Bar {
public:
Bar(Foo* owner) : owner(owner) {}
int getter() { return owner->a; }
private:
Foo* const owner;
@mastbaum
mastbaum / Makefile
Created July 3, 2013 14:50
Example of fitting in ROOT
ifndef ROOTSYS
$(error ROOTSYS is not defined)
endif
all: minuit_example
minuit_example:
g++ -o minuit_example minuit_example.cpp `root-config --libs --cflags` -lMinuit
clean:
@mastbaum
mastbaum / nhit_cut.py
Created June 12, 2013 21:36
An example PyROOT analysis which applies several different NHIT cuts, calculates the fraction of events failing, and produces a plot.
'''An example PyROOT analysis which applies several different NHIT cuts,
calculates the fraction of events failing, and produces a plot.
'''
import sys
import array
from rat import ROOT
def fast_dsreader(filename):
@mastbaum
mastbaum / tntuple_correlation.h
Last active December 18, 2015 03:19
Compute a correlation matrix for pairs of variables in a ROOT TNtuple.
/** Get a value from a TNtuple by event id and name */
float get_ntuple_entry(TNtuple* nt, int i, std::string field) {
float v;
nt->SetBranchAddress(field.c_str(), &v);
nt->GetEvent(i);
nt->ResetBranchAddresses();
return v;
}
@mastbaum
mastbaum / matplotlib_legend.py
Created November 28, 2012 16:21
labeling only some plots in matplotlib
'''Example of labeling only some plots in a matplotlib legend.
Tested with matplotlib==1.0.1
'''
from matplotlib import pyplot as plt
plt.figure(1, facecolor='white')
plt.plot([1,2,3,4], [1,2,3,4], label='foo')
plt.plot([1,2,3,4], [2,4,6,8])
@mastbaum
mastbaum / null_member.cpp
Created September 28, 2012 00:04
null member return value
#include <stdio.h>
class foo {
public:
int thinger() {}
};
int main(int argc, char* argv[]) {
int i;
foo f;
@mastbaum
mastbaum / custom_directive.py
Created May 10, 2012 20:32
Example of a custom ReST directive in Python docutils
'''Example of a custom ReST directive in Python docutils'''
import docutils.core
from docutils.nodes import TextElement, Inline
from docutils.parsers.rst import Directive, directives
from docutils.writers.html4css1 import Writer, HTMLTranslator
class foo(Inline, TextElement):
'''This node class is a no-op -- just a fun way to define some parameters.
There are lots of base classes to choose from in `docutils.nodes`.
@mastbaum
mastbaum / thread-cpp11.cxx
Created January 24, 2012 05:41
member function pointers in c++11
#include <iostream>
#include <thread>
/* g++ -std=c++0x -o thread-cpp11 thread-cpp11.cxx -lpthread */
class Bar {
public:
Bar() : i(0) {}
int i;
};
@mastbaum
mastbaum / mfp_specialfn.cpp
Created January 24, 2012 05:21
member function pointers with special function
#include <iostream>
#include <pthread.h>
/** Member function pointers given function name */
// class Launcher exists as a container of an instance and an argument.
// Using templates allows us to use the Launcher for a member of any class
template <class T>
class Launcher {
@mastbaum
mastbaum / mfn_superclass.cpp
Created January 24, 2012 05:16
member function pointers with mysterious superclass
#include <iostream>
#include <pthread.h>
/** Calling a member function using a mysterious superclass */
#include <stdio.h>
class Super;
typedef void* (Super::*pSuperMF)(void*);