Event.cxx
Go to the documentation of this file.
1 /*
2  *\file Event.cxx
3  * \brief CovarianceMatrixFit event record container.
4  *
5  * Created on: \date Feb 3, 2016
6  * Original author: \author J. Wolcott <jwolcott@fnal.gov>
7  *
8  */
9 
10 #include <algorithm>
11 
12 #include "TTree.h"
13 #include "TFile.h"
14 
15 #include "cetlib_except/exception.h"
20 
23 
24 namespace cmf
25 {
26  //----------------------------------------------------------------------------
27  std::ostream& operator<< (std::ostream& o, cmf::Event const& event)
28  {
29  o << "Event Variables are: \n";
30 
31  if(event.EventIDFilled() ) o << event.EventID() ;
32  if(event.DataValsFilled()) o << event.DataVals();
33  if(event.MCValsFilled() ) o << event.MCVals() ;
34 
35  o << std::endl;
36 
37  return o;
38  }
39 
40  //----------------------------------------------------------------------------
41  void SerializeEvents(TTree *eventTree,
42  cmf::MetaData const& md,
43  cmf::EventList const& events)
44  {
45 
46  cmf::DataVars dataVars;
47  cmf::TruthVars truthVars;
48  cmf::WeightVars weightVars;
49  std::vector<cmf::GENIEVar> genieVars(cmf::kCCQEPauliSupViaKF);
50  cmf::EventId eventId;
51 
52  std::string eventidst("run/I:subrun/I:event/I:cycle/I:slice/I");
53  std::string datavarst("fNue_CVN/F:fNuE_NumMichel/F:fHad_RecoE/F:fLep_RecoE/F:fLep_RecoE_MCFrac/F:fRecoQ2/F:fFake_Weight/F");
54  std::string weightvst("fXSecCVPPFX_Weight/F:fRPACCQE_Weight/F:fRPARES_Weight/F:fDISvnCC1pi_Weight/F:fEmpiricalMEC_Weight/F:fEmpiricalMECtoGENIEQE_Weight/F:fEmpiricalMECtoGENIERES_Weight/F:fOtherDIS_Weight/F");
55  std::string truthvrst("fTrueE/F:fTruePDG/F:fTrueCCNC/F:fTrueIntType/F:fTruePDGOrig/F:fTrueParentPDG/F:fTrueNucHit/F:fTrueParentPT/F:fTrueParentPZ/F:fTrueParentDecay/F:fTrueParentTargetPDG/F:fTrueIntMode/F");
56  // truthvrst. append("fTrueParentPT/F:fTrueParentPZ/F:fTrueParentDecay/F:fTrueParentTargetPDG/F");
57  std::string genievrst("fMinus2Sigma/F:fMinus1Sigma/F:fPlus1Sigma/F:fPlus2Sigma/F");
58 
59  eventTree->Branch("eventId" , &eventId , eventidst.c_str());
60  eventTree->Branch("dataVars", &dataVars, datavarst.c_str());
61 
62  if(md.isMC){
63  eventTree->Branch("truthVars" , &truthVars , truthvrst.c_str());
64  eventTree->Branch("weightVars", &weightVars, weightvst.c_str());
65 
66  //for(auto key = cmf::kMaNCEL; key < cmf::kOtherGENIE + 1; ++key){
67  //for(auto key = cmf::kMaCCQE; key < cmf::kSumSmallXSecJoint2017 + 1; ++key){
68  for(auto key = cmf::kMaCCQE; key < cmf::kCCQEPauliSupViaKF + 1; ++key){
69  LOG_DEBUG("Event")
70  << "For branch "
72  << ", get genieVars["
73  << key - cmf::kMaCCQE
74  << "]";
75  eventTree->Branch(cmf::KeyToVarName(key).c_str(), &genieVars[key - cmf::kMaCCQE], genievrst.c_str());
76  }
77  } // end if MC
78 
79  for(auto & itr : events){
80  eventId = itr->EventID();
81  dataVars = itr->DataVals().DataVars();
82 
83  if(md.isMC){
84  truthVars = itr->MCVals().TruthVars();
85  weightVars = itr->MCVals().WeightVars();
86 
87  // for(auto key = cmf::kMaNCEL; key < cmf::kOtherGENIE + 1; ++key)
88  //for(auto key = cmf::kMaCCQE; key < cmf::kSumSmallXSecJoint2017 + 1; ++key){
89  for(auto key = cmf::kMaCCQE; key < cmf::kCCQEPauliSupViaKF + 1; ++key){
90  LOG_DEBUG("Event")
91  << "For branch "
93  << ", get genieVars["
94  << key - cmf::kMaCCQE
95  << "]";
96  genieVars[key - cmf::kMaCCQE] = itr->MCVals().GENIEVars().GENIEVarByKey(key);
97  }
98  }
99 
100  eventTree->Fill();
101  }
102  }
103 
104 
105  //----------------------------------------------------------------------------
106  // method to write out a file with the capped event lists to speed up processing
107  void SerializeEventListMap(std::string const& dirName,
108  cmf::EventListMap const& listMap)
109  {
110 
111  // make a directory in the output file, and the act of making it puts us
112  // into that directory of the file
114  auto dir = tfs->mkdir(dirName.c_str());
115 
118 
119  // first the metadata tree
120  // leave this pointer dangling because the fileService created & manages it
121  TTree *metadataTree = dir.make<TTree>( "metadata", "Event list metadata" );
122  metadataTree->Branch("metadata", &md);
123  metadataTree->Branch("spillsummary", &ss);
124 
125  for(auto const& itr : listMap){
126 
127  md = itr.first;
128  ss = itr.second.ListSpillSummary();
129 
130  metadataTree->Fill();
131 
132  // same principle as above for the event tree
133  TTree *eventTree = dir.make<TTree>(md.ToString().c_str(), "CovarianceMatrixFit event records");
134 
135  cmf::SerializeEvents(eventTree,
136  md,
137  itr.second);
138 
139  } // end loop over the lists
140  }
141 
142 } // namespace cmf
#define LOG_DEBUG(stream)
Definition: Messenger.h:149
static const unsigned char kCCQEPauliSupViaKF
Definition: VarVals.h:80
MCVarVals const & MCVals() const
Definition: Event.h:84
bool EventIDFilled() const
Definition: Event.h:75
bool MCValsFilled() const
Definition: Event.h:77
TFileDirectory mkdir(std::string const &dir, std::string const &descr="")
Float_t ss
Definition: plot.C:24
bool isMC
Definition: Structs.h:64
std::ostream & operator<<(std::ostream &o, cmf::Event const &event)
Definition: Event.cxx:27
EventId const & EventID() const
Definition: Event.h:82
void SerializeEvents(TTree *eventTree, cmf::MetaData const &md, cmf::EventList const &events)
Definition: Event.cxx:41
DataVarVals const & DataVals() const
Definition: Event.h:83
void SerializeEventListMap(std::string const &dirName, cmf::EventListMap const &listMap)
Definition: Event.cxx:107
bool DataValsFilled() const
Definition: Event.h:76
std::string ToString() const
Definition: Structs.cxx:138
Module to combine a set of results into a single file.
Definition: Event.cxx:24
std::map< cmf::MetaData, cmf::EventList > EventListMap
Definition: Event.h:148
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
TDirectory * dir
Definition: macro.C:5
std::string KeyToVarName(unsigned char const &key)
Definition: VarVals.h:354
static const unsigned char kMaCCQE
Definition: VarVals.h:69
void events(int which)
Definition: Cana.C:52