map_multiplex.C 12.4 KB
Newer Older
positron cool guys's avatar
positron cool guys committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
74
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#include <vector>
#ifdef __MAKECINT__
#pragma link C++ class vector<vector<short> >+;
#pragma link C++ class vector<char>+;
#endif
//#pragma link C++ class vector<vector<short> >+;
#include <iostream>
#include <string>
using namespace std;
#include "Riostream.h"
#include "TH1.h"
#include "TH2.h"
#include "TF1.h"
#include "TH1D.h"
#include "TROOT.h"
#include "TCanvas.h"
#include "TRandom.h"
#include "TStyle.h"
#include "TLegend.h"
#include "TFile.h"
#include "TMath.h"
#include "TLatex.h"
#include "TBranch.h"
#include "TTree.h"
#include "TKey.h"
#include "TClass.h"
#include "TDirectory.h"

//#define sleontsi

//TFile *root_file;

#include "apv_raw.C"
#include "APV_RAW_PED.C"
apv_raw *apv_raw_obj;
APV_RAW_PED *apv_raw_ped;

void CopyDir(TDirectory *source) {
   //copy all objects and subdirs of directory source as a subdir of the current directory   
   TDirectory *savdir = gDirectory;
   TDirectory *adir = savdir->mkdir("config");
   adir->cd();
   //loop on all entries of this directory
   TKey *key;
   TIter nextkey(source->GetListOfKeys());
   while ((key = (TKey*)nextkey())) {
      const char *classname = key->GetClassName();
      TClass *cl = gROOT->GetClass(classname);
      if (!cl) continue;
      if (cl->InheritsFrom(TDirectory::Class())) {
         source->cd(key->GetName());
         TDirectory *subdir = gDirectory;
         adir->cd();
         CopyDir(subdir);
         adir->cd();
//      }
//       else if (cl->InheritsFrom(TTree::Class())) {
//         TTree *T = (TTree*)source->Get(key->GetName());
//         adir->cd();
//         TTree *newT = T->CloneTree(-1,"fast");
//         newT->Write();
      } else {
         source->cd();
         TObject *obj = key->ReadObj();
         TObjString *takis = new TObjString(obj->GetName());
         savdir->cd();
         takis->Write(key->GetName());
     }
  }
  adir->SaveSelf(kTRUE);
  savdir->cd();
}

