Skip to content

Commit

Permalink
cms-YYYY-luminosity: black formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
katilp authored and tiborsimko committed May 30, 2023
1 parent b2d66a5 commit 3658e37
Showing 1 changed file with 92 additions and 56 deletions.
148 changes: 92 additions & 56 deletions cms-YYYY-luminosity/code/lumi_records.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import requests

import sys
sys.path.insert(1, '../cms-release-info')

sys.path.insert(1, "../cms-release-info")

"""
Create a luminosity record.
Expand All @@ -23,6 +24,7 @@
"""


def create_record(recid, year, era, runtype, uncertainty, lumi_ref, val_recid):
"""Create record for the given year."""

Expand All @@ -31,56 +33,89 @@ def create_record(recid, year, era, runtype, uncertainty, lumi_ref, val_recid):
year_created = year
year_published = datetime.date.today().strftime("%Y")
runtype = str(runtype)
if "pphiref" in runtype :
display_runtype = 'pp'
if "pphiref" in runtype:
display_runtype = "pp"
else:
display_runtype = runtype

# Get the energy
# Using the run_era, for pp it is needed only here
# Could be done differently but this is good enough
url = 'http://api-server-cms-release-info.app.cern.ch/runeras/?run_era='+era
this_json=json.loads(requests.get(url).text.strip())
energy=this_json[0]["energy"]
url = "http://api-server-cms-release-info.app.cern.ch/runeras/?run_era=" + era
this_json = json.loads(requests.get(url).text.strip())
energy = this_json[0]["energy"]

# NB the reference needs to be to cds for this to work:
url = lumi_ref+'/?of=tm&ot=245__a'
url = lumi_ref + "/?of=tm&ot=245__a"
lumi_ref_title = requests.get(url).text.strip()

pp_text=''
pp_text = ""
if "pphiref" in runtype:
collision_text = energy+' proton-proton collision data, needed as reference data for heavy-ion data analysis,'
collision_text = (
energy
+ " proton-proton collision data, needed as reference data for heavy-ion data analysis,"
)
elif "PbPb" in runtype:
collision_text = energy+' PbPb heavy-ion collision data'
collision_text = energy + " PbPb heavy-ion collision data"
elif "pPb" in runtype:
collision_text = energy+' proton-Pb heavy-ion collision data'
collision_text = energy + " proton-Pb heavy-ion collision data"
elif "pp" in runtype:
collision_text = energy+' proton-proton collision data'
collision_text = energy + " proton-proton collision data"
run_range_input = year
pp_text='(The integrated luminosity for validated runs and luminosity sections of all '+year+' p-p data taking is available in '+year+'lumi.txt.)'
pp_text = (
"(The integrated luminosity for validated runs and luminosity sections of all "
+ year
+ " p-p data taking is available in "
+ year
+ "lumi.txt.)"
)
else:
print('Runtype unknown!')
print("Runtype unknown!")

# normtag file only after Run-1
normtag_text=''
# normtag file only after Run-1
normtag_text = ""
if int(year) > 2014:
normtag_text='The luminometer giving the best value for each luminosity section is recorded in a <strong>normtag</strong> file <a href=\"/record/'+str(recid)+'/files/normtag_PHYSICS_'+runtype+'_'+year+'.json\">normtag_PHYSICS_'+runtype+'_'+year+'.json</a> that is used in the luminosity calculation.'
normtag_text = (
'The luminometer giving the best value for each luminosity section is recorded in a <strong>normtag</strong> file <a href="/record/'
+ str(recid)
+ "/files/normtag_PHYSICS_"
+ runtype
+ "_"
+ year
+ '.json">normtag_PHYSICS_'
+ runtype
+ "_"
+ year
+ ".json</a> that is used in the luminosity calculation."
)

rec["abstract"] = {}

url = 'http://api-server-cms-release-info.app.cern.ch/runeras/run_era?year='+year+'&type='+runtype+'-phys&released=yes'
url = (
"http://api-server-cms-release-info.app.cern.ch/runeras/run_era?year="
+ year
+ "&type="
+ runtype
+ "-phys&released=yes"
)
od_runs = json.loads(requests.get(url).text.strip())

# The use of variable in string with two different notations could be fixed in the following...
# NB in the +var+ notation, var needs to be a string
# The use of variable in string with two different notations could be fixed in the following...
# NB in the +var+ notation, var needs to be a string
rec["abstract"]["description"] = (
"<p>CMS measures the luminosity using different luminometers (luminosity detectors) and algorithms. "+normtag_text+"</p>"
"<p>The integrated luminosity for validated runs and luminosity sections of the %s taken in %s (%s) is available in %slumi.txt. %s</p>" % (collision_text, year, ",".join(od_runs), ",".join(od_runs), pp_text)
+ "<p> For luminosity calculation, a detailed list of luminosity by lumi section is provided in <a href=\"/record/%s/files/%s_%slumibyls.csv\">%s_%slumibyls.csv</a> for the <a href=\"/record/%s\">list of validated runs</a> and lumi sections.</p>" % (recid, runtype, year, runtype, year, val_recid)
+ "<p>The uncertainty in the luminosity measurement of %s data should be considered as %s%% (reference <a href=\"%s\">%s</a>).</p>" % (year, uncertainty, lumi_ref, lumi_ref_title)
+ "<p>In your estimate for the integrated luminosity, check for which runs the trigger you have selected is active and sum the values for those runs. For prescaled triggers, the change of prescales (run, lumi section, index of prescales referring to the PrescaleService module in the High-Level Trigger configuration files) is recorded in <a href=\"/record/%s/files/prescale_%s%s.csv\">prescale_%s%s.csv</a>.</p>" % (recid, runtype, year, runtype, year)
+ "<p>Additional information on how to extract luminosity values using the <strong>brilcalc tool</strong> can be found in the <a href=\"/docs/cms-guide-luminosity-calculation\"> luminosity calculation guide</a>.</p>"
)
"<p>CMS measures the luminosity using different luminometers (luminosity detectors) and algorithms. "
+ normtag_text
+ "</p>"
"<p>The integrated luminosity for validated runs and luminosity sections of the %s taken in %s (%s) is available in %slumi.txt. %s</p>"
% (collision_text, year, ",".join(od_runs), ",".join(od_runs), pp_text)
+ '<p> For luminosity calculation, a detailed list of luminosity by lumi section is provided in <a href="/record/%s/files/%s_%slumibyls.csv">%s_%slumibyls.csv</a> for the <a href="/record/%s">list of validated runs</a> and lumi sections.</p>'
% (recid, runtype, year, runtype, year, val_recid)
+ '<p>The uncertainty in the luminosity measurement of %s data should be considered as %s%% (reference <a href="%s">%s</a>).</p>'
% (year, uncertainty, lumi_ref, lumi_ref_title)
+ '<p>In your estimate for the integrated luminosity, check for which runs the trigger you have selected is active and sum the values for those runs. For prescaled triggers, the change of prescales (run, lumi section, index of prescales referring to the PrescaleService module in the High-Level Trigger configuration files) is recorded in <a href="/record/%s/files/prescale_%s%s.csv">prescale_%s%s.csv</a>.</p>'
% (recid, runtype, year, runtype, year)
+ '<p>Additional information on how to extract luminosity values using the <strong>brilcalc tool</strong> can be found in the <a href="/docs/cms-guide-luminosity-calculation"> luminosity calculation guide</a>.</p>'
)

rec["accelerator"] = "CERN-LHC"

Expand All @@ -106,32 +141,28 @@ def create_record(recid, year, era, runtype, uncertainty, lumi_ref, val_recid):
rec["license"]["attribution"] = "CC0"

rec["links"] = {}
rec["links"] = [
{
"url": lumi_ref,
"title": lumi_ref_title
}
]
rec["links"] = [{"url": lumi_ref, "title": lumi_ref_title}]

rec["publisher"] = "CERN Open Data Portal"

rec["recid"] = str(recid)

rec["relations"] = {}
rec["relations"] = [
{
"recid": str(val_recid),
"type": "isRelatedTo"
}
]

url = 'http://api-server-cms-release-info.app.cern.ch/runeras/run_era?year='+year+'&type='+runtype+'-phys'
#rec["run_period"] = read_run_periods(year, 'pp-phys')
rec["relations"] = [{"recid": str(val_recid), "type": "isRelatedTo"}]

url = (
"http://api-server-cms-release-info.app.cern.ch/runeras/run_era?year="
+ year
+ "&type="
+ runtype
+ "-phys"
)
# rec["run_period"] = read_run_periods(year, 'pp-phys')
rec["run_period"] = json.loads(requests.get(url).text.strip())

rec["title"] = (
"CMS luminosity information for "+collision_text+" taken in "+year
)
"CMS luminosity information for " + collision_text + " taken in " + year
)

rec["type"] = {}
rec["type"]["primary"] = "Supplementaries"
Expand All @@ -141,6 +172,7 @@ def create_record(recid, year, era, runtype, uncertainty, lumi_ref, val_recid):

return rec


# @click.command()
def main():
"Do the job."
Expand All @@ -154,28 +186,33 @@ def main():
# this would read from the local json file
# with open('./inputs/cms_release_info.json') as f:
# data = f.read()

# # reconstructing the data as a dictionary
# all_years = json.loads(data)
# all_years = json.loads(data)
# this_year = all_years[year]

# this gets json from the api server
url = 'http://api-server-cms-release-info.app.cern.ch/years?year='+year+'&type='+runtype+'&output=plain'
this_year = json.loads(requests.get(url).text.strip())

# this gets json from the api server
url = (
"http://api-server-cms-release-info.app.cern.ch/years?year="
+ year
+ "&type="
+ runtype
+ "&output=plain"
)
this_year = json.loads(requests.get(url).text.strip())

records.append(
create_record(
recid,
year,
era,
runtype,
runtype,
this_year["lumi_uncertainty"],
this_year["luminosity_reference"],
this_year["val_json"][0]["recid"]) # This requires the json files to be in a specific order, with "golden" first
this_year["val_json"][0]["recid"],
) # This requires the json files to be in a specific order, with "golden" first
)



print(
json.dumps(
records,
Expand All @@ -189,4 +226,3 @@ def main():

if __name__ == "__main__":
main()

0 comments on commit 3658e37

Please sign in to comment.