Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
noiMsgTree.h
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file noiMsgTree.h
1 #ifndef noiMsgTree__h
2 #define noiMsgTree__h
3 class noiMsgTree {
4  private:
5  string b_msg;
6  TTree tree;
7  public:
8  bool echo_to_cout {false};
9  noiMsgTree(bool set_echo) :
10  b_msg{""},
11  tree{"Messages", "Tree of messages"}
12  {
13  tree.Branch("messages", &b_msg);
14  dash();
15  msg(" Start of msg tree ");
16  dash();
17  echo_to_cout = set_echo;
18  };
19  void msg(string msg) {
20  b_msg = msg;
21  if (echo_to_cout) cout << b_msg << endl;
22  tree.Fill();
23  };
24  void msg(vector<string> messages) {
25  for (auto& msg : messages) {
26  b_msg = msg;
27  if (echo_to_cout) cout << b_msg << endl;
28  tree.Fill();
29  }
30  };
31  void dash() {
32  b_msg = "---------------";
33  if (echo_to_cout) cout << b_msg << endl;
34  tree.Fill();
35  };
36  void write(){
37  tree.Write();
38  };
39  static void read_messages(const char* f_name){
40  cout << " Reading file: " << f_name << endl;
41  /* TTree *tree; */
42  TFile* fin = new TFile(f_name, "read");
43  if (!fin->IsOpen()) {
44  cout << " Input file: " << f_name << " is not open." << endl;
45  delete fin;
46  return;
47  }
48  TTreeReader myReader("Messages",fin);
49  TTreeReaderValue<string> msg(myReader, "messages");
50  cout << " Contents of TFile(\""<<f_name<<"\") TTree(\"Messages\"):" << endl;
51  while (myReader.Next()) cout << " " << *msg << endl;
52  fin->Close();
53  delete fin;
54  /* } */
55  };
56  void slurp_file(const char* which_file) {
57  // try and read all lines of which_file into the tree
58  msg(Form("--Begin contents of file \"%s\"",which_file));
59  ifstream f_in {which_file};
60  if (!f_in.is_open()) {
61  msg(Form(" error: couldn't open file \"%s\"",which_file));
62  } else {
63  string line;
64  while (getline(f_in,line)) msg(line);
65  }
66  msg(Form("--End contents of file \"%s\"",which_file));
67  return;
68  };
69 };
70 #endif
71