Skip to content

Instantly share code, notes, and snippets.

View swang373's full-sized avatar

Sean-Jiun Wang swang373

View GitHub Profile
@swang373
swang373 / hex2ascii.py
Created July 21, 2023 14:17
Hex to ASCII conversion in Python
import binascii
HEXSTRING = (
'546869732069732066696e652e2049276d206f6b61792077697468207468'
'65206576656e747320746861742061726520756e666f6c64696e67206375'
'7272656e746c792e20546861742773206f6b61792c207468696e67732061'
'726520676f696e6720746f206265206f6b61792e0a'
)
print(binascii.unhexlify(HEXSTRING))
@swang373
swang373 / requests_user_agent.py
Created October 25, 2019 14:32
Add User-Agent to request header
import requests
URL = 'https://www.google.com'
HEADERS = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:61.0) Gecko/20100101 Firefox/70.0'
}
response = requests.get(URL, headers=HEADERS)
@swang373
swang373 / headless_firefox_example.py
Created October 24, 2019 16:03
Running a headless Firefox browser using Selenium webdriver
from selenium import webdriver
def main():
# Setup options to run Firefox in headless mode.
firefox_options = webdriver.FirefoxOptions()
firefox_options.headless = True
# Open a Firefox browser and check the Python official site.
driver = webdriver.Firefox(options=firefox_options)
@swang373
swang373 / simple_request_example.py
Created October 24, 2019 15:56
Example of URL fetching function using requests library
import requests
class HTTPRequestError(Exception):
pass
def fetch(url, params=None, **kwargs):
try:
response = requests.get(url, params, **kwargs)
@swang373
swang373 / discord_bot_await_mention.py
Last active October 24, 2019 15:59
Awaiting bot mentions using discord.py
import logging
import discord
logger = logging.getLogger('DiscordBot')
class Bot(discord.Client):
@swang373
swang373 / move_output_files_lpc.sh
Created June 13, 2018 02:05
Move AnalysisTools output files from one LPC EOS directory to another
SRC="/store/group/lpchbb/VHbbAnalysisNtuples/RESUBMIT_June9_2016V4"
DST="/store/group/lpchbb/VHbbAnalysisNtuples/CMSConnect_June9_2016V4/ggZH125_ZLL_powheg"
FILES=(
output_ggZH125_ZLL_powheg_177part0.root
output_ggZH125_ZLL_powheg_177part1.root
output_ggZH125_ZLL_powheg_177part10.root
output_ggZH125_ZLL_powheg_177part11.root
output_ggZH125_ZLL_powheg_177part12.root
@swang373
swang373 / split_jobs_again.py
Created June 12, 2018 18:18
Split an AnalysisTools resubmission job into more subjobs and calculate the starts and stops
import sys
# The only command line argument should be the path to a file
# where each line is a set of arguments for resubmission.
with open(sys.argv[1]) as f:
lines = f.read().splitlines()
for line in lines:
start, stop = line.split()[-3:-1]
start = float(start)
@swang373
swang373 / find_failed_job_args.py
Last active June 12, 2018 18:06
Finding the arguments for AnalysisTools CMSConnect jobs that require resubmission
import glob
import os
SEARCH_PATTERN = '/path/to/your/output/dir/{0}/*.root'
SAMPLES = [
'DYToLL_madgraph',
'ZH125_ZNuNu_powheg',
]
@swang373
swang373 / check_output_directory.py
Last active June 12, 2018 17:59
Check the integrity of files in an AnalysisTools output directory
import glob
import ROOT
import concurrent.futures
def check_file(path):
f = ROOT.TFile.Open(path)
# The file is somehow unreadable.
if not f:
@swang373
swang373 / dump_hists_varial.py
Created May 16, 2018 04:06
Dump histograms in parallel using Varial
#!/usr/bin/env python
import glob
import sys
import varial.tools
from varial_ext.treeprojector import TreeProjectorFileBased
# The following all caps variables are module level constants that I would tweak by hand.
# Most of them are parameters are passed to the treeprojection_mr_impl.map_projection
# function, so check its docstring for more information.