CMS 3D CMS Logo

CentralityTableProducer.cc
Go to the documentation of this file.
1 
2 // system include files
3 #include <iostream>
4 #include <fstream>
5 #include <vector>
6 #include <string>
7 
8 // user include files
11 
17 
20 
24 
26 
28 
32 
33 #include <TFile.h>
34 
35 using namespace std;
36 //
37 // class decleration
38 //
39 
41 public:
43  ~CentralityTableProducer() override;
44 
45 private:
46  void analyze(const edm::Event&, const edm::EventSetup&) override;
47  void endJob() override;
48  void printBin(const CentralityTable::CBin*);
49  // ----------member data ---------------------------
50 
54  bool debug_;
55 
57  TFile* inputTFile_;
60 
61  string rootTag_;
62  ofstream text_;
63 
66 
67  unsigned int runnum_;
68 };
69 
70 //
71 // constants, enums and typedefs
72 //
73 
74 //
75 // static data member definitions
76 //
77 
78 //
79 // constructors and destructor
80 //
81 CentralityTableProducer::CentralityTableProducer(const edm::ParameterSet& iConfig) : text_("bins.txt"), runnum_(0) {
82  //now do what ever initialization is needed
83  makeDBFromTFile_ = iConfig.getUntrackedParameter<bool>("makeDBFromTFile", true);
84  makeTFileFromDB_ = iConfig.getUntrackedParameter<bool>("makeTFileFromDB", false);
85  firstRunOnly_ = iConfig.getUntrackedParameter<bool>("isMC", false);
86  debug_ = iConfig.getUntrackedParameter<bool>("debug", false);
87  if (makeDBFromTFile_) {
88  inputTFileName_ = iConfig.getParameter<string>("inputTFile");
89  rootTag_ = iConfig.getParameter<string>("rootTag");
90  inputTFile_ = new TFile(inputTFileName_.data(), "read");
91  cout << inputTFileName_.data() << endl;
92  }
93 }
94 
96  // do anything here that needs to be done at desctruction time
97  // (e.g. close files, deallocate resources etc.)
98 }
99 
100 //
101 // member functions
102 //
103 
104 // ------------ method called to for each event ------------
106  if (!makeTFileFromDB_)
107  return;
108  if ((!firstRunOnly_ && runnum_ != iEvent.id().run()) || (firstRunOnly_ && runnum_ == 0)) {
109  runnum_ = iEvent.id().run();
110  cout << "Adding table for run : " << runnum_ << endl;
111  TFileDirectory subDir = fs->mkdir(Form("run%d", runnum_));
112  CB = subDir.make<CentralityBins>();
113  }
114 }
115 
116 // ------------ method called once each job just after ending the event loop ------------
118  if (makeDBFromTFile_) {
119  runnum_ = 1;
120  // Get values from root file
121  CB = (CentralityBins*)inputTFile_->Get(Form("%s/run%d", rootTag_.data(), runnum_));
122  cout << rootTag_.data() << endl;
123  CT = new CentralityTable();
124  CT->m_table.reserve(CB->getNbins());
125 
126  text_ << "# BinEdge NpartMean NpartVar NcollMean NcollVar NhardMean NhardVar bMean bVar" << endl;
127  for (int j = 0; j < CB->getNbins(); j++) {
129  thisBin->bin_edge = CB->lowEdgeOfBin(j);
130  thisBin->n_part.mean = CB->NpartMeanOfBin(j);
131  thisBin->n_part.var = CB->NpartSigmaOfBin(j);
132  thisBin->n_coll.mean = CB->NcollMeanOfBin(j);
133  thisBin->n_coll.var = CB->NcollSigmaOfBin(j);
134  thisBin->n_hard.mean = CB->NhardMeanOfBin(j);
135  thisBin->n_hard.var = CB->NhardSigmaOfBin(j);
136  thisBin->b.mean = CB->bMeanOfBin(j);
137  thisBin->b.var = CB->bSigmaOfBin(j);
138  printBin(thisBin);
139  CT->m_table.push_back(*thisBin);
140  if (thisBin)
141  delete thisBin;
142  }
143 
145  if (pool.isAvailable()) {
146  if (pool->isNewTagRequest("HeavyIonRcd")) {
147  pool->createNewIOV<CentralityTable>(CT, pool->beginOfTime(), pool->endOfTime(), "HeavyIonRcd");
148  } else {
149  pool->appendSinceTime<CentralityTable>(CT, pool->currentTime(), "HeavyIonRcd");
150  }
151  }
152  }
153 }
154 
156  cout << "HF Cut = " << thisBin->bin_edge << endl;
157  cout << "Npart = " << thisBin->n_part.mean << endl;
158  cout << "sigma = " << thisBin->n_part.var << endl;
159  cout << "Ncoll = " << thisBin->n_coll.mean << endl;
160  cout << "sigma = " << thisBin->n_coll.var << endl;
161  cout << "B = " << thisBin->b.mean << endl;
162  cout << "sigma = " << thisBin->b.var << endl;
163  text_ << Form("%0.2f\t%0.2f\t%0.2f\t%0.2f\t%0.2f\t%0.2f\t%0.2f\t%0.2f\t%0.2f",
164  (Float_t)thisBin->bin_edge,
165  (Float_t)thisBin->n_part.mean,
166  (Float_t)thisBin->n_part.var,
167  (Float_t)thisBin->n_coll.mean,
168  (Float_t)thisBin->n_coll.var,
169  (Float_t)thisBin->n_hard.mean,
170  (Float_t)thisBin->n_hard.var,
171  (Float_t)thisBin->b.mean,
172  (Float_t)thisBin->b.var)
173  << endl;
174  cout << "__________________________________________________" << endl;
175 }
176 
177 //define this as a plug-in
RunNumber_t run() const
Definition: EventID.h:38
T getParameter(std::string const &) const
void printBin(const CentralityTable::CBin *)
T getUntrackedParameter(std::string const &, T const &) const
std::vector< CBin > m_table
float NpartSigmaOfBin(int bin) const
void analyze(const edm::Event &, const edm::EventSetup &) override
float lowEdgeOfBin(int bin) const
example_stream void analyze(const edm::Event &, const edm::EventSetup &) override
void appendSinceTime(T *payloadObj, cond::Time_t sinceTime, const std::string &recordName, bool withlogging=false)
int iEvent
Definition: GenABIO.cc:224
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
float NcollMeanOfBin(int bin) const
bool isNewTagRequest(const std::string &recordName)
bool isAvailable() const
Definition: Service.h:40
float bSigmaOfBin(int bin) const
float NhardMeanOfBin(int bin) const
T * make(const Args &...args) const
make new ROOT object
void createNewIOV(T *firstPayloadObj, cond::Time_t firstSinceTime, cond::Time_t firstTillTime, const std::string &recordName, bool withlogging=false)
TFileDirectory mkdir(const std::string &dir, const std::string &descr="")
create a new subdirectory
Definition: TFileService.h:69
float NpartMeanOfBin(int bin) const
int getNbins() const
edm::ESHandle< CentralityTable > inputDB_
edm::Service< TFileService > fs
edm::EventID id() const
Definition: EventBase.h:59
float bMeanOfBin(int bin) const
CentralityTableProducer(const edm::ParameterSet &)
float NhardSigmaOfBin(int bin) const
float NcollSigmaOfBin(int bin) const