void run(string  file){

	TFile *root_file = new TFile(file.c_str(),"READ");
	
	apv_raw_obj = new apv_raw((TTree*)root_file->Get("apv_raw"));
	apv_raw_ped = new APV_RAW_PED((TTree*)root_file->Get("apv_raw_ped"));	

	int entries = (apv_raw_obj->fChain)->GetEntries();
	int entries_ped = (apv_raw_ped->fChain)->GetEntries();

   std::size_t found_run = file.find("run");
   std::size_t found_dotroot = file.find(".root");
   std::string run_name = file.substr(found_run,found_dotroot-found_run);
   std::string path = file.substr(0,found_run);

   std::string new_file = path+"mapped/"+run_name+"_mapped.root";
   
   
   
  TFile *ntuple = new TFile(new_file.c_str(), "RECREATE");
  TTree *aux;
  aux = new TTree("apv_raw", "apv_raw");
  ULong64_t               write_evt;
  ULong64_t               write_evt_temp=0;
  UInt_t                  write_error;
  Int_t                   write_daqTimeSec;
  Int_t                   write_daqTimeMicroSec;
  Int_t                   write_srsTimeStamp;
  UInt_t                  write_srsTrigger;
  std::vector<unsigned int>   write_srsFec;
  std::vector<unsigned int>   write_srsChip;
  std::vector<unsigned int>   write_srsChan;
  std::vector<string>         write_mmChamber;
  std::vector<int>            write_mmLayer;
  std::vector<char>           write_mmReadout;
  std::vector<int>            write_mmStrip;
  std::vector<vector<short> >      write_raw_q_parse;
  std::vector<short> 				charge_info_to_be_puched_back_temp;

  aux->Branch("evt"              , &write_evt            );
  aux->Branch("error"            , &write_error          );
  aux->Branch("daqTimeSec"       , &write_daqTimeSec     );
  aux->Branch("daqTimeMicroSec"  , &write_daqTimeMicroSec);
  aux->Branch("srsTimeStamp"     , &write_srsTimeStamp   ); 
  aux->Branch("srsTrigger"       , &write_srsTrigger     );
  aux->Branch("srsFec"           , &write_srsFec         );
  aux->Branch("srsChip"          , &write_srsChip        );
  aux->Branch("srsChan"          , &write_srsChan        );
  aux->Branch("mmChamber"        , &write_mmChamber      );
  aux->Branch("mmLayer"          , &write_mmLayer        );
  aux->Branch("mmReadout"        , &write_mmReadout      );
  aux->Branch("mmStrip"          , &write_mmStrip        );
  aux->Branch("raw_q"            , &write_raw_q_parse    );


   
   // loop on multiplexed map file X
   int numOfLines_mapx=320;
   double first_mapx[numOfLines_mapx], second_mapx[numOfLines_mapx], third_mapx[numOfLines_mapx], fourth_mapx[numOfLines_mapx], fifth_mapx[numOfLines_mapx];
   ifstream InputRead_mapx ("multiplexing_maps/Mapping_new_1.txt");
   for (int i=0; i<numOfLines_mapx; i++) {
      InputRead_mapx >> first_mapx[i] >> second_mapx[i] >> third_mapx[i] >> fourth_mapx[i] >> fifth_mapx[i];
      //std::cout<< first_mapx[i] <<" "<< second_mapx[i] <<" "<< third_mapx[i] <<" "<< fourth_mapx[i] <<" "<< fifth_mapx[i] <<" "<<std::endl;
   } // loop on map file
   // loop on multiplexed map file X
   
   // loop on multiplexed map file Y
   int numOfLines_mapy=320;
   double first_mapy[numOfLines_mapy], second_mapy[numOfLines_mapy], third_mapy[numOfLines_mapy], fourth_mapy[numOfLines_mapy], fifth_mapy[numOfLines_mapy];
   ifstream InputRead_mapy ("multiplexing_maps/Mapping_new_2.txt");
   for (int i=0; i<numOfLines_mapy; i++) {
      InputRead_mapy >> first_mapy[i] >> second_mapy[i] >> third_mapy[i] >> fourth_mapy[i] >> fifth_mapy[i];
   } // loop on map file
   // loop on multiplexed map file Y
   
   
   for(int iEntry=0; iEntry<entries; iEntry++) {
      if(iEntry%200==0)
         cout << "\r" << (double)iEntry/(double)entries*100 << "\% processed" << flush;
      
      (apv_raw_obj->fChain)->GetEntry(iEntry);
      
      write_srsFec.clear();
      write_srsChip.clear();
      write_srsChan.clear();
      write_mmChamber.clear();
      write_mmLayer.clear();
      write_mmReadout.clear();
      write_mmStrip.clear();
      write_raw_q_parse.clear();
      
      write_evt =  apv_raw_obj->evt;
      write_error = apv_raw_obj->error;
      write_daqTimeSec = apv_raw_obj->daqTimeSec;
      write_daqTimeMicroSec = apv_raw_obj->daqTimeMicroSec;
      write_srsTimeStamp = apv_raw_obj->srsTimeStamp;
      write_srsTrigger = apv_raw_obj->srsTrigger;
      
      for (int j=0; j<(int)apv_raw_obj->srsFec->size(); j++) {
         
	
	//X remapping
            if(apv_raw_obj->mmReadout->at(j)==49){
               for (int p=0; p<numOfLines_mapx; p++) {
                  if(apv_raw_obj->srsChan->at(j)==(int)first_mapx[p]-2)
                  {
                     charge_info_to_be_puched_back_temp.clear();
                     for(int i_sample=0; i_sample<apv_raw_obj->raw_q->at(j).size(); i_sample++)
                     {
                        charge_info_to_be_puched_back_temp.push_back(apv_raw_obj->raw_q->at(j).at(i_sample));
                     }

                     write_raw_q_parse.push_back(charge_info_to_be_puched_back_temp);
                     write_mmStrip.push_back(fifth_mapx[p]);
                     write_srsChan.push_back(apv_raw_obj->srsChan->at(j));
                     write_srsFec.push_back(apv_raw_obj->srsFec->at(j));
                     write_srsChip.push_back(apv_raw_obj->srsChip->at(j));
                     write_mmChamber.push_back(apv_raw_obj->mmChamber->at(j));
                     write_mmLayer.push_back(apv_raw_obj->mmLayer->at(j));
                     write_mmReadout.push_back(apv_raw_obj->mmReadout->at(j));
                  }
               }
            }
	//Y remapping
            else if(apv_raw_obj->mmReadout->at(j)==48){
               for (int p=0; p<numOfLines_mapy; p++) {
                  if(apv_raw_obj->srsChan->at(j)==(int)first_mapy[p]-2)
                  {
                     charge_info_to_be_puched_back_temp.clear();
                     for(int i_sample=0; i_sample<apv_raw_obj->raw_q->at(j).size(); i_sample++)
                     {
                        charge_info_to_be_puched_back_temp.push_back(apv_raw_obj->raw_q->at(j).at(i_sample));
                     }
                     
                     write_raw_q_parse.push_back(charge_info_to_be_puched_back_temp);
                     write_mmStrip.push_back(fifth_mapy[p]);
                     write_srsChan.push_back(apv_raw_obj->srsChan->at(j));
                     write_srsFec.push_back(apv_raw_obj->srsFec->at(j));
                     write_srsChip.push_back(apv_raw_obj->srsChip->at(j));
                     write_mmChamber.push_back(apv_raw_obj->mmChamber->at(j));
                     write_mmLayer.push_back(apv_raw_obj->mmLayer->at(j));
                     write_mmReadout.push_back(apv_raw_obj->mmReadout->at(j));
                  }
                  
               }
            }
      }
   
   
    aux->Fill();

   }

   
   // make second tree
   
   TTree *aux_ped;
   aux_ped = new TTree("apv_raw_ped", "apv_raw_ped");
   
   ULong64_t       write_pedevt;
   UInt_t          write_pederror;
   Int_t           write_peddaqTimeSec;
   Int_t           write_peddaqTimeMicroSec;
   Int_t           write_pedsrsTimeStamp;
   UInt_t          write_pedsrsTrigger;
   std::vector<unsigned int> write_pedsrsFec;
   std::vector<unsigned int> write_pedsrsChip;
   std::vector<unsigned int> write_pedsrsChan;
   std::vector<string>  write_pedmmChamber;
   std::vector<int>     write_pedmmLayer;
   std::vector<char>    write_pedmmReadout;
   std::vector<int>     write_pedmmStrip;
   std::vector<double>  write_pedped_mean;
   std::vector<double>  write_pedped_stdev;
   std::vector<double>  write_pedped_sigma;
   
   aux_ped->Branch("evt"              , &write_pedevt            );
   aux_ped->Branch("error"            , &write_pederror          );
   aux_ped->Branch("daqTimeSec"       , &write_peddaqTimeSec     );
   aux_ped->Branch("daqTimeMicroSec"  , &write_peddaqTimeMicroSec);
   aux_ped->Branch("srsTimeStamp"     , &write_pedsrsTimeStamp   );
   aux_ped->Branch("srsTrigger"       , &write_pedsrsTrigger     );
   aux_ped->Branch("srsFec"           , &write_pedsrsFec         );
   aux_ped->Branch("srsChip"          , &write_pedsrsChip        );
   aux_ped->Branch("srsChan"          , &write_pedsrsChan        );
   aux_ped->Branch("mmChamber"        , &write_pedmmChamber      );
   aux_ped->Branch("mmLayer"          , &write_pedmmLayer        );
   aux_ped->Branch("mmReadout"        , &write_pedmmReadout      );
   aux_ped->Branch("mmStrip"          , &write_pedmmStrip        );
   aux_ped->Branch("ped_mean"         , &write_pedped_mean       );
   aux_ped->Branch("ped_stdev"        , &write_pedped_stdev      );
   aux_ped->Branch("ped_sigma"        , &write_pedped_sigma      );
   
   cout << endl;
   for(int iEntry=0; iEntry<entries_ped; iEntry++) {
      
      if(iEntry%200==0)
         cout << std::setprecision(2) << "\r" << (double)iEntry/(double)entries_ped*100 << "\% processed" << flush;
      
      (apv_raw_ped->fChain)->GetEntry(iEntry);
      
      write_pedsrsFec.clear();
      write_pedsrsChip.clear();
      write_pedsrsChan.clear();
      write_pedmmChamber.clear();
      write_pedmmLayer.clear();
      write_pedmmReadout.clear();
      write_pedmmStrip.clear();
      
      write_pedevt = apv_raw_ped->evt;
      write_pederror = apv_raw_ped->error;
      write_peddaqTimeSec = apv_raw_ped->daqTimeSec;
      write_peddaqTimeMicroSec = apv_raw_ped->daqTimeMicroSec;
      write_pedsrsTimeStamp = apv_raw_ped->srsTimeStamp;
      write_pedsrsTrigger = apv_raw_ped->srsTrigger;
      for (int j=0; j<(int)apv_raw_ped->srsFec->size(); j++) {
         write_pedsrsFec.push_back(apv_raw_ped->srsFec->at(j));
         write_pedsrsChip.push_back(apv_raw_ped->srsChip->at(j));
         write_pedsrsChan.push_back(apv_raw_ped->srsChan->at(j));
         write_pedmmChamber.push_back(apv_raw_ped->mmChamber->at(j));
         write_pedmmLayer.push_back(apv_raw_ped->mmLayer->at(j));
         write_pedmmReadout.push_back(apv_raw_ped->mmReadout->at(j));
         write_pedmmStrip.push_back(apv_raw_ped->mmStrip->at(j));
         write_pedped_mean.push_back(apv_raw_ped->ped_mean->at(j));
         write_pedped_stdev.push_back(apv_raw_ped->ped_stdev->at(j));
         write_pedped_sigma.push_back(apv_raw_ped->ped_sigma->at(j));
      }
      
      aux_ped->Fill();
   }
 
   
  ntuple->cd();
  TDirectory *target = gDirectory;
  target->cd();
  CopyDir(root_file);
  target->cd();

  ntuple->Write();
  ntuple->Close();

  cout << endl;


}