SharedData.cc 3.48 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/** @file SharedData.cxx
 *  @brief Implementation of SharedData.
 *
 *  SharedData class holds data common to analysis packages.
 *  This includes histograms, trees, event counter.
 *  Analysis packages will have a pointer to this class passed from
 *  the manager.
 * 
 *  @author Yakov Kulinich
 *  @bug No known bugs.
 */
#include "SharedData.hh"
#include <iostream>

/** @brief Default Constructor for SharedData.
 */
SharedData :: SharedData ()
{
  m_eventCounter = 0;     
  m_outputFileName = "myOut.root";
21
  m_configFileName = std::getenv("JZCaPA");
aricct2's avatar
aricct2 committed
22
23
  m_configFileName.replace(m_configFileName.length()-15,15,"/JZCaPA/MonteCarlo/config/config.cfg");
 // std::cout << "* current config file path * = " << m_configFileName << std::endl;
24
 // m_configFileName = "config/config.cfg";
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
  m_fout = NULL;
  m_tree = NULL;
  m_config = NULL;

}
/** @brief Constructor for SharedData. 
 *
 *  @param1 string with output filename
 *  @param2 string with name of config file
 */
SharedData :: SharedData ( const std::string& outputFileName,
			   const std::string& configFileName )
{
  m_eventCounter = 0;
  m_outputFileName = outputFileName;
  m_configFileName = configFileName;
aricct2's avatar
aricct2 committed
41
 // std::cout << "* current config file path * = " << m_configFileName << std::endl;
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
  m_fout = NULL ;
  m_tree = NULL;
  m_config = NULL;
}

/** @brief Destructor for SharedData.
 *
 *  Cleans up an SharedData object.
 */
SharedData :: ~SharedData() 
{
  delete m_tree;
  delete m_fout;
  delete m_config;
}


/** @brief Function to add an event store.
 *
 *  Initialize TFile, Tree, TEnv (config) 
 *
 *  @return void
 */

void SharedData :: Initialize()
{
  m_fout         = new TFile( m_outputFileName.c_str(), "RECREATE" );
  m_tree         = new TTree( "tree"                  , "tree"     );

  m_config       = new TEnv ();
  int success;
  success = m_config->ReadFile( m_configFileName.c_str(), EEnvLevel(0));
aricct2's avatar
aricct2 committed
74
  std::cout << " Config File path = " <<  m_configFileName.c_str() << " Check if read successfully (0 = success) " << success << std::endl;
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
}

/** @brief Function to add an output histo.
 *
 *  @param1 Pointer to histogram
 *
 *  @return void
 */
void SharedData :: AddOutputHistogram( TH1* h )
{
  m_v_hists.push_back( h );
}
/** @brief End of event
 *
 *  Fill the tree, increment event counter 
 *
 *  @return void 
 */
void SharedData :: EndOfEvent()
{
  //std::cout << " filling tree " << std::endl;
  //  m_tree->Fill();
  //    std::cout << " filling tree 2" << std::endl;
  m_eventCounter++;
}

/** @brief Function to check if printing necessary
 *
 *  Print every event until 10, then every 10,
 *  then every 100, then every 1000, etc.
 *
 *  @return bool to print or not
 */
bool SharedData :: DoPrint() 
{
  int statSize=1;
  if( m_eventCounter != 0){
    double power=std::floor(log10(m_eventCounter));
    statSize=(int)std::pow(10.,power);
  }
  if(m_eventCounter%statSize==0) return true;
  return false;
}

/** @brief Finalize Shared Data
    
    Writes histos and closes the TFile

    @return void
*/
void SharedData :: Finalize() 
{
  m_fout->cd();
  
  m_tree->Write();
  for( auto& h : m_v_hists ) { h->Write(); }
  
  m_fout->Close();
}

/*
void SharedData :: AddDoubleToTree( const std::string& name, std::vector<double> *pObj ){
  std::cout << "Adding " << name << std::endl;
  m_tree->Branch( name.c_str(), "std::vector<double>", &pObj );
}

void SharedData :: AddIntToTree( const std::string& name, std::vector<int> *pObj ){
  std::cout << "Adding " << name << std::endl;
  m_tree->Branch( name.c_str(), "std::vector<int>", &pObj );
}
*/