Logo coherent WaveBurst  
Library Reference Guide
Logo
cwb_fix_slag_missed.C
Go to the documentation of this file.
1 /*
2 # Copyright (C) 2019 Gabriele Vedovato
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17 
18 
19 {
20  // this fix must be used if live tree has been produced without slag leaf
21 
22  #include <vector>
23 
25 
26  TB.checkFile(gSystem->Getenv("CWB_ROOTLOGON_FILE"));
27  TB.checkFile(gSystem->Getenv("CWB_PARAMETERS_FILE"));
28  TB.checkFile(gSystem->Getenv("CWB_UPARAMETERS_FILE"));
29 
31  if(gSystem->Getenv("CWB_MERGE_LABEL")==NULL) {
32  cout << "Error : environment CWB_MERGE_LABEL is not defined!!!" << endl;exit(1);
33  } else {
34  cwb_merge_label=TString(gSystem->Getenv("CWB_MERGE_LABEL"));
35  }
36 
37  char net_file_name[256];
38  char liv_file_name[256];
39 
40  if(simulation) {
41  cout << "Error : cwb_fix_slag_missed.C must be applied only to production files !!!" << endl;exit(1);
42  } else {
43  sprintf(net_file_name,"%s/wave_%s.%s.root",merge_dir,data_label,cwb_merge_label.Data());
44  sprintf(liv_file_name,"%s/live_%s.%s.root",merge_dir,data_label,cwb_merge_label.Data());
45  cout << net_file_name << endl;
46  cout << liv_file_name << endl;
47 
48  // Check if files exist
49  TB.checkFile(net_file_name);
50  TB.checkFile(liv_file_name);
51  }
52 
53  // open wave tree
54  TFile *fwave = TFile::Open(net_file_name);
55  if(fwave==NULL) {cout << "Error opening file " << net_file_name << endl;exit(1);}
56  TTree* twave = (TTree *) gROOT->FindObject("waveburst");
57  if(twave==NULL) {cout << "Error opening tree wave" << endl;exit(1);}
58 
59  // check if slag is presents in wave tree
60  TBranch* branch;
61  bool net_slag=false;
62  TIter next(twave->GetListOfBranches());
63  while ((branch=(TBranch*)next())) {
64  if (TString("slag").CompareTo(branch->GetName())==0) net_slag=true;
65  }
66  next.Reset();
67 
68  // open live tree
69  TFile *flive = TFile::Open(liv_file_name);
70  if(flive==NULL) {cout << "Error opening file " << liv_file_name << endl;exit(1);}
71  TTree* tlive = (TTree *) gROOT->FindObject("liveTime");
72  if(tlive==NULL) {cout << "Error opening tree livetime" << endl;exit(1);}
73 
74  // check if slag is presents in live tree
75  TBranch* branch;
76  bool liv_slag=false;
77  TIter next(tlive->GetListOfBranches());
78  while ((branch=(TBranch*)next())) {
79  if (TString("slag").CompareTo(branch->GetName())==0) liv_slag=true;
80  }
81  next.Reset();
82  if(liv_slag&&net_slag) {
83  cout << "Files " << endl;
84  cout << liv_file_name << endl;
85  cout << net_file_name << endl;
86  cout << "already contains slag leaf, exit" << endl;
87  exit(1);
88  }
89 
90  if(!liv_slag&&net_slag) {
91  cout << "File " << net_file_name << " aleady contains slag leaf" << endl;
92  cout << "only " << liv_file_name << " needs to be fixed" << endl;
93  cout << "apply cwb_fix_live_slag_missed.C " << endl;
94  exit(1);
95  }
96 
97  if(liv_slag&&!net_slag) {
98  cout << "File " << liv_file_name << " aleady contains slag leaf" << endl;
99  cout << "only " << net_file_name << " needs to be fixed" << endl;
100  cout << "something wrong during production, please check" << endl;
101  exit(1);
102  }
103 
104  // insert slag=0 in wave and live trees
105  // tree has been produced without slag
106  bool fixit=false;
107  if (!liv_slag) {
108  char answer[256];
109  strcpy(answer,"");
110  do {
111  cout << "File " << net_file_name << " do not contains slag leaf" << endl;
112  cout << "File " << liv_file_name << " do not contains slag leaf" << endl;
113  cout << "Do you want to fix them ? (y/n) ";
114  cin >> answer;
115  cout << endl << endl;
116  } while ((strcmp(answer,"y")!=0)&&(strcmp(answer,"n")!=0));
117  if (strcmp(answer,"y")==0) fixit=true;
118  }
119  if(!fixit) exit(0);
120 
121  // -------------------------------------------------------------------------
122  // create slag fix live root file
123  // -------------------------------------------------------------------------
124 
125  TString fix_liv_file_name(liv_file_name);
126  fix_liv_file_name.ReplaceAll(".root",".FIX.root");
127  cout << "Fixed live file : " << fix_liv_file_name.Data() << endl;
128 
129  TFile* flive_fix = new TFile(fix_liv_file_name,"RECREATE");
130  if (flive_fix->IsZombie()) {
131  cout << "CWB::Toolbox::setVeto - Error opening file " << fix_liv_file_name.Data() << endl;
132  exit(1);
133  }
134  TTree *tlive_fix = (TTree*)tlive->CloneTree(0);
135  tlive_fix->SetMaxTreeSize(5000000000);
136  // add slag leaf
137  float xslag[6];
138  tlive_fix->Branch("slag",xslag,"slag[6]/F");
139  tlive_fix->Write();
140 
141  Int_t xrun;
142  tlive->SetBranchAddress("run",&xrun);
143  int ntrg_live = tlive->GetEntries();
144  for(int i=0; i<ntrg_live; i++) {
145  if(i%100000==0) cout << i << "/" << ntrg_live << endl;
146  tlive->GetEntry(i);
147  for (int n=0;n<nIFO;n++) xslag[n] = 0.;
148  xslag[nIFO] = 0.;
149  for (int n=nIFO+1;n<6;n++) xslag[n] = -1;
150  tlive_fix->Fill();
151  }
152 
153  tlive_fix->Write();
154  flive_fix->Close();
155 
156 
157  // -------------------------------------------------------------------------
158  // create slag fix wave root file
159  // -------------------------------------------------------------------------
160 
161  TString fix_net_file_name(net_file_name);
162  fix_net_file_name.ReplaceAll(".root",".FIX.root");
163  cout << "Fixed wave file : " << fix_net_file_name.Data() << endl;
164 
165  TFile* fwave_fix = new TFile(fix_net_file_name,"RECREATE");
166  if (fwave_fix->IsZombie()) {
167  cout << "CWB::Toolbox::setVeto - Error opening file " << fix_net_file_name.Data() << endl;
168  exit(1);
169  }
170  TTree *twave_fix = (TTree*)twave->CloneTree(0);
171  twave_fix->SetMaxTreeSize(5000000000);
172  // add slag leaf
173  float xslag[6];
174  twave_fix->Branch("slag",xslag,"slag[6]/F");
175  twave_fix->Write();
176 
177  int ntrg_wave = twave->GetEntries();
178  for(int i=0; i<ntrg_wave; i++) {
179  if(i%100000==0) cout << i << "/" << ntrg_wave << endl;
180  twave->GetEntry(i);
181  for (int n=0;n<nIFO;n++) xslag[n] = 0.;
182  xslag[nIFO] = 0.;
183  for (int n=nIFO+1;n<6;n++) xslag[n] = -1;
184  twave_fix->Fill();
185  }
186 
187  twave_fix->Write();
188  fwave_fix->Close();
189 
190  // -------------------------------------------------------------------------
191  // create a symbolic link to merge*.lst file name
192  // -------------------------------------------------------------------------
193  vector<int> jobList;
194  char ilstfname[1024];
195  sprintf(ilstfname,"merge_%s.%s.lst",data_label,cwb_merge_label.Data());
197  olstfname.ReplaceAll("wave_","merge_");
198  olstfname.ReplaceAll(".root",".lst");
199  olstfname.Remove(0,olstfname.Last('/')+1); // strip path
200  cout << olstfname << endl;
201  int estat;
202  Long_t id,size,flags,mt;
203  estat = gSystem->GetPathInfo(TString(merge_dir)+"/"+ilstfname,&id,&size,&flags,&mt);
204  if (estat==0) {
205  char cmd[1024];
206  sprintf(cmd,"cd %s;ln -sf %s %s",merge_dir,ilstfname,olstfname.Data());
207  cout << cmd << endl;
208  gSystem->Exec(cmd);
209  }
210 
211  exit(0);
212 }
char liv_file_name[256]
sprintf(liv_file_name,"%s/live_%s.%s.root", merge_dir, data_label, cwb_merge_label.Data())
int n
Definition: cwb_net.C:28
Int_t xrun
TString("c")
CWB::Toolbox TB
TString fix_liv_file_name(liv_file_name)
Long_t flags
Long_t size
i drho i
static bool checkFile(TString fName, bool question=false, TString message="")
Definition: Toolbox.cc:4670
int ntrg_live
#define nIFO
char data_label[512]
Definition: test_config1.C:160
TIter next(twave->GetListOfBranches())
char net_file_name[256]
vector< int > jobList
TTree * tlive
bool liv_slag
exit(0)
bool net_slag
char merge_dir[512]
Definition: test_config1.C:147
TBranch * branch
bool fixit
char answer[256]
TString fix_net_file_name(net_file_name)
TFile * flive
char cmd[1024]
int estat
strcpy(RunLabel, RUN_LABEL)
char ilstfname[1024]
Long_t mt
float xslag[6]
TString olstfname
Long_t id
TString cwb_merge_label
simulation
Definition: cwb_eced.C:26
int ntrg_wave