FileWriter.C 7.3 KB
Newer Older
1
2
3
4
5
#include "FileWriter.h"

FileWriter::FileWriter(bool debugging)
: Algorithm("FileWriter"){
  debug = debugging;
6
  m_onlyDUT = true;
Daniel Hynds's avatar
Daniel Hynds committed
7
  m_writePixels = true;
8
  m_writeClusters = false;
9
  m_writeTracks = true;
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
  m_fileName = "outputTuples.root";
}

/*
 
 This algorithm writes an output file and fills it with trees containing
 the requested data. 
 
 Any object which inherits from TestBeamObject can in principle be written
 to file. In order to enable this for a new type, the TestBeamObject::Factory
 function must know how to return an instantiation of that type (see 
 TestBeamObject.C file to see how to do this). The new type can then simply
 be added to the object list and will be written out correctly.
 
 */


void FileWriter::initialise(Parameters* par){
 
  // Pick up the global parameters
  parameters = par;
31
  m_fileName = parameters->outputTupleFile;
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

  // Decide what objects will be written out
  if(m_writePixels) m_objectList.push_back("pixels");
  if(m_writeClusters) m_objectList.push_back("clusters");
  if(m_writeTracks) m_objectList.push_back("tracks");
  
  // Create output file and directories
  m_outputFile = new TFile(m_fileName.c_str(),"RECREATE");
  m_outputFile->cd();
  
  // Loop over all objects to be written to file, and set up the trees
  for(unsigned int itList=0;itList<m_objectList.size();itList++){
    
    // Check the type of object
    string objectType = m_objectList[itList];
    
    // Make a directory in the ouput folder
    m_outputFile->mkdir(objectType.c_str());
    m_outputFile->cd(objectType.c_str());
    
    // Section to set up object writing per detector (such as pixels, clusters)
    if(objectType == "pixels" || objectType == "clusters"){
      
      // Loop over all detectors and make trees for data
      for(int det = 0; det<parameters->nDetectors; det++){
        
        // Get the detector ID and type
        string detectorID = parameters->detectors[det];
        string detectorType = parameters->detector[detectorID]->type();
        
62
63
64
        // If only writing information for the DUT
        if(m_onlyDUT && detectorID != parameters->DUT) continue;
        
65
66
67
68
69
70
71
72
73
74
75
76
77
        // Create the tree
        string objectID = detectorID + "_" + objectType;
        string treeName = detectorID + "_" + detectorType + "_" + objectType;
        m_outputTrees[objectID] = new TTree(treeName.c_str(),treeName.c_str());
        m_outputTrees[objectID]->Branch("time", &m_time);
        
        // Cast the TestBeamObject as a specific type using a Factory
        // This will return a Timepix1Pixel*, Timepix3Pixel* etc.
        m_objects[objectID] = TestBeamObject::Factory(detectorType, objectType);
        m_outputTrees[objectID]->Branch(objectType.c_str(), &m_objects[objectID]);
        
      }
    }
78
    // If not an object to be written per detector
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
    else{
      // Make the tree
      string treeName = objectType;
      m_outputTrees[objectType] = new TTree(treeName.c_str(),treeName.c_str());
      // Branch the tree to the timestamp and object
      m_outputTrees[objectType]->Branch("time", &m_time);
      m_objects[objectType] = TestBeamObject::Factory(objectType);
      m_outputTrees[objectType]->Branch(objectType.c_str(), &m_objects[objectType]);
    }
  }
  
  // Initialise member variables
  m_eventNumber = 0;
}

StatusCode FileWriter::run(Clipboard* clipboard){

  // Loop over all objects to be written to file, and get the objects currently
  // held on the Clipboard
  for(unsigned int itList=0;itList<m_objectList.size();itList++){
    
    // Check the type of object
    string objectType = m_objectList[itList];

    // If this is written per device, loop over all devices
    if(objectType == "pixels" || objectType == "clusters"){

      // Loop over all detectors
      for(int det = 0; det<parameters->nDetectors; det++){
        
        // Get the detector and object ID
        string detectorID = parameters->detectors[det];
        string objectID = detectorID + "_" + objectType;
        
113
114
115
        // If only writing information for the DUT
        if(m_onlyDUT && detectorID != parameters->DUT) continue;

116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
        // Get the objects, if they don't exist then continue
        if(debug) tcout<<"Checking for "<<objectType<<" on device "<<detectorID<<endl;
        TestBeamObjects* objects = clipboard->get(detectorID,objectType);
        if(objects == NULL) continue;
        if(debug) tcout<<"Picked up "<<objects->size()<<" "<<objectType<<" from device "<<detectorID<<endl;
        
        // Check if the output tree exists
        if(!m_outputTrees[objectID]) continue;
        
        // Fill the objects into the tree
        for(int itObject=0;itObject<objects->size();itObject++){
          m_objects[objectID] = (*objects)[itObject];
          m_time = m_objects[objectID]->timestamp();
          m_outputTrees[objectID]->Fill();
        }
      }
    } // If object is not written per device
    else{
      
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
      // Get the objects, if they don't exist then continue
      if(debug) tcout<<"Checking for "<<objectType<<endl;
      TestBeamObjects* objects = clipboard->get(objectType);
      if(objects == NULL) continue;
      if(debug) tcout<<"Picked up "<<objects->size()<<" "<<objectType<<endl;
      
      // Check if the output tree exists
      if(!m_outputTrees[objectType]) continue;
      
      // Fill the objects into the tree
      for(int itObject=0;itObject<objects->size();itObject++){
        m_objects[objectType] = (*objects)[itObject];
        m_time = m_objects[objectType]->timestamp();
        m_outputTrees[objectType]->Fill();
      }
      if(debug) tcout<<"Written "<<objectType<<" to file"<<endl;

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
    }
  }

   // Increment event counter
  m_eventNumber++;
  
  // Return value telling analysis to keep running
  return Success;
}

void FileWriter::finalise(){
  
  // Write the trees to file
  // Loop over all objects to be written to file, and get the objects currently
  // held on the Clipboard
  for(unsigned int itList=0;itList<m_objectList.size();itList++){
    
    // Check the type of object
    string objectType = m_objectList[itList];
    
    // If this is written per device, loop over all devices
    if(objectType == "pixels" || objectType == "clusters"){
      
      // Loop over all detectors
      for(int det = 0; det<parameters->nDetectors; det++){
        
        // Get the detector and object ID
        string detectorID = parameters->detectors[det];
        string objectID = detectorID + "_" + objectType;
        
        // If there is no output tree then do nothing
        if(!m_outputTrees[objectID]) continue;
        
        // Move to the write output file
        m_outputFile->cd();
        m_outputFile->cd(objectType.c_str());
        m_outputTrees[objectID]->Write();
        
        // Clean up the tree and remove object pointer
        delete m_outputTrees[objectID];
        m_objects[objectID] = 0;
        
      }
    }// Write trees for devices which are not detector dependent
    else{
      
198
199
200
201
202
203
204
205
206
207
208
209
      // If there is no output tree then do nothing
      if(!m_outputTrees[objectType]) continue;
      
      // Move to the write output file
      m_outputFile->cd();
      m_outputFile->cd(objectType.c_str());
      m_outputTrees[objectType]->Write();
      
      // Clean up the tree and remove object pointer
      delete m_outputTrees[objectType];
      m_objects[objectType] = 0;

210
211
212
213
214
215
    }
  }
  
  if(debug) tcout<<"Analysed "<<m_eventNumber<<" events"<<endl;
  
}