Event.cxx
Go to the documentation of this file.
1 /*
2  *\file Event.cxx
3  * \brief FNEX 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 
21 #include "FNEX/core/Event.h"
23 
24 namespace fnex
25 {
26  //----------------------------------------------------------------------------
27  std::ostream& operator<< (std::ostream& o, fnex::Event const& event)
28  {
29  o << "Event Variables are: \n";
30 
31  if(event.dataVals.get() != nullptr) o << *(event.dataVals);
32  if(event.mcVals.get() != nullptr) o << *(event.mcVals );
33  if(event.eventId.get() != nullptr) o << *(event.eventId );
34 
35  o << std::endl;
36 
37  return o;
38  }
39 
40  //----------------------------------------------------------------------------
41  void EventList::operator+(const EventList& otherList)
42  {
43  throw std::logic_error("Forgot to implement EventList::operator+");
44 
45  // first check that the metadata is consistent.
46  // then:
47  // * add events to the end of my list
48  // * add the POT counts together
49  }
50 
51  //----------------------------------------------------------------------------
52  void EventList::RemoveEvent(std::shared_ptr<Event> ev)
53  {
54  auto it = std::find(this->fEvents.begin(), this->fEvents.end(), ev);
55  if (it != this->fEvents.end())
56  this->fEvents.erase(it);
57  }
58 
59  //----------------------------------------------------------------------------
60  EventContainer EventList::Slice(const std::vector<std::size_t> & indices)
61  {
62  EventContainer ec;
63  for (const auto i : indices)
64  ec.push_back(this->EventAlias(i));
65  return ec;
66  }
67 
68  //----------------------------------------------------------------------------
70  size_t const& pos)
71  {
72  if(pos < fEvents.size() ) fEvents[pos] = ev;
73  else
74  throw cet::exception("EventList")
75  << "attempting to add EventPtr to unavailable postion: "
76  << pos;
77 
78  return;
79  }
80 
81  //----------------------------------------------------------------------------
82  void SerializeEvents(TTree *eventTree,
83  fnex::MetaData const& md,
84  std::vector<fnex::EventInfo> const& events)
85  {
86 
87  fnex::DataVars dataVars;
88  fnex::TruthVars truthVars;
89  fnex::WeightVars weightVars;
90  std::vector<fnex::GENIEVar> genieVars(fnex::kCCQEPauliSupViaKF);
91  fnex::EventId eventId;
92 
93  std::string eventidst("run/I:subrun/I:event/I:cycle/I:slice/I");
94  std::string datavarst("fNue_CVN/F:fNuE_NumMichel/F:fHad_RecoE/F:fLep_RecoE/F:fLep_RecoE_MCFrac/F:fRecoQ2/F:fFake_Weight/F");
95  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");
96  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");
97  // truthvrst. append("fTrueParentPT/F:fTrueParentPZ/F:fTrueParentDecay/F:fTrueParentTargetPDG/F");
98  std::string genievrst("fMinus2Sigma/F:fMinus1Sigma/F:fPlus1Sigma/F:fPlus2Sigma/F");
99 
100  eventTree->Branch("eventId" , &eventId , eventidst.c_str());
101  eventTree->Branch("dataVars", &dataVars, datavarst.c_str());
102 
103  if(md.isMC){
104  eventTree->Branch("truthVars" , &truthVars , truthvrst.c_str());
105  eventTree->Branch("weightVars", &weightVars, weightvst.c_str());
106 
107  //for(auto key = fnex::kMaNCEL; key < fnex::kOtherGENIE + 1; ++key){
108  //for(auto key = fnex::kMaCCQE; key < fnex::kSumSmallXSecJoint2017 + 1; ++key){
109  for(auto key = fnex::kMaCCQE; key < fnex::kCCQEPauliSupViaKF + 1; ++key){
110  LOG_DEBUG("Event")
111  << "For branch "
113  << ", get genieVars["
114  << key - fnex::kMaCCQE
115  << "]";
116  eventTree->Branch(fnex::KeyToVarName(key).c_str(), &genieVars[key - fnex::kMaCCQE], genievrst.c_str());
117  }
118  } // end if MC
119 
120  for(auto & itr : events){
121  eventId = itr.eventId;
122  dataVars = itr.dataVars.DataVars();
123 
124  if(md.isMC){
125  truthVars = itr.mcVars.TruthVars();
126  weightVars = itr.mcVars.WeightVars();
127 
128  // for(auto key = fnex::kMaNCEL; key < fnex::kOtherGENIE + 1; ++key)
129  //for(auto key = fnex::kMaCCQE; key < fnex::kSumSmallXSecJoint2017 + 1; ++key){
130  for(auto key = fnex::kMaCCQE; key < fnex::kCCQEPauliSupViaKF + 1; ++key){
131  LOG_DEBUG("Event")
132  << "For branch "
134  << ", get genieVars["
135  << key - fnex::kMaCCQE
136  << "]";
137  genieVars[key - fnex::kMaCCQE] = itr.mcVars.GENIEVars().GENIEVarByKey(key);
138  }
139  }
140 
141  eventTree->Fill();
142  }
143 
144  return;
145  }
146 
147  //----------------------------------------------------------------------------
148  void SerializeEvents(TTree *eventTree,
149  fnex::MetaData const& md,
150  fnex::EventList const& events)
151  {
152  // Turn the event list into a vector of EventInfo structs
153  std::vector<fnex::EventInfo> evInfo;
154 
155  for(auto itr : events){
156  if(md.isMC)
157  evInfo.emplace_back(*(itr->eventId),
158  *(itr->dataVals),
159  *(itr->mcVals));
160  else
161  evInfo.emplace_back(*(itr->eventId),
162  *(itr->dataVals));
163 
164  }
165 
166  // call the other version of SerializeEvents that does all the work
167  fnex::SerializeEvents(eventTree,
168  md,
169  evInfo);
170 
171  return;
172  }
173 
174  //----------------------------------------------------------------------------
175  // method to write out a file with the capped event lists to speed up processing
176  void SerializeEventListMap(std::string const& dirName,
177  fnex::EventListMap const& listMap)
178  {
179 
180  // make a directory in the output file, and the act of making it puts us
181  // into that directory of the file
183  auto dir = tfs->mkdir(dirName.c_str());
184 
187 
188  // first the metadata tree
189  // leave this pointer dangling because the fileService created & manages it
190  TTree *metadataTree = dir.make<TTree>( "metadata", "Event list metadata" );
191  metadataTree->Branch("metadata", &md);
192  metadataTree->Branch("spillsummary", &ss);
193 
194  for(auto itr : listMap){
195 
196  md = itr.first;
197  ss = itr.second.ListSpillSummary();
198 
199  LOG_DEBUG("Event")
200  << "Filling metadata tree for "
201  << md.ToString()
202  << " branch variable "
203  << ss
204  << " map spill summary "
205  << itr.second.ListSpillSummary();
206 
207  metadataTree->Fill();
208 
209  // same principle as above for the event tree
210  TTree *eventTree = dir.make<TTree>(md.ToString().c_str(), "FNEX event records");
211 
212  fnex::SerializeEvents(eventTree,
213  md,
214  itr.second);
215 
216  } // end loop over the lists
217 
218  return;
219  }
220 
221 } // namespace fnex
#define LOG_DEBUG(stream)
Definition: Messenger.h:149
set< int >::iterator it
EventPtr EventAlias(std::size_t idx)
Get a (shared_ptr) copy of the pointer to a particular event.
Definition: Event.h:137
void SerializeEventListMap(std::string const &dirName, fnex::EventListMap const &listMap)
Definition: Event.cxx:176
void SerializeEvents(TTree *eventTree, fnex::MetaData const &md, std::vector< fnex::EventInfo > const &events)
Definition: Event.cxx:82
TFileDirectory mkdir(std::string const &dir, std::string const &descr="")
std::unique_ptr< EventId > eventId
Definition: Event.h:34
Float_t ss
Definition: plot.C:24
::xsd::cxx::tree::exception< char > exception
Definition: Database.h:225
std::map< fnex::MetaData, fnex::EventList > EventListMap
Definition: Event.h:186
Create a list of fnex::Events to be used in fits.
fnex::EventContainer fEvents
Definition: Event.h:143
std::string const ToString() const
Definition: Structs.cxx:114
void operator+(const EventList &otherList)
operator+() is the means for concatenating EventLists
Definition: Event.cxx:41
static const unsigned char kMaCCQE
Definition: VarVals.h:70
static const unsigned char kCCQEPauliSupViaKF
Definition: VarVals.h:81
EventContainer Slice(const std::vector< std::size_t > &indices)
Gives back a new EventContainer containing aliases to the events with the given indices in my EventCo...
Definition: Event.cxx:60
std::ostream & operator<<(std::ostream &o, fnex::Event const &event)
Definition: Event.cxx:27
void AddEventAt(EventPtr ev, size_t const &pos)
Definition: Event.cxx:69
std::vector< EventPtr > EventContainer
Definition: Event.h:51
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
TDirectory * dir
Definition: macro.C:5
std::shared_ptr< Event > EventPtr
Definition: Event.h:50
void RemoveEvent(std::size_t idx)
Definition: Event.h:118
std::string KeyToVarName(unsigned char const &key)
Definition: VarVals.h:445
std::unique_ptr< MCVarVals > mcVals
Definition: Event.h:36
void events(int which)
Definition: Cana.C:52
std::unique_ptr< DataVarVals > dataVals
Definition: Event.h:35