ATLAS Offline Software
GetAllXsec.py
Go to the documentation of this file.
1 #! /usr/bin/python
2 
3 # Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
4 
5 import os
6 import sys
7 import StatusFiles
8 import glob
9 
10 import DefineTags
11 import SimpleFunctions
12 
13 import logging
14 
15 # Init the logger for the script and various modules
16 logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s', datefmt='%H:%M:%S')
17 log = logging.getLogger("myXsecLogger")
18 log.setLevel(logging.INFO)
19 
20 
21 def is_number(s):
22  try:
23  float(s)
24  return True
25  except ValueError:
26  return False
27 
28 
29 mainFolder = os.getcwd()
30 xsecFolder = mainFolder+"/XsecFiles/"
31 SimpleFunctions.MakeFolder(xsecFolder)
32 
33 # Retrieve list of classification tags
34 allProcesses = DefineTags.ReturnSampleCategory()
35 
36 # Get a pointer to all Status files
37 allFolders_mc15 = glob.glob(mainFolder+"/AllSampleInfo_mc15_13TeV/DSID*/*")
38 allFolders_mc15 = sorted(allFolders_mc15)
39 
40 allFolders_mc16 = glob.glob(mainFolder+"/AllSampleInfo_mc16_13TeV/DSID*/*")
41 allFolders_mc16 = sorted(allFolders_mc16)
42 
43 allFolders = allFolders_mc15 + allFolders_mc16
44 
45 allXsec = []
46 
47 # Using this list to avoid duplicates
48 # could be made more clever in the future
49 processed_DSIDs = []
50 
51 for entry in allFolders:
52 
53  DSID = (entry.split("XXX/"))[1]
54  if DSID in processed_DSIDs:
55  continue
56  processed_DSIDs.append(DSID)
57 
58  statusFiles_list = glob.glob(entry+"/Status*.txt")
59 
60  if len(statusFiles_list) == 0:
61  continue
62 
63  nEvents = 0
64 
65  for iStatus in statusFiles_list:
66 
67  AllInfo = StatusFiles.StatusFileReader(iStatus)
68  totalEvents = AllInfo.totalEvents
69 
70  log.debug("Reading nevents for %s", AllInfo.dataset)
71 
72  if not is_number(totalEvents):
73  log.error("Total Number of Events: %s - Sample %s", str(totalEvents), str(iStatus))
74  totalEvents = 0
75  else:
76  totalEvents = float(AllInfo.totalEvents)
77  if totalEvents > nEvents:
78  inputFile = iStatus
79  nEvents = totalEvents
80 
81  AllInfo = StatusFiles.StatusFileReader(inputFile)
82 
83  SortingTag = AllInfo.SortingTag
84 
85  xsec = AllInfo.Xsec
86  if not is_number(xsec):
87  log.error("Xsec is NAN (%s) for DSID %s", str(xsec), DSID)
88  continue
89  else:
90  xsec = float(xsec)*1000.
91  filterEff = AllInfo.filterEff
92  if not is_number(filterEff):
93  log.error("filterEff is NAN (%s) for DSID %s", str(filterEff), DSID)
94  continue
95  kFactor = AllInfo.kFactor
96  if not is_number(kFactor):
97  log.error("kFactor is NAN (%s) for DSID %s", str(kFactor), DSID)
98  continue
99  dataset = AllInfo.dataset
100  higherOrderXsec = AllInfo.higherOrderXsec
101  higherOrderXsecSample = str(float(xsec)*float(kFactor)*float(filterEff))
102 
103  # Check AODs for various subcampaigns here
104  nEv_mc15 = 0.
105  nEv_mc16a = 0.
106  nEv_mc16c = 0.
107  nEv_mc16d = 0.
108  nEv_mc16e = 0.
109 
110  if os.path.isfile(mainFolder+"/AllSampleInfo_mc15_13TeV/SummaryInfo/ListAOD_mc15_13TeV..txt"):
111  arrayAOD_mc15 = SimpleFunctions.GetArraysFromTxtWithString(mainFolder+"/AllSampleInfo_mc15_13TeV/SummaryInfo/ListAOD_mc15_13TeV..txt", DSID)
112  for iAOD in arrayAOD_mc15:
113  if "r7725" in iAOD[0] or "r7772" in iAOD[0] or "a818" in iAOD[0] or "a821" in iAOD[0]:
114  nEv_mc15 = nEv_mc15 + float(iAOD[1])
115 
116  if os.path.isfile(mainFolder+"/AllSampleInfo_mc16_13TeV/SummaryInfo/ListAOD_mc16_13TeV..txt"):
117  arrayAOD_mc16 = SimpleFunctions.GetArraysFromTxtWithString(mainFolder+"/AllSampleInfo_mc16_13TeV/SummaryInfo/ListAOD_mc16_13TeV..txt", DSID)
118  for iAOD in arrayAOD_mc16:
119  if "r9294" in iAOD[0] or "r9315" in iAOD[0]:
120  nEv_mc16a = nEv_mc16a + float(iAOD[1])
121  if "r9778" in iAOD[0] or "r10009" in iAOD[0]:
122  nEv_mc16c = nEv_mc16c + float(iAOD[1])
123  if "r10210" in iAOD[0]:
124  nEv_mc16d = nEv_mc16d + float(iAOD[1])
125  if "r999999999999" in iAOD[0]: # Ask Josh which tag to put here
126  nEv_mc16e = nEv_mc16e + float(iAOD[1])
127 
128  SampleLumi = "Undef"
129  datasetSplit = dataset.split(".evgen")
130  datasetLabel = dataset
131 
132  entry = ["", "", "", "", "", "", "", "", "", ""]
133 
134  if float(higherOrderXsecSample) > 0.:
135  if nEv_mc16a > 0.:
136  SampleLumi = round(float(nEv_mc16a)/(float(higherOrderXsecSample))/1000.0, 2)
137  elif nEv_mc15 > 0.:
138  SampleLumi = round(float(nEv_mc15)/(float(higherOrderXsecSample))/1000.0, 2)
139  else:
140  SampleLumi = 0.
141 
142  tableEntry = [DSID, datasetLabel, str(float(xsec)), str(float(filterEff)), higherOrderXsec, str(float(kFactor)), SortingTag, str(SampleLumi), str(nEv_mc15), str(nEv_mc16a), str(nEv_mc16c), str(nEv_mc16d), str(nEv_mc16e), higherOrderXsecSample]
143 
144  allXsec.append(tableEntry)
145 
146 for Process in allProcesses:
147  OutputFile = xsecFolder+"/list_Xsec_"+Process+".txt"
148  f = open(OutputFile, "w")
149  f.write("| DSID | Sample | Events (mc15) | Events (mc16a) | Events (mc16c) | Events (mc16d) | Events (mc16e) | Sample Lumi (mc15/16a only) [fb-1] | Cross section [pb] | FiltEff | Total higher order xsec [pb] | K-factor | Higher order xsec sample [pb] | \n")
150 
151  for entry in allXsec:
152 
153  if Process == entry[6]:
154 
155  finalXsec = str(float(entry[2])*float(entry[3])*float(entry[5]))
156 
157  f.write("| "+entry[0]+" | "+entry[1]+" | "+entry[8]+" | "+entry[9]+" | "+entry[10]+" | "+entry[11]+" | "+entry[12]+" | "+ entry[7] +" | "+entry[2]+" | " +str(round(float(entry[3]), 6))+" | " +entry[4]+" | "+str(round(float(entry[5]), 4))+" | " +str(round(float(entry[13]), 9))+ " | \n")
158 
159  f.close()
160 
161  OutputFile = xsecFolder+"/list_Xsec_"+Process+"_Download.txt"
162 
163  f = open(OutputFile, "w")
164  f.write("# DSID \t Sample \t Xsec \t FiltEff \t Total higher order xsec \t K-factor \t Higher order xsec sample \n")
165 
166  for entry in allXsec:
167  if Process == entry[6]:
168 
169  finalXsec = str(float(entry[2])*float(entry[3])*float(entry[5]))
170 
171  if len(entry[4]) != 1:
172  f.write(entry[0]+" \t "+entry[1]+" \t "+entry[2] +" \t " +entry[3]+" \t " +entry[4]+" \t "+entry[5]+" \t " +entry[9]+ " \n")
173  else:
174  if "Undef" in entry[9]:
175  f.write(entry[0]+" \t "+entry[1]+" \t "+entry[2] +" \t " +entry[3]+" \t " +entry[4]+" \t Undef. \t " +finalXsec+ " \n")
176  else:
177  f.write(entry[0]+" \t "+entry[1]+" \t "+entry[2] +" \t " +entry[3]+" \t " +entry[4]+" \t Undef. \t " +entry[9]+ " \n")
178  f.close()
179 
180 OutputFile = xsecFolder+"/list_Xsec_TopDataPrep.txt"
181 f = open(OutputFile, "w")
182 f.write("# DSID \t \t AMIXsec * FiltEff \t K-factor \n")
MuonGM::round
float round(const float toRound, const unsigned int decimals)
Definition: Mdt.cxx:27
GetAllXsec.is_number
def is_number(s)
Definition: GetAllXsec.py:21
DerivationFramework::TriggerMatchingUtils::sorted
std::vector< typename T::value_type > sorted(T begin, T end)
Helper function to create a sorted vector from an unsorted one.
Trk::open
@ open
Definition: BinningType.h:40
str
Definition: BTagTrackIpAccessor.cxx:11
readCCLHist.float
float
Definition: readCCLHist.py:83