(*******************************************************************
This file was generated automatically by the Mathematica front end.
It contains Initialization cells from a Notebook file, which
typically will have the same name as this file except ending in
".nb" instead of ".m".

This file is intended to be loaded into the Mathematica kernel using
the package loading commands Get or Needs.  Doing so is equivalent
to using the Evaluate Initialization Cells menu command in the front
end.

DO NOT EDIT THIS FILE.  This entire file is regenerated
automatically each time the parent Notebook file is saved in the
Mathematica front end.  Any changes you make to this file will be
overwritten.
***********************************************************************)









Off[General::spell1];
Off[General::spell1];



<<Graphics`Graphics`
<<Graphics`MultipleListPlot`
<<Statistics`DataManipulation`
<<DiscreteMath`Combinatorica`



If[$VersionNumber<5.1,Except[p1_,p2_]:=(p2?(Not[MatchQ[#,p1]]&));
  Except[p1_]:=_?(Not[MatchQ[#,p1]]&);]





SplitData[rawObjList_]:=
    Split[Drop[rawObjList,If[NumberQ[rawObjList[[1,1]]],0,1]],
      Less[First[#1],First[#2]]&];



$PTCUT=25;
$PTCUTJET=50;
$ETACUT=2.0;
$ETACUTJET=3.0;
$MISSINGPTCUT=50;
$TOTALPTCUT=0;

MakeMasterCuts[]:=(
    If[!ValueQ[ETACUT],ETACUT=$ETACUT];
    If[!ValueQ[PTCUT],PTCUT=$PTCUT];
    If[!ValueQ[ETACUTJET],ETACUTJET=$ETACUTJET];
    If[!ValueQ[PTCUTJET],PTCUTJET=$PTCUTJET];
    If[!ValueQ[MISSINGPTCUT],MISSINGPTCUT=$MISSINGPTCUT];
    If[!ValueQ[TOTALPTCUT],TOTALPTCUT=$TOTALPTCUT];
    
    goodPhoton={_,0,_?(Abs[#]<ETACUT&),_,_?(#>PTCUT&),_,_,_};
        goodElectron={_,1,_?(Abs[#]<ETACUT&),_,_?(#>PTCUT&),_,_,_};
        goodMuon={_,2,_?(Abs[#]<ETACUT&),_,_?(#>PTCUT&),_,_,_}; 
        goodTau={_,3,_?(Abs[#]<ETACUT&),_,_?(#>PTCUT&),_,_,_};
        goodJet={_,4,_?(Abs[#]<ETACUTJET&),_,_?(#>PTCUTJET&),_,_,_};
        goodMissingPT={_,6,_,_,_?(#>MISSINGPTCUT&),_,_,_};
        goodParticle  = (goodPhoton | goodElectron | goodMuon | goodTau | 
          goodJet | goodMissingPT);
    )

ApplyMasterCuts[splitData_]:= (MakeMasterCuts[];
      DeleteCases[
        DeleteCases[splitData,
          Except[goodParticle],{2}] ,{}|_?(EffMass[#]<TOTALPTCUT&)]);



FCheck[fname_]:=
  If[MatchQ[FileType[fname],File],fname,Experimental`FileBrowse[]]
GetFromFile[fname_]:=ApplyMasterCuts[SplitData[Import[fname,"Table"]]];
GetFromFile[$Failed]:=Null;
LoadEvents[fname_]:=GetFromFile[FCheck[fname]];
LoadEvents[fname_,nfiles_]:=Module[{fhead},
      fhead=FCheck[fname];
      fhead=StringReplace[fhead,".dat.0"\[Rule]".dat"];
      Flatten[
        Join[Table[GetFromFile[fhead<>"."<>ToString[i]],{i,0,nfiles-1}]],1]];



oElectron = oE={_, 1,_, _, _, _, _, _};
oElectronPlus = oEP = {_, 1,_, _, _, 1.0, _, _} ;
oElectronMinus = oEM = {_, 1,_, _, _, -1.0, _, _} ;

oMuon = o\[Mu]={_, 2,_, _, _, _, _, _};
oMuonPlus = o\[Mu]P={_, 2,_, _, _, 1.0, _, _} ;
oMuonMinus = o\[Mu]M={_, 2,_, _, _, -1.0, _, _} ;

oTau = o\[Tau]={_, 3,_, _, _, _, _, _};
oTauPlus = o\[Tau]P={_, 3,_, _, _, 1.0, _, _} ;
oTauMinus = o\[Tau]M={_, 3,_, _, _, -1.0, _, _} ;

oLepton  = oL=(oElectron | oMuon | oTau);
oLeptonPlus  = oLP=(oElectronPlus | oMuonPlus | oTauPlus);
oLeptonMinus = oLM=(oElectronMinus | oMuonMinus | oTauMinus);

oLeptonType[-3] =  oTauMinus;
oLeptonType[-2] =  oMuonMinus;
oLeptonType[-1] =  oElectronMinus;
oLeptonType[0] =   object_ /; False;
oLeptonType[1] =  oElectronPlus;
oLeptonType[2] =  oMuonPlus;
oLeptonType[3] =  oTauPlus;

oPhoton = o\[Gamma]={_, 0,_, _, _, _, _, _};
oJet = {_, 4,_, _, _, _, _, _};
oBJet =oB= {_, 4,_, _, _, _, _, 1.0};
oNonBJet = oNonB={_, 4,_, _, _, _, _, 0.0};
oMissingPT = oMPT= {_, 6,_, _, _, _, _, _};

oAny = {_,_,_,_,_,_,_,_};

oNot[oType_]:=Except[oType,oAny];



oHard[oType_,pthard_] =oType?((ptOf[ #]\[GreaterEqual]pthard)&);
oLessHard[oType_,pthard_] =oType?((ptOf[#]<pthard)&);







Q[patt_][obj_]:=MatchQ[obj,patt];





\!\(etaOf[{_, \ _, eta_, \ phi_, \ pt_, \ ___}] := \ eta\[IndentingNewLine]
  phiOf[{_, \ _, eta_, \ phi_, \ pt_, \ ___}] := \ phi\[IndentingNewLine]
  ptOf[{_, \ _, eta_, \ phi_, \ pt_, \ ___}] := \ pt\[IndentingNewLine]
  etaOf[{a_}] := \ etaOf[a]\[IndentingNewLine]
  phiOf[{a_}] := \ phiOf[a]\[IndentingNewLine]
  \(\(ptOf[{a_}] := \ ptOf[a]\)\(\[IndentingNewLine]\)
  \)\[IndentingNewLine]
  \(FourVectorFrom[{_, \ _, eta_, \ phi_, \ pt_, \ ___}]\  := \ {pt\ Cosh[eta], \ pt\ Sinh[eta], \ pt\ Cos[phi], pt\ Sin[phi]\ };\)\[IndentingNewLine]
  \(FourVectorFrom[{_, 4, eta_, \ phi_, \ pt_, \ jetM_, ___}]\  := \ {\@\(pt\^2\ Cosh[eta]\^2 + jetM\^2\), \ pt\ Sinh[eta], \ pt\ Cos[phi], pt\ Sin[phi]\ };\)\[IndentingNewLine]
  \(FourLength[{pe_, pz_, px_, py_}]\  := \ Sqrt[Max[pe^2\  - \ pz^2\  - \ px^2\  - \ py^2, 0.0]];\)\)







PTSort[objList_]:=Sort[objList,Greater[ptOf[#1],ptOf[#2]]&]



FSort[func_][objList_]:=Sort[objList,Greater[func[#1],func[#2]]&]



\!\(\(InvMass[objList_]\  := \ FourLength[Apply[Plus, Map[FourVectorFrom, objList]]];\)\n
  \(EffMass[objList_]\ \  := \ Apply[Plus, \ Map[ptOf, \ objList]];\)\[IndentingNewLine]
  \(MissingPT[objList_] := If[\((NumOf[oMissingPT]~eq~1)\)[objList], ptOf[\(HGet[oMissingPT, 1]\)[objList]], 0];\)\[IndentingNewLine]
  \(DeltaR[{obj1_, obj2_}] := \@\(\((etaOf[obj1] - etaOf[obj2])\)\^2 + \((phiOf[obj1] - phiOf[obj2])\)\^2\);\)\[IndentingNewLine]
  \(DeltaR[{a_}] := DeltaR[a];\)\[IndentingNewLine]
  \(InvMass[{}] = 0;\)\[IndentingNewLine]
  \(EffMass[{}] = 0;\)\)



NumOf[objType_][objList_]:=Count[objList,objType];



eq[attr_,n_][objList_]:=attr[objList]\[Equal]n;
gt[attr_,n_][objList_]:=attr[objList]>n;
lt[attr_,n_][objList_]:=attr[objList]<n;
ge[attr_,n_][objList_]:=attr[objList]\[GreaterEqual]n;
le[attr_,n_][objList_]:=attr[objList]\[LessEqual]n;



ListAlternative[p_]:=Alternatives@@If[MatchQ[Head[p],PatternTest],p[[1]],p]



KTuples[k_,crit__][objList_]:=Select[KSubsets[objList,k],crit];



OrdTuples[k_,patt_][objList_]:=
    Cases[Flatten[Permutations/@KSubsets[objList,k],1],patt];





KTest[k_,crit_][objList_]:=MemberQ[KSubsets[objList,k],_?crit];

OrdTest[k_,patt_][objList_]:=
    MemberQ[Flatten[Permutations/@KSubsets[objList,k],1],patt];



PickATuple[TupleFunc_,sortf_,n_][evt_]:=Take[FSort[sortf][TupleFunc[evt]],n];
PickATuple[TupleFunc_,sortf_,{n__}][evt_]:=
    Part[FSort[sortf][TupleFunc[evt]],{n}];





all[evt_]:=True;



ShowEvent[evtList_, num_]:= 
  MatrixForm[
    Join[{{"#","id","eta","phi","pt","jmas","ntrack","btag"}},
      Identity[evtList[[num]]]]]



LeptonIndexCount[typelist_,n_][
    evt_]:=(NumOf[oLeptonType[n]]~eq~Count[typelist,n])[evt]

LeptonContent[typelist_][evt_]:=LeptonIndexCount[typelist,-3][evt]&&
      LeptonIndexCount[typelist,-2][evt]&&
      LeptonIndexCount[typelist,-1][evt]&&
      LeptonIndexCount[typelist,1][evt]&&
      LeptonIndexCount[typelist,2][evt]&&
      LeptonIndexCount[typelist,3][evt];

OSDiLepton[evt_]:=
    or[LeptonContent[{1,-1}],LeptonContent[{2,-2}],LeptonContent[{3,-3}]][
      evt];
SSDiLepton[evt_]:=
    or[LeptonContent[{1,1}],LeptonContent[{2,2}],LeptonContent[{3,3}],
        LeptonContent[{-1,-1}],LeptonContent[{-2,-2}],LeptonContent[{-3,-3}]][
      evt];





HGet[patt_,n_][evt_]:=Take[PTSort[Cases[evt,patt]],n];
HGet[patt_,{n__}][evt_]:=Part[PTSort[Cases[evt,patt]],{n}];
HGet[patt_,All][evt_]:=Cases[evt,patt];
HGet[{{patt1_,sel1_},MoreArgs___}][evt_]:=
    Join[HGet[patt1,sel1][evt],HGet[{MoreArgs}][evt]];
HGet[patt_,UpTo[n_]][evt_]:=
    If[Abs[n]<Length[Cases[evt,patt]],Take[PTSort[Cases[evt,patt]],n],
      Cases[evt,patt]];
HGet[{}][evt_]:=List[];



GetAll[evt_]:=evt;





(*NiceTake[List_,n_]=
      Take[List,If[n>0,Min[Length[List],n],-Min[Length[List],-n]]];*)

FGet[func_,patt_,n_][evt_]:=Take[FSort[func][Cases[evt,patt]],n];
FGet[func_,patt_,{n__}][evt_]:=Part[FSort[func][Cases[evt,patt]],{n}];
FGet[func_,patt_,All][evt_]:=Cases[evt,patt];
FGet[func_,patt_,UpTo[n_]][evt_]:=
    If[Abs[n]<Length[Cases[evt,patt]],Take[FSort[func][Cases[evt,patt]],n],
      Cases[evt,patt]];
FGet[{{func1_,patt1_,sel1_},MoreArgs___}][evt_]:=
    Join[FGet[func1,patt1,sel1][evt],HGet[{MoreArgs}][evt]];
FGet[{}][evt_]:=List[];





and[f_][evt_]:=f[evt];
and[f_,g__][evt_]:=f[evt]&&and[g][evt];
or[f_][evt_]:=f[evt];
or[f_,g__][evt_]:=f[evt]||or[g][evt];

join[f_][evt_]:=f[evt];
join[f_,g__][evt_]:=Join[f[evt],join[g[evt]]];





CriterionCount[evtList_,crit_]:=
    Count[evtList,_?crit];



SpeciesCount[evtList_,patt_,Nmax_]:=
    MatrixForm[
      Transpose[
        Table[{i,CriterionCount[evtList,NumOf[patt]~eq~i]},{i,0,Nmax}]]];

LEPLIST={-1,-2,-3,1,2,3};

SingleLeptonCount[evtList_]:=Module[{TMPLIST},
      TMPLIST=Select[evtList,(NumOf[oLepton]~eq~1)];
      MatrixForm[{{"e-","\[Mu]-","\[Tau]-","e+","\[Mu]+","\[Tau]+"},
          Map[CriterionCount[TMPLIST,LeptonContent[{#}]]&,LEPLIST]}]];

DiLeptonCount[evtList_]:=Module[{TMPLIST,F1,UTfnc},
      TMPLIST=Select[evtList,(NumOf[oLepton]~eq~2)];
      F1[a_]:=Abs[a]+2 Sign[a];
      UTfnc[l_]:=F1[l[[1]]]\[LessEqual]F1[l[[2]]];
      MatrixForm[
        Transpose[
          Join[{{"-","e-","\[Mu]-","\[Tau]-","e+","\[Mu]+","\[Tau]+"}},
            Transpose[
              Join[{{"e-","\[Mu]-","\[Tau]-","e+","\[Mu]+","\[Tau]+"}},
                
                Map[If[UTfnc[#],CriterionCount[TMPLIST,LeptonContent[#]],0]&,
                  Outer[List,LEPLIST,LEPLIST],{2}]]]]]]];



AdvancedCount[evtList_,crit_,objsel_,boolFunc_]:=
    Count[objsel/@Select[evtList,crit],ev_/;boolFunc[ev]];



\!\(\(\(Flatten4[List_] := Flatten[List, Max[0, Depth[List] - 4]];\)\(\[IndentingNewLine]\)
  \)\[IndentingNewLine]
  \(Freq[evtList_, crit_, objsel_, plotfunc_, {min_, max_, nbins_}] := BinCounts[plotfunc /@ Flatten4[objsel /@ Select[evtList, crit]], {min, max, \(max - min\)\/nbins}];\)\[IndentingNewLine]
  \(Hist[freqcombo__, {min_, max_, nbins_}, HistogramOptions___] := Histogram[freqcombo /. Bin[args__] \[Rule] Freq[args, {min, max, nbins}], FrequencyData \[Rule] True, HistogramCategories \[Rule] Table[i, {i, min, max, \(max - min\)\/nbins}], HistogramOptions];\)\[IndentingNewLine]
  \(MakeFreq[freqcombo__, {min_, max_, nbins_}] := freqcombo /. Bin[args__] \[Rule] Freq[args, {min, max, nbins}];\)\)





RunInclusiveCounts[evtList_]:=
  Print["\nPhotons: ",SpeciesCount[evtList,o\[Gamma],4],"   Jets: ",
    SpeciesCount[evtList,oJet,6],"   BJets: ",SpeciesCount[evtList,oBJet,4],"   Lepton: ",
    SpeciesCount[evtList,oL,4] ,"  (OSDL: ",
    CriterionCount[evtList,OSDiLepton],", SSDL: ",
    CriterionCount[evtList,SSDiLepton],")\nSingle- and Di- Lepton Counts",
    SingleLeptonCount[evtList],DiLeptonCount[evtList],"\n Single- and Di-Lepton Counts (events with no jets): ",
    SingleLeptonCount[Select[evtList,NumOf[oJet]~eq~0]],
    DiLeptonCount[Select[evtList,NumOf[oJet]~eq~0]]]





SetAttributes[fastMake,HoldFirst];
SetAttributes[NumOf, HoldFirst];

SetAttributes[SpeciesCount,HoldAll];
fastReset[]:=(
    ClearAll[fast];
    SetAttributes[fast,HoldFirst];
    fast/:LeptonContent[typelist_][fast[list_,num_]]  :=
      fast/:LeptonContent[typelist][fast[list,num]] =
        LeptonContent[typelist][list[[num]]];
    fast/:NumOf[obj_][fast[list_,num_]]:= 
      fast/:NumOf[obj][fast[list,num]] = NumOf[obj][list[[num]]];
    
    fast/: eq[attr_,n_][fast[list_,num_]] := attr[fast[list,num]]\[Equal]n;
    fast/: gt[attr_,n_][fast[list_,num_]] := attr[fast[list,num]]>n;
    fast/: lt[attr_,n_][fast[list_,num_]] := attr[fast[list,num]]<n;
    fast/: ge[attr_,n_][fast[list_,num_]] := 
      attr[fast[list,num]]\[GreaterEqual] n;
    fast/: le[attr_,n_][fast[list_,num_]] := 
      attr[fast[list,num]]\[LessEqual] n;
    
    fast /: and[f_][fast[list_,num_]]:=f[fast[list,num]];
    fast /: and[f_,g__][fast[list_,num_]]:=
      f[fast[list,num]]&&and[g][fast[list,num]];
    fast /: or[f_][fast[list_,num_]]:=f[fast[list,num]];
    fast /: or[f_,g__][fast[list_,num_]]:=
      f[fast[list,num]]||or[g][fast[list,num]];
    fast /: join[f_][fast[list_,num_]]:=f[fast[list,num]];
    fast /: join[f_,g__][fast[list_,num_]]:=
      Join[f[fast[list,num]],join[g[fast[list,num]]]];
    
    fast /: func_[fast[list_,num_]] := func[list[[num]]];
    )
fastReset[];
fastMake[list_] := Table[fast[list,i], {i,1, Length[list]}];









