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  // This function essentially does what std::find does, ie loop over all the
42  // entries in the collection to find the desired event list, but does not
43  // cause a compiler complaint about EventLists not being MetaData
44  cmf::EventListColl::iterator FindEventList(cmf::MetaData const& md,
45  cmf::EventListColl & eventListColl)
46  {
47  for(auto itr = eventListColl.begin(); itr < eventListColl.end(); ++itr){
48  if( itr->ListMetaData() == md) return itr;
49  }
50 
51  return eventListColl.end();
52  }
53 
54  //----------------------------------------------------------------------------
55  void SerializeEvents(TTree *eventTree,
56  cmf::MetaData const& md,
57  cmf::EventList const& events)
58  {
59 
60  cmf::DataVars dataVars;
61  cmf::TruthVars truthVars;
62  cmf::WeightVars weightVars;
63  cmf::EventId eventId;
64 
65  std::string eventidst("run/I:subrun/I:event/I:cycle/I:slice/I");
66  std::string datavarst("fNue_CVN/F:fNuE_NumMichel/F:fHad_RecoE/F:fLep_RecoE/F:fLep_RecoE_MCFrac/F:fRecoQ2/F:fFake_Weight/F");
67  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:fXSecCVPPFX2020_Weight/F:fPPFXFluxCV_Weight/F");
68  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");
69 
70  eventTree->Branch("eventId" , &eventId , eventidst.c_str());
71  eventTree->Branch("dataVars", &dataVars, datavarst.c_str());
72 
73  std::vector<unsigned char> systVarKeys;
74  std::vector<float> systVarMinus2Wgts;
75  std::vector<float> systVarMinus1Wgts;
76  std::vector<float> systVarPlus1Wgts;
77  std::vector<float> systVarPlus2Wgts;
78  if(md.isMC){
79  eventTree->Branch("truthVars" , &truthVars , truthvrst.c_str());
80  eventTree->Branch("weightVars", &weightVars, weightvst.c_str());
81  eventTree->Branch("systVarKeys", &systVarKeys );
82  eventTree->Branch("systVarMinus2Wgts", &systVarMinus2Wgts );
83  eventTree->Branch("systVarMinus1Wgts", &systVarMinus1Wgts );
84  eventTree->Branch("systVarPlus1Wgts", &systVarPlus1Wgts );
85  eventTree->Branch("systVarPlus2Wgts", &systVarPlus2Wgts );
86  } // end if MC
87 
88  for(auto & itr : events){
89 
90  eventId = itr->EventID();
91  dataVars = itr->DataVals().DataVars();
92 
93  if(md.isMC){
94  truthVars = itr->MCVals().TruthVars();
95  weightVars = itr->MCVals().WeightVars();
96  systVarKeys .clear();
97  systVarMinus2Wgts.clear();
98  systVarMinus1Wgts.clear();
99  systVarPlus1Wgts .clear();
100  systVarPlus2Wgts .clear();
101  for(auto const& systItr : itr->MCVals().SystematicShifts()){
102  systVarKeys.emplace_back(systItr.Type());
103  systVarMinus2Wgts.emplace_back(systItr.SigmaWeight(cmf::kMinus2Sigma));
104  systVarMinus1Wgts.emplace_back(systItr.SigmaWeight(cmf::kMinus1Sigma));
105  systVarPlus1Wgts .emplace_back(systItr.SigmaWeight(cmf::kPlus1Sigma));
106  systVarPlus2Wgts .emplace_back(systItr.SigmaWeight(cmf::kPlus2Sigma));
107  }
108  }
109 
110  eventTree->Fill();
111  }
112  }
113 
114 
115  //----------------------------------------------------------------------------
116  // method to write out a file with the capped event lists to speed up processing
118  cmf::EventListColl const& listColl)
119  {
120 
121  // make a directory in the output file, and the act of making it puts us
122  // into that directory of the file
124  auto dir = tfs->mkdir(dirName.c_str());
125 
128 
129  // first the metadata tree
130  // leave this pointer dangling because the fileService created & manages it
131  TTree *metadataTree = dir.make<TTree>( "metadata", "Event list metadata" );
132  metadataTree->Branch("metadata", &md);
133  metadataTree->Branch("spillsummary", &ss);
134 
135  for(auto const& itr : listColl){
136 
137  md = itr.ListMetaData();
138  ss = itr.ListSpillSummary();
139 
140  metadataTree->Fill();
141 
142  // same principle as above for the event tree
143  TTree *eventTree = dir.make<TTree>(md.ToString().c_str(), "CovarianceMatrixFit event records");
144 
145  cmf::SerializeEvents(eventTree,
146  md,
147  itr);
148 
149  } // end loop over the lists
150  }
151 
152 } // namespace cmf
MCVarVals const & MCVals() const
Definition: Event.h:80
bool EventIDFilled() const
Definition: Event.h:71
bool MCValsFilled() const
Definition: Event.h:73
TFileDirectory mkdir(std::string const &dir, std::string const &descr="")
Float_t ss
Definition: plot.C:24
cmf::EventListColl::iterator FindEventList(cmf::MetaData const &md, cmf::EventListColl &eventListColl)
Definition: Event.cxx:44
std::ostream & operator<<(std::ostream &o, cmf::Event const &event)
Definition: Event.cxx:27
EventId const & EventID() const
Definition: Event.h:78
void SerializeEvents(TTree *eventTree, cmf::MetaData const &md, cmf::EventList const &events)
Definition: Event.cxx:55
DataVarVals const & DataVals() const
Definition: Event.h:79
bool DataValsFilled() const
Definition: Event.h:72
void SerializeEventListColl(std::string const &dirName, cmf::EventListColl const &listColl)
Definition: Event.cxx:117
std::string ToString() const
Definition: Structs.cxx:135
Module to combine a set of results into a single file currently only does one data product type at a ...
Definition: Event.cxx:24
std::vector< cmf::EventList > EventListColl
Definition: Event.h:147
std::string dirName
Definition: PlotSpectra.h:47
TDirectory * dir
Definition: macro.C:5
void events(int which)
Definition: Cana.C:52
enum BeamMode